1 // Copyright 2023 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_SRC_PARTITION_ALLOC_POINTERS_RAW_PTR_CAST_H_ 6 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_SRC_PARTITION_ALLOC_POINTERS_RAW_PTR_CAST_H_ 7 8 #include <bit> 9 #include <memory> 10 #include <type_traits> 11 12 // This header is explicitly allowlisted from a clang plugin rule at 13 // "tools/clang/plugins/FindBadRawPtrPatterns.cpp". You can bypass these checks 14 // by performing casts explicitly with functions here. 15 namespace base { 16 17 // Wrapper for |static_cast<T>(src)|. 18 template <typename Dest, typename Source> unsafe_raw_ptr_static_cast(Source && source)19inline constexpr Dest unsafe_raw_ptr_static_cast(Source&& source) noexcept { 20 return static_cast<Dest>(source); 21 } 22 23 // Wrapper for |reinterpret_cast<T>(src)|. 24 template <typename Dest, typename Source> unsafe_raw_ptr_reinterpret_cast(Source && source)25inline constexpr Dest unsafe_raw_ptr_reinterpret_cast( 26 Source&& source) noexcept { 27 return reinterpret_cast<Dest>(source); 28 } 29 30 // Wrapper for |std::bit_cast<T>(src)|. 31 template <typename Dest, typename Source> unsafe_raw_ptr_bit_cast(const Source & source)32inline constexpr Dest unsafe_raw_ptr_bit_cast(const Source& source) noexcept { 33 return std::bit_cast<Dest>(source); 34 } 35 36 } // namespace base 37 38 #endif // BASE_ALLOCATOR_PARTITION_ALLOCATOR_SRC_PARTITION_ALLOC_POINTERS_RAW_PTR_CAST_H_ 39