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 <boost/property_tree/ptree.hpp>
62#include <boost/property_tree/json_parser.hpp>
63#include "rapidjson/document.h"
64#include <boost/thread/recursive_mutex.hpp>
65#include <boost/thread/thread.hpp>
66#include <boost/thread/condition_variable.hpp>
67#include <chrono>
68#include <thread>
69#include <atomic>
70#include "include_base_utils.h"
71#include "common/util.h"
72
76namespace gen_utils
77{
78
84 static std::string get_uuid() {
85 boost::uuids::random_generator generator;
86 boost::uuids::uuid uuid = generator();
87 return boost::uuids::to_string(uuid);
88 }
89
95 static void wait_for(uint64_t duration_ms) {
96 std::this_thread::sleep_for(std::chrono::milliseconds(duration_ms));
97 }
98
99 static bool bool_equals(bool val, const boost::optional<bool>& opt_val) {
100 return opt_val == boost::none ? false : val == *opt_val;
101 }
102
103 // ------------------------- SERIALIZATION ---------------------------
104
105 // TODO: fully switch from property trees to rapidjson
106
107 std::string serialize(const rapidjson::Document& doc);
108 std::string serialize(const boost::property_tree::ptree& node);
109 void deserialize(const std::string& json, boost::property_tree::ptree& root);
110
111 // ------------------------- VALUE RECONCILATION ----------------------------
112
113 // TODO: refactor common template code
114 template <class T, typename std::enable_if<std::is_same<T, std::string>::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 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("")));
127 }
128 template <class T, typename std::enable_if<std::is_same<T, std::string>::value, T>::type* = nullptr>
129 boost::optional<T> reconcile(const boost::optional<T>& val1, const boost::optional<T>& val2, const std::string& err_msg = "") {
130 return reconcile(val1, val2, boost::none, boost::none, boost::none, err_msg);
131 }
132
133 template <class T, typename std::enable_if<std::is_integral<T>::value, T>::type* = nullptr>
134 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 = "") {
135
136 // check for equality
137 if (val1 == val2) return val1;
138
139 // resolve one value none
140 if (val1 == boost::none || val2 == boost::none) {
141 if (resolve_defined != boost::none && *resolve_defined == false) return boost::none;
142 else return val1 == boost::none ? val2 : val1;
143 }
144
145 // resolve different booleans
146 if (resolve_true != boost::none) return (bool) val1 == *resolve_true ? val1 : val2; // if resolve true, return true, else return false
147
148 // resolve different numbers
149 if (resolve_max != boost::none) return *resolve_max ? std::max(*val1, *val2) : std::min(*val1, *val2);
150
151 // cannot reconcile
152 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("")));
153 }
154 template <class T, typename std::enable_if<std::is_integral<T>::value, T>::type* = nullptr>
155 boost::optional<T> reconcile(const boost::optional<T>& val1, const boost::optional<T>& val2, const std::string& err_msg = "") {
156 return reconcile(val1, val2, boost::none, boost::none, boost::none, err_msg);
157 }
158
159 template <class T>
160 std::vector<T> reconcile(const std::vector<T>& v1, const std::vector<T>& v2, const std::string& err_msg = "") {
161
162 // check for equality
163 if (v1 == v2) return v1;
164
165 // resolve one vector empty
166 if (v1.empty()) return v2;
167 if (v2.empty()) return v1;
168
169 // otherwise cannot reconcile
170 throw std::runtime_error("Cannot reconcile vectors" + (!err_msg.empty() ? std::string(". ") + err_msg : std::string("")));
171 }
172
173 // ------------------------- THREAD POLLER ----------------------------
174
175 // WARNING: Only destroy a thread_poller from a thread other than its own pool thread.
176 // (e.g. a listener callback running on the poll thread, drops the last owning reference/unique_ptr
177 // to the poller.)
178 class thread_poller {
179 public:
180 thread_poller();
181 virtual ~thread_poller();
182
183 bool is_polling() const { return m_is_polling; }
184 void set_is_polling(bool is_polling);
185 void request_is_polling(bool is_polling);
186
187 void set_period_in_ms(uint64_t period_ms) { m_poll_period_ms = period_ms; }
188 virtual void poll() = 0;
189 void wait_for_callbacks_idle();
190
191 class announce_scope {
192 public:
193 explicit announce_scope(thread_poller& poller);
194 ~announce_scope();
195 announce_scope(const announce_scope&) = delete;
196 announce_scope& operator=(const announce_scope&) = delete;
197 private:
198 thread_poller& m_poller;
199 };
200
201 protected:
202 std::string m_name;
203 boost::recursive_mutex m_mutex;
204 boost::mutex m_polling_mutex; // guards the interruptible periodic sleep (m_poll_cv)
205 boost::mutex m_lifecycle_mutex; // guards m_poll_loop_running/m_poll_thread_id and start/stop coordination
206 boost::thread::id m_poll_thread_id; // id of the currently running loop thread; guarded by m_lifecycle_mutex
207 std::atomic<bool> m_is_polling;
208 bool m_poll_loop_running; // guarded by m_lifecycle_mutex
209 std::atomic<uint64_t> m_poll_period_ms;
210 boost::condition_variable m_poll_cv; // periodic-sleep interrupt
211 boost::condition_variable m_lifecycle_cv; // notified (under m_lifecycle_mutex) when the loop actually exits
212 boost::mutex m_announce_mutex; // guards m_announce_count
213 boost::condition_variable m_announce_cv; // notified (under m_announce_mutex) when m_announce_count reaches 0
214 int m_announce_count = 0; // number of announce_scope instances currently alive, across all threads
215
216 void init_common(const std::string& name);
217 void run_poll_loop();
218 };
219}
220#endif /* gen_utils_h */
Definition gen_utils.h:77