The Sparta Modeling Framework
Loading...
Searching...
No Matches
Tag.hpp
Go to the documentation of this file.
1// <Tag.hpp> -*- C++ -*-
2
3
11#pragma once
12
13#include <inttypes.h>
14#include <string>
17
18namespace sparta
19{
20
30class Tag
31{
32 typedef uint32_t SequenceType;
33public:
38 Tag() :
39 this_seq_ (global_seq_++)
40 {}
41
47 Tag(const Tag& other) = default;
48
56 Tag(Tag* parent) :
57 parent_(parent)
58 {
59 if(SPARTA_EXPECT_TRUE(parent != nullptr)) {
60 this_seq_ = parent->child_seq_;
61 ++(parent->child_seq_);
62 }
63 else {
64 this_seq_ = global_seq_++;
65 }
66 }
67
73 operator std::string() const
74 {
75 std::stringstream ss;
76 if(parent_) {
77 ss << std::string(*parent_) << '.' << this_seq_;
78 }
79 else {
80 ss << this_seq_;
81 }
82 return ss.str();
83 }
84
92 bool operator==(const Tag& other) const
93 {
94 if(parent_) {
95 if(other.parent_ == nullptr) {
96 // not equivalent, not the same hierarchy
97 return false;
98 }
99 return (this_seq_ == other.this_seq_) &&
100 (*parent_ == *other.parent_);
101 }
102 else {
103 if(other.parent_ != nullptr) {
104 // not equivalent, not the same hierarchy
105 return false;
106 }
107 return (this_seq_ == other.this_seq_);
108 }
109 }
110
118 bool operator!=(const Tag& other) const
119 {
120 return !operator==(other);
121 }
122
130 bool operator==(const std::string& s) const
131 {
132 return (std::string(*this) == s);
133 }
134
142 bool operator!=(const std::string& s) const
143 {
144 return !operator==(s);
145 }
146
147 static void resetGlobalSeqNum() {
148 global_seq_ = 1;
149 }
150
151private:
152 Tag *parent_ = nullptr;
153 SequenceType this_seq_ = 0;
154 SequenceType child_seq_ = 1;
155 static SequenceType global_seq_;
156};
157
166inline std::ostream& operator<<(std::ostream& os, const Tag& tag)
167{
168 os << std::string(tag);
169 return os;
170}
171
180inline std::ostream& operator<<(std::ostream& os, const Tag* tag)
181{
182 if (tag != nullptr) {
183 os << std::string(*tag);
184 } else {
185 // throw an exception here?
186 os << "NULL TAG";
187 }
188
189 return os;
190}
191
192
193} // sparta
194
195#define SPARTA_TAG_BODY \
196 namespace sparta { \
197 Tag::SequenceType Tag::global_seq_ = 1; \
198 }
199
Set of macros for Sparta assertions. Caught by the framework.
#define SPARTA_EXPECT_TRUE(x)
A macro for hinting to the compiler a particular condition should be considered most likely true.
Exception class for all of Sparta.
Tag(): Simple class to provide nested sequence numbering.
Definition Tag.hpp:31
bool operator==(const std::string &s) const
==(): Equality operator (vs. string)
Definition Tag.hpp:130
bool operator!=(const Tag &other) const
!=(): Inequality operator
Definition Tag.hpp:118
bool operator!=(const std::string &s) const
!=(): Inequality operator (vs. string)
Definition Tag.hpp:142
Tag(Tag *parent)
Tag(): Construct as a child of a parent Tag. This will increment the child sequence counter in the pa...
Definition Tag.hpp:56
Tag()
Tag(): Constructor Construct a new tag, using the internal global sequence number.
Definition Tag.hpp:38
bool operator==(const Tag &other) const
==(): Equality operator
Definition Tag.hpp:92
Tag(const Tag &other)=default
Tag(): Copy constructor.
Macros for handling exponential backoff.
std::ostream & operator<<(std::ostream &o, const SimulationInfo &info)
ostream insertion operator for SimulationInfo