Monero C++ Library
Loading...
Searching...
No Matches
gen_utils.h
1
52#pragma once
53
54#ifndef gen_utils_h
55#define gen_utils_h
56
57#include <boost/lexical_cast.hpp>
58#include <boost/uuid/uuid.hpp>
59#include <boost/uuid/uuid_generators.hpp>
60#include <boost/uuid/uuid_io.hpp>
61#include <chrono>
62#include <thread>
63#include "include_base_utils.h"
64#include "common/util.h"
65
69namespace gen_utils
70{
71
77 static std::string get_uuid() {
78 boost::uuids::random_generator generator;
79 boost::uuids::uuid uuid = generator();
80 return boost::uuids::to_string(uuid);
81 }
82
88 static void wait_for(uint64_t duration_ms) {
89 std::this_thread::sleep_for(std::chrono::milliseconds(duration_ms));
90 }
91
92 // ------------------------- VALUE RECONCILATION ----------------------------
93
94 // TODO: refactor common template code
95 template <class T, typename std::enable_if<std::is_same<T, std::string>::value, T>::type* = nullptr>
96 boost::optional<T> reconcile(const boost::optional<T>& val1, const boost::optional<T>& val2, boost::optional<bool> resolve_defined, boost::optional<bool> resolve_true, boost::optional<bool> resolve_max, const std::string& err_msg = "") {
97
98 // check for equality
99 if (val1 == val2) return val1;
100
101 // resolve one value none
102 if (val1 == boost::none || val2 == boost::none) {
103 if (resolve_defined != boost::none && *resolve_defined == false) return boost::none;
104 else return val1 == boost::none ? val2 : val1;
105 }
106
107 throw std::runtime_error(std::string("Cannot reconcile strings: ") + boost::lexical_cast<std::string>(val1) + std::string(" vs ") + boost::lexical_cast<std::string>(val2) + (!err_msg.empty() ? std::string(". ") + err_msg : std::string("")));
108 }
109 template <class T, typename std::enable_if<std::is_same<T, std::string>::value, T>::type* = nullptr>
110 boost::optional<T> reconcile(const boost::optional<T>& val1, const boost::optional<T>& val2, const std::string& err_msg = "") {
111 return reconcile(val1, val2, boost::none, boost::none, boost::none, err_msg);
112 }
113
114 template <class T, typename std::enable_if<std::is_integral<T>::value, T>::type* = nullptr>
115 boost::optional<T> reconcile(const boost::optional<T>& val1, const boost::optional<T>& val2, boost::optional<bool> resolve_defined, boost::optional<bool> resolve_true, boost::optional<bool> resolve_max, const std::string& err_msg = "") {
116
117 // check for equality
118 if (val1 == val2) return val1;
119
120 // resolve one value none
121 if (val1 == boost::none || val2 == boost::none) {
122 if (resolve_defined != boost::none && *resolve_defined == false) return boost::none;
123 else return val1 == boost::none ? val2 : val1;
124 }
125
126 // resolve different booleans
127 if (resolve_true != boost::none) return (bool) val1 == *resolve_true ? val1 : val2; // if resolve true, return true, else return false
128
129 // resolve different numbers
130 if (resolve_max != boost::none) return *resolve_max ? std::max(*val1, *val2) : std::min(*val1, *val2);
131
132 // cannot reconcile
133 throw std::runtime_error(std::string("Cannot reconcile integrals: ") + boost::lexical_cast<std::string>(val1) + std::string(" vs ") + boost::lexical_cast<std::string>(val2) + (!err_msg.empty() ? std::string(". ") + err_msg : std::string("")));
134 }
135 template <class T, typename std::enable_if<std::is_integral<T>::value, T>::type* = nullptr>
136 boost::optional<T> reconcile(const boost::optional<T>& val1, const boost::optional<T>& val2, const std::string& err_msg = "") {
137 return reconcile(val1, val2, boost::none, boost::none, boost::none, err_msg);
138 }
139
140 template <class T>
141 std::vector<T> reconcile(const std::vector<T>& v1, const std::vector<T>& v2, const std::string& err_msg = "") {
142
143 // check for equality
144 if (v1 == v2) return v1;
145
146 // resolve one vector empty
147 if (v1.empty()) return v2;
148 if (v2.empty()) return v1;
149
150 // otherwise cannot reconcile
151 throw std::runtime_error("Cannot reconcile vectors" + (!err_msg.empty() ? std::string(". ") + err_msg : std::string("")));
152 }
153}
154#endif /* gen_utils_h */
Definition gen_utils.h:70