File indexing completed on 2025-01-31 10:12:24
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef GOOGLE_PROTOBUF_RAW_PTR_H__
0009 #define GOOGLE_PROTOBUF_RAW_PTR_H__
0010
0011 #include <algorithm>
0012
0013 #include "absl/base/optimization.h"
0014
0015
0016 #include "google/protobuf/port_def.inc"
0017
0018 namespace google {
0019 namespace protobuf {
0020 namespace internal {
0021
0022 PROTOBUF_EXPORT ABSL_CACHELINE_ALIGNED extern const char
0023 kZeroBuffer[std::max(ABSL_CACHELINE_SIZE, 64)];
0024
0025
0026
0027
0028 template <typename T>
0029 class RawPtr {
0030 public:
0031 constexpr RawPtr() : RawPtr(kZeroBuffer) {
0032 static_assert(sizeof(T) <= sizeof(kZeroBuffer), "");
0033 static_assert(alignof(T) <= ABSL_CACHELINE_SIZE, "");
0034 }
0035 explicit constexpr RawPtr(const void* p) : p_(const_cast<void*>(p)) {}
0036
0037 bool IsDefault() const { return p_ == kZeroBuffer; }
0038 void DeleteIfNotDefault() {
0039 if (!IsDefault()) delete Get();
0040 }
0041 void ClearIfNotDefault() {
0042 if (!IsDefault()) Get()->Clear();
0043 }
0044
0045 void Set(const void* p) { p_ = const_cast<void*>(p); }
0046 T* Get() const { return reinterpret_cast<T*>(p_); }
0047 T* operator->() const { return Get(); }
0048 T& operator*() const { return *Get(); }
0049
0050 private:
0051 void* p_;
0052 };
0053
0054 constexpr void* DefaultRawPtr() {
0055 return const_cast<void*>(static_cast<const void*>(kZeroBuffer));
0056 }
0057
0058 }
0059 }
0060 }
0061
0062 #include "google/protobuf/port_undef.inc"
0063
0064 #endif