/src/abseil-cpp/absl/base/casts.h
Line | Count | Source |
1 | | // |
2 | | // Copyright 2017 The Abseil Authors. |
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 | | // https://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 | | // File: casts.h |
18 | | // ----------------------------------------------------------------------------- |
19 | | // |
20 | | // This header file defines casting templates to fit use cases not covered by |
21 | | // the standard casts provided in the C++ standard. As with all cast operations, |
22 | | // use these with caution and only if alternatives do not exist. |
23 | | |
24 | | #ifndef ABSL_BASE_CASTS_H_ |
25 | | #define ABSL_BASE_CASTS_H_ |
26 | | |
27 | | #include <cstring> |
28 | | #include <memory> |
29 | | #include <type_traits> |
30 | | #include <utility> |
31 | | |
32 | | #if defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L |
33 | | #include <bit> // For std::bit_cast. |
34 | | #endif // defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L |
35 | | |
36 | | #include "absl/base/macros.h" |
37 | | #include "absl/meta/type_traits.h" |
38 | | |
39 | | namespace absl { |
40 | | ABSL_NAMESPACE_BEGIN |
41 | | |
42 | | // implicit_cast() |
43 | | // |
44 | | // Performs an implicit conversion between types following the language |
45 | | // rules for implicit conversion; if an implicit conversion is otherwise |
46 | | // allowed by the language in the given context, this function performs such an |
47 | | // implicit conversion. |
48 | | // |
49 | | // Example: |
50 | | // |
51 | | // // If the context allows implicit conversion: |
52 | | // From from; |
53 | | // To to = from; |
54 | | // |
55 | | // // Such code can be replaced by: |
56 | | // implicit_cast<To>(from); |
57 | | // |
58 | | // An `implicit_cast()` may also be used to annotate numeric type conversions |
59 | | // that, although safe, may produce compiler warnings (such as `long` to `int`). |
60 | | // Additionally, an `implicit_cast()` is also useful within return statements to |
61 | | // indicate a specific implicit conversion is being undertaken. |
62 | | // |
63 | | // Example: |
64 | | // |
65 | | // return implicit_cast<double>(size_in_bytes) / capacity_; |
66 | | // |
67 | | // Annotating code with `implicit_cast()` allows you to explicitly select |
68 | | // particular overloads and template instantiations, while providing a safer |
69 | | // cast than `reinterpret_cast()` or `static_cast()`. |
70 | | // |
71 | | // Additionally, an `implicit_cast()` can be used to allow upcasting within a |
72 | | // type hierarchy where incorrect use of `static_cast()` could accidentally |
73 | | // allow downcasting. |
74 | | // |
75 | | // Finally, an `implicit_cast()` can be used to perform implicit conversions |
76 | | // from unrelated types that otherwise couldn't be implicitly cast directly; |
77 | | // C++ will normally only implicitly cast "one step" in such conversions. |
78 | | // |
79 | | // That is, if C is a type which can be implicitly converted to B, with B being |
80 | | // a type that can be implicitly converted to A, an `implicit_cast()` can be |
81 | | // used to convert C to B (which the compiler can then implicitly convert to A |
82 | | // using language rules). |
83 | | // |
84 | | // Example: |
85 | | // |
86 | | // // Assume an object C is convertible to B, which is implicitly convertible |
87 | | // // to A |
88 | | // A a = implicit_cast<B>(C); |
89 | | // |
90 | | // Such implicit cast chaining may be useful within template logic. |
91 | | template <typename To> |
92 | | constexpr To implicit_cast(typename absl::type_identity_t<To> to) { |
93 | | return to; |
94 | | } |
95 | | |
96 | | // bit_cast() |
97 | | // |
98 | | // Creates a value of the new type `Dest` whose representation is the same as |
99 | | // that of the argument, which is of (deduced) type `Source` (a "bitwise cast"; |
100 | | // every bit in the value representation of the result is equal to the |
101 | | // corresponding bit in the object representation of the source). Source and |
102 | | // destination types must be of the same size, and both types must be trivially |
103 | | // copyable. |
104 | | // |
105 | | // As with most casts, use with caution. A `bit_cast()` might be needed when you |
106 | | // need to treat a value as the value of some other type, for example, to access |
107 | | // the individual bits of an object which are not normally accessible through |
108 | | // the object's type, such as for working with the binary representation of a |
109 | | // floating point value: |
110 | | // |
111 | | // float f = 3.14159265358979; |
112 | | // int i = bit_cast<int>(f); |
113 | | // // i = 0x40490fdb |
114 | | // |
115 | | // Reinterpreting and accessing a value directly as a different type (as shown |
116 | | // below) usually results in undefined behavior. |
117 | | // |
118 | | // Example: |
119 | | // |
120 | | // // WRONG |
121 | | // float f = 3.14159265358979; |
122 | | // int i = reinterpret_cast<int&>(f); // Wrong |
123 | | // int j = *reinterpret_cast<int*>(&f); // Equally wrong |
124 | | // int k = *bit_cast<int*>(&f); // Equally wrong |
125 | | // |
126 | | // Reinterpret-casting results in undefined behavior according to the ISO C++ |
127 | | // specification, section [basic.lval]. Roughly, this section says: if an object |
128 | | // in memory has one type, and a program accesses it with a different type, the |
129 | | // result is undefined behavior for most "different type". |
130 | | // |
131 | | // Using bit_cast on a pointer and then dereferencing it is no better than using |
132 | | // reinterpret_cast. You should only use bit_cast on the value itself. |
133 | | // |
134 | | // Such casting results in type punning: holding an object in memory of one type |
135 | | // and reading its bits back using a different type. A `bit_cast()` avoids this |
136 | | // issue by copying the object representation to a new value, which avoids |
137 | | // introducing this undefined behavior (since the original value is never |
138 | | // accessed in the wrong way). |
139 | | // |
140 | | // The requirements of `absl::bit_cast` are more strict than that of |
141 | | // `std::bit_cast` unless compiler support is available. Specifically, without |
142 | | // compiler support, this implementation also requires `Dest` to be |
143 | | // default-constructible. In C++20, `absl::bit_cast` is replaced by |
144 | | // `std::bit_cast`. |
145 | | #if defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L |
146 | | |
147 | | using std::bit_cast; |
148 | | |
149 | | #else // defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L |
150 | | |
151 | | template < |
152 | | typename Dest, typename Source, |
153 | | typename std::enable_if<sizeof(Dest) == sizeof(Source) && |
154 | | std::is_trivially_copyable<Source>::value && |
155 | | std::is_trivially_copyable<Dest>::value |
156 | | #if !ABSL_HAVE_BUILTIN(__builtin_bit_cast) |
157 | | && std::is_default_constructible<Dest>::value |
158 | | #endif // !ABSL_HAVE_BUILTIN(__builtin_bit_cast) |
159 | | , |
160 | | int>::type = 0> |
161 | | #if ABSL_HAVE_BUILTIN(__builtin_bit_cast) |
162 | 10.6M | inline constexpr Dest bit_cast(const Source& source) { |
163 | 10.6M | return __builtin_bit_cast(Dest, source); |
164 | 10.6M | } Unexecuted instantiation: _ZN4absl8bit_castItsTnNSt3__19enable_ifIXaaaaeqstT_stT0_sr3std21is_trivially_copyableIS4_EE5valuesr3std21is_trivially_copyableIS3_EE5valueEiE4typeELi0EEES3_RKS4_ _ZN4absl8bit_castIstTnNSt3__19enable_ifIXaaaaeqstT_stT0_sr3std21is_trivially_copyableIS4_EE5valuesr3std21is_trivially_copyableIS3_EE5valueEiE4typeELi0EEES3_RKS4_ Line | Count | Source | 162 | 36 | inline constexpr Dest bit_cast(const Source& source) { | 163 | 36 | return __builtin_bit_cast(Dest, source); | 164 | 36 | } |
Unexecuted instantiation: _ZN4absl8bit_castIjiTnNSt3__19enable_ifIXaaaaeqstT_stT0_sr3std21is_trivially_copyableIS4_EE5valuesr3std21is_trivially_copyableIS3_EE5valueEiE4typeELi0EEES3_RKS4_ Unexecuted instantiation: _ZN4absl8bit_castIijTnNSt3__19enable_ifIXaaaaeqstT_stT0_sr3std21is_trivially_copyableIS4_EE5valuesr3std21is_trivially_copyableIS3_EE5valueEiE4typeELi0EEES3_RKS4_ _ZN4absl8bit_castImlTnNSt3__19enable_ifIXaaaaeqstT_stT0_sr3std21is_trivially_copyableIS4_EE5valuesr3std21is_trivially_copyableIS3_EE5valueEiE4typeELi0EEES3_RKS4_ Line | Count | Source | 162 | 36 | inline constexpr Dest bit_cast(const Source& source) { | 163 | 36 | return __builtin_bit_cast(Dest, source); | 164 | 36 | } |
Unexecuted instantiation: _ZN4absl8bit_castIlmTnNSt3__19enable_ifIXaaaaeqstT_stT0_sr3std21is_trivially_copyableIS4_EE5valuesr3std21is_trivially_copyableIS3_EE5valueEiE4typeELi0EEES3_RKS4_ _ZN4absl8bit_castIdmTnNSt3__19enable_ifIXaaaaeqstT_stT0_sr3std21is_trivially_copyableIS4_EE5valuesr3std21is_trivially_copyableIS3_EE5valueEiE4typeELi0EEES3_RKS4_ Line | Count | Source | 162 | 10.6M | inline constexpr Dest bit_cast(const Source& source) { | 163 | 10.6M | return __builtin_bit_cast(Dest, source); | 164 | 10.6M | } |
Unexecuted instantiation: _ZN4absl8bit_castIfjTnNSt3__19enable_ifIXaaaaeqstT_stT0_sr3std21is_trivially_copyableIS4_EE5valuesr3std21is_trivially_copyableIS3_EE5valueEiE4typeELi0EEES3_RKS4_ Unexecuted instantiation: _ZN4absl8bit_castImdTnNSt3__19enable_ifIXaaaaeqstT_stT0_sr3std21is_trivially_copyableIS4_EE5valuesr3std21is_trivially_copyableIS3_EE5valueEiE4typeELi0EEES3_RKS4_ Unexecuted instantiation: _ZN4absl8bit_castIjfTnNSt3__19enable_ifIXaaaaeqstT_stT0_sr3std21is_trivially_copyableIS4_EE5valuesr3std21is_trivially_copyableIS3_EE5valueEiE4typeELi0EEES3_RKS4_ _ZN4absl8bit_castIlNSt3__15arrayIcLm8EEETnNS1_9enable_ifIXaaaaeqstT_stT0_sr3std21is_trivially_copyableIS6_EE5valuesr3std21is_trivially_copyableIS5_EE5valueEiE4typeELi0EEES5_RKS6_ Line | Count | Source | 162 | 1 | inline constexpr Dest bit_cast(const Source& source) { | 163 | 1 | return __builtin_bit_cast(Dest, source); | 164 | 1 | } |
Unexecuted instantiation: _ZN4absl8bit_castINSt3__15arrayIcLm8EEElTnNS1_9enable_ifIXaaaaeqstT_stT0_sr3std21is_trivially_copyableIS6_EE5valuesr3std21is_trivially_copyableIS5_EE5valueEiE4typeELi0EEES5_RKS6_ Unexecuted instantiation: _ZN4absl8bit_castINS_14flags_internal19FlagValueAndInitBitIbEElTnNSt3__19enable_ifIXaaaaeqstT_stT0_sr3std21is_trivially_copyableIS7_EE5valuesr3std21is_trivially_copyableIS6_EE5valueEiE4typeELi0EEES6_RKS7_ |
165 | | #else // ABSL_HAVE_BUILTIN(__builtin_bit_cast) |
166 | | inline Dest bit_cast(const Source& source) { |
167 | | Dest dest; |
168 | | memcpy(static_cast<void*>(std::addressof(dest)), |
169 | | static_cast<const void*>(std::addressof(source)), sizeof(dest)); |
170 | | return dest; |
171 | | } |
172 | | #endif // ABSL_HAVE_BUILTIN(__builtin_bit_cast) |
173 | | |
174 | | #endif // defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L |
175 | | |
176 | | ABSL_NAMESPACE_END |
177 | | } // namespace absl |
178 | | |
179 | | #endif // ABSL_BASE_CASTS_H_ |