diff options
Diffstat (limited to 'apps/apps.h')
-rw-r--r-- | apps/apps.h | 69 |
1 files changed, 60 insertions, 9 deletions
diff --git a/apps/apps.h b/apps/apps.h index f3d6eae..9173ae0 100644 --- a/apps/apps.h +++ b/apps/apps.h @@ -8,6 +8,8 @@ #include <iosfwd> #include <experimental/optional> #include <json.hpp> +#include <mutex> +#include <condition_variable> namespace trygvis { namespace apps { @@ -22,12 +24,61 @@ using json = nlohmann::json; class missing_key : public std::runtime_error { public: - missing_key(const json::string_t key) : runtime_error("Missing key: " + key), key(key) {} + missing_key(const json::string_t key) : runtime_error("Missing key: " + key), key(key) { } const json::string_t key; }; -template <typename T> +class waitable { +protected: + std::condition_variable cv; + std::mutex mutex; + + waitable() { + } + + virtual ~waitable() { + } + +public: + void wait() { + std::unique_lock<std::mutex> lock(mutex); + cv.wait(lock); + } + + template<class Predicate> + void wait(Predicate predicate) { + std::unique_lock<std::mutex> lock(mutex); + cv.wait(lock, predicate); + } + + template<class Rep, class Period> + std::cv_status wait_for(const std::chrono::duration<Rep, Period> &rel_time) { + std::unique_lock<std::mutex> lock(mutex); + return cv.wait_for(lock, rel_time); + } + + template<class Rep, class Period, class Predicate> + bool wait_for(const std::chrono::duration<Rep, Period> &rel_time, Predicate predicate) { + std::unique_lock<std::mutex> lock(mutex); + return cv.wait_for(lock, rel_time, predicate); + } + + template<class Clock, class Duration> + std::cv_status wait_until(const std::chrono::time_point<Clock, Duration> &timeout_time) { + std::unique_lock<std::mutex> lock(mutex); + return cv.wait_until(lock, timeout_time); + }; + + template<class Clock, class Duration, class Predicate> + bool wait_until(const std::chrono::time_point<Clock, Duration> &timeout_time, + Predicate predicate) { + std::unique_lock<std::mutex> lock(mutex); + return cv.wait_until(lock, timeout_time, predicate); + } +}; + +template<typename T> T get(json j, std::string key) { auto ref = j[key]; @@ -41,7 +92,7 @@ T get(json j, std::string key) { class app_execution { public: app_execution(po::options_description desc, po::variables_map vm, Logger logger) - : desc(desc), vm(vm), logger(logger) {} + : desc(desc), vm(vm), logger(logger) { } const po::options_description desc; const po::variables_map vm; @@ -52,7 +103,7 @@ public: class app { public: - app(std::string app_name) : app_name(app_name) {} + app(std::string app_name) : app_name(app_name) { } app(const app &) = delete; @@ -62,9 +113,9 @@ public: app &operator=(const app &) = delete; - virtual void add_options(po::options_description_easy_init &options) {} + virtual void add_options(po::options_description_easy_init &options) { } - virtual void add_extra_options(po::options_description &options) {} + virtual void add_extra_options(po::options_description &options) { } virtual int main(app_execution &execution) = 0; @@ -75,12 +126,12 @@ std::string get_hostname(); int real_main(app *app, int argc, const char *argv[]); -template <typename T> +template<typename T> class noop_delete { public: - void operator()(T *) const {} + void operator()(T *) const { } }; -static inline void noop_deleter(void *) {} +static inline void noop_deleter(void *) { } } } |