1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef INCLUDE_PERFETTO_BASE_NO_DESTRUCTOR_H_ 18 #define INCLUDE_PERFETTO_BASE_NO_DESTRUCTOR_H_ 19 20 #include <new> 21 #include <utility> 22 23 namespace perfetto { 24 namespace base { 25 26 // Wrapper that can hold an object of type T, without invoking the contained 27 // object's destructor when being destroyed. Useful for creating statics while 28 // avoiding static destructors. 29 // 30 // Stores the object inline, and therefore doesn't incur memory allocation and 31 // pointer indirection overheads. 32 // 33 // Example of use: 34 // 35 // const std::string& GetStr() { 36 // static base::NoDestructor<std::string> s("hello"); 37 // return s.ref(); 38 // } 39 // 40 template <typename T> 41 class NoDestructor { 42 public: 43 // Forward arguments to T's constructor. Note that this doesn't cover 44 // construction from initializer lists. 45 template <typename... Args> NoDestructor(Args &&...args)46 explicit NoDestructor(Args&&... args) { 47 new (storage_) T(std::forward<Args>(args)...); 48 } 49 50 NoDestructor(const NoDestructor&) = delete; 51 NoDestructor& operator=(const NoDestructor&) = delete; 52 NoDestructor(NoDestructor&&) = delete; 53 NoDestructor& operator=(NoDestructor&&) = delete; 54 55 ~NoDestructor() = default; 56 ref()57 const T& ref() const { return *reinterpret_cast<const T*>(storage_); } ref()58 T& ref() { return *reinterpret_cast<T*>(storage_); } 59 60 private: 61 alignas(T) char storage_[sizeof(T)]; 62 }; 63 64 } // namespace base 65 } // namespace perfetto 66 67 #endif // INCLUDE_PERFETTO_BASE_NO_DESTRUCTOR_H_ 68