1*1b3f573fSAndroid Build Coastguard Worker // Protocol Buffers - Google's data interchange format 2*1b3f573fSAndroid Build Coastguard Worker // Copyright 2008 Google Inc. All rights reserved. 3*1b3f573fSAndroid Build Coastguard Worker // https://developers.google.com/protocol-buffers/ 4*1b3f573fSAndroid Build Coastguard Worker // 5*1b3f573fSAndroid Build Coastguard Worker // Redistribution and use in source and binary forms, with or without 6*1b3f573fSAndroid Build Coastguard Worker // modification, are permitted provided that the following conditions are 7*1b3f573fSAndroid Build Coastguard Worker // met: 8*1b3f573fSAndroid Build Coastguard Worker // 9*1b3f573fSAndroid Build Coastguard Worker // * Redistributions of source code must retain the above copyright 10*1b3f573fSAndroid Build Coastguard Worker // notice, this list of conditions and the following disclaimer. 11*1b3f573fSAndroid Build Coastguard Worker // * Redistributions in binary form must reproduce the above 12*1b3f573fSAndroid Build Coastguard Worker // copyright notice, this list of conditions and the following disclaimer 13*1b3f573fSAndroid Build Coastguard Worker // in the documentation and/or other materials provided with the 14*1b3f573fSAndroid Build Coastguard Worker // distribution. 15*1b3f573fSAndroid Build Coastguard Worker // * Neither the name of Google Inc. nor the names of its 16*1b3f573fSAndroid Build Coastguard Worker // contributors may be used to endorse or promote products derived from 17*1b3f573fSAndroid Build Coastguard Worker // this software without specific prior written permission. 18*1b3f573fSAndroid Build Coastguard Worker // 19*1b3f573fSAndroid Build Coastguard Worker // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20*1b3f573fSAndroid Build Coastguard Worker // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21*1b3f573fSAndroid Build Coastguard Worker // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22*1b3f573fSAndroid Build Coastguard Worker // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23*1b3f573fSAndroid Build Coastguard Worker // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24*1b3f573fSAndroid Build Coastguard Worker // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25*1b3f573fSAndroid Build Coastguard Worker // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26*1b3f573fSAndroid Build Coastguard Worker // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27*1b3f573fSAndroid Build Coastguard Worker // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28*1b3f573fSAndroid Build Coastguard Worker // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29*1b3f573fSAndroid Build Coastguard Worker // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30*1b3f573fSAndroid Build Coastguard Worker 31*1b3f573fSAndroid Build Coastguard Worker #ifndef GOOGLE_PROTOBUF_EXPLICITLY_CONSTRUCTED_H__ 32*1b3f573fSAndroid Build Coastguard Worker #define GOOGLE_PROTOBUF_EXPLICITLY_CONSTRUCTED_H__ 33*1b3f573fSAndroid Build Coastguard Worker 34*1b3f573fSAndroid Build Coastguard Worker #include <stdint.h> 35*1b3f573fSAndroid Build Coastguard Worker 36*1b3f573fSAndroid Build Coastguard Worker #include <utility> 37*1b3f573fSAndroid Build Coastguard Worker 38*1b3f573fSAndroid Build Coastguard Worker #include <google/protobuf/stubs/logging.h> 39*1b3f573fSAndroid Build Coastguard Worker #include <google/protobuf/stubs/common.h> 40*1b3f573fSAndroid Build Coastguard Worker 41*1b3f573fSAndroid Build Coastguard Worker // clang-format off 42*1b3f573fSAndroid Build Coastguard Worker #include <google/protobuf/port_def.inc> 43*1b3f573fSAndroid Build Coastguard Worker // clang-format on 44*1b3f573fSAndroid Build Coastguard Worker 45*1b3f573fSAndroid Build Coastguard Worker namespace google { 46*1b3f573fSAndroid Build Coastguard Worker namespace protobuf { 47*1b3f573fSAndroid Build Coastguard Worker namespace internal { 48*1b3f573fSAndroid Build Coastguard Worker 49*1b3f573fSAndroid Build Coastguard Worker // Wraps a variable whose constructor and destructor are explicitly 50*1b3f573fSAndroid Build Coastguard Worker // called. It is particularly useful for a global variable, without its 51*1b3f573fSAndroid Build Coastguard Worker // constructor and destructor run on start and end of the program lifetime. 52*1b3f573fSAndroid Build Coastguard Worker // This circumvents the initial construction order fiasco, while keeping 53*1b3f573fSAndroid Build Coastguard Worker // the address of the empty string a compile time constant. 54*1b3f573fSAndroid Build Coastguard Worker // 55*1b3f573fSAndroid Build Coastguard Worker // Pay special attention to the initialization state of the object. 56*1b3f573fSAndroid Build Coastguard Worker // 1. The object is "uninitialized" to begin with. 57*1b3f573fSAndroid Build Coastguard Worker // 2. Call Construct() or DefaultConstruct() only if the object is 58*1b3f573fSAndroid Build Coastguard Worker // uninitialized. After the call, the object becomes "initialized". 59*1b3f573fSAndroid Build Coastguard Worker // 3. Call get() and get_mutable() only if the object is initialized. 60*1b3f573fSAndroid Build Coastguard Worker // 4. Call Destruct() only if the object is initialized. 61*1b3f573fSAndroid Build Coastguard Worker // After the call, the object becomes uninitialized. 62*1b3f573fSAndroid Build Coastguard Worker template <typename T, size_t min_align = 1> 63*1b3f573fSAndroid Build Coastguard Worker class ExplicitlyConstructed { 64*1b3f573fSAndroid Build Coastguard Worker public: DefaultConstruct()65*1b3f573fSAndroid Build Coastguard Worker void DefaultConstruct() { new (&union_) T(); } 66*1b3f573fSAndroid Build Coastguard Worker 67*1b3f573fSAndroid Build Coastguard Worker template <typename... Args> Construct(Args &&...args)68*1b3f573fSAndroid Build Coastguard Worker void Construct(Args&&... args) { 69*1b3f573fSAndroid Build Coastguard Worker new (&union_) T(std::forward<Args>(args)...); 70*1b3f573fSAndroid Build Coastguard Worker } 71*1b3f573fSAndroid Build Coastguard Worker Destruct()72*1b3f573fSAndroid Build Coastguard Worker void Destruct() { get_mutable()->~T(); } 73*1b3f573fSAndroid Build Coastguard Worker get()74*1b3f573fSAndroid Build Coastguard Worker constexpr const T& get() const { return reinterpret_cast<const T&>(union_); } get_mutable()75*1b3f573fSAndroid Build Coastguard Worker T* get_mutable() { return reinterpret_cast<T*>(&union_); } 76*1b3f573fSAndroid Build Coastguard Worker 77*1b3f573fSAndroid Build Coastguard Worker private: 78*1b3f573fSAndroid Build Coastguard Worker union AlignedUnion { 79*1b3f573fSAndroid Build Coastguard Worker alignas(min_align > alignof(T) ? min_align 80*1b3f573fSAndroid Build Coastguard Worker : alignof(T)) char space[sizeof(T)]; 81*1b3f573fSAndroid Build Coastguard Worker int64_t align_to_int64; 82*1b3f573fSAndroid Build Coastguard Worker void* align_to_ptr; 83*1b3f573fSAndroid Build Coastguard Worker } union_; 84*1b3f573fSAndroid Build Coastguard Worker }; 85*1b3f573fSAndroid Build Coastguard Worker 86*1b3f573fSAndroid Build Coastguard Worker // ArenaStringPtr compatible explicitly constructed string type. 87*1b3f573fSAndroid Build Coastguard Worker // This empty string type is aligned with a minimum alignment of 8 bytes 88*1b3f573fSAndroid Build Coastguard Worker // which is the minimum requirement of ArenaStringPtr 89*1b3f573fSAndroid Build Coastguard Worker using ExplicitlyConstructedArenaString = ExplicitlyConstructed<std::string, 8>; 90*1b3f573fSAndroid Build Coastguard Worker 91*1b3f573fSAndroid Build Coastguard Worker } // namespace internal 92*1b3f573fSAndroid Build Coastguard Worker } // namespace protobuf 93*1b3f573fSAndroid Build Coastguard Worker } // namespace google 94*1b3f573fSAndroid Build Coastguard Worker 95*1b3f573fSAndroid Build Coastguard Worker #include <google/protobuf/port_undef.inc> 96*1b3f573fSAndroid Build Coastguard Worker 97*1b3f573fSAndroid Build Coastguard Worker #endif // GOOGLE_PROTOBUF_EXPLICITLY_CONSTRUCTED_H__ 98