12 template <
class DerivedSST>
27 template <
class DerivedSST>
29 using pred = std::function<bool(const DerivedSST&)>;
30 using trig = std::function<void(DerivedSST&)>;
31 using pred_list = std::list<std::unique_ptr<std::pair<pred, std::shared_ptr<trig>>>>;
41 friend class SST<DerivedSST>;
48 typename pred_list::iterator
iter;
55 : valid{
true}, iter{iter}, type{type} {}
63 iter = std::move(other.iter);
70 return valid && (*iter);
80 pred_handle
insert(
pred predicate,
const std::list<trig>& triggers,
82 return insert(predicate, [triggers](DerivedSST& t) {
83 for(
const auto& trigger : triggers)
90 void remove(pred_handle&
pred);
106 template <
class DerivedSST>
108 std::lock_guard<std::mutex> lock(predicate_mutex);
110 one_time_predicates.push_back(std::make_unique<std::pair<
pred, std::shared_ptr<trig>>>(
111 predicate, std::make_shared<trig>(trigger)));
112 return pred_handle(--one_time_predicates.end(), type);
114 recurrent_predicates.push_back(std::make_unique<std::pair<
pred, std::shared_ptr<trig>>>(
115 predicate, std::make_shared<trig>(trigger)));
116 return pred_handle(--recurrent_predicates.end(), type);
118 transition_predicates.push_back(std::make_unique<std::pair<
pred, std::shared_ptr<trig>>>(
119 predicate, std::make_shared<trig>(trigger)));
120 transition_predicate_states.push_back(
false);
121 return pred_handle(--transition_predicates.end(), type);
125 template <
class DerivedSST>
127 std::lock_guard<std::mutex> lock(predicate_mutex);
128 if(!handle.is_valid()) {
131 handle.iter->reset();
132 handle.valid =
false;
135 template <
class DerivedSST>
137 std::lock_guard<std::mutex> lock(predicate_mutex);
138 using ptr_to_pred = std::unique_ptr<std::pair<pred, std::shared_ptr<trig>>>;
139 std::for_each(one_time_predicates.begin(), one_time_predicates.end(),
140 [](ptr_to_pred& ptr) { ptr.reset(); });
141 std::for_each(recurrent_predicates.begin(), recurrent_predicates.end(),
142 [](ptr_to_pred& ptr) { ptr.reset(); });
143 std::for_each(transition_predicates.begin(), transition_predicates.end(),
144 [](ptr_to_pred& ptr) { ptr.reset(); });
One-time predicates only fire once; they are deleted once they become true.
std::function< bool(const DerechoSST &)> pred
pred_handle insert(pred predicate, trig trigger, PredicateType type=PredicateType::ONE_TIME)
Inserts a single (predicate, trigger) pair to the appropriate predicate list.
pred_handle(typename pred_list::iterator iter, PredicateType type)
pred_list transition_predicates
Predicate list for transition predicates.
void remove(pred_handle &pred)
Removes a (predicate, trigger) pair previously registered with insert().
PredicateType
Enumeration defining the kinds of predicates an SST can handle.
std::mutex predicate_mutex
pred_list recurrent_predicates
Predicate list for recurrent predicates.
Transition predicates persist as long as the SST instance, but only fire their triggers when they tra...
pred_handle insert(pred predicate, const std::list< trig > &triggers, PredicateType type=PredicateType::ONE_TIME)
Inserts a predicate with a list of triggers (which will be run in sequence) to the appropriate predic...
std::list< bool > transition_predicate_states
Contains one entry for every predicate in transition_predicates, in parallel.
std::list< std::unique_ptr< std::pair< pred, std::shared_ptr< trig > >> > pred_list
pred_handle(pred_handle &&other)
std::function< void(DerechoSST &)> trig
Recurrent predicates persist as long as the SST instance and fire their triggers every time they are ...
pred_list one_time_predicates
Predicate list for one-time predicates.
void clear()
Deletes all predicates, including evolvers and their triggers.
pred_handle & operator=(pred_handle &&other)