Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:12:24

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
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 // Must be included last.
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 // This class is trivially copyable/trivially destructible and constexpr
0026 // constructible. The class allows for storing a raw pointer to a non-trivial
0027 // object in a constexpr context.
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 }  // namespace internal
0059 }  // namespace protobuf
0060 }  // namespace google
0061 
0062 #include "google/protobuf/port_undef.inc"
0063 
0064 #endif  // GOOGLE_PROTOBUF_RAW_PTR_H__