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