4#include "sparta/utils/Enum.hpp"
6#include "sparta/simulation/Audience.hpp"
8#include "sparta/simulation/StateTracker.hpp"
16 class SimulationConfiguration;
49 enum_val_(val ? BoolValue::TRUE : BoolValue::FALSE) {}
60 enum_val_(other.enum_val_) {}
64 enum_val_(std::move(other.enum_val_)) {}
68 enum_val_ = other.enum_val_;
74 enum_val_ = std::move(other.enum_val_);
80 operator uint32_t()
const {
81 return static_cast<uint32_t
>(enum_val_);
121 template<
class EnumT,
class MetaDataT =
void, u
int32_t MAX_MARKERS = 15>
132 typedef typename std::conditional<
133 !std::is_same<EnumT, bool>::value,
141 typedef typename std::conditional<
142 !std::is_same<EnumT, bool>::value,
143 EnumT, BoolEnum::BoolValue>::type
163 void initialize(MarkerSet * marker_set,
const EnumTValueType & val) {
164 marker_set_ = marker_set;
176 marker_set_->jointSet_(ptr);
185 marker_set_->clearMark_();
205 MarkerSet * marker_set_ =
nullptr;
208 bool marked_ =
false;
256 MarkerSet(
const MarkerSet&) =
default;
263 Marker * makeMarker(State * state) {
266 markers_[marker_cnt_].initialize(
this, transition_val_);
267 return &markers_[marker_cnt_++];
274 uint32_t numMarkers()
const {
279 uint32_t numMarks()
const {
280 return marked_count_;
287 uint32_t numMarked()
const {
289 for(uint32_t i = 0; i < marker_cnt_; ++i) {
290 cnt += markers_[i].isMarked();
297 for(uint32_t i = 0; i < marker_cnt_; ++i) {
298 markers_[i].reset_();
305 void observe(
const ScheduleableHandle & ev_hand) {
306 audience_.enroll(ev_hand);
310 void withdraw(
const ScheduleableHandle & ev_hand) {
311 audience_.withdraw(ev_hand);
320 void setThreshold(uint32_t thresh) {
321 marked_thresh_ = thresh;
325 void attachMonitor(Monitor * mon) {
326 monitors_.emplace_back(mon);
330 void detachMonitor(Monitor *mon)
332 auto i = std::find(monitors_.begin(), monitors_.end(), mon);
333 if (i != monitors_.end()) {
341 void notifyObservers() {
364 for (
auto& i : monitors_) {
365 i->signalSet(transition_val_, ptr);
368 }
else if ((marker_cnt_ - marked_count_) <= marked_thresh_) {
369 state_->setValue_(transition_val_);
383 uint32_t marked_count_ = 0;
384 uint32_t marked_thresh_ = 0;
386 Marker markers_[MAX_MARKERS];
387 uint32_t marker_cnt_ = 0;
388 State *state_ =
nullptr;
391 bool is_set_ =
false;
392 std::vector<Monitor *> monitors_;
401 current_state_ = val;
408 template<
typename U = EnumT>
409 typename std::enable_if<std::is_same<U, bool>::value,
void>::type
411 setValue(BoolEnum::BoolValue::FALSE);
419 template<
typename U = EnumT>
420 typename std::enable_if<!std::is_same<U, bool>::value,
void>::type
421 attemptSetValue_() {}
439 initial_value_(initial_value),
440 current_state_(initial_value),
461 state_tracker_unit_(std::move(
462 tracker::StatePoolManager::getInstance().
469 if(state_tracker_unit_) {
470 state_tracker_unit_->startState(initial_value_);
474 marker_set_[i].transition_val_ =
static_cast<EnumType>(i);
476 static_assert(std::is_enum<EnumType>::value,
477 "ERROR: State classes must be templatized on a valid enum or enum class type");
483 initial_value_{other.initial_value_},
484 current_state_{other.current_state_},
485 marker_set_{other.marker_set_},
486 state_tracker_unit_{other.state_tracker_unit_ ? new auto {*other.state_tracker_unit_} : nullptr}
498 return current_state_;
501 const EnumType & getEnumValue()
const {
502 return (current_state_.
getValue()).getEnum();
510 "This method can only be called on this State class with tracking enabled");
511 return state_tracker_unit_->getActiveTime();
523 if(state_tracker_unit_) {
524 state_tracker_unit_->startState(val);
531 marker_set_[val].notifyObservers();
542 for(
auto & ms : marker_set_) {
556 current_state_ = val;
557 for(
auto & ms : marker_set_) {
601 return marker_set_[val].makeMarker(
this);
610 return marker_set_[val].numMarks();
619 return marker_set_[val].numMarkers();
640 marker_set_[val].setThreshold(thresh);
656 return (marker_set_[val].
isSet() ==
true);
666 return (marker_set_[val].
isSet() ==
false);
684 marker_set_[val].observe(ev_hand);
695 marker_set_[val].withdraw(ev_hand);
700 marker_set_[val].release();
718 marker_set_[val].attachMonitor(mon);
728 marker_set_[val].detachMonitor(mon);
733#ifndef DO_NOT_DOCUMENT
737 const std::vector<Scheduler::Tick> & getRawAccumulatedTime()
const {
739 return state_tracker_unit_->getStateSet().state_delta_set;
747 utils::ValidValue<EnumTValueType> current_state_;
748 std::array<MarkerSet, static_cast<uint32_t>(EnumType::__LAST)> marker_set_;
753 tracker::state_tracker_ptr<EnumType> state_tracker_unit_;
765 typename std::enable_if<
766 std::is_same<T, PhasedObject::TreePhase>::value>::type> {
File that defines the Scheduleable class.
A simple time-based, event precedence based scheduler.
Set of macros for Sparta assertions. Caught by the framework.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
#define SPARTA_EXPECT_FALSE(x)
A macro for hinting to the compiler a particular condition should be considered most likely false.
Exception class for all of Sparta.
This is the internal enum class which holds the enum values for bool instances. This enum has the sam...
The class responsible for handling State<bool> instances and converts bool instances into actual Enum...
A light-weight reference counting handle for Scheduleables – DOES NOT delete.
uint64_t Tick
Typedef for our unit of time.
Class that will mark the state for transition.
bool isMarked() const
Is the marker set? true if so.
void set(MetaDataTPtr ptr=nullptr)
Set the marker.
const EnumTValueType & getMarkerValue() const
Return the marker value for this marker (debug use)
void clear()
Clear the marker.
Monitor a particular State value and allow the user to determine a full state change.
State::EnumTValueType EnumTValueType
Expose the State's EnumTValueType.
virtual ~Monitor()
prevent deletion via the base pointer
virtual void signalSet(const EnumTValueType &val, MetaDataTPtr)=0
The State class for watching transition between enum states.
void reset(const EnumTValueType &val)
Reset this State class to the given value.
bool isClear(const EnumTValueType &val=BoolEnum::BoolValue::TRUE) const
Determine if the State value is clear (never set)
void detachMonitor(const EnumTValueType &val, Monitor *mon)
Decouple a monitor from a state value.
State(const EnumTValueType &initial_value=EnumType::__FIRST)
Construct a State class.
Marker * newMarker(const EnumTValueType &val)
Get a new marker for the enum type.
State & operator=(const State &)=delete
Deleting default assignment operator to prevent copies.
void reset()
Reset this State class.
Scheduler::Tick getTimeInState() const
bool operator==(const EnumTValueType &rhs_val) const
void withdraw(const EnumTValueType &val, const ScheduleableHandle &ev_hand)
withdraw event from this state
bool isSet(const EnumTValueType &val=BoolEnum::BoolValue::TRUE) const
Determine if the State was ever set to value val.
MetaDataT * MetaDataTPtr
A peeter to the MetaData.
std::conditional<!std::is_same< EnumT, bool >::value, typenameutils::Enum< EnumT >::Value, BoolEnum >::type EnumTValueType
Convert the EnumT to a EnumT::Value or BoolEnum type.
void setValue(const EnumTValueType &val)
Set a new enum value explicit and fire observers.
void operator=(const EnumTValueType &val)
Assign a new state.
bool operator!=(const EnumTValueType &rhs_val) const
bool complete(const EnumTValueType &val) const
For a particular value, have all the marks been made.
virtual ~State()
Virtual destructor.
State(const State &other)
Copy Constructor for State.
void setMarkedThreshold(const EnumTValueType &val, uint32_t thresh)
Set the marker set threshold for the given state value.
const EnumTValueType & getValue() const
Get the current value of the state.
void observe(const EnumTValueType &val, const ScheduleableHandle &ev_hand)
Observe this state, specifically, when it transitions to the given enum val.
uint32_t numMarkers(const EnumTValueType &val) const
Get the number of markers for the enum type.
void attachMonitor(const EnumTValueType &val, Monitor *mon)
Attach a Monitor to a state value.
std::conditional<!std::is_same< EnumT, bool >::value, EnumT, BoolEnum::BoolValue >::type EnumType
If EnumT is not an actual enum, we need to use.
uint32_t numMarks(const EnumTValueType &val) const
Get the number of marks for the enum type.
Simulator which builds a sparta DeviceTree.
const value_type & getValue() const
Get the value - const version.
Macros for handling exponential backoff.