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);
95 static void wait_for(uint64_t duration_ms) {
96 std::this_thread::sleep_for(std::chrono::milliseconds(duration_ms));
99 static bool bool_equals(
bool val,
const boost::optional<bool>& opt_val) {
100 return opt_val == boost::none ? false : val == *opt_val;
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);
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 =
"") {
118 if (val1 == val2)
return val1;
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;
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(
"")));
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);
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 =
"") {
137 if (val1 == val2)
return val1;
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;
146 if (resolve_true != boost::none)
return (
bool) val1 == *resolve_true ? val1 : val2;
149 if (resolve_max != boost::none)
return *resolve_max ? std::max(*val1, *val2) : std::min(*val1, *val2);
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(
"")));
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);
160 std::vector<T> reconcile(
const std::vector<T>& v1,
const std::vector<T>& v2,
const std::string& err_msg =
"") {
163 if (v1 == v2)
return v1;
166 if (v1.empty())
return v2;
167 if (v2.empty())
return v1;
170 throw std::runtime_error(
"Cannot reconcile vectors" + (!err_msg.empty() ? std::string(
". ") + err_msg : std::string(
"")));
178 class thread_poller {
181 virtual ~thread_poller();
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);
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();
191 class announce_scope {
193 explicit announce_scope(thread_poller& poller);
195 announce_scope(
const announce_scope&) =
delete;
196 announce_scope& operator=(
const announce_scope&) =
delete;
198 thread_poller& m_poller;
203 boost::recursive_mutex m_mutex;
204 boost::mutex m_polling_mutex;
205 boost::mutex m_lifecycle_mutex;
206 boost::thread::id m_poll_thread_id;
207 std::atomic<bool> m_is_polling;
208 bool m_poll_loop_running;
209 std::atomic<uint64_t> m_poll_period_ms;
210 boost::condition_variable m_poll_cv;
211 boost::condition_variable m_lifecycle_cv;
212 boost::mutex m_announce_mutex;
213 boost::condition_variable m_announce_cv;
214 int m_announce_count = 0;
216 void init_common(
const std::string& name);
217 void run_poll_loop();