Indestructible.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright 2016-present 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. #pragma once
  17. #include <cassert>
  18. #include <type_traits>
  19. #include <utility>
  20. #include <folly/Traits.h>
  21. namespace folly {
  22. /***
  23. * Indestructible
  24. *
  25. * When you need a Meyers singleton that will not get destructed, even at
  26. * shutdown, and you also want the object stored inline.
  27. *
  28. * Use like:
  29. *
  30. * void doSomethingWithExpensiveData();
  31. *
  32. * void doSomethingWithExpensiveData() {
  33. * static const Indestructible<map<string, int>> data{
  34. * map<string, int>{{"key1", 17}, {"key2", 19}, {"key3", 23}},
  35. * };
  36. * callSomethingTakingAMapByRef(*data);
  37. * }
  38. *
  39. * This should be used only for Meyers singletons, and, even then, only when
  40. * the instance does not need to be destructed ever.
  41. *
  42. * This should not be used more generally, e.g., as member fields, etc.
  43. *
  44. * This is designed as an alternative, but with one fewer allocation at
  45. * construction time and one fewer pointer dereference at access time, to the
  46. * Meyers singleton pattern of:
  47. *
  48. * void doSomethingWithExpensiveData() {
  49. * static const auto data = // never `delete`d
  50. * new map<string, int>{{"key1", 17}, {"key2", 19}, {"key3", 23}};
  51. * callSomethingTakingAMapByRef(*data);
  52. * }
  53. */
  54. template <typename T>
  55. class Indestructible final {
  56. public:
  57. template <typename S = T, typename = decltype(S())>
  58. constexpr Indestructible() noexcept(noexcept(T())) {}
  59. /**
  60. * Constructor accepting a single argument by forwarding reference, this
  61. * allows using list initialzation without the overhead of things like
  62. * in_place, etc and also works with std::initializer_list constructors
  63. * which can't be deduced, the default parameter helps there.
  64. *
  65. * auto i = folly::Indestructible<std::map<int, int>>{{{1, 2}}};
  66. *
  67. * This provides convenience
  68. *
  69. * There are two versions of this constructor - one for when the element is
  70. * implicitly constructible from the given argument and one for when the
  71. * type is explicitly but not implicitly constructible from the given
  72. * argument.
  73. */
  74. template <
  75. typename U = T,
  76. _t<std::enable_if<std::is_constructible<T, U&&>::value>>* = nullptr,
  77. _t<std::enable_if<
  78. !std::is_same<Indestructible<T>, remove_cvref_t<U>>::value>>* =
  79. nullptr,
  80. _t<std::enable_if<!std::is_convertible<U&&, T>::value>>* = nullptr>
  81. explicit constexpr Indestructible(U&& u) noexcept(
  82. noexcept(T(std::declval<U>())))
  83. : storage_(std::forward<U>(u)) {}
  84. template <
  85. typename U = T,
  86. _t<std::enable_if<std::is_constructible<T, U&&>::value>>* = nullptr,
  87. _t<std::enable_if<
  88. !std::is_same<Indestructible<T>, remove_cvref_t<U>>::value>>* =
  89. nullptr,
  90. _t<std::enable_if<std::is_convertible<U&&, T>::value>>* = nullptr>
  91. /* implicit */ constexpr Indestructible(U&& u) noexcept(
  92. noexcept(T(std::declval<U>())))
  93. : storage_(std::forward<U>(u)) {}
  94. template <typename... Args, typename = decltype(T(std::declval<Args>()...))>
  95. explicit constexpr Indestructible(Args&&... args) noexcept(
  96. noexcept(T(std::declval<Args>()...)))
  97. : storage_(std::forward<Args>(args)...) {}
  98. template <
  99. typename U,
  100. typename... Args,
  101. typename = decltype(
  102. T(std::declval<std::initializer_list<U>&>(),
  103. std::declval<Args>()...))>
  104. explicit constexpr Indestructible(std::initializer_list<U> il, Args... args) noexcept(
  105. noexcept(
  106. T(std::declval<std::initializer_list<U>&>(),
  107. std::declval<Args>()...)))
  108. : storage_(il, std::forward<Args>(args)...) {}
  109. ~Indestructible() = default;
  110. Indestructible(Indestructible const&) = delete;
  111. Indestructible& operator=(Indestructible const&) = delete;
  112. Indestructible(Indestructible&& other) noexcept(
  113. noexcept(T(std::declval<T>())))
  114. : storage_(std::move(other.storage_.value)) {
  115. other.erased_ = true;
  116. }
  117. Indestructible& operator=(Indestructible&& other) noexcept(
  118. noexcept(T(std::declval<T>()))) {
  119. storage_.value = std::move(other.storage_.value);
  120. other.erased_ = true;
  121. }
  122. T* get() noexcept {
  123. check();
  124. return &storage_.value;
  125. }
  126. T const* get() const noexcept {
  127. check();
  128. return &storage_.value;
  129. }
  130. T& operator*() noexcept {
  131. return *get();
  132. }
  133. T const& operator*() const noexcept {
  134. return *get();
  135. }
  136. T* operator->() noexcept {
  137. return get();
  138. }
  139. T const* operator->() const noexcept {
  140. return get();
  141. }
  142. private:
  143. void check() const noexcept {
  144. assert(!erased_);
  145. }
  146. union Storage {
  147. T value;
  148. template <typename S = T, typename = decltype(S())>
  149. constexpr Storage() noexcept(noexcept(T())) : value() {}
  150. template <typename... Args, typename = decltype(T(std::declval<Args>()...))>
  151. explicit constexpr Storage(Args&&... args) noexcept(
  152. noexcept(T(std::declval<Args>()...)))
  153. : value(std::forward<Args>(args)...) {}
  154. ~Storage() {}
  155. };
  156. Storage storage_{};
  157. bool erased_{false};
  158. };
  159. } // namespace folly