From 08550f0959f586304b532598e805ad129eeeb660 Mon Sep 17 00:00:00 2001 From: Simon Brand Date: Wed, 1 Nov 2017 19:54:18 +0000 Subject: [PATCH] Clean up code --- optional.hpp | 56 +++++++++++++++++++++------------------------------- 1 file changed, 23 insertions(+), 33 deletions(-) diff --git a/optional.hpp b/optional.hpp index 223709e..a276cbe 100644 --- a/optional.hpp +++ b/optional.hpp @@ -321,11 +321,9 @@ template struct optional_storage_base { template struct optional_operations_base : optional_storage_base { using optional_storage_base::optional_storage_base; - void reset() noexcept { - if (this->m_has_value) { - get().~T(); - this->m_has_value = false; - } + void hard_reset() noexcept { + get().~T(); + this->m_has_value = false; } template void construct(Args &&... args) noexcept { @@ -334,8 +332,8 @@ template struct optional_operations_base : optional_storage_base { } template void assign(Opt &&rhs) { - if (this->m_has_value) { - if (rhs.m_has_value) { + if (this->has_value()) { + if (rhs.has_value()) { this->m_value = std::forward(rhs).get(); } else { this->m_value.~T(); @@ -343,11 +341,13 @@ template struct optional_operations_base : optional_storage_base { } } - if (rhs.m_has_value) { + if (rhs.has_value()) { construct(std::forward(rhs).get()); } } + bool has_value() const { return this->m_has_value; } + TL_OPTIONAL_11_CONSTEXPR T &get() & { return this->m_value; } TL_OPTIONAL_11_CONSTEXPR const T &get() const & { return this->m_value; } TL_OPTIONAL_11_CONSTEXPR T &&get() && { std::move(this->m_value); } @@ -367,10 +367,10 @@ struct optional_copy_base : optional_operations_base { optional_copy_base() = default; optional_copy_base(const optional_copy_base &rhs) { - if (rhs.m_has_value) { + if (rhs.has_value()) { this->construct(rhs.get()); } else { - this->reset(); + this->hard_reset(); } } @@ -397,10 +397,10 @@ template struct optional_move_base : optional_copy_base { optional_move_base(optional_move_base &&rhs) noexcept( std::is_nothrow_move_constructible::value) { - if (rhs.m_has_value) { + if (rhs.has_value()) { this->construct(std::move(rhs.get())); } else { - this->reset(); + this->hard_reset(); } } optional_move_base &operator=(const optional_move_base &rhs) = default; @@ -1025,8 +1025,7 @@ public: Args &&...>::value, in_place_t>, std::initializer_list il, Args &&... args) { - this->m_has_value = true; - new (std::addressof(this->m_value)) T(il, std::forward(args)...); + this->construct(il, std::forward(args)...); } /// Constructs the stored value with `u`. @@ -1050,8 +1049,7 @@ public: class U, detail::enable_from_other * = nullptr, detail::enable_if_t::value> * = nullptr> optional(const optional &rhs) { - this->m_has_value = true; - new (std::addressof(this->m_value)) T(*rhs); + this->construct(*rhs); } /// \exclude @@ -1059,8 +1057,7 @@ public: detail::enable_if_t::value> * = nullptr> explicit optional(const optional &rhs) { - this->m_has_value = true; - new (std::addressof(this->m_value)) T(*rhs); + this->construct(*rhs); } /// Converting move constructor. @@ -1069,8 +1066,7 @@ public: class U, detail::enable_from_other * = nullptr, detail::enable_if_t::value> * = nullptr> optional(optional &&rhs) { - this->m_has_value = true; - new (std::addressof(this->m_value)) T(std::move(*rhs)); + this->construct(std::move(*rhs)); } /// \exclude @@ -1078,8 +1074,7 @@ public: class U, detail::enable_from_other * = nullptr, detail::enable_if_t::value> * = nullptr> explicit optional(optional &&rhs) { - this->m_has_value = true; - new (std::addressof(this->m_value)) T(std::move(*rhs)); + this->construct(std::move(*rhs)); } /// Destroys the stored value if there is one. @@ -1117,8 +1112,7 @@ public: if (has_value()) { this->m_value = std::forward(u); } else { - new (std::addressof(this->m_value)) T(std::forward(u)); - this->m_has_value = true; + this->construct(std::forward(u)); } return *this; @@ -1136,14 +1130,12 @@ public: if (rhs.has_value()) { this->m_value = *rhs; } else { - this->m_value.~T(); - this->m_has_value = false; + this->hard_reset(); } } if (rhs.has_value()) { - new (std::addressof(this->m_value)) T(*rhs); - this->m_has_value = true; + this->construct(*rhs); } return *this; @@ -1160,14 +1152,12 @@ public: if (rhs.has_value()) { this->m_value = std::move(*rhs); } else { - this->m_value.~T(); - this->m_has_value = false; + this->hard_reset(); } } if (rhs.has_value()) { - new (std::addressof(this->m_value)) T(std::move(*rhs)); - this->m_has_value = true; + this->construct(std::move(*rhs)); } return *this; @@ -1209,7 +1199,7 @@ public: using std::swap; swap(**this, *rhs); } else { - new (&rhs.m_value) T(std::move(this->m_value)); + new (std::addressof(rhs.m_value)) T(std::move(this->m_value)); this->m_value.T::~T(); } } else if (rhs.has_value()) {