From ed1f15151babee9ebc690ffa5c2593119540e8f0 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 23 Nov 2018 19:18:51 +0900 Subject: Omittable: make operator-> and operator* return an empty object if omitted == true That is, instead of Q_ASSERTing in debug builds (release builds already work that way). The idea is that since the value is default-initialised anyway it can be used as a "blank canvas" to access specific fields inside the value's structure. The next commit will use that. --- lib/util.h | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'lib/util.h') diff --git a/lib/util.h b/lib/util.h index 6b5c89e6..1028e059 100644 --- a/lib/util.h +++ b/lib/util.h @@ -88,17 +88,19 @@ namespace QMatrixClient static_assert(!std::is_reference::value, "You cannot make an Omittable<> with a reference type"); public: + using value_type = std::decay_t; + explicit Omittable() : Omittable(none) { } - Omittable(NoneTag) : _value(std::decay_t()), _omitted(true) { } - Omittable(const std::decay_t& val) : _value(val) { } - Omittable(std::decay_t&& val) : _value(std::move(val)) { } - Omittable& operator=(const std::decay_t& val) + Omittable(NoneTag) : _value(value_type()), _omitted(true) { } + Omittable(const value_type& val) : _value(val) { } + Omittable(value_type&& val) : _value(std::move(val)) { } + Omittable& operator=(const value_type& val) { _value = val; _omitted = false; return *this; } - Omittable& operator=(std::decay_t&& val) + Omittable& operator=(value_type&& val) { _value = std::move(val); _omitted = false; @@ -106,15 +108,15 @@ namespace QMatrixClient } bool omitted() const { return _omitted; } - const std::decay_t& value() const { Q_ASSERT(!_omitted); return _value; } - std::decay_t& value() { Q_ASSERT(!_omitted); return _value; } - std::decay_t&& release() { _omitted = true; return std::move(_value); } + const value_type& value() const { Q_ASSERT(!_omitted); return _value; } + value_type& value() { Q_ASSERT(!_omitted); return _value; } + value_type&& release() { _omitted = true; return std::move(_value); } operator bool() const { return !omitted(); } - const std::decay* operator->() const { return &value(); } - std::decay_t* operator->() { return &value(); } - const std::decay_t& operator*() const { return value(); } - std::decay_t& operator*() { return value(); } + const value_type* operator->() const { return &_value; } + value_type* operator->() { return &_value; } + const value_type& operator*() const { return _value; } + value_type& operator*() { return _value; } private: T _value; -- cgit v1.2.3