reconcile working for integrals, strs, and bools using enable_if

This commit is contained in:
woodser
2019-06-12 20:32:21 -04:00
parent 7c6c06cd8e
commit 5d118b3aef
2 changed files with 54 additions and 10 deletions
+36 -8
View File
@@ -95,7 +95,30 @@ namespace MoneroUtils
return ss.str();
}
template <class T>
// TODO: refactor common template code
template <class T, typename std::enable_if<std::is_same<T, string>::value, T>::type* = nullptr>
boost::optional<T> reconcile(const boost::optional<T>& val1, const boost::optional<T>& val2, boost::optional<bool> resolveDefined, boost::optional<bool> resolveTrue, boost::optional<bool> 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;
}
throw runtime_error("Cannot reconcile strings");
}
template <class T, typename std::enable_if<std::is_same<T, string>::value, T>::type* = nullptr>
boost::optional<T> reconcile(const boost::optional<T>& val1, const boost::optional<T>& val2) {
return reconcile(val1, val2, boost::none, boost::none, boost::none);
}
template <class T, typename std::enable_if<std::is_integral<T>::value, T>::type* = nullptr>
boost::optional<T> reconcile(const boost::optional<T>& val1, const boost::optional<T>& val2, boost::optional<bool> resolveDefined, boost::optional<bool> resolveTrue, boost::optional<bool> resolveMax) {
cout << "reconcile(" << val1 << ", " << val2 << ")" << endl;
@@ -114,16 +137,16 @@ namespace MoneroUtils
}
// throw runtime_error("Cannot reconcile values " + val1 + " and " + val2 + " with config: [" + resolveDefined + ", " + resolveTrue + ", " + resolveMax + "]", val1, val2);
throw runtime_error("Cannot reconcile non-booleans");
throw runtime_error("Cannot reconcile integrals");
}
template <class T>
template <class T, typename std::enable_if<std::is_integral<T>::value, T>::type* = nullptr>
boost::optional<T> reconcile(const boost::optional<T>& val1, const boost::optional<T>& val2) {
return reconcile(val1, val2, boost::none, boost::none, boost::none);
}
template <class T, typename std::enable_if<std::is_same<T, bool>::value, T>::type* = nullptr>
boost::optional<bool> reconcile(const boost::optional<T>& val1, const boost::optional<T>& val2, boost::optional<bool> resolveDefined, boost::optional<bool> resolveTrue, boost::optional<bool> resolveMax) {
template <class T, typename std::enable_if<std::is_same<T, bool>::value, T>::type>
boost::optional<T> reconcile(const boost::optional<T>& val1, const boost::optional<T>& val2, boost::optional<bool> resolveDefined, boost::optional<bool> resolveTrue, boost::optional<bool> resolveMax) {
cout << "reconcile(" << val1 << ", " << val2 << ")" << endl;
// check for equality
@@ -139,15 +162,20 @@ namespace MoneroUtils
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");
}
throw runtime_error("Cannot reconcile booleans");
}
template <class T, typename std::enable_if<std::is_same<T, bool>::value, T>::type* = nullptr>
boost::optional<bool> reconcile(const boost::optional<T>& val1, const boost::optional<T>& val2) {
template <class T, typename std::enable_if<std::is_same<T, bool>::value, T>::type>
boost::optional<T> reconcile(const boost::optional<T>& val1, const boost::optional<T>& val2) {
return reconcile(val1, val2, boost::none, boost::none, boost::none);
}
// template <class T>
// boost::optional<T> reconcile(const boost::optional<T>& val1, const boost::optional<T>& val2) {
// return reconcile(val1, val2, boost::none, boost::none, boost::none);
// }
}
#endif /* MoneroUtils_h */