2017
[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 <utility>
20 #include <glog/logging.h>
21 #include <folly/Likely.h>
22 #include <folly/Portability.h>
23
24 namespace folly {
25
26 /***
27  *  Indestructible
28  *
29  *  When you need a Meyers singleton that will not get destructed, even at
30  *  shutdown, and you also want the object stored inline.
31  *
32  *  Use like:
33  *
34  *      void doSomethingWithExpensiveData();
35  *
36  *      void doSomethingWithExpensiveData() {
37  *        static const Indestructible<map<string, int>> data{
38  *          map<string, int>{{"key1", 17}, {"key2", 19}, {"key3", 23}},
39  *        };
40  *        callSomethingTakingAMapByRef(*data);
41  *      }
42  *
43  *  This should be used only for Meyers singletons, and, even then, only when
44  *  the instance does not need to be destructed ever.
45  *
46  *  This should not be used more generally, e.g., as member fields, etc.
47  *
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:
51  *
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);
56  *    }
57  */
58
59 template <typename T>
60 class Indestructible final {
61
62  public:
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) {}
67
68   ~Indestructible() = default;
69
70   Indestructible(Indestructible const&) = delete;
71   Indestructible& operator=(Indestructible const&) = delete;
72
73   Indestructible(Indestructible&& other) noexcept(
74       std::is_nothrow_move_constructible<T>::value)
75       : storage_(std::move(other.storage_.value)) {
76     other.inited_ = false;
77   }
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;
82   }
83
84   T* get() {
85     check();
86     return &storage_.value;
87   }
88   T const* get() const {
89     check();
90     return &storage_.value;
91   }
92   T& operator*() { return *get(); }
93   T const& operator*() const { return *get(); }
94   T* operator->() { return get(); }
95   T const* operator->() const { return get(); }
96
97  private:
98   void check() const {
99     if (UNLIKELY(!inited_)) {
100       fail();
101     }
102   }
103
104   [[noreturn]] FOLLY_NOINLINE static void fail() {
105     LOG(FATAL) << "Indestructible is not initialized";
106   }
107
108   union Storage {
109     T value;
110
111     template <typename... Args>
112     explicit constexpr Storage(Args&&... args)
113         : value(std::forward<Args>(args)...) {}
114
115     ~Storage() {}
116   };
117
118   Storage storage_;
119   bool inited_{false};
120 };
121 }