2 * Copyright 2017 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>
28 * When you need a Meyers singleton that will not get destructed, even at
29 * shutdown, and you also want the object stored inline.
33 * void doSomethingWithExpensiveData();
35 * void doSomethingWithExpensiveData() {
36 * static const Indestructible<map<string, int>> data{
37 * map<string, int>{{"key1", 17}, {"key2", 19}, {"key3", 23}},
39 * callSomethingTakingAMapByRef(*data);
42 * This should be used only for Meyers singletons, and, even then, only when
43 * the instance does not need to be destructed ever.
45 * This should not be used more generally, e.g., as member fields, etc.
47 * This is designed as an alternative, but with one fewer allocation at
48 * construction time and one fewer pointer dereference at access time, to the
49 * Meyers singleton pattern of:
51 * void doSomethingWithExpensiveData() {
52 * static const auto data = // never `delete`d
53 * new map<string, int>{{"key1", 17}, {"key2", 19}, {"key3", 23}};
54 * callSomethingTakingAMapByRef(*data);
59 class Indestructible final {
62 template <typename S = T, typename = decltype(S())>
63 constexpr Indestructible() noexcept(noexcept(T())) {}
65 template <typename... Args, typename = decltype(T(std::declval<Args&&>()...))>
66 explicit constexpr Indestructible(Args&&... args) noexcept(
67 std::is_nothrow_constructible<T, Args&&...>::value)
68 : storage_(std::forward<Args>(args)...) {}
70 ~Indestructible() = default;
72 Indestructible(Indestructible const&) = delete;
73 Indestructible& operator=(Indestructible const&) = delete;
75 Indestructible(Indestructible&& other) noexcept(
76 std::is_nothrow_move_constructible<T>::value)
77 : storage_(std::move(other.storage_.value)) {
80 Indestructible& operator=(Indestructible&& other) noexcept(
81 std::is_nothrow_move_assignable<T>::value) {
82 storage_.value = std::move(other.storage_.value);
88 return &storage_.value;
90 T const* get() const {
92 return &storage_.value;
94 T& operator*() { return *get(); }
95 T const& operator*() const { return *get(); }
96 T* operator->() { return get(); }
97 T const* operator->() const { return get(); }
107 template <typename S = T, typename = decltype(S())>
108 constexpr Storage() noexcept(noexcept(T())) : value() {}
112 typename = decltype(T(std::declval<Args&&>()...))>
113 explicit constexpr Storage(Args&&... args)
114 : value(std::forward<Args>(args)...) {}