X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FIndestructible.h;h=285d9ed6a6d3529850de5ca31612aa5d3dcf9a36;hb=f57ddfc76b3835033a36343df22ae821168dcc0f;hp=686bbce3014b73fe94cd0fbc0323985446a18b34;hpb=ed8c80a0e0988e4ce687f51ca832a00e4a6b7930;p=folly.git diff --git a/folly/Indestructible.h b/folly/Indestructible.h index 686bbce3..285d9ed6 100644 --- a/folly/Indestructible.h +++ b/folly/Indestructible.h @@ -16,10 +16,9 @@ #pragma once +#include +#include #include -#include -#include -#include namespace folly { @@ -60,10 +59,13 @@ template class Indestructible final { public: - template + template + constexpr Indestructible() noexcept(noexcept(T())) {} + + template ()...))> explicit constexpr Indestructible(Args&&... args) noexcept( std::is_nothrow_constructible::value) - : storage_(std::forward(args)...), inited_(true) {} + : storage_(std::forward(args)...) {} ~Indestructible() = default; @@ -73,12 +75,12 @@ class Indestructible final { Indestructible(Indestructible&& other) noexcept( std::is_nothrow_move_constructible::value) : storage_(std::move(other.storage_.value)) { - other.inited_ = false; + other.erased_ = true; } Indestructible& operator=(Indestructible&& other) noexcept( std::is_nothrow_move_assignable::value) { storage_.value = std::move(other.storage_.value); - other.inited_ = false; + other.erased_ = true; } T* get() { @@ -96,26 +98,25 @@ class Indestructible final { private: void check() const { - if (UNLIKELY(!inited_)) { - fail(); - } - } - - [[noreturn]] FOLLY_NOINLINE static void fail() { - LOG(FATAL) << "Indestructible is not initialized"; + assert(!erased_); } union Storage { T value; - template + template + constexpr Storage() noexcept(noexcept(T())) : value() {} + + template < + typename... Args, + typename = decltype(T(std::declval()...))> explicit constexpr Storage(Args&&... args) : value(std::forward(args)...) {} ~Storage() {} }; - Storage storage_; - bool inited_{false}; + Storage storage_{}; + bool erased_{false}; }; }