The Sparta Modeling Framework
Loading...
Searching...
No Matches
VirtualGlobalTreeNode.hpp
Go to the documentation of this file.
1// <VirtualGlobalTreeNode> -*- C++ -*-
2
3#pragma once
4
5#include <regex>
6#include <string>
7#include <vector>
8#include <memory>
9
11#include "sparta/log/MessageSource.hpp"
12
19namespace sparta
20{
29 class VirtualGlobalTreeNode final : public TreeNode
30 {
31 public:
32
38 "Global pseudo-TreeNode capturing propagating messages from ANY TreeNode in the simulator")
39 {
40 untrackParentlessNode_(this); // Does not actually have a parent
41
42 // Construct in teardown so that static destruction can kill this
43 // and its subtree
45 }
46
51
58
59 // Returns true. This node is always considered "attached"
60 virtual bool isAttached() const override final {
61 return true;
62 }
63
64 // Returns 0. Global node will never have a parent
65 virtual TreeNode* getParent() override final { return 0; }
66
67 // Returns 0. Global node will never have a parent
68 virtual const TreeNode* getParent() const override final { return 0; }
69
70 // Searches parentless nodes
71 virtual TreeNode* getImmediateChildByIdentity_(const std::string& name,
72 bool must_exist=true) override final {
73 for(auto& n : statics_->parentless_map_){
74 sparta::TreeNode::WeakPtr& child_wptr = n.second;
75 if(child_wptr.expired() == true){
76 continue;
77 }
78 std::shared_ptr<sparta::TreeNode> child = child_wptr.lock();
79 sparta_assert(child != nullptr, "No null nodes (groups) should ever be added to the parentless_nodes list");
80 std::vector<const std::string*> idents = child->getIdentifiers();
81 for(const std::string* ident_id : idents){
82 if(*ident_id == name){
83 return child.get();
84 }
85 }
86 }
87
88 if(false == must_exist){
89 return nullptr;
90 }
91
92 // Note: parentless nodes will never be groups (see asserts above)
93
94 throw SpartaException("Could not get immediate child named \"")
95 << name << "\" in node \"" << getLocation();
96 }
97
98 // Overload of getImmediateChildByIdentity_
100 virtual const TreeNode* getImmediateChildByIdentity_(const std::string& name,
101 bool must_exist=true) const override final {
102 for(auto& n : statics_->parentless_map_){
103 sparta::TreeNode::WeakPtr& child_wptr = n.second;
104 if(child_wptr.expired() == true){
105 continue;
106 }
107 std::shared_ptr<sparta::TreeNode> child = child_wptr.lock();
108 sparta_assert(child != nullptr, "No null nodes (groups) should ever be added to the parentless_nodes list");
109 std::vector<const std::string*> idents = child->getIdentifiers();
110 for(const std::string* ident_id : idents){
111 if(*ident_id == name){
112 return child.get();
113 }
114 }
115 }
116
117 if(false == must_exist){
118 return nullptr;
119 }
120
121 // Note: parentless nodes will never be groups (see asserts above)
122
123 throw SpartaException("Could not get immediate child named \"")
124 << name << "\" in node \"" << getLocation();
125 }
126
127 // Searches parentless nodes
128
129 // Const variant of findImmediateChildren_
130 virtual uint32_t findImmediateChildren_(std::regex& expr,
131 std::vector<TreeNode*>& found,
132 std::vector<std::vector<std::string>>& replacements,
133 bool allow_private) override final {
134 uint32_t num_found = 0;
135 for(auto& n : statics_->parentless_map_){
136 sparta::TreeNode::WeakPtr& child_wptr = n.second;
137 if(child_wptr.expired() == true){
138 continue;
139 }
140 std::shared_ptr<TreeNode> child = child_wptr.lock();
141 std::vector<std::string> replaced; // Replacements per child
142 if(identityMatchesPattern_(child->getName(), expr, replaced)){
143 if(child != nullptr){ // Ensure child is not null (e.g. grouping)
144 if (allow_private || canSeeChild_(child.get()))
145 {
146 ++num_found;
147 found.push_back(child.get());
148 replacements.push_back(replaced);
149 }
150 }
151 }
152 }
153 return num_found;
154 }
155
156 // Const variant of findImmediateChildren_
157 virtual uint32_t findImmediateChildren_(std::regex& expr,
158 std::vector<const TreeNode*>& found,
159 std::vector<std::vector<std::string>>& replacements,
160 bool allow_private) const override final {
161 uint32_t num_found = 0;
162 for(auto& n : statics_->parentless_map_){
163 sparta::TreeNode::WeakPtr& child_wptr = n.second;
164 if(child_wptr.expired() == true){
165 continue;
166 }
167 std::shared_ptr<TreeNode> child = child_wptr.lock();
168 std::vector<std::string> replaced; // Replacements per child
169 if(identityMatchesPattern_(child->getName(), expr, replaced)){
170 if(child != nullptr){ // Ensure child is not null (e.g. grouping)
171 if (allow_private || canSeeChild_(child.get()))
172 {
173 ++num_found;
174 found.push_back(child.get());
175 replacements.push_back(replaced);
176 }
177 }
178 }
179 }
180 return num_found;
181 }
182
183 private:
184
185 // Override form TreeNode
186 // Disallow adding children
187 virtual void onAddingChild_(TreeNode*) override final {
188 // Should disallow adding childre, but global loggers must be allowe to be attached
189 //throw SpartaException("Cannot add a child to the virtual global TreeNode unless it "
190 // "is one of its builtin message sources");
191 }
192
193 // Override from TreeNode
194 // Broadcast to all nodes except self (excluded from all_nodes_ list)
195 virtual void broadcastRegistrationForNotificationToChildren_(const std::type_info& tinfo,
196 const std::vector<const std::string*>& name_ids,
197 TreeNode* obs_node,
198 const delegate* del,
199 const bool private_only) override final {
200 (void)private_only;
201 for(auto& n : statics_->parentless_map_){
202 sparta::TreeNode::WeakPtr& child = n.second;
203 if(child.expired() == false){
204 child.lock()->broadcastRegistrationForNotificationToChildren_(tinfo, name_ids, obs_node, del, private_only);
205 }
206 }
207 }
208
209 // Override from TreeNode
210 // Broadcast to all nodes except self (excluded from all_nodes_ list)
211 virtual void broadcastDeregistrationForNotificationToChildren_(const std::type_info& tinfo,
212 const std::vector<const std::string*>& name_ids,
213 TreeNode* obs_node,
214 const delegate* del,
215 const bool private_only) override final {
216 (void)private_only;
217 for(auto& n : statics_->parentless_map_){
218 sparta::TreeNode::WeakPtr& child = n.second;
219 if(child.expired() == false){
220 child.lock()->broadcastDeregistrationForNotificationToChildren_(tinfo, name_ids, obs_node, del, private_only);
221 }
222 }
223 }
224
225 // Override from TreeNode
226 // Virtual global node cannot generate any notifications!
227 virtual bool canGenerateNotification_(const std::type_info&,
228 const std::string*,
229 const std::string*&) const override final {
230 return false;
231 }
232 };
233
234} // namespace sparta
235
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Basic Node framework in sparta device tree composite pattern.
void setPhase_(TreePhase phase)
Sets the current phase.
@ TREE_TEARDOWN
Simulation is complete. Tree and all resources are now allowed to be deleted (phase does not imply th...
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 constexpr char NODE_NAME_VIRTUAL_GLOBAL[]
Node name for the virtual glopbal node.
Definition TreeNode.hpp:329
std::weak_ptr< TreeNode > WeakPtr
Weak pointer to a TreeNode. Acquire with getWeakPtr.
Definition TreeNode.hpp:266
std::string getLocation() const override final
TreeNode()=delete
Not default-constructable.
static bool identityMatchesPattern_(const std::string &ident, std::regex &expr, std::vector< std::string > &replacements)
Performs pattern matching on a identity string.
Virtual global node for all device trees in a single simulation. This node acts a potential notificat...
static VirtualGlobalTreeNode * getInstance()
Gets the virtual global node singleton. This is the same object as TreeNode::getVirtualGlobalNode but...
virtual const TreeNode * getImmediateChildByIdentity_(const std::string &name, bool must_exist=true) const override final
virtual uint32_t findImmediateChildren_(std::regex &expr, std::vector< const TreeNode * > &found, std::vector< std::vector< std::string > > &replacements, bool allow_private) const override final
Const-qualified variant of findImmediateChildren_.
virtual bool isAttached() const override final
Is this node part of a device tree with a proper RootTreeNode at the root.
virtual uint32_t findImmediateChildren_(std::regex &expr, std::vector< TreeNode * > &found, std::vector< std::vector< std::string > > &replacements, bool allow_private) override final
Finds immediate children with some identity (name or alias) matching a regex.
virtual TreeNode * getImmediateChildByIdentity_(const std::string &name, bool must_exist=true) override final
Attempts to get a single child by its unique local identity (name or alias)
virtual TreeNode * getParent() override final
Gets immediate parent of this node if one exists.
Macros for handling exponential backoff.