blob: a5e31c4cc7d1b66cf8be480208abd714b1eeee56 [file] [log] [blame]
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_CASTS_H_
4#define ART_SRC_CASTS_H_
5
Carl Shapiro6d860c12011-07-20 16:40:16 -07006#include <assert.h>
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -07007#include <string.h>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07008#include "macros.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -07009
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070010namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070011
Carl Shapiro6d860c12011-07-20 16:40:16 -070012// Use implicit_cast as a safe version of static_cast or const_cast
13// for upcasting in the type hierarchy (i.e. casting a pointer to Foo
14// to a pointer to SuperclassOfFoo or casting a pointer to Foo to
15// a const pointer to Foo).
16// When you use implicit_cast, the compiler checks that the cast is safe.
17// Such explicit implicit_casts are necessary in surprisingly many
18// situations where C++ demands an exact type match instead of an
19// argument type convertable to a target type.
20//
21// The From type can be inferred, so the preferred syntax for using
22// implicit_cast is the same as for static_cast etc.:
23//
24// implicit_cast<ToType>(expr)
25//
26// implicit_cast would have been part of the C++ standard library,
27// but the proposal was submitted too late. It will probably make
28// its way into the language in the future.
29template<typename To, typename From>
30inline To implicit_cast(From const &f) {
31 return f;
32}
33
34// When you upcast (that is, cast a pointer from type Foo to type
35// SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
36// always succeed. When you downcast (that is, cast a pointer from
37// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
38// how do you know the pointer is really of type SubclassOfFoo? It
39// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
40// when you downcast, you should use this macro. In debug mode, we
41// use dynamic_cast<> to double-check the downcast is legal (we die
42// if it's not). In normal mode, we do the efficient static_cast<>
43// instead. Thus, it's important to test in debug mode to make sure
44// the cast is legal!
45// This is the only place in the code we should use dynamic_cast<>.
46// In particular, you SHOULDN'T be using dynamic_cast<> in order to
47// do RTTI (eg code like this:
48// if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
49// if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
50// You should design the code some other way not to need this.
51
52template<typename To, typename From> // use like this: down_cast<T*>(foo);
53inline To down_cast(From* f) { // so we only accept pointers
54 // Ensures that To is a sub-type of From *. This test is here only
55 // for compile-time type checking, and has no overhead in an
56 // optimized build at run-time, as it will be optimized away
57 // completely.
58 if (false) {
59 implicit_cast<From*, To>(0);
60 }
61
Brian Carlstrom07d579f2011-07-27 13:31:51 -070062 //
63 // assert(f == NULL || dynamic_cast<To>(f) != NULL); // RTTI: debug mode only!
Carl Shapiro6d860c12011-07-20 16:40:16 -070064 return static_cast<To>(f);
65}
66
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070067template <class Dest, class Source>
68inline Dest bit_cast(const Source& source) {
69 // Compile time assertion: sizeof(Dest) == sizeof(Source)
70 // A compile error here means your Dest and Source have different sizes.
71 COMPILE_ASSERT(sizeof(Dest) == sizeof(Source), verify_sizes_are_equal);
72 Dest dest;
73 memcpy(&dest, &source, sizeof(dest));
74 return dest;
75}
76
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070077} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070078
79#endif // ART_SRC_CASTS_H_