From cab30e850847ac221b08c3a8b715d0298bb63ddf Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 8 Jul 2018 09:02:23 +0900 Subject: Move Omittable<> to util.h --- lib/converters.h | 40 ---------------------------------------- lib/util.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 40 deletions(-) (limited to 'lib') diff --git a/lib/converters.h b/lib/converters.h index c01d7c8e..0b37fc48 100644 --- a/lib/converters.h +++ b/lib/converters.h @@ -56,46 +56,6 @@ class QVariant; namespace QMatrixClient { - struct NoneTag {}; - constexpr NoneTag none {}; - - /** A crude substitute for `optional` while we're not C++17 - * - * Only works with default-constructible types. - */ - template - class Omittable - { - public: - explicit Omittable() : Omittable(none) { } - Omittable(NoneTag) : _omitted(true) { } - Omittable(const T& val) : _value(val) { } - Omittable(T&& val) : _value(std::move(val)) { } - Omittable& operator=(const T& val) - { - _value = val; - _omitted = false; - return *this; - } - Omittable& operator=(T&& val) - { - _value = std::move(val); - _omitted = false; - return *this; - } - - bool omitted() const { return _omitted; } - const T& value() const { return _value; } - T& value() { return _value; } - T&& release() { _omitted = true; return std::move(_value); } - - operator bool() const { return !omitted(); } - - private: - T _value; - bool _omitted = false; - }; - // This catches anything implicitly convertible to QJsonValue/Object/Array inline auto toJson(const QJsonValue& val) { return val; } inline auto toJson(const QJsonObject& o) { return o; } diff --git a/lib/util.h b/lib/util.h index 78d90626..aa44893f 100644 --- a/lib/util.h +++ b/lib/util.h @@ -64,6 +64,50 @@ namespace QMatrixClient return std::unique_ptr(static_cast(p.release())); } + struct NoneTag {}; + constexpr NoneTag none {}; + + /** A crude substitute for `optional` while we're not C++17 + * + * Only works with default-constructible types. + */ + template + class Omittable + { + public: + explicit Omittable() : Omittable(none) { } + Omittable(NoneTag) : _omitted(true) { } + Omittable(const T& val) : _value(val) { } + Omittable(T&& val) : _value(std::move(val)) { } + Omittable& operator=(const T& val) + { + _value = val; + _omitted = false; + return *this; + } + Omittable& operator=(T&& val) + { + _value = std::move(val); + _omitted = false; + return *this; + } + + bool omitted() const { return _omitted; } + const T& value() const { return _value; } + T& value() { return _value; } + T&& release() { _omitted = true; return std::move(_value); } + + operator bool() const { return !omitted(); } + const T& operator->() const { return &_value; } + T& operator->() { return &_value; } + const T& operator*() const { return _value; } + T& operator*() { return _value; } + + private: + T _value; + bool _omitted = false; + }; + /** Determine traits of an arbitrary function/lambda/functor * This only works with arity of 1 (1-argument) for now but is extendable * to other cases. Also, doesn't work with generic lambdas and function -- cgit v1.2.3