mirror of
https://github.com/wownero/wownero-cpp.git
synced 2026-10-20 08:38:41 +02:00
reconcile working for integrals, strs, and bools using enable_if
This commit is contained in:
@@ -101,8 +101,24 @@ namespace monero {
|
||||
void MoneroBlockHeader::merge(const MoneroBlockHeader& header) {
|
||||
cout << "MoneroBlockHeader::merge()" << endl;
|
||||
if (this == &header) return;
|
||||
id = MoneroUtils::reconcile(id, header.id, boost::none, boost::none, boost::none);
|
||||
throw runtime_error("Not implemented");
|
||||
id = MoneroUtils::reconcile(id, header.id);
|
||||
height = MoneroUtils::reconcile(height, header.height, boost::none, boost::none, true); // height can increase
|
||||
timestamp = MoneroUtils::reconcile(timestamp, header.timestamp, boost::none, boost::none, true); // timestamp can increase
|
||||
size = MoneroUtils::reconcile(size, header.size);
|
||||
weight = MoneroUtils::reconcile(weight, header.weight);
|
||||
longTermWeight = MoneroUtils::reconcile(longTermWeight, header.longTermWeight);
|
||||
depth = MoneroUtils::reconcile(depth, header.depth);
|
||||
difficulty = MoneroUtils::reconcile(difficulty, header.difficulty);
|
||||
cumulativeDifficulty = MoneroUtils::reconcile(cumulativeDifficulty, header.cumulativeDifficulty);
|
||||
majorVersion = MoneroUtils::reconcile(majorVersion, header.majorVersion);
|
||||
minorVersion = MoneroUtils::reconcile(minorVersion, header.minorVersion);
|
||||
nonce = MoneroUtils::reconcile(nonce, header.nonce);
|
||||
coinbaseTxId = MoneroUtils::reconcile(coinbaseTxId, header.coinbaseTxId);
|
||||
numTxs = MoneroUtils::reconcile(numTxs, header.numTxs);
|
||||
orphanStatus = MoneroUtils::reconcile(orphanStatus, header.orphanStatus);
|
||||
prevId = MoneroUtils::reconcile(prevId, header.prevId);
|
||||
reward = MoneroUtils::reconcile(reward, header.reward);
|
||||
powHash = MoneroUtils::reconcile(powHash, header.powHash);
|
||||
}
|
||||
|
||||
void MoneroBlock::merge(const MoneroBlock& block) {
|
||||
|
||||
+36
-8
@@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user