From d03d69933160caf14cc037e1b332c3f2e416a6a7 Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 5 Jun 2018 21:46:05 +0300 Subject: [PATCH] Improve base::optional, add emplace(). --- Telegram/SourceFiles/base/optional.h | 41 +++++++++++++++++++--------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/Telegram/SourceFiles/base/optional.h b/Telegram/SourceFiles/base/optional.h index 06de29ccc..4c8f41841 100644 --- a/Telegram/SourceFiles/base/optional.h +++ b/Telegram/SourceFiles/base/optional.h @@ -87,6 +87,15 @@ public: return _impl >= other._impl; } + template + T &set(Args &&...args) { + _impl.set(std::forward(args)...); + return get_unchecked(); + } + void clear() { + _impl.set(); + } + template decltype(auto) is() const { return _impl.template is(); @@ -146,28 +155,34 @@ using optional_chain_result_t = typename optional_chain_result::type; template class optional : public optional_variant { + using parent = optional_variant; + public: - using optional_variant::optional_variant; + using parent::parent; Type &operator*() { - auto result = get_if(this); - Expects(result != nullptr); - return *result; + Expects(parent::template is()); + + return parent::template get_unchecked(); } const Type &operator*() const { - auto result = get_if(this); - Expects(result != nullptr); - return *result; + Expects(parent::template is()); + + return parent::template get_unchecked(); } Type *operator->() { - auto result = get_if(this); - Expects(result != nullptr); - return result; + Expects(parent::template is()); + + return std::addressof(parent::template get_unchecked()); } const Type *operator->() const { - auto result = get_if(this); - Expects(result != nullptr); - return result; + Expects(parent::template is()); + + return std::addressof(parent::template get_unchecked()); + } + template + Type &emplace(Args &&...args) { + return parent::template set(std::forward(args)...); } };