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