Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_BASE_CASTS_H_ |
| 18 | #define ART_RUNTIME_BASE_CASTS_H_ |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 19 | |
Carl Shapiro | 6d860c1 | 2011-07-20 16:40:16 -0700 | [diff] [blame] | 20 | #include <assert.h> |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 21 | #include <string.h> |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 22 | #include "base/macros.h" |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 23 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 24 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 25 | |
Carl Shapiro | 6d860c1 | 2011-07-20 16:40:16 -0700 | [diff] [blame] | 26 | // Use implicit_cast as a safe version of static_cast or const_cast |
| 27 | // for upcasting in the type hierarchy (i.e. casting a pointer to Foo |
| 28 | // to a pointer to SuperclassOfFoo or casting a pointer to Foo to |
| 29 | // a const pointer to Foo). |
| 30 | // When you use implicit_cast, the compiler checks that the cast is safe. |
| 31 | // Such explicit implicit_casts are necessary in surprisingly many |
| 32 | // situations where C++ demands an exact type match instead of an |
| 33 | // argument type convertable to a target type. |
| 34 | // |
| 35 | // The From type can be inferred, so the preferred syntax for using |
| 36 | // implicit_cast is the same as for static_cast etc.: |
| 37 | // |
| 38 | // implicit_cast<ToType>(expr) |
| 39 | // |
| 40 | // implicit_cast would have been part of the C++ standard library, |
| 41 | // but the proposal was submitted too late. It will probably make |
| 42 | // its way into the language in the future. |
| 43 | template<typename To, typename From> |
| 44 | inline To implicit_cast(From const &f) { |
| 45 | return f; |
| 46 | } |
| 47 | |
| 48 | // When you upcast (that is, cast a pointer from type Foo to type |
| 49 | // SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts |
| 50 | // always succeed. When you downcast (that is, cast a pointer from |
| 51 | // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because |
| 52 | // how do you know the pointer is really of type SubclassOfFoo? It |
| 53 | // could be a bare Foo, or of type DifferentSubclassOfFoo. Thus, |
| 54 | // when you downcast, you should use this macro. In debug mode, we |
| 55 | // use dynamic_cast<> to double-check the downcast is legal (we die |
| 56 | // if it's not). In normal mode, we do the efficient static_cast<> |
| 57 | // instead. Thus, it's important to test in debug mode to make sure |
| 58 | // the cast is legal! |
| 59 | // This is the only place in the code we should use dynamic_cast<>. |
| 60 | // In particular, you SHOULDN'T be using dynamic_cast<> in order to |
| 61 | // do RTTI (eg code like this: |
| 62 | // if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo); |
| 63 | // if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo); |
| 64 | // You should design the code some other way not to need this. |
| 65 | |
| 66 | template<typename To, typename From> // use like this: down_cast<T*>(foo); |
| 67 | inline To down_cast(From* f) { // so we only accept pointers |
| 68 | // Ensures that To is a sub-type of From *. This test is here only |
| 69 | // for compile-time type checking, and has no overhead in an |
| 70 | // optimized build at run-time, as it will be optimized away |
| 71 | // completely. |
| 72 | if (false) { |
| 73 | implicit_cast<From*, To>(0); |
| 74 | } |
| 75 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 76 | // |
Brian Carlstrom | 07d579f | 2011-07-27 13:31:51 -0700 | [diff] [blame] | 77 | // assert(f == NULL || dynamic_cast<To>(f) != NULL); // RTTI: debug mode only! |
Carl Shapiro | 6d860c1 | 2011-07-20 16:40:16 -0700 | [diff] [blame] | 78 | return static_cast<To>(f); |
| 79 | } |
| 80 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 81 | template <class Dest, class Source> |
| 82 | inline Dest bit_cast(const Source& source) { |
| 83 | // Compile time assertion: sizeof(Dest) == sizeof(Source) |
| 84 | // A compile error here means your Dest and Source have different sizes. |
| 85 | COMPILE_ASSERT(sizeof(Dest) == sizeof(Source), verify_sizes_are_equal); |
| 86 | Dest dest; |
| 87 | memcpy(&dest, &source, sizeof(dest)); |
| 88 | return dest; |
| 89 | } |
| 90 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 91 | } // namespace art |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 92 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 93 | #endif // ART_RUNTIME_BASE_CASTS_H_ |