Symmetri
Toggle main menu visibility
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
10
namespace
symmetri {
11
21
template
<
typename
T>
22
bool
isSynchronous
(
const
T &) {
23
return
false
;
24
}
25
34
template
<
typename
T>
35
void
cancel
(
const
T &) {}
36
43
template
<
typename
T>
44
void
pause
(
const
T &) {}
45
52
template
<
typename
T>
53
void
resume
(
const
T &) {}
54
64
template
<
typename
T>
65
Token
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
82
template
<
typename
T>
83
Eventlog
getLog
(
const
T &) {
84
return
{};
85
}
86
87
template
<
typename
T>
88
struct
identity
{
89
typedef
T type;
90
};
91
104
class
Callback
{
105
public
:
106
template
<
typename
Transition>
112
Callback
(
Transition
callback)
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
symmetri::fire
Token fire(const T &callback)
Generates a Token based on what kind of information the Callback returns.
Definition
callback.h:65
symmetri::getLog
Eventlog getLog(const T &)
Get the Log object. By default it returns an empty vector.
Definition
callback.h:83
symmetri::isSynchronous
bool isSynchronous(const T &)
Checks if the callback is synchronous. Synchronous callbacks are immediately executed inside the Petr...
Definition
callback.h:22
symmetri::resume
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
symmetri::cancel
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
symmetri::pause
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
symmetri::Callback::Callback
Callback(Transition callback)
Construct a new Callback object.
Definition
callback.h:112
symmetri::Token
Tokens are elements that can reside in places. Tokens can have a color which makes them distinguishab...
Definition
colors.hpp:74
symmetri::Success
Definition
colors.hpp:168
symmetri::identity
Definition
callback.h:88
types.h
symmetri::Eventlog
std::vector< Event > Eventlog
Definition
types.h:36
symmetri::Transition
std::string Transition
Definition
types.h:18
symmetri
include
symmetri
callback.h
Generated by
1.17.0