From adfa6178376e1f7b20c6959b8a3bdbdd866f4d75 Mon Sep 17 00:00:00 2001 From: woodser Date: Wed, 12 Jun 2019 18:03:39 -0400 Subject: [PATCH] reconcile overloaded to handle boost::optional --- src/daemon/MoneroDaemon.cpp | 2 +- src/utils/MoneroUtils.h | 33 +++++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/daemon/MoneroDaemon.cpp b/src/daemon/MoneroDaemon.cpp index a7192bf..73c70c7 100644 --- a/src/daemon/MoneroDaemon.cpp +++ b/src/daemon/MoneroDaemon.cpp @@ -101,7 +101,7 @@ namespace monero { void MoneroBlockHeader::merge(const MoneroBlockHeader& header) { cout << "MoneroBlockHeader::merge()" << endl; if (this == &header) return; - id = MoneroUtils::reconcile(id, header.id); + id = MoneroUtils::reconcile(id, header.id, boost::none, boost::none, boost::none); throw runtime_error("Not implemented"); } diff --git a/src/utils/MoneroUtils.h b/src/utils/MoneroUtils.h index 42b4081..c450a0f 100644 --- a/src/utils/MoneroUtils.h +++ b/src/utils/MoneroUtils.h @@ -95,7 +95,8 @@ namespace MoneroUtils return ss.str(); } - template boost::optional reconcile(const boost::optional& val1, const boost::optional& val2, boost::optional resolveDefined, boost::optional resolveTrue, boost::optional resolveMax) { + template + boost::optional reconcile(const boost::optional& val1, const boost::optional& val2, boost::optional resolveDefined, boost::optional resolveTrue, boost::optional resolveMax) { cout << "reconcile(" << val1 << ", " << val2 << ")" << endl; // check for equality @@ -107,12 +108,36 @@ namespace MoneroUtils else return val1 == boost::none ? val2 : val1; } + // resolve different numbers + if (resolveMax != boost::none) { + return *resolveMax ? max(*val1, *val2) : min(*val1, *val2); + } + // throw runtime_error("Cannot reconcile values " + val1 + " and " + val2 + " with config: [" + resolveDefined + ", " + resolveTrue + ", " + resolveMax + "]", val1, val2); - throw runtime_error("Cannot reconcile values"); + throw runtime_error("Cannot reconcile non-booleans"); } - template boost::optional reconcile(const boost::optional& val1, const boost::optional& val2) { - return reconcile(val1, val2, boost::none, boost::none, boost::none); + template ::value, T>::type* = nullptr> + boost::optional reconcile(const boost::optional& val1, const boost::optional& val2, boost::optional resolveDefined, boost::optional resolveTrue, boost::optional resolveMax) { + cout << "reconcile(" << val1 << ", " << val2 << ")" << endl; + + // check for equality + if (val1 == val2) return val1; + + // resolve one value none + if (val1 == boost::none || val2 == boost::none) { + if (resolveDefined == false) return boost::none; // TODO: boost::optional equality comparitor wokrs like this? + else return val1 == boost::none ? val2 : val1; + } + + // resolve different booleans + if (resolveTrue != boost::none) { + return val1 == resolveTrue ? val1 : val2; // if resolve true, return true, else return false + } else { + throw runtime_error("Cannot reconcile boolean values"); + } + + throw runtime_error("Cannot reconcile booleans"); } } #endif /* MoneroUtils_h */