From 0dd491fbd189baaabadcab2344a05e3cf3898287 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sat, 2 Jun 2018 13:32:47 +0900 Subject: csapi: Use Omittable<> container instead of intrusive 'omitted' field Also: use the latest feature of GTAD, +set/+on, to make gtad.yaml more compact. --- lib/converters.h | 63 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 12 deletions(-) (limited to 'lib/converters.h') diff --git a/lib/converters.h b/lib/converters.h index fb1da6d7..916124a6 100644 --- a/lib/converters.h +++ b/lib/converters.h @@ -45,6 +45,47 @@ namespace std 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), _omitted(false) { } + Omittable(T&& val) : _value(std::move(val)), _omitted(false) { } + 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; + }; + + // This catches anything implicitly convertible to QJsonValue/Object/Array inline QJsonValue toJson(const QJsonValue& val) { return val; } inline QJsonObject toJson(const QJsonObject& o) { return o; } @@ -99,6 +140,16 @@ namespace QMatrixClient return json; } + template + inline auto toJson(const Omittable& omittable) + -> decltype(toJson(omittable.value())) + { + if (omittable) + return toJson(omittable.value()); + + return {}; + } + #if 0 template inline auto toJson(const optional& optVal) @@ -287,16 +338,4 @@ namespace QMatrixClient _impl::AddNode> ::impl(o, std::move(key), std::move(json)); } - - /** Construct an "omitted" value of a given type - * This is a workaround for the time being while we're not C++17 and - * cannot use `optional`. - */ - template - inline T omitted() - { - T val; - val.omitted = true; - return val; - } } // namespace QMatrixClient -- cgit v1.2.3