folly: support FOLLY_FALLTHROUGH on GCC
[folly.git] / folly / Indestructible.h
1 /*
2  * Copyright 2017 Facebook, Inc.
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 #pragma once
18
19 #include <cassert>
20 #include <utility>
21
22 namespace folly {
23
24 /***
25  *  Indestructible
26  *
27  *  When you need a Meyers singleton that will not get destructed, even at
28  *  shutdown, and you also want the object stored inline.
29  *
30  *  Use like:
31  *
32  *      void doSomethingWithExpensiveData();
33  *
34  *      void doSomethingWithExpensiveData() {
35  *        static const Indestructible<map<string, int>> data{
36  *          map<string, int>{{"key1", 17}, {"key2", 19}, {"key3", 23}},
37  *        };
38  *        callSomethingTakingAMapByRef(*data);
39  *      }
40  *
41  *  This should be used only for Meyers singletons, and, even then, only when
42  *  the instance does not need to be destructed ever.
43  *
44  *  This should not be used more generally, e.g., as member fields, etc.
45  *
46  *  This is designed as an alternative, but with one fewer allocation at
47  *  construction time and one fewer pointer dereference at access time, to the
48  *  Meyers singleton pattern of:
49  *
50  *    void doSomethingWithExpensiveData() {
51  *      static const auto data =  // never `delete`d
52  *          new map<string, int>{{"key1", 17}, {"key2", 19}, {"key3", 23}};
53  *      callSomethingTakingAMapByRef(*data);
54  *    }
55  */
56
57 template <typename T>
58 class Indestructible final {
59
60  public:
61   template <typename S = T, typename = decltype(S())>
62   constexpr Indestructible() noexcept(noexcept(T())) {}
63
64   template <typename... Args, typename = decltype(T(std::declval<Args&&>()...))>
65   explicit constexpr Indestructible(Args&&... args) noexcept(
66       noexcept(T(std::declval<Args&&>()...)))
67       : storage_(std::forward<Args>(args)...) {}
68
69   ~Indestructible() = default;
70
71   Indestructible(Indestructible const&) = delete;
72   Indestructible& operator=(Indestructible const&) = delete;
73
74   Indestructible(Indestructible&& other) noexcept(
75       noexcept(T(std::declval<T&&>())))
76       : storage_(std::move(other.storage_.value)) {
77     other.erased_ = true;
78   }
79   Indestructible& operator=(Indestructible&& other) noexcept(
80       noexcept(T(std::declval<T&&>()))) {
81     storage_.value = std::move(other.storage_.value);
82     other.erased_ = true;
83   }
84
85   T* get() noexcept {
86     check();
87     return &storage_.value;
88   }
89   T const* get() const noexcept {
90     check();
91     return &storage_.value;
92   }
93   T& operator*() noexcept { return *get(); }
94   T const& operator*() const noexcept { return *get(); }
95   T* operator->() noexcept { return get(); }
96   T const* operator->() const noexcept { return get(); }
97
98  private:
99   void check() const noexcept {
100     assert(!erased_);
101   }
102
103   union Storage {
104     T value;
105
106     template <typename S = T, typename = decltype(S())>
107     constexpr Storage() noexcept(noexcept(T())) : value() {}
108
109     template <
110         typename... Args,
111         typename = decltype(T(std::declval<Args&&>()...))>
112     explicit constexpr Storage(Args&&... args) noexcept(
113         noexcept(T(std::declval<Args&&>()...)))
114         : value(std::forward<Args>(args)...) {}
115
116     ~Storage() {}
117   };
118
119   Storage storage_{};
120   bool erased_{false};
121 };
122 } // namespace folly