From 03714c6cebf90dc7db8e3997a18cdd19e039c667 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Sat, 17 Mar 2018 17:28:47 +0100 Subject: Revert "Use std::optional instead of OptionalBool" This reverts commit ba879a882e031d7b8503f78fe41d1210000c96ca. --- src/database/database.hpp | 2 +- src/database/query.cpp | 12 +++++----- src/database/query.hpp | 4 ++-- src/database/select_query.hpp | 9 +++---- src/utils/optional_bool.cpp | 4 ++-- src/utils/optional_bool.hpp | 48 ++++++++++++++++++++++++------------- src/xmpp/biboumi_adhoc_commands.cpp | 14 +++++------ tests/database.cpp | 8 +++---- 8 files changed, 59 insertions(+), 42 deletions(-) diff --git a/src/database/database.hpp b/src/database/database.hpp index 03d82ae..8a967d8 100644 --- a/src/database/database.hpp +++ b/src/database/database.hpp @@ -69,7 +69,7 @@ class Database struct RecordHistory: Column { static constexpr auto name = "recordhistory_"; RecordHistory(): Column(true) {}}; - struct RecordHistoryOptional: Column> { static constexpr auto name = "recordhistory_"; }; + struct RecordHistoryOptional: Column { static constexpr auto name = "recordhistory_"; }; struct VerifyCert: Column { static constexpr auto name = "verifycert_"; VerifyCert(): Column(true) {} }; diff --git a/src/database/query.cpp b/src/database/query.cpp index 13c881b..d72066e 100644 --- a/src/database/query.cpp +++ b/src/database/query.cpp @@ -11,11 +11,11 @@ void actual_bind(Statement& statement, const std::int64_t& value, int index) statement.bind_int64(index, value); } -void actual_bind(Statement& statement, const std::optional& value, int index) +void actual_bind(Statement& statement, const OptionalBool& value, int index) { - if (!value) + if (!value.is_set) statement.bind_int64(index, 0); - else if (*value) + else if (value.value) statement.bind_int64(index, 1); else statement.bind_int64(index, -1); @@ -26,11 +26,11 @@ void actual_add_param(Query& query, const std::string& val) query.params.push_back(val); } -void actual_add_param(Query& query, const std::optional& val) +void actual_add_param(Query& query, const OptionalBool& val) { - if (!val) + if (!val.is_set) query.params.push_back("0"); - else if (*val) + else if (val.value) query.params.push_back("1"); else query.params.push_back("-1"); diff --git a/src/database/query.hpp b/src/database/query.hpp index 1c4a5ff..ba28b1a 100644 --- a/src/database/query.hpp +++ b/src/database/query.hpp @@ -18,7 +18,7 @@ void actual_bind(Statement& statement, const T& value, int index) { actual_bind(statement, static_cast(value), index); } -void actual_bind(Statement& statement, const std::optional& value, int index); +void actual_bind(Statement& statement, const OptionalBool& value, int index); #ifdef DEBUG_SQL_QUERIES #include @@ -71,6 +71,7 @@ void actual_add_param(Query& query, const T& val) } void actual_add_param(Query& query, const std::string& val); +void actual_add_param(Query& query, const OptionalBool& val); template typename std::enable_if::value, Query&>::type @@ -79,7 +80,6 @@ operator<<(Query& query, const T&) query.body += T::name; return query; } -void actual_add_param(Query& query, const std::optional& val); Query& operator<<(Query& query, const char* str); Query& operator<<(Query& query, const std::string& str); diff --git a/src/database/select_query.hpp b/src/database/select_query.hpp index cd9943c..5a17f38 100644 --- a/src/database/select_query.hpp +++ b/src/database/select_query.hpp @@ -29,15 +29,16 @@ extract_row_value(Statement& statement, const int i) } template -typename std::enable_if, T>::value, T>::type +typename std::enable_if::value, T>::type extract_row_value(Statement& statement, const int i) { const auto integer = statement.get_column_int(i); + OptionalBool result; if (integer > 0) - return true; + result.set_value(true); else if (integer < 0) - return false; - return std::nullopt; + result.set_value(false); + return result; } template diff --git a/src/utils/optional_bool.cpp b/src/utils/optional_bool.cpp index 1d1c375..56fdca2 100644 --- a/src/utils/optional_bool.cpp +++ b/src/utils/optional_bool.cpp @@ -1,8 +1,8 @@ #include -std::ostream& operator<<(std::ostream& os, const std::optional& o) +std::ostream& operator<<(std::ostream& os, const OptionalBool& o) { - os << std::to_string(o); + os << o.to_string(); return os; } diff --git a/src/utils/optional_bool.hpp b/src/utils/optional_bool.hpp index c652ed3..867aca2 100644 --- a/src/utils/optional_bool.hpp +++ b/src/utils/optional_bool.hpp @@ -1,21 +1,37 @@ #pragma once -#include - #include -namespace std -{ -inline -std::string to_string(const std::optional b) +struct OptionalBool { - if (!b) - return "unset"; - else if (*b) - return "true"; - else - return "false"; -} -} - -std::ostream& operator<<(std::ostream& os, const std::optional& o); + OptionalBool() = default; + + OptionalBool(bool value): + is_set(true), value(value) {} + + void set_value(bool value) + { + this->is_set = true; + this->value = value; + } + + void unset() + { + this->is_set = false; + } + + std::string to_string() const + { + if (this->is_set == false) + return "unset"; + else if (this->value) + return "true"; + else + return "false"; + } + + bool is_set{false}; + bool value{false}; +}; + +std::ostream& operator<<(std::ostream& os, const OptionalBool& o); diff --git a/src/xmpp/biboumi_adhoc_commands.cpp b/src/xmpp/biboumi_adhoc_commands.cpp index 53ec98b..38b6165 100644 --- a/src/xmpp/biboumi_adhoc_commands.cpp +++ b/src/xmpp/biboumi_adhoc_commands.cpp @@ -493,7 +493,7 @@ void insert_irc_channel_configuration_form(XmlNode& node, const Jid& requester, { // Value selected by default XmlSubNode value(record_history, "value"); - value.set_inner(std::to_string(options.col())); + value.set_inner(options.col().to_string()); } // All three possible values for (const auto& val: {"unset", "true", "false"}) @@ -594,19 +594,19 @@ bool handle_irc_channel_configuration_form(XmppComponent& xmpp_component, const else if (field->get_tag("var") == "record_history" && value && !value->get_inner().empty()) { - std::optional& database_value = options.col(); + OptionalBool& database_value = options.col(); if (value->get_inner() == "true") - database_value = true; + database_value.set_value(true); else if (value->get_inner() == "false") - database_value = false; + database_value.set_value(false); else - database_value.reset(); + database_value.unset(); auto& biboumi_component = dynamic_cast(xmpp_component); Bridge* bridge = biboumi_component.find_user_bridge(requester.bare()); if (bridge) { - if (database_value) - bridge->set_record_history(*database_value); + if (database_value.is_set) + bridge->set_record_history(database_value.value); else { // It is unset, we need to fetch the Global option, to // know if it’s enabled or not diff --git a/tests/database.cpp b/tests/database.cpp index 88b4834..7ab6da8 100644 --- a/tests/database.cpp +++ b/tests/database.cpp @@ -56,13 +56,13 @@ TEST_CASE("Database") CHECK(o.col() == ""); o.col() = "ISO-8859-1"; - CHECK(!o.col()); - o.col() = false; + CHECK(o.col().is_set == false); + o.col().set_value(false); o.save(Database::db); auto b = Database::get_irc_channel_options("zouzou@example.com", "irc.example.com", "#foo"); CHECK(o.col() == "ISO-8859-1"); - CHECK(o.col()); - CHECK(*o.col() == false); + CHECK(o.col().is_set == true); + CHECK(o.col().value == false); } SECTION("Channel options with server default") -- cgit v1.2.3