Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_CASTS_H_ |
| 4 | #define ART_SRC_CASTS_H_ |
| 5 | |
Carl Shapiro | 6d860c1 | 2011-07-20 16:40:16 -0700 | [diff] [blame] | 6 | #include <assert.h> |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 7 | #include <string.h> |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 8 | #include "macros.h" |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 9 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 10 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 11 | |
Carl Shapiro | 6d860c1 | 2011-07-20 16:40:16 -0700 | [diff] [blame] | 12 | // 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. |
| 29 | template<typename To, typename From> |
| 30 | inline 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 | |
| 52 | template<typename To, typename From> // use like this: down_cast<T*>(foo); |
| 53 | inline 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 Carlstrom | 07d579f | 2011-07-27 13:31:51 -0700 | [diff] [blame] | 62 | // |
| 63 | // assert(f == NULL || dynamic_cast<To>(f) != NULL); // RTTI: debug mode only! |
Carl Shapiro | 6d860c1 | 2011-07-20 16:40:16 -0700 | [diff] [blame] | 64 | return static_cast<To>(f); |
| 65 | } |
| 66 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 67 | template <class Dest, class Source> |
| 68 | inline 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 Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 77 | } // namespace art |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 78 | |
| 79 | #endif // ART_SRC_CASTS_H_ |