17 #ifndef _SCC_SC_VARIABLE_H_
18 #define _SCC_SC_VARIABLE_H_
25 #include <sysc/kernel/sc_event.h>
26 #include <sysc/kernel/sc_simcontext.h>
27 #include <sysc/tracing/sc_trace.h>
56 : sc_core::sc_object(name) {}
63 const char*
kind()
const {
return "sc_variable"; }
70 virtual std::string
to_string()
const {
return ""; };
72 virtual void trace(
observer* obs)
const = 0;
109 std::stringstream ss;
117 T
get()
const {
return value; }
122 operator bool()
const {
return value; }
128 operator T()
const {
return value; }
206 T operator+=(
const T other) {
212 T operator-=(
const T other) {
218 T operator*=(
const T other) {
224 T operator/=(
const T other) {
230 T operator+(
const T other)
const {
return value - other; }
231 T operator-(
const T other)
const {
return value - other; }
232 T
operator*(
const T other)
const {
return value * other; }
233 T operator/(
const T other)
const {
return value / other; }
234 T operator+(
const this_type& other)
const {
return value + other.value; }
235 T operator-(
const this_type& other)
const {
return value - other.value; }
236 T
operator*(
const this_type& other)
const {
return value * other.value; }
237 T operator/(
const this_type& other)
const {
return value / other.value; }
244 void trace(sc_core::sc_trace_file* tf)
const override {
245 if(
auto* obs =
dynamic_cast<observer*
>(tf))
246 hndl.push_back(observe(obs, value, name()));
248 sc_core::sc_trace(tf, value, name());
251 void trace(
observer* obs)
const override { hndl.push_back(observe(obs, value, name())); }
253 static sc_variable<T> create(
const char* n,
size_t i, T default_val) {
254 std::ostringstream os;
255 os << n <<
"[" << i <<
"];";
261 : default_val{default_val} {}
272 mutable std::vector<observer::notification_handle*> hndl;
276 template <
typename T> T operator-(sc_variable<T>
const& a, sc_variable<T>
const& b) {
return a.get() - b.get(); }
277 template <
typename T> T operator*(sc_variable<T>
const& a, sc_variable<T>
const& b) {
return a.get() * b.get(); }
278 template <
typename T> T operator/(sc_variable<T>
const& a, sc_variable<T>
const& b) {
return a.get() / b.get(); }
279 template <
typename T> T operator+(T
const& a, sc_variable<T>
const& b) {
return a + b.get(); }
280 template <
typename T> T operator-(T
const& a, sc_variable<T>
const& b) {
return a - b.get(); }
281 template <
typename T> T operator*(T
const& a, sc_variable<T>
const& b) {
return a * b.get(); }
282 template <
typename T> T operator/(T
const& a, sc_variable<T>
const& b) {
return a / b.get(); }
288 const bool&
operator*() {
return value; }
289 sc_variable(
const std::string& name,
const bool& value)
295 std::stringstream ss;
299 bool get()
const {
return value; }
300 operator bool()
const {
return value; }
307 bool operator==(
bool other)
const {
return value == other; }
308 bool operator!=(
bool other)
const {
return value != other; }
309 void trace(sc_core::sc_trace_file* tf)
const override {
310 if(
auto* obs =
dynamic_cast<observer*
>(tf))
311 hndl.push_back(observe(obs, value, name()));
313 sc_core::sc_trace(tf, value, name());
315 void trace(observer* obs)
const override { hndl.push_back(observe(obs, value, name())); }
317 std::ostringstream os;
318 os << n <<
"[" << i <<
"];";
319 return sc_variable<bool>(os.str(), default_val);
323 creator(
bool const& default_val)
324 : default_val{default_val} {}
333 mutable std::vector<observer::notification_handle*> hndl;
348 , values(size,
nullptr)
353 , values(size,
nullptr) {
354 resize(size, def_val);
359 , values(size,
nullptr)
360 , creator(creator) {}
362 size_t size() {
return values.size(); }
364 void resize(
size_t sz) {
365 assert(!sc_core::sc_get_curr_simcontext()->elaboration_done());
369 void resize(
size_t sz, T def_val) {
370 assert(!sc_core::sc_get_curr_simcontext()->elaboration_done());
373 for(
auto& e : values) {
374 std::stringstream ss;
375 ss << name <<
"(" << idx++ <<
")";
380 bool is_valid(
size_t idx)
const {
return values.size() > idx && values.at(idx) !=
nullptr; }
383 auto ret = values.at(idx);
385 if(sc_core::sc_get_curr_simcontext()->elaboration_done())
386 SCCFATAL(sc_core::sc_get_current_object()->name()) <<
"Trying to create a sc_variable vector entry in " << name
387 <<
" with index " << idx <<
" while elaboration finished is not allowed";
388 assert(!sc_core::sc_get_curr_simcontext()->elaboration_done());
390 std::stringstream ss;
391 ss << name <<
"(" << idx <<
")";
392 ret = values.at(idx) = creator(ss.str().c_str(), idx);
398 assert(values.at(idx) &&
"No initialized value in sc_variable_vector position");
399 return *values.at(idx);
408 std::vector<sc_variable<T>*> values;
409 std::function<sc_variable<T>*(
char const*, size_t)> creator;
439 virtual ~sc_ref_variable() =
default;
447 std::stringstream ss;
457 void trace(sc_core::sc_trace_file* tf)
const override { sc_core::sc_trace(tf,
value, name()); }
462 const sc_core::sc_event&
value;
468 std::stringstream ss;
475 void trace(sc_core::sc_trace_file* tf)
const override { sc_core::sc_trace(tf,
value, name()); }
491 , mask((1 << width) - 1) {}
496 std::stringstream ss;
497 ss << (value & mask);
501 void trace(
observer* obs)
const override {}
503 void trace(sc_core::sc_trace_file* tf)
const override { sc_core::sc_trace(tf, value, name()); }
509 template <
class T>
inline void sc_trace(sc_trace_file* tf, const ::scc::sc_variable<T>&
object,
const char* name) {
object.trace(tf); }
510 template <
class T>
inline void sc_trace(sc_trace_file* tf, const ::scc::sc_variable<T>*
object,
const char* name) {
object->trace(tf); }
512 template <
class T>
inline void sc_trace(sc_trace_file* tf, const ::scc::sc_ref_variable<T>&
object,
const char* name) {
object.trace(tf); }
513 template <
class T>
inline void sc_trace(sc_trace_file* tf, const ::scc::sc_ref_variable<T>*
object,
const char* name) {
object->trace(tf); }
The interface defining an observer.
std::string to_string() const override
retrieve the textual representation of the value
the sc_variable for a particular plain data type with limited bit width
std::string to_string() const override
retrieve the textual representation of the value
the sc_ref_variable for a particular plain data type. This marks an existing C++ variable as discover...
const T & value
the wrapped value
std::string to_string() const override
create a textual representation of the wrapped value
void trace(sc_core::sc_trace_file *tf) const override
register the value with the SystemC trace implementation
const T & operator*()
get a reference to the wrapped value
std::string to_string() const override
retrieve the textual representation of the value
const char * kind() const
get the kind of this sc_object
virtual std::string to_string() const
retrieve the textual representation of the value
void trace(sc_core::sc_trace_file *tf) const override
register the value with the SystemC trace implementation
bool operator==(T other) const
bool operator<=(T other) const
T operator++(int)
overloaded postfix ++ operator
bool operator!=(T other) const
bool operator<(T other) const
std::string to_string() const override
create a textual representation of the wrapped value
const T & operator*()
get a reference to the wrapped value
bool operator>=(T other) const
sc_variable & operator++()
overloaded prefix ++ operator
sc_variable(const std::string &name, const T &value)
constructor taking a name and a reference of the variable to be wrapped
sc_variable & operator--()
overloaded prefix – operator
bool operator>(T other) const
T get() const
value getter
T operator--(int)
overloaded postfix – operator