2 * Copyright 2016-present 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 <type_traits>
23 #include <folly/Traits.h>
30 * When you need a Meyers singleton that will not get destructed, even at
31 * shutdown, and you also want the object stored inline.
35 * void doSomethingWithExpensiveData();
37 * void doSomethingWithExpensiveData() {
38 * static const Indestructible<map<string, int>> data{
39 * map<string, int>{{"key1", 17}, {"key2", 19}, {"key3", 23}},
41 * callSomethingTakingAMapByRef(*data);
44 * This should be used only for Meyers singletons, and, even then, only when
45 * the instance does not need to be destructed ever.
47 * This should not be used more generally, e.g., as member fields, etc.
49 * This is designed as an alternative, but with one fewer allocation at
50 * construction time and one fewer pointer dereference at access time, to the
51 * Meyers singleton pattern of:
53 * void doSomethingWithExpensiveData() {
54 * static const auto data = // never `delete`d
55 * new map<string, int>{{"key1", 17}, {"key2", 19}, {"key3", 23}};
56 * callSomethingTakingAMapByRef(*data);
61 class Indestructible final {
64 template <typename S = T, typename = decltype(S())>
65 constexpr Indestructible() noexcept(noexcept(T())) {}
68 * Constructor accepting a single argument by forwarding reference, this
69 * allows using list initialzation without the overhead of things like
70 * in_place, etc and also works with std::initializer_list constructors
71 * which can't be deduced, the default parameter helps there.
73 * auto i = folly::Indestructible<std::map<int, int>>{{{1, 2}}};
75 * This provides convenience
77 * There are two versions of this constructor - one for when the element is
78 * implicitly constructible from the given argument and one for when the
79 * type is explicitly but not implicitly constructible from the given
84 _t<std::enable_if<std::is_constructible<T, U&&>::value>>* = nullptr,
86 !std::is_same<Indestructible<T>, remove_cvref_t<U>>::value>>* =
88 _t<std::enable_if<!std::is_convertible<U&&, T>::value>>* = nullptr>
89 explicit constexpr Indestructible(U&& u) noexcept(
90 noexcept(T(std::declval<U>())))
91 : storage_(std::forward<U>(u)) {}
94 _t<std::enable_if<std::is_constructible<T, U&&>::value>>* = nullptr,
96 !std::is_same<Indestructible<T>, remove_cvref_t<U>>::value>>* =
98 _t<std::enable_if<std::is_convertible<U&&, T>::value>>* = nullptr>
99 /* implicit */ constexpr Indestructible(U&& u) noexcept(
100 noexcept(T(std::declval<U>())))
101 : storage_(std::forward<U>(u)) {}
103 template <typename... Args, typename = decltype(T(std::declval<Args>()...))>
104 explicit constexpr Indestructible(Args&&... args) noexcept(
105 noexcept(T(std::declval<Args>()...)))
106 : storage_(std::forward<Args>(args)...) {}
111 T(std::declval<std::initializer_list<U>&>(),
112 std::declval<Args>()...))>
113 explicit constexpr Indestructible(std::initializer_list<U> il, Args... args) noexcept(
115 T(std::declval<std::initializer_list<U>&>(),
116 std::declval<Args>()...)))
117 : storage_(il, std::forward<Args>(args)...) {}
119 ~Indestructible() = default;
121 Indestructible(Indestructible const&) = delete;
122 Indestructible& operator=(Indestructible const&) = delete;
124 Indestructible(Indestructible&& other) noexcept(
125 noexcept(T(std::declval<T>())))
126 : storage_(std::move(other.storage_.value)) {
127 other.erased_ = true;
129 Indestructible& operator=(Indestructible&& other) noexcept(
130 noexcept(T(std::declval<T>()))) {
131 storage_.value = std::move(other.storage_.value);
132 other.erased_ = true;
137 return &storage_.value;
139 T const* get() const noexcept {
141 return &storage_.value;
143 T& operator*() noexcept { return *get(); }
144 T const& operator*() const noexcept { return *get(); }
145 T* operator->() noexcept { return get(); }
146 T const* operator->() const noexcept { return get(); }
149 void check() const noexcept {
156 template <typename S = T, typename = decltype(S())>
157 constexpr Storage() noexcept(noexcept(T())) : value() {}
159 template <typename... Args, typename = decltype(T(std::declval<Args>()...))>
160 explicit constexpr Storage(Args&&... args) noexcept(
161 noexcept(T(std::declval<Args>()...)))
162 : value(std::forward<Args>(args)...) {}