The Sparta Modeling Framework
Loading...
Searching...
No Matches
SharedData.hpp
Go to the documentation of this file.
1// <SharedData.h> -*- C++ -*-
2
3
10#pragma once
11
12#include <array>
15
16namespace sparta
17{
36 template<class DataT, bool manual_update = false>
38 {
39 // Convenient typedef for internal data type
40 typedef std::array<utils::ValidValue<DataT>, 2> PSNSData;
41
42 // Next state index
43 uint32_t NState_() const {
44 return (current_state_ + 1) & 0x1;
45 }
46
47 // Present state index
48 uint32_t PState_() const {
49 return current_state_;
50 }
51
52 void resetHandler_()
53 {
54 ev_update_.resetHandler(CREATE_SPARTA_HANDLER(SharedData, update_));
55 }
56
57 public:
64 template<typename U = DataT>
65 SharedData(const std::string & name,
66 const Clock * clk,
67 U && init_val = U()) :
68 name_(name),
69 ev_update_(clk, CREATE_SPARTA_HANDLER(SharedData, update_))
70 {
71 writePS(std::forward<U>(init_val));
72 }
73
74 SharedData(const SharedData& rhs) :
75 name_(rhs.name_),
76 ev_update_(rhs.ev_update_),
77 current_state_(rhs.current_state_),
78 data_(rhs.data_)
79 {
80 resetHandler_();
81 }
82
83 SharedData(SharedData&& rhs) :
84 name_(std::move(rhs.name_)),
85 ev_update_(std::move(rhs.ev_update_)),
86 current_state_(std::move(rhs.current_state_)),
87 data_(std::move(rhs.data_))
88 {
89 resetHandler_();
90 }
91
92 SharedData& operator=(const SharedData& rhs)
93 {
94 name_ = rhs.name_;
95 ev_update_ = rhs.ev_update_;
96 resetHandler_();
97
98 current_state_ = rhs.current_state_;
99 data_ = rhs.data_;
100
101 return *this;
102 }
103
104 SharedData& operator=(SharedData&& rhs)
105 {
106 name_ = std::move(rhs.name_);
107 ev_update_ = std::move(rhs.ev_update_);
108 resetHandler_();
109
110 current_state_ = std::move(rhs.current_state_);
111 data_ = std::move(rhs.data_);
112
113 return *this;
114 }
115
120 const std::string & getName() const {
121 return name_;
122 }
123
128 void writePS(const DataT & dat) {
129 writePSImpl_(dat);
130 }
131
136 void writePS(DataT && dat) {
137 writePSImpl_(std::move(dat));
138 }
139
144 bool isValid() const {
145 return data_[PState_()].isValid();
146 }
147
153 const DataT & read() const {
155 return data_[PState_()];
156 }
157
163 DataT & access() {
165 return data_[PState_()];
166 }
167
172 void write(const DataT & dat) {
173 writeImpl_(dat);
174 }
175
180 void write(DataT && dat) {
181 writeImpl_(std::move(dat));
182 }
183
188 bool isValidNS() const {
189 return data_[NState_()].isValid();
190 }
191
198 const DataT & readNS() const {
200 return data_[NState_()];
201 }
202
209 DataT & accessNS() {
211 return data_[NState_()];
212 }
213
215 void clear() {
216 data_[PState_()].clearValid();
217 data_[NState_()].clearValid();
218 }
219
221 void clearNS() {
222 data_[NState_()].clearValid();
223 }
224
226 void clearPS() {
227 data_[PState_()].clearValid();
228 }
229
233 void update() {
234 static_assert(manual_update == true,
235 "Cannot call update on a SharedData object if manual_update == false");
236 update_();
237 }
238
239 private:
240 template<typename U>
241 void writePSImpl_(U && dat) {
242 data_[PState_()] = std::forward<U>(dat);
243 }
244
245 template<typename U>
246 void writeImpl_(U && dat) {
247 data_[NState_()] = std::forward<U>(dat);
248 if constexpr (!manual_update) {
249 ev_update_.schedule(1);
250 }
251 }
252
253 void update_()
254 {
255 current_state_ = NState_();
256 clearNS();
257 }
258
259 // SharedData's name
260 std::string name_;
261
262 // Global Event
263 GlobalEvent<SchedulingPhase::Update> ev_update_;
264
265 // Current state
266 uint32_t current_state_ = 0;
267
268 // The PS/NS data
269 PSNSData data_;
270 };
271}
File that defines the GlobalEvent class.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
#define CREATE_SPARTA_HANDLER(clname, meth)
File that defines a ValidValue.
A representation of simulated time.
Definition Clock.hpp:44
Class that allows the writing of data this cycle, but not visable until next cycle.
void clearPS()
Clear Present State valid.
DataT & accessNS()
Get a non-constant reference to the data that will be visible next cycle.
bool isValidNS() const
Is there data for the next cycle?
bool isValid() const
Is there data in the current view.
void update()
Update the SharedData class – move next cycle data to current view. Can only be called on a manually ...
void write(const DataT &dat)
Write data for the next cycle view.
SharedData(const std::string &name, const Clock *clk, U &&init_val=U())
Construct a SharedData item.
void clearNS()
Clear Next State valid.
void write(DataT &&dat)
Write data for the next cycle view.
void writePS(DataT &&dat)
Write data to the current view.
void clear()
Clear both Present State and Next State valids.
const std::string & getName() const
Get the name of this SharedData.
const DataT & readNS() const
Get a constant reference to the data that will be visible next cycle.
const DataT & read() const
Get a constant reference to the data visible this cycle.
DataT & access()
Get a non-constant reference to the data visible this cycle.
void writePS(const DataT &dat)
Write data to the current view.
Macros for handling exponential backoff.