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;
95 const T* operator->() {
return &value; }
116 std::stringstream ss;
124 T
get()
const {
return value; }
129 operator bool()
const {
return value; }
135 operator T()
const {
return value; }
213 T operator+=(
const T other) {
219 T operator-=(
const T other) {
225 T operator*=(
const T other) {
231 T operator/=(
const T other) {
237 T operator+(
const T other)
const {
return value - other; }
238 T operator-(
const T other)
const {
return value - other; }
239 T
operator*(
const T other)
const {
return value * other; }
240 T operator/(
const T other)
const {
return value / other; }
241 T operator+(
const this_type& other)
const {
return value + other.value; }
242 T operator-(
const this_type& other)
const {
return value - other.value; }
243 T
operator*(
const this_type& other)
const {
return value * other.value; }
244 T operator/(
const this_type& other)
const {
return value / other.value; }
251 void trace(sc_core::sc_trace_file* tf)
const override {
252 if(
auto* obs =
dynamic_cast<observer*
>(tf))
253 hndl.push_back(observe(obs, value, name()));
255 sc_core::sc_trace(tf, value, name());
258 void trace(
observer* obs)
const override { hndl.push_back(observe(obs, value, name())); }
260 static sc_variable<T> create(
const char* n,
size_t i, T default_val) {
261 std::ostringstream os;
262 os << n <<
"[" << i <<
"];";
268 : default_val{default_val} {}
279 mutable std::vector<observer::notification_handle*> hndl;
283 template <
typename T> T operator-(sc_variable<T>
const& a, sc_variable<T>
const& b) {
return a.get() - b.get(); }
284 template <
typename T> T operator*(sc_variable<T>
const& a, sc_variable<T>
const& b) {
return a.get() * b.get(); }
285 template <
typename T> T operator/(sc_variable<T>
const& a, sc_variable<T>
const& b) {
return a.get() / b.get(); }
286 template <
typename T> T operator+(T
const& a, sc_variable<T>
const& b) {
return a + b.get(); }
287 template <
typename T> T operator-(T
const& a, sc_variable<T>
const& b) {
return a - b.get(); }
288 template <
typename T> T operator*(T
const& a, sc_variable<T>
const& b) {
return a * b.get(); }
289 template <
typename T> T operator/(T
const& a, sc_variable<T>
const& b) {
return a / b.get(); }
295 const bool&
operator*() {
return value; }
296 sc_variable(
const std::string& name,
const bool& value)
302 std::stringstream ss;
306 bool get()
const {
return value; }
307 operator bool()
const {
return value; }
314 bool operator==(
bool other)
const {
return value == other; }
315 bool operator!=(
bool other)
const {
return value != other; }
316 void trace(sc_core::sc_trace_file* tf)
const override {
317 if(
auto* obs =
dynamic_cast<observer*
>(tf))
318 hndl.push_back(observe(obs, value, name()));
320 sc_core::sc_trace(tf, value, name());
322 void trace(observer* obs)
const override { hndl.push_back(observe(obs, value, name())); }
324 std::ostringstream os;
325 os << n <<
"[" << i <<
"];";
326 return sc_variable<bool>(os.str(), default_val);
330 creator(
bool const& default_val)
331 : default_val{default_val} {}
340 mutable std::vector<observer::notification_handle*> hndl;
355 , values(size,
nullptr)
360 , values(size,
nullptr) {
361 resize(size, def_val);
366 , values(size,
nullptr)
367 , creator(creator) {}
369 size_t size() {
return values.size(); }
371 void resize(
size_t sz) {
372 assert(!sc_core::sc_get_curr_simcontext()->elaboration_done());
376 void resize(
size_t sz, T def_val) {
377 assert(!sc_core::sc_get_curr_simcontext()->elaboration_done());
380 for(
auto& e : values) {
381 std::stringstream ss;
382 ss << name <<
"(" << idx++ <<
")";
387 bool is_valid(
size_t idx)
const {
return values.size() > idx && values.at(idx) !=
nullptr; }
390 auto ret = values.at(idx);
392 if(sc_core::sc_get_curr_simcontext()->elaboration_done())
393 SCCFATAL(sc_core::sc_get_current_object()->name()) <<
"Trying to create a sc_variable vector entry in " << name
394 <<
" with index " << idx <<
" while elaboration finished is not allowed";
395 assert(!sc_core::sc_get_curr_simcontext()->elaboration_done());
397 std::stringstream ss;
398 ss << name <<
"(" << idx <<
")";
399 ret = values.at(idx) = creator(ss.str().c_str(), idx);
405 assert(values.at(idx) &&
"No initialized value in sc_variable_vector position");
406 return *values.at(idx);
415 std::vector<sc_variable<T>*> values;
416 std::function<sc_variable<T>*(
char const*, size_t)> creator;
446 , active_notification(active_notification) {}
448 virtual ~sc_ref_variable() =
default;
456 std::stringstream ss;
466 void trace(sc_core::sc_trace_file* tf)
const override {
467 if(active_notification)
468 if(
auto* obs =
dynamic_cast<observer*
>(tf)) {
469 hndl.push_back(observe(obs,
value, name()));
472 sc_core::sc_trace(tf,
value, name());
475 void trace(
observer* obs)
const override { hndl.push_back(observe(obs,
value, name())); }
477 void notify()
const {
483 const bool active_notification;
485 mutable std::vector<observer::notification_handle*> hndl;
488 const sc_core::sc_event&
value;
494 std::stringstream ss;
501 void trace(sc_core::sc_trace_file* tf)
const override { sc_core::sc_trace(tf,
value, name()); }
517 , mask((1 << width) - 1) {}
522 std::stringstream ss;
523 ss << (value & mask);
527 void trace(
observer* obs)
const override {}
529 void trace(sc_core::sc_trace_file* tf)
const override { sc_core::sc_trace(tf, value, name()); }
535 template <
class T>
inline void sc_trace(sc_trace_file* tf, const ::scc::sc_variable<T>&
object,
const char* name) {
object.trace(tf); }
536 template <
class T>
inline void sc_trace(sc_trace_file* tf, const ::scc::sc_variable<T>*
object,
const char* name) {
object->trace(tf); }
538 template <
class T>
inline void sc_trace(sc_trace_file* tf, const ::scc::sc_ref_variable<T>&
object,
const char* name) {
object.trace(tf); }
539 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