abst_ext.hpp
Go to the documentation of this file.
1 /**********************************************************************
2  * abst_ext.hpp -- Absent-extended values data-type *
3  * *
4  * Author: Hosein Attarzadeh (shan2@kth.se) *
5  * *
6  * Purpose: Extend normal values with their absent-extended *
7  * version, required by some MoCs (e.g., synhronous) *
8  * *
9  * Usage: This file is included automatically *
10  * *
11  * License: BSD3 *
12  *******************************************************************/
13 
14 #ifndef ABST_EXT_HPP
15 #define ABST_EXT_HPP
16 
21 namespace ForSyDe
22 {
23 
24 using namespace sc_core;
25 
27 
31 template <typename T>
32 class abst_ext
33 {
34 public:
36  abst_ext(const T& val) : present(true), value(val) {}
37 
39  abst_ext() : present(false) {}
40 
42  T from_abst_ext (const T& defval) const
43  {
44  if (present) return value; else return defval;
45  }
46 
48  inline friend T from_abst_ext (const abst_ext& absval, const T& defval)
49  {
50  if (absval.present) return absval.value; else return defval;
51  }
52 
54  T unsafe_from_abst_ext () const {return value;}
55 
57  inline friend T unsafe_from_abst_ext(const abst_ext& absval)
58  {
59  return absval.value;
60  }
61 
63  void set_abst() {present=false;}
64 
66  inline friend void set_abst(abst_ext& absval) {absval.present=false;}
67 
69  void set_val(const T& val) {present=true;value=val;}
70 
72  inline friend void set_val(abst_ext& absval, const T& val)
73  {
74  absval.present=true;
75  absval.value=val;
76  }
77 
79  bool is_absent() const {return !present;}
80 
82  inline friend bool is_absent(const abst_ext& absval) {return !absval.present;}
83 
85  bool is_present() const {return present;}
86 
88  inline friend bool is_present(const abst_ext& absval) {return absval.present;}
89 
91  bool operator== (const abst_ext& rs) const
92  {
93  if (is_absent() || rs.is_absent())
94  return is_absent() && rs.is_absent();
95  else
96  return unsafe_from_abst_ext() == rs.unsafe_from_abst_ext();
97  }
98 
100  friend std::ostream& operator<< (std::ostream& os, const abst_ext &abst_ext)
101  {
102  if (abst_ext.is_present())
103  os << abst_ext.unsafe_from_abst_ext();
104  else
105  os << "_";
106  return os;
107  }
108 private:
109  bool present;
110  T value;
111 };
112 
114 
117 #define CHECK_PRESENCE(VAL) \
118  if (is_absent(VAL)) SC_REPORT_ERROR(this->name(),"Unexpected absent value received in");
119 
120 }
121 #endif
T unsafe_from_abst_ext() const
Unsafely converts a value from an extended value assuming it is present.
Definition: abst_ext.hpp:54
The namespace for ForSyDe.
Definition: abssemantics.hpp:30
bool is_present() const
Checks for the presence of a value.
Definition: abst_ext.hpp:85
bool is_absent() const
Checks for the absence of a value.
Definition: abst_ext.hpp:79
abst_ext()
The constructor with an absent value.
Definition: abst_ext.hpp:39
abst_ext(const T &val)
The constructor with a present value.
Definition: abst_ext.hpp:36
friend bool is_present(const abst_ext &absval)
Checks for the presence of a value.
Definition: abst_ext.hpp:88
friend void set_val(abst_ext &absval, const T &val)
Sets the value.
Definition: abst_ext.hpp:72
friend void set_abst(abst_ext &absval)
Sets absent.
Definition: abst_ext.hpp:66
T from_abst_ext(const T &defval) const
Converts a value from an extended value, returning a default value if absent.
Definition: abst_ext.hpp:42
Absent-extended data types.
Definition: abst_ext.hpp:32
friend T unsafe_from_abst_ext(const abst_ext &absval)
Unsafely converts a value from an extended value assuming it is present.
Definition: abst_ext.hpp:57
void set_val(const T &val)
Sets the value.
Definition: abst_ext.hpp:69
friend T from_abst_ext(const abst_ext &absval, const T &defval)
Converts a value from an extended value, returning a default value if absent.
Definition: abst_ext.hpp:48
void set_abst()
Sets absent.
Definition: abst_ext.hpp:63
friend bool is_absent(const abst_ext &absval)
Checks for the absence of a value.
Definition: abst_ext.hpp:82