Futex::futexWait returns FutexResult
[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 #include <folly/Traits.h>
24
25 namespace folly {
26
27 /***
28  *  Indestructible
29  *
30  *  When you need a Meyers singleton that will not get destructed, even at
31  *  shutdown, and you also want the object stored inline.
32  *
33  *  Use like:
34  *
35  *      void doSomethingWithExpensiveData();
36  *
37  *      void doSomethingWithExpensiveData() {
38  *        static const Indestructible<map<string, int>> data{
39  *          map<string, int>{{"key1", 17}, {"key2", 19}, {"key3", 23}},
40  *        };
41  *        callSomethingTakingAMapByRef(*data);
42  *      }
43  *
44  *  This should be used only for Meyers singletons, and, even then, only when
45  *  the instance does not need to be destructed ever.
46  *
47  *  This should not be used more generally, e.g., as member fields, etc.
48  *
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:
52  *
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);
57  *    }
58  */
59
60 template <typename T>
61 class Indestructible final {
62
63  public:
64   template <typename S = T, typename = decltype(S())>
65   constexpr Indestructible() noexcept(noexcept(T())) {}
66
67   /**
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.
72    *
73    *    auto i = folly::Indestructible<std::map<int, int>>{{{1, 2}}};
74    *
75    * This provides convenience
76    *
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
80    * argument.
81    */
82   template <
83       typename U = T,
84       _t<std::enable_if<std::is_constructible<T, U&&>::value>>* = nullptr,
85       _t<std::enable_if<
86           !std::is_same<Indestructible<T>, remove_cvref_t<U>>::value>>* =
87           nullptr,
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)) {}
92   template <
93       typename U = T,
94       _t<std::enable_if<std::is_constructible<T, U&&>::value>>* = nullptr,
95       _t<std::enable_if<
96           !std::is_same<Indestructible<T>, remove_cvref_t<U>>::value>>* =
97           nullptr,
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)) {}
102
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)...) {}
107   template <
108       typename U,
109       typename... Args,
110       typename = decltype(
111           T(std::declval<std::initializer_list<U>&>(),
112             std::declval<Args>()...))>
113   explicit constexpr Indestructible(std::initializer_list<U> il, Args... args) noexcept(
114       noexcept(
115           T(std::declval<std::initializer_list<U>&>(),
116             std::declval<Args>()...)))
117       : storage_(il, std::forward<Args>(args)...) {}
118
119   ~Indestructible() = default;
120
121   Indestructible(Indestructible const&) = delete;
122   Indestructible& operator=(Indestructible const&) = delete;
123
124   Indestructible(Indestructible&& other) noexcept(
125       noexcept(T(std::declval<T>())))
126       : storage_(std::move(other.storage_.value)) {
127     other.erased_ = true;
128   }
129   Indestructible& operator=(Indestructible&& other) noexcept(
130       noexcept(T(std::declval<T>()))) {
131     storage_.value = std::move(other.storage_.value);
132     other.erased_ = true;
133   }
134
135   T* get() noexcept {
136     check();
137     return &storage_.value;
138   }
139   T const* get() const noexcept {
140     check();
141     return &storage_.value;
142   }
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(); }
147
148  private:
149   void check() const noexcept {
150     assert(!erased_);
151   }
152
153   union Storage {
154     T value;
155
156     template <typename S = T, typename = decltype(S())>
157     constexpr Storage() noexcept(noexcept(T())) : value() {}
158
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)...) {}
163
164     ~Storage() {}
165   };
166
167   Storage storage_{};
168   bool erased_{false};
169 };
170 } // namespace folly