Symmetri
Loading...
Searching...
No Matches
callback.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <atomic>
6#include <memory>
7
8#include "symmetri/types.h"
9
10namespace symmetri {
11
21template <typename T>
22bool isSynchronous(const T &) {
23 return false;
24}
25
34template <typename T>
35void cancel(const T &) {}
36
43template <typename T>
44void pause(const T &) {}
45
52template <typename T>
53void resume(const T &) {}
54
64template <typename T>
65Token fire(const T &callback) {
66 if constexpr (std::is_same_v<Token, decltype(callback())>) {
67 return callback();
68 } else if constexpr (std::is_convertible_v<decltype(callback()), Token>) {
69 return Token(callback());
70 } else if constexpr (std::is_same_v<void, decltype(callback())>) {
71 callback();
72 return Success;
73 }
74}
75
82template <typename T>
83Eventlog getLog(const T &) {
84 return {};
85}
86
87template <typename T>
88struct identity {
89 typedef T type;
90};
91
104class Callback {
105 public:
106 template <typename Transition>
113 : self_(std::make_shared<model<Transition>>(std::move(callback))) {}
114
115 template <typename T, typename... Args>
116 Callback(identity<T>, Args &&...args)
117 : self_(std::make_shared<model<T>>(std::forward<Args>(args)...)) {}
118
119 Callback(Callback &&f) = default;
120 Callback &operator=(Callback &&other) = default;
121 Callback(const Callback &o) = delete;
122 Callback &operator=(const Callback &other) = delete;
123
124 friend Token fire(const Callback &callback) {
125 return callback.self_->fire_();
126 }
127 friend Eventlog getLog(const Callback &callback) {
128 return callback.self_->get_log_();
129 }
130 friend bool isSynchronous(const Callback &callback) {
131 return callback.self_->is_synchronous_();
132 }
133 friend void cancel(const Callback &callback) {
134 return callback.self_->cancel_();
135 }
136 friend void pause(const Callback &callback) {
137 return callback.self_->pause_();
138 }
139 friend void resume(const Callback &callback) {
140 return callback.self_->resume_();
141 }
142 auto getEndTime() const {
143 return self_->end_t_.load(std::memory_order_relaxed);
144 }
145
146 private:
147 struct concept_t {
148 virtual ~concept_t() = default;
149 virtual Token fire_() const = 0;
150 virtual Eventlog get_log_() const = 0;
151 virtual void cancel_() const = 0;
152 virtual void pause_() const = 0;
153 virtual void resume_() const = 0;
154 virtual bool is_synchronous_() const = 0;
155 mutable std::atomic<Clock::time_point> end_t_{Clock::time_point::min()};
156 };
157
165 template <typename Transition>
166 struct model final : concept_t {
167 explicit model(Transition &&t) : transition_(std::move(t)) {}
168 template <typename... Args>
169 model(Args &&...args) : transition_(std::forward<Args>(args)...) {}
170
171 Token fire_() const override {
172 auto res = fire(transition_);
173 end_t_.store(Clock::now(), std::memory_order_relaxed);
174 return res;
175 }
176 Eventlog get_log_() const override { return getLog(transition_); }
177 void cancel_() const override { return cancel(transition_); }
178 bool is_synchronous_() const override { return isSynchronous(transition_); }
179 void pause_() const override { return pause(transition_); }
180 void resume_() const override { return resume(transition_); }
181 Transition transition_;
182 };
183
184 std::shared_ptr<const concept_t> self_;
185};
186
187} // namespace symmetri
Token fire(const T &callback)
Generates a Token based on what kind of information the Callback returns.
Definition callback.h:65
Eventlog getLog(const T &)
Get the Log object. By default it returns an empty vector.
Definition callback.h:83
bool isSynchronous(const T &)
Checks if the callback is synchronous. Synchronous callbacks are immediately executed inside the Petr...
Definition callback.h:22
void resume(const T &)
Templates a resume action for a Callback. By default it does nothing and not resume the Callback.
Definition callback.h:53
void cancel(const T &)
The default cancel implementation is naive. It only returns a user-exit state and does nothing to the...
Definition callback.h:35
void pause(const T &)
Implements a pause action for a Callback. By default it does nothing and not pause the Callback.
Definition callback.h:44
Callback(Transition callback)
Construct a new Callback object.
Definition callback.h:112
Tokens are elements that can reside in places. Tokens can have a color which makes them distinguishab...
Definition colors.hpp:107
Definition colors.hpp:201
Definition callback.h:88
std::vector< Event > Eventlog
Definition types.h:32
std::string Transition
Definition types.h:14