The Sparta Modeling Framework
Loading...
Searching...
No Matches
ReadOnlyCounter.hpp
1// <Counter> -*- C++ -*-
2
3
4#pragma once
5
7#include "sparta/statistics/CounterBase.hpp"
10
11
12namespace sparta
13{
26 {
27 public:
28
32
50 const std::string& name,
51 const std::string& group,
53 const std::string& desc,
54 CounterBehavior behave,
55 const counter_type* ref,
56 visibility_t visibility) :
57 CounterBase(parent,
58 name,
59 group,
60 group_idx,
61 desc,
62 behave,
63 visibility),
64 ref_(ref)
65 { }
66
71 const std::string& name,
72 const std::string& group,
74 const std::string& desc,
75 CounterBehavior behave,
76 visibility_t visibility) :
77 ReadOnlyCounter(parent,
78 name,
79 group,
80 group_idx,
81 desc,
82 behave,
83 nullptr,
84 visibility)
85 {
86 // Initialization handled in delegated constructor
87 }
88
89 // Alternate constructor
91 const std::string& name,
92 const std::string& desc,
93 CounterBehavior behave,
94 const counter_type* ref,
95 visibility_t visibility) :
96 ReadOnlyCounter(parent,
97 name,
100 desc,
101 behave,
102 ref,
103 visibility)
104 {
105 // Initialization handled in delegated constructor
106 }
107
108 // Alternate constructor
109 ReadOnlyCounter(TreeNode* parent,
110 const std::string& name,
111 const std::string& desc,
112 CounterBehavior behave,
113 visibility_t visibility) :
114 ReadOnlyCounter(parent,
115 name,
116 TreeNode::GROUP_NAME_NONE,
117 TreeNode::GROUP_IDX_NONE,
118 desc,
119 behave,
120 nullptr,
121 visibility)
122 {
123 // Initialization handled in delegated constructor
124 }
125
126 // Alternate Constructor
127 ReadOnlyCounter(TreeNode* parent,
128 const std::string& name,
129 const std::string& group,
130 TreeNode::group_idx_type group_idx,
131 const std::string& desc,
132 CounterBehavior behave,
133 const counter_type* ref) :
134 ReadOnlyCounter(parent,
135 name,
136 group,
137 group_idx,
138 desc,
139 behave,
140 ref,
142 { }
143
148 const std::string& name,
149 const std::string& group,
150 TreeNode::group_idx_type group_idx,
151 const std::string& desc,
152 CounterBehavior behave) :
153 ReadOnlyCounter(parent,
154 name,
155 group,
156 group_idx,
157 desc,
158 behave,
159 nullptr,
161 {
162 // Initialization handled in delegated constructor
163 }
164
165 // Alternate constructor
167 const std::string& name,
168 const std::string& desc,
169 CounterBehavior behave,
170 const counter_type* ref) :
171 ReadOnlyCounter(parent,
172 name,
175 desc,
176 behave,
177 ref,
179 {
180 // Initialization handled in delegated constructor
181 }
182
183 // Alternate constructor
184 ReadOnlyCounter(TreeNode* parent,
185 const std::string& name,
186 const std::string& desc,
187 CounterBehavior behave) :
188 ReadOnlyCounter(parent,
189 name,
190 TreeNode::GROUP_NAME_NONE,
191 TreeNode::GROUP_IDX_NONE,
192 desc,
193 behave,
194 nullptr,
196 {
197 // Initialization handled in delegated constructor
198 }
199
205 CounterBase(std::move(rhp)),
206 ref_(rhp.ref_)
207 {
208 TreeNode* parent = rhp.getParent();
209 if(parent != nullptr){
210 parent->addChild(this);
211 }
212 }
213
219
222
226
234 virtual counter_type get() const override {
235 sparta_assert(ref_ != nullptr,
236 "Canot 'get()' on ReadOnlyCounter "
237 << getLocation() << " because it has a null ref_ "
238 "pointer. If constructed without a variale "
239 "reference, the get() method must be overridden.");
240 return *ref_;
241 }
242
246 operator counter_type() const {
247 return get();
248 }
249
253 bool operator==(const CounterBase& rhp) const {
254 return get() == rhp.get();
255 }
256
259
262 virtual bool supportsCompression() const override {
263 return true;
264 }
265
269
270 // Override from TreeNode
271 virtual std::string stringize(bool pretty=false) const override {
272 (void) pretty;
273 std::stringstream ss;
274 ss << '<' << getLocation() << " val:" << std::dec;
275 ss << get() << ' ';
277 ss << " vis:" << getVisibility() << '>';
278 return ss.str();
279 }
280
281 protected:
282
288 virtual void onAddingChild_(TreeNode* child) override {
289 (void) child;
290 throw SpartaException("Cannot add children to a ReadOnlyCounter");
291 }
292
293 private:
294
298 const counter_type* const ref_;
299
300 }; // class ReadOnlyCounter
301
302} // namespace sparta
303
Byte order types and byte-swapping routines.
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.
Exception class for all of Sparta.
The base class for all Counters.
virtual counter_type get() const =0
Gets the value of this counter.
static std::string getBehaviorName(CounterBehavior behave)
Returns a string containing the name of the given behavior.
uint64_t counter_type
Counter value type.
CounterBehavior
Behavior of this counter.
CounterBehavior getBehavior() const
Gets the behavior for this counter specified at construction.
uint32_t visibility_t
Continuous visibility level. Several key points along continum are indicated within Visibility.
static constexpr visibility_t DEFAULT_VISIBILITY
Default node visibility.
visibility_t getVisibility() const
Gets the visibility hint of this node. This is invariant after construction.
Represents a non-writable and non-observable counter with a very similar interface to sparta::Counter...
ReadOnlyCounter(TreeNode *parent, const std::string &name, const std::string &group, TreeNode::group_idx_type group_idx, const std::string &desc, CounterBehavior behave)
ReadOnlyCounter(TreeNode *parent, const std::string &name, const std::string &group, TreeNode::group_idx_type group_idx, const std::string &desc, CounterBehavior behave, const counter_type *ref, visibility_t visibility)
ReadOnlyCounter constructor.
virtual void onAddingChild_(TreeNode *child) override
React to child registration.
virtual std::string stringize(bool pretty=false) const override
Create a string representation of this node.
virtual counter_type get() const override
Gets the value of this counter.
ReadOnlyCounter(ReadOnlyCounter &&rhp)
Move constructor.
ReadOnlyCounter(TreeNode *parent, const std::string &name, const std::string &group, TreeNode::group_idx_type group_idx, const std::string &desc, CounterBehavior behave, visibility_t visibility)
bool operator==(const CounterBase &rhp) const
Comparison against another counter.
virtual bool supportsCompression() const override
Used to construct and throw a standard C++ exception. Inherits from std::exception.
Node in a composite tree representing a sparta Tree item.
Definition TreeNode.hpp:205
static const group_idx_type GROUP_IDX_NONE
GroupIndex indicating that a node has no group index because it belongs to no group.
Definition TreeNode.hpp:303
std::string getLocation() const override final
static constexpr char GROUP_NAME_NONE[]
Group name indicating that a node belongs to no group.
Definition TreeNode.hpp:314
void addChild(TreeNode *child, bool inherit_phase=true)
Adds a TreeNode to this node as a child.
virtual TreeNode * getParent()
Gets immediate parent of this node if one exists.
Definition TreeNode.hpp:965
uint32_t group_idx_type
Index within a group.
Definition TreeNode.hpp:261
Macros for handling exponential backoff.