1*9507f98cSAndroid Build Coastguard Worker // Copyright (c) 2018 The LevelDB Authors. All rights reserved. 2*9507f98cSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*9507f98cSAndroid Build Coastguard Worker // found in the LICENSE file. See the AUTHORS file for names of contributors. 4*9507f98cSAndroid Build Coastguard Worker 5*9507f98cSAndroid Build Coastguard Worker #ifndef STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_ 6*9507f98cSAndroid Build Coastguard Worker #define STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_ 7*9507f98cSAndroid Build Coastguard Worker 8*9507f98cSAndroid Build Coastguard Worker #include <type_traits> 9*9507f98cSAndroid Build Coastguard Worker #include <utility> 10*9507f98cSAndroid Build Coastguard Worker 11*9507f98cSAndroid Build Coastguard Worker namespace leveldb { 12*9507f98cSAndroid Build Coastguard Worker 13*9507f98cSAndroid Build Coastguard Worker // Wraps an instance whose destructor is never called. 14*9507f98cSAndroid Build Coastguard Worker // 15*9507f98cSAndroid Build Coastguard Worker // This is intended for use with function-level static variables. 16*9507f98cSAndroid Build Coastguard Worker template <typename InstanceType> 17*9507f98cSAndroid Build Coastguard Worker class NoDestructor { 18*9507f98cSAndroid Build Coastguard Worker public: 19*9507f98cSAndroid Build Coastguard Worker template <typename... ConstructorArgTypes> NoDestructor(ConstructorArgTypes &&...constructor_args)20*9507f98cSAndroid Build Coastguard Worker explicit NoDestructor(ConstructorArgTypes&&... constructor_args) { 21*9507f98cSAndroid Build Coastguard Worker static_assert(sizeof(instance_storage_) >= sizeof(InstanceType), 22*9507f98cSAndroid Build Coastguard Worker "instance_storage_ is not large enough to hold the instance"); 23*9507f98cSAndroid Build Coastguard Worker static_assert( 24*9507f98cSAndroid Build Coastguard Worker alignof(decltype(instance_storage_)) >= alignof(InstanceType), 25*9507f98cSAndroid Build Coastguard Worker "instance_storage_ does not meet the instance's alignment requirement"); 26*9507f98cSAndroid Build Coastguard Worker new (&instance_storage_) 27*9507f98cSAndroid Build Coastguard Worker InstanceType(std::forward<ConstructorArgTypes>(constructor_args)...); 28*9507f98cSAndroid Build Coastguard Worker } 29*9507f98cSAndroid Build Coastguard Worker 30*9507f98cSAndroid Build Coastguard Worker ~NoDestructor() = default; 31*9507f98cSAndroid Build Coastguard Worker 32*9507f98cSAndroid Build Coastguard Worker NoDestructor(const NoDestructor&) = delete; 33*9507f98cSAndroid Build Coastguard Worker NoDestructor& operator=(const NoDestructor&) = delete; 34*9507f98cSAndroid Build Coastguard Worker get()35*9507f98cSAndroid Build Coastguard Worker InstanceType* get() { 36*9507f98cSAndroid Build Coastguard Worker return reinterpret_cast<InstanceType*>(&instance_storage_); 37*9507f98cSAndroid Build Coastguard Worker } 38*9507f98cSAndroid Build Coastguard Worker 39*9507f98cSAndroid Build Coastguard Worker private: 40*9507f98cSAndroid Build Coastguard Worker typename std::aligned_storage<sizeof(InstanceType), 41*9507f98cSAndroid Build Coastguard Worker alignof(InstanceType)>::type instance_storage_; 42*9507f98cSAndroid Build Coastguard Worker }; 43*9507f98cSAndroid Build Coastguard Worker 44*9507f98cSAndroid Build Coastguard Worker } // namespace leveldb 45*9507f98cSAndroid Build Coastguard Worker 46*9507f98cSAndroid Build Coastguard Worker #endif // STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_ 47