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 */
16
17 #ifndef ART_RUNTIME_BASE_CASTS_H_
18 #define ART_RUNTIME_BASE_CASTS_H_
19
20 #include <assert.h>
21 #include <string.h>
22 #include "base/macros.h"
23
24 namespace art {
25
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>
implicit_cast(From const & f)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);
down_cast(From * f)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
76 //
77 // assert(f == NULL || dynamic_cast<To>(f) != NULL); // RTTI: debug mode only!
78 return static_cast<To>(f);
79 }
80
81 template <class Dest, class Source>
bit_cast(const Source & 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
91 } // namespace art
92
93 #endif // ART_RUNTIME_BASE_CASTS_H_
94