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_LIBARTBASE_BASE_CASTS_H_ 18 #define ART_LIBARTBASE_BASE_CASTS_H_ 19 20 #include <assert.h> 21 #include <stdint.h> 22 #include <string.h> 23 24 #include <limits> 25 #include <type_traits> 26 27 #include <android-base/logging.h> 28 29 #include "stl_util_identity.h" 30 31 namespace art { 32 33 // Use implicit_cast as a safe version of static_cast or const_cast 34 // for upcasting in the type hierarchy (i.e. casting a pointer to Foo 35 // to a pointer to SuperclassOfFoo or casting a pointer to Foo to 36 // a const pointer to Foo). 37 // When you use implicit_cast, the compiler checks that the cast is safe. 38 // Such explicit implicit_casts are necessary in surprisingly many 39 // situations where C++ demands an exact type match instead of an 40 // argument type convertible to a target type. 41 // 42 // The From type can be inferred, so the preferred syntax for using 43 // implicit_cast is the same as for static_cast etc.: 44 // 45 // implicit_cast<ToType>(expr) 46 // 47 // implicit_cast would have been part of the C++ standard library, 48 // but the proposal was submitted too late. It will probably make 49 // its way into the language in the future. 50 template<typename To, typename From> 51 inline To implicit_cast(From const &f) { 52 return f; 53 } 54 55 // When you upcast (that is, cast a pointer from type Foo to type 56 // SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts 57 // always succeed. When you downcast (that is, cast a pointer from 58 // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because 59 // how do you know the pointer is really of type SubclassOfFoo? It 60 // could be a bare Foo, or of type DifferentSubclassOfFoo. Thus, 61 // when you downcast, you should use this macro. In debug mode, we 62 // use dynamic_cast<> to double-check the downcast is legal (we die 63 // if it's not). In normal mode, we do the efficient static_cast<> 64 // instead. Thus, it's important to test in debug mode to make sure 65 // the cast is legal! 66 // This is the only place in the code we should use dynamic_cast<>. 67 // In particular, you SHOULDN'T be using dynamic_cast<> in order to 68 // do RTTI (eg code like this: 69 // if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo); 70 // if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo); 71 // You should design the code some other way not to need this. 72 73 template<typename To, typename From> // use like this: down_cast<T*>(foo); 74 inline To down_cast(From* f) { // so we only accept pointers 75 static_assert(std::is_base_of<From, typename std::remove_pointer<To>::type>::value, 76 "down_cast unsafe as To is not a subtype of From"); 77 78 return static_cast<To>(f); 79 } 80 81 template<typename To, typename From> // use like this: down_cast<T&>(foo); 82 inline To down_cast(From& f) { // so we only accept references 83 static_assert(std::is_base_of<From, typename std::remove_reference<To>::type>::value, 84 "down_cast unsafe as To is not a subtype of From"); 85 86 return static_cast<To>(f); 87 } 88 89 template <class Dest, class Source> 90 inline Dest bit_cast(const Source& source) { 91 // Compile time assertion: sizeof(Dest) == sizeof(Source) 92 // A compile error here means your Dest and Source have different sizes. 93 static_assert(sizeof(Dest) == sizeof(Source), "sizes should be equal"); 94 Dest dest; 95 memcpy(&dest, &source, sizeof(dest)); 96 return dest; 97 } 98 99 // A version of static_cast that DCHECKs that the value can be precisely represented 100 // when converting to Dest. 101 template <typename Dest, typename Source> 102 constexpr Dest dchecked_integral_cast(Source source) { 103 DCHECK( 104 // Check that the value is within the lower limit of Dest. 105 (static_cast<intmax_t>(std::numeric_limits<Dest>::min()) <= 106 static_cast<intmax_t>(std::numeric_limits<Source>::min()) || 107 source >= static_cast<Source>(std::numeric_limits<Dest>::min())) && 108 // Check that the value is within the upper limit of Dest. 109 (static_cast<uintmax_t>(std::numeric_limits<Dest>::max()) >= 110 static_cast<uintmax_t>(std::numeric_limits<Source>::max()) || 111 source <= static_cast<Source>(std::numeric_limits<Dest>::max()))) 112 << "dchecked_integral_cast failed for " << source 113 << " (would be " << static_cast<Dest>(source) << ")"; 114 115 return static_cast<Dest>(source); 116 } 117 118 // A version of dchecked_integral_cast casting between an integral type and an enum type. 119 // When casting to an enum type, the cast does not check if the value corresponds to an enumerator. 120 // When casting from an enum type, the target type can be omitted and the enum's underlying type 121 // shall be used. 122 123 template <typename Dest, typename Source> 124 constexpr 125 typename std::enable_if<!std::is_enum<Source>::value, Dest>::type 126 enum_cast(Source value) { 127 return static_cast<Dest>( 128 dchecked_integral_cast<typename std::underlying_type<Dest>::type>(value)); 129 } 130 131 template <typename Dest = void, typename Source> 132 constexpr 133 typename std::enable_if<std::is_enum<Source>::value, 134 typename std::conditional<std::is_same<Dest, void>::value, 135 std::underlying_type<Source>, 136 Identity<Dest>>::type>::type::type 137 enum_cast(Source value) { 138 using return_type = typename std::conditional<std::is_same<Dest, void>::value, 139 std::underlying_type<Source>, 140 Identity<Dest>>::type::type; 141 return dchecked_integral_cast<return_type>( 142 static_cast<typename std::underlying_type<Source>::type>(value)); 143 } 144 145 // A version of reinterpret_cast<>() between pointers and int64_t/uint64_t 146 // that goes through uintptr_t to avoid treating the pointer as "signed." 147 148 template <typename Dest, typename Source> 149 inline Dest reinterpret_cast64(Source source) { 150 // This is the overload for casting from int64_t/uint64_t to a pointer. 151 static_assert(std::is_same<Source, int64_t>::value || std::is_same<Source, uint64_t>::value, 152 "Source must be int64_t or uint64_t."); 153 static_assert(std::is_pointer<Dest>::value, "Dest must be a pointer."); 154 // Check that we don't lose any non-0 bits here. 155 DCHECK_EQ(static_cast<Source>(static_cast<uintptr_t>(source)), source); 156 return reinterpret_cast<Dest>(static_cast<uintptr_t>(source)); 157 } 158 159 template <typename Dest, typename Source> 160 inline Dest reinterpret_cast64(Source* ptr) { 161 // This is the overload for casting from a pointer to int64_t/uint64_t. 162 static_assert(std::is_same<Dest, int64_t>::value || std::is_same<Dest, uint64_t>::value, 163 "Dest must be int64_t or uint64_t."); 164 static_assert(sizeof(uintptr_t) <= sizeof(Dest), "Expecting at most 64-bit pointers."); 165 return static_cast<Dest>(reinterpret_cast<uintptr_t>(ptr)); 166 } 167 168 // A version of reinterpret_cast<>() between pointers and int32_t/uint32_t that enforces 169 // zero-extension and checks that the values are converted without loss of precision. 170 171 template <typename Dest, typename Source> 172 inline Dest reinterpret_cast32(Source source) { 173 // This is the overload for casting from int32_t/uint32_t to a pointer. 174 static_assert(std::is_same<Source, int32_t>::value || std::is_same<Source, uint32_t>::value, 175 "Source must be int32_t or uint32_t."); 176 static_assert(std::is_pointer<Dest>::value, "Dest must be a pointer."); 177 // Check that we don't lose any non-0 bits here. 178 static_assert(sizeof(uintptr_t) >= sizeof(Source), "Expecting at least 32-bit pointers."); 179 return reinterpret_cast<Dest>(static_cast<uintptr_t>(static_cast<uint32_t>(source))); 180 } 181 182 template <typename Dest, typename Source> 183 inline Dest reinterpret_cast32(Source* ptr) { 184 // This is the overload for casting from a pointer to int32_t/uint32_t. 185 static_assert(std::is_same<Dest, int32_t>::value || std::is_same<Dest, uint32_t>::value, 186 "Dest must be int32_t or uint32_t."); 187 static_assert(sizeof(uintptr_t) >= sizeof(Dest), "Expecting at least 32-bit pointers."); 188 return static_cast<Dest>(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr))); 189 } 190 191 } // namespace art 192 193 #endif // ART_LIBARTBASE_BASE_CASTS_H_ 194