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 <limits>
22 #include <string.h>
23 #include <type_traits>
24 
25 #include "base/logging.h"
26 #include "base/macros.h"
27 
28 namespace art {
29 
30 // Use implicit_cast as a safe version of static_cast or const_cast
31 // for upcasting in the type hierarchy (i.e. casting a pointer to Foo
32 // to a pointer to SuperclassOfFoo or casting a pointer to Foo to
33 // a const pointer to Foo).
34 // When you use implicit_cast, the compiler checks that the cast is safe.
35 // Such explicit implicit_casts are necessary in surprisingly many
36 // situations where C++ demands an exact type match instead of an
37 // argument type convertable to a target type.
38 //
39 // The From type can be inferred, so the preferred syntax for using
40 // implicit_cast is the same as for static_cast etc.:
41 //
42 //   implicit_cast<ToType>(expr)
43 //
44 // implicit_cast would have been part of the C++ standard library,
45 // but the proposal was submitted too late.  It will probably make
46 // its way into the language in the future.
47 template<typename To, typename From>
implicit_cast(From const & f)48 inline To implicit_cast(From const &f) {
49   return f;
50 }
51 
52 // When you upcast (that is, cast a pointer from type Foo to type
53 // SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
54 // always succeed.  When you downcast (that is, cast a pointer from
55 // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
56 // how do you know the pointer is really of type SubclassOfFoo?  It
57 // could be a bare Foo, or of type DifferentSubclassOfFoo.  Thus,
58 // when you downcast, you should use this macro.  In debug mode, we
59 // use dynamic_cast<> to double-check the downcast is legal (we die
60 // if it's not).  In normal mode, we do the efficient static_cast<>
61 // instead.  Thus, it's important to test in debug mode to make sure
62 // the cast is legal!
63 //    This is the only place in the code we should use dynamic_cast<>.
64 // In particular, you SHOULDN'T be using dynamic_cast<> in order to
65 // do RTTI (eg code like this:
66 //    if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
67 //    if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
68 // You should design the code some other way not to need this.
69 
70 template<typename To, typename From>     // use like this: down_cast<T*>(foo);
down_cast(From * f)71 inline To down_cast(From* f) {                   // so we only accept pointers
72   static_assert(std::is_base_of<From, typename std::remove_pointer<To>::type>::value,
73                 "down_cast unsafe as To is not a subtype of From");
74 
75   return static_cast<To>(f);
76 }
77 
78 template <class Dest, class Source>
bit_cast(const Source & source)79 inline Dest bit_cast(const Source& source) {
80   // Compile time assertion: sizeof(Dest) == sizeof(Source)
81   // A compile error here means your Dest and Source have different sizes.
82   static_assert(sizeof(Dest) == sizeof(Source), "sizes should be equal");
83   Dest dest;
84   memcpy(&dest, &source, sizeof(dest));
85   return dest;
86 }
87 
88 // A version of static_cast that DCHECKs that the value can be precisely represented
89 // when converting to Dest.
90 template <typename Dest, typename Source>
dchecked_integral_cast(const Source source)91 inline Dest dchecked_integral_cast(const Source source) {
92   DCHECK(
93       // Check that the value is within the lower limit of Dest.
94       (static_cast<intmax_t>(std::numeric_limits<Dest>::min()) <=
95           static_cast<intmax_t>(std::numeric_limits<Source>::min()) ||
96           source >= static_cast<Source>(std::numeric_limits<Dest>::min())) &&
97       // Check that the value is within the upper limit of Dest.
98       (static_cast<uintmax_t>(std::numeric_limits<Dest>::max()) >=
99           static_cast<uintmax_t>(std::numeric_limits<Source>::max()) ||
100           source <= static_cast<Source>(std::numeric_limits<Dest>::max())));
101 
102   return static_cast<Dest>(source);
103 }
104 
105 }  // namespace art
106 
107 #endif  // ART_RUNTIME_BASE_CASTS_H_
108