From 2518f3d4a7bac333dd053f18ec4f8d3353dffcac Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 9 Apr 2018 21:47:07 +0200 Subject: [PATCH] added Easy::Entity::was_set() --- CMakeLists.txt | 3 ++- src/easy/easy.hpp | 31 +++++++++++++++++++++++++++++++ src/easy/entity.cpp | 21 +++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a6008e..5cf26d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,12 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.8.6 + VERSION 0.8.7 LANGUAGES CXX ) include(GNUInstallDirs) + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall") set(CMAKE_CXX_STANDARD 14) diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index d774e7b..9ad425b 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -223,6 +223,34 @@ public: */ const string error() const; + /*! + * @brief Returns true if the last requested value was set, false if + * it was unset. + * + * Members of Easy::Entity-derived classes return a default + * value depending on its type when the requested value is not + * found in the JSON. "" for strings, false for bools and so + * on. Most of the time this is no problem, but sometimes you + * need to know for sure. + * + * Example: + * @code + * Easy::Account a(jsonstring); + * if (a.note().empty()) + * { + * if (a.was_set()) + * { + * cout << "Account has an empty description.\n"; + * } + * else + * { + * cout << "Account has no description.\n"; + * } + * } + * @endcode + */ + const bool was_set() const; + protected: /*! * @brief Returns the value of key as Json::Value @@ -279,6 +307,9 @@ public: private: Json::Value _tree; bool _valid; + bool _was_set; + // TODO: Look up if this is such a good idea + bool *_ptr_was_set; }; class Account; diff --git a/src/easy/entity.cpp b/src/easy/entity.cpp index f164225..a0907a1 100644 --- a/src/easy/entity.cpp +++ b/src/easy/entity.cpp @@ -29,6 +29,8 @@ using std::chrono::system_clock; Easy::Entity::Entity(const string &json) : _tree(Json::nullValue) , _valid(false) +,_was_set(false) +,_ptr_was_set(&_was_set) { from_string(json); } @@ -80,6 +82,11 @@ const string Easy::Entity::error() const return get_string("error"); } +const bool Easy::Entity::was_set() const +{ + return _was_set; +} + const Json::Value Easy::Entity::get(const string &key) const { const Json::Value *node; @@ -111,11 +118,13 @@ const Json::Value Easy::Entity::get(const string &key) const if (!node->isNull()) { + *_ptr_was_set = true; return *node; } error: ttdebug << "Could not get data: " << key << '\n'; + *_ptr_was_set = false; return Json::Value(); } @@ -125,9 +134,11 @@ const string Easy::Entity::get_string(const string &key) const if (node.isString()) { + *_ptr_was_set = true; return node.asString(); } + *_ptr_was_set = false; return ""; } @@ -137,9 +148,11 @@ const uint_fast64_t Easy::Entity::get_uint64(const string &key) const if (node.isUInt64()) { + *_ptr_was_set = true; return node.asUInt64(); } + *_ptr_was_set = false; return 0; } @@ -149,9 +162,11 @@ const double Easy::Entity::get_double(const string &key) const if (node.isDouble()) { + *_ptr_was_set = true; return node.asDouble(); } + *_ptr_was_set = false; return 0.0; } @@ -161,9 +176,11 @@ const bool Easy::Entity::get_bool(const string &key) const if (node.isBool()) { + *_ptr_was_set = true; return node.asBool(); } + *_ptr_was_set = false; return false; } @@ -178,9 +195,11 @@ const system_clock::time_point struct std::tm tm = {0}; sstime >> std::get_time(&tm, "%Y-%m-%dT%T"); std::time_t time = timegm(&tm); + *_ptr_was_set = true; return system_clock::from_time_t(time); } + *_ptr_was_set = false; // Return clocks epoch return system_clock::time_point(); } @@ -196,8 +215,10 @@ const std::vector Easy::Entity::get_vector(const string &key) const { vec.push_back(value.asString()); } + *_ptr_was_set = true; return vec; } + *_ptr_was_set = false; return {}; }