2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <glog/logging.h>
21 #include <folly/Likely.h>
22 #include <folly/Portability.h>
29 * When you need a Meyers singleton that will not get destructed, even at
30 * shutdown, and you also want the object stored inline.
34 * void doSomethingWithExpensiveData();
36 * void doSomethingWithExpensiveData() {
37 * static const Indestructible<map<string, int>> data{
38 * map<string, int>{{"key1", 17}, {"key2", 19}, {"key3", 23}},
40 * callSomethingTakingAMapByRef(*data);
43 * This should be used only for Meyers singletons, and, even then, only when
44 * the instance does not need to be destructed ever.
46 * This should not be used more generally, e.g., as member fields, etc.
48 * This is designed as an alternative, but with one fewer allocation at
49 * construction time and one fewer pointer dereference at access time, to the
50 * Meyers singleton pattern of:
52 * void doSomethingWithExpensiveData() {
53 * static const auto data = // never `delete`d
54 * new map<string, int>{{"key1", 17}, {"key2", 19}, {"key3", 23}};
55 * callSomethingTakingAMapByRef(*data);
60 class Indestructible final {
63 template <typename... Args>
64 explicit constexpr Indestructible(Args&&... args) noexcept(
65 std::is_nothrow_constructible<T, Args&&...>::value)
66 : storage_(std::forward<Args>(args)...), inited_(true) {}
68 ~Indestructible() = default;
70 Indestructible(Indestructible const&) = delete;
71 Indestructible& operator=(Indestructible const&) = delete;
73 Indestructible(Indestructible&& other) noexcept(
74 std::is_nothrow_move_constructible<T>::value)
75 : storage_(std::move(other.storage_.value)) {
76 other.inited_ = false;
78 Indestructible& operator=(Indestructible&& other) noexcept(
79 std::is_nothrow_move_assignable<T>::value) {
80 storage_.value = std::move(other.storage_.value);
81 other.inited_ = false;
86 return &storage_.value;
88 T const* get() const {
90 return &storage_.value;
92 T& operator*() { return *get(); }
93 T const& operator*() const { return *get(); }
94 T* operator->() { return get(); }
95 T const* operator->() const { return get(); }
99 if (UNLIKELY(!inited_)) {
104 [[noreturn]] FOLLY_NOINLINE static void fail() {
105 LOG(FATAL) << "Indestructible is not initialized";
111 template <typename... Args>
112 explicit constexpr Storage(Args&&... args)
113 : value(std::forward<Args>(args)...) {}