The Sparta Modeling Framework
Loading...
Searching...
No Matches
TreeNode.hpp
Go to the documentation of this file.
1// <TreeNode> -*- C++ -*-
2
3
9#pragma once
10
11#include <cstddef>
12#include <cstdint>
13#include <iostream>
14#include <fstream>
15#include <string>
16#include <ostream>
17#include <vector>
18#include <sstream>
19#include <set>
20#include <map>
21#include <unordered_map>
22#include <regex>
23#include <functional>
24#include <memory>
25#include <type_traits>
26#include <typeinfo>
27#include <utility>
28
31#include "sparta/simulation/TreeNodeExtensions.hpp"
33#include "sparta/utils/Utils.hpp"
37#include "sparta/utils/StringManager.hpp"
39
40namespace sparta {
42} // namespace sparta
43
44#ifndef TREENODE_LIFETIME_TRACE
53//#define TREENODE_LIFETIME_TRACE 1
54#endif // #ifndef TREENODE_LIFETIME_TRACE
55
56
58#define ALPHANUM_CHARS \
59 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_"
60
62#define DIGIT_CHARS "0123456789"
63
69#define RESERVED_WORDS {"and", "del", "from", "not", \
70 "while", "as", "elif", "global", \
71 "or", "with", "assert", "else", \
72 "if", "pass", "yield", "break", \
73 "except", "import", "print", "class", \
74 "exec", "in", "raise", "continue", \
75 "finally", "is", "return", "def", \
76 "for", "lambda", "try", "__init__", \
77 "__del__", "__cinit__", "__dealloc__", \
78 /* End of Python reserved names */ \
79 "name", "author", "report", "content", \
80 "include", \
81 /* End of SPARTA Report definition reserved nodes */ \
82 "extension" \
83 /* End of SPARTA extensions reserved words */ \
84 }
85
149#define REGISTER_FOR_NOTIFICATION(func, datat, name) \
150 registerForNotification<datat, \
151 typename std::remove_reference<decltype(*this)>::type, \
152 &std::remove_reference<decltype(*this)>::type::func>(this, name);
153
159#define DEREGISTER_FOR_NOTIFICATION(func, datat, name) \
160 deregisterForNotification<datat, \
161 typename std::remove_reference<decltype(*this)>::type, \
162 &std::remove_reference<decltype(*this)>::type::func>(this, name);
163
164#define NOTIFY(func, datat, name) \
165
166
167namespace sparta
168{
169 namespace app {
170 class Simulation;
171 }
172
173 class Clock;
174 class VirtualGlobalTreeNode;
175 class ClockManager;
176 class TreeNodePrivateAttorney;
177 class ParameterBase;
178 class ParameterSet;
179 class Scheduler;
180
204 {
205 public:
212
216 friend class ClockManager;
217
223
229
233
237 typedef uint64_t node_uid_type;
238
242 typedef std::vector<TreeNode*> ChildrenVector;
243
247 typedef std::vector<std::string> AliasVector;
248
255 typedef std::multimap<std::string, TreeNode*> ChildNameMapping;
256
260 typedef uint32_t group_idx_type;
261
265 typedef std::weak_ptr<TreeNode> WeakPtr;
266
270 typedef std::weak_ptr<const TreeNode> ConstWeakPtr;
271
275 typedef std::shared_ptr<TreeNode> SharedPtr;
276
280 typedef std::map<const std::string*, std::vector<TreeNode*>> TagsMap;
281
289 struct ANY_TYPE {};
290
293
297
303
309
313 static constexpr char GROUP_NAME_NONE[] = "";
314
318 static constexpr char NODE_NAME_NONE[] = "";
319
323 static const std::string DEBUG_DUMP_SECTION_DIVIDER;
324
328 static constexpr char NODE_NAME_VIRTUAL_GLOBAL[] = "_SPARTA_virtual_global_";
329
336 static constexpr char LOCATION_NODE_SEPARATOR_ATTACHED = '.';
337
345 static constexpr char LOCATION_NODE_SEPARATOR_EXPECTING = ',';
346
354 static constexpr char LOCATION_NODE_SEPARATOR_UNATTACHED = '~';
355
361 static const uint32_t RENDER_SUBTREE_INDENT = 2;
362
369 static constexpr char GROUP_NAME_BUILTIN[] = "_SPARTA_builtin_group_";
370
391 static const uint64_t CHILD_FIND_THRESHOLD = 100000;
392
399 static const uint64_t CHILD_GET_THRESHOLD = 100000;
400
405 static const uint32_t TEARDOWN_ERROR_LIMIT = 5;
406
411 static const std::vector<std::pair<const char*, std::function<void (std::string&)>>> TREE_NODE_PATTERN_SUBS;
412
415
419
432 throw SpartaException("Device tree with root \"")
433 << getRoot()->getLocation() << "\" not currently in the TREE_BUILDING phase or "
434 << "TREE_CONFIGURING phase, so it cannot enter TREE_LOCKDOWN";
435 }
436 lockdownParametersUtil_();
437 }
438
439 static const std::map<const TreeNode*, WeakPtr>& getParentlessNodes();
440
445 static const std::map<const TreeNode*, WeakPtr>& getAllNodes();
446
451 static std::string formatAllNodes();
452
457 static bool isNodeConstructed(const TreeNode*);
458
461
465
467 TreeNode() = delete;
468
472 TreeNode(const TreeNode&) = delete;
473
484
486 TreeNode& operator=(const TreeNode&) = delete;
487
572 const std::string& name,
573 const std::string& group,
574 group_idx_type group_idx,
575 const std::string& desc,
576 bool is_indexable);
577
584 const std::string& name,
585 const std::string& group,
586 group_idx_type group_idx,
587 const std::string& desc);
588
593 TreeNode(const std::string& name,
594 const std::string& group,
595 group_idx_type group_idx,
596 const std::string& desc);
597
610 const std::string& name,
611 const std::string& desc);
612
626 const std::string& group,
627 group_idx_type group_idx,
628 const std::string& desc);
629
637 TreeNode(const std::string& name,
638 const std::string& desc);
639
645 virtual ~TreeNode();
646
649
653
668 void addAlias(const std::string& alias);
669
676 void addAliases(const AliasVector& v);
677
694 void addTag(const std::string& tag);
695
702 void addTags(const std::vector<std::string>& v);
703
706
710
722 virtual std::string stringize(bool pretty=false) const {
723 (void) pretty;
724 std::stringstream ss;
725 ss << "<" << getLocation();
726 if(is_expired_){
727 ss << " EXPIRED";
728 }
729 stringizeTags(ss);
730 ss << ">";
731 return ss.str();
732 }
733
742 void stringizeTags(std::stringstream& ss) const {
743 if(tags_.size() > 0){
744 ss << " tags:[";
745 uint32_t i = 0;
746 for(auto & tag : tags_){
747 if(i != 0){
748 ss << ", ";
749 }
750 ss << *tag;
751 ++i;
752 }
753 ss << "]";
754 }
755
756 }
757
760
764
769
774 const std::string& getName() const override;
775
781 const std::string* getNamePtr() const;
782
787 bool isAnonymous() const;
788
794 bool isExpired() const;
795
800 bool isIndexableByGroup() const;
801
807 const std::string& getGroup() const;
808
816 const std::string* getGroupNamePtr() const;
817
824
838 const std::vector<const std::string*>& getTags() const;
839
843 bool hasTag(const std::string& tag) const;
844
852 bool hasTag(const std::string* interned_tag_name) const;
853
858 const std::string& getDesc() const;
859
864 bool isBuiltin() const;
865
871 void markHidden(bool hidden=true);
872
880 bool isHidden() const;
881
884
888
901 void validateName(const std::string& nm);
902
928 void validateGroup(const std::string& name,
929 const std::string& group,
930 group_idx_type idx);
931
941 void validateDesc(const std::string& desc);
942
945
949
956 virtual bool isAttached() const {
957 return is_attached_;
958 }
959
964 virtual TreeNode* getParent() {
965 return parent_;
966 }
967
971 virtual const TreeNode* getParent() const {
972 return parent_;
973 }
974
988 template <class T, typename = typename std::enable_if<std::is_pointer<T>::value>::type>
989 const T getParentAs(bool must_exist=true) const {
990 const T result = dynamic_cast<const T>(getParent());
991 if(result == nullptr){
992 if(must_exist){
993 throw SpartaException("Could not get parent of ")
994 << getLocation() << " with type: const " << demangle(typeid(T*).name());
995 }
996 return nullptr;
997 }
998 return result;
999 }
1000
1001 // Overload of getParentAs for const access with a non-pointer T type
1002 template <class T, typename = typename std::enable_if<!std::is_pointer<T>::value>::type>
1003 const T* getParentAs(bool must_exist=true) const {
1004 return getParentAs<const T*>(must_exist);
1005 }
1006
1007 // Overload of getParentAs for non-const access with a pointer T type
1008 template <class T, typename = typename std::enable_if<std::is_pointer<T>::value>::type>
1009 T getParentAs(bool must_exist=true) {
1010 T result = dynamic_cast<T>(getParent());
1011 if(result == nullptr){
1012 if(must_exist){
1013 throw SpartaException("Could not get parent of ")
1014 << getLocation() << " with type: const " << demangle(typeid(T*).name());
1015 }
1016 return nullptr;
1017 }
1018 return result;
1019 }
1020
1021 // Overload of getParentAs for non-const access with a non-pointer T type
1022 template <class T, typename = typename std::enable_if<!std::is_pointer<T>::value>::type>
1023 T* getParentAs(bool must_exist=true) {
1024 return getParentAs<T*>(must_exist);
1025 }
1026
1033 virtual TreeNode* getRoot();
1034
1038 virtual const TreeNode* getRoot() const;
1039
1044
1048 const TreeNode *getScopeRoot() const;
1049
1057
1066
1076 uint32_t getNumChildren() const;
1077
1090 TreeNode* getChildAt(uint32_t idx) const;
1091
1107
1123 uint32_t getChildren(std::vector<TreeNode*>& results,
1124 bool ignore_builtin_group=true,
1125 bool ignore_anonymous_nodes=true);
1126
1134 template <class T>
1135 uint32_t getChildrenOfType(std::vector<T*>& results) const
1136 {
1137 uint32_t orig_size = results.size();
1138 for (auto child : getChildren())
1139 {
1140 if (dynamic_cast<T*>(child))
1141 {
1142 results.emplace_back(static_cast<T*>(child));
1143 }
1144 }
1145 return results.size() - orig_size;
1146 }
1147
1153 sparta::TreeNode *findAncestorByName(const std::string &name) {
1154 sparta::TreeNode *parent = getParent();
1155 while (parent != getRoot()) {
1156 if (parent->locationMatchesPattern(name, parent->getParent())) {
1157 return parent;
1158 }
1159 parent = parent->getParent();
1160 }
1161 return nullptr;
1162 }
1163
1179 template <typename T>
1181 sparta::TreeNode *parent = getParent();
1182 while (parent != getRoot()) {
1183 if (parent->getResourceTypeRaw() == typeid(T).name()) {
1184 return parent;
1185 }
1186 parent = parent->getParent();
1187 }
1188 return nullptr;
1189 }
1190
1202 sparta::TreeNode *findAncestorByTag(const std::string& tag)
1203 {
1204 sparta::TreeNode *parent = getParent();
1205 while (parent != getRoot()) {
1206 if (parent->hasTag(tag))
1207 {
1208 return parent;
1209 }
1210 parent = parent->getParent();
1211 }
1212 return nullptr;
1213 }
1214
1224 template<typename T>
1226 {
1227 static_assert(!std::is_pointer<T>::value && !std::is_reference<T>::value,
1228 "Expected a non-pointer/non-reference T template argument");
1230 if (node)
1231 {
1232 return node->getResourceAs<T*>();
1233 }
1234 return nullptr;
1235 }
1236
1243 const AliasVector& getAliases() const;
1244
1268 uint32_t getChildrenIdentifiers(std::vector<std::string>& idents,
1269 bool ignore_builtin_group=true) const;
1270
1280 std::vector<const std::string*> getIdentifiers() const;
1281
1293 uint32_t getGroupSize(const std::string& group);
1294
1304 group_idx_type getGroupIndexMax(const std::string& group);
1305
1316 uint32_t getGroup(const std::string& group,
1317 std::vector<TreeNode*>& results);
1318
1420 uint32_t findChildren(const std::string& pattern,
1421 std::vector<TreeNode*>& results,
1422 std::vector<std::vector<std::string>>& replacements)
1423 {
1424 return findChildren_(pattern, results, replacements, false /*allow_private*/);
1425 }
1426
1430 uint32_t findChildren(const std::string& pattern,
1431 std::vector<TreeNode*>& results)
1432 {
1433 return findChildren_(pattern, results, false /*allow_private*/);
1434 }
1435
1445 virtual uint32_t findImmediateChildren_(std::regex& expr,
1446 std::vector<TreeNode*>& found,
1447 std::vector<std::vector<std::string>>& replacements,
1448 bool allow_private=false);
1449
1453 uint32_t findImmediateChildren_(std::regex& expr,
1454 std::vector<TreeNode*>& found,
1455 bool allow_private=false);
1456
1460 virtual uint32_t findImmediateChildren_(std::regex& expr,
1461 std::vector<const TreeNode*>& found,
1462 std::vector<std::vector<std::string>>& replacements,
1463 bool allow_private=false) const;
1464
1469 uint32_t findImmediateChildren_(std::regex& expr,
1470 std::vector<const TreeNode*>& found,
1471 bool allow_private=false) const;
1472
1503 bool locationMatchesPattern(const std::string& pattern,
1504 const TreeNode* pat_loc) const;
1505
1530 TreeNode* getChild(const std::string& name,
1531 bool must_exist=true)
1532 {
1533 return getChild_(name, must_exist, false /*private_also*/);
1534 }
1535
1537 const TreeNode* getChild(const std::string& name,
1538 bool must_exist=true) const
1539 {
1540 return getChild_(name, must_exist, false /*private also */);
1541 }
1542
1543
1560 std::string getDeepestMatchingPath(const std::string& path) const;
1561
1570 bool hasChild(const std::string& name) const noexcept
1571 {
1572 return hasChild_(name, false /*private also */);
1573 }
1574
1580 bool hasImmediateChild(const TreeNode* n) const noexcept;
1581
1602 template <class T,
1603 typename = typename std::enable_if<std::is_pointer<T>::value>::type,
1604 class ConstT = typename ptr_to_const_obj_ptr<T>::type>
1605 const ConstT getChildAs(const std::string& name, bool must_exist=true) const {
1606 static_assert(std::is_base_of<TreeNode, typename std::remove_pointer<T>::type>::value == true,
1607 "Cannot use getChildAs with a type argument that is not a subclass of " \
1608 "TreeNode. If the caller is looking for a resource, use " \
1609 "getChild(name)->getResource instead.");
1610 const TreeNode* child = TreeNode::getChild(name, must_exist);
1611 const ConstT result = dynamic_cast<ConstT>(child);
1612 if(result == nullptr){
1613 if(must_exist){
1614 throw SpartaException("Could not find child of \"")
1615 << getLocation() << "\" with the relative path \""
1616 << name << "\" that was of type: const "
1617 << demangle(typeid(T*).name()) << ". Found node of type "
1618 << demangle(typeid(*child).name());
1619 }
1620 return nullptr;
1621 }
1622 return result;
1623 }
1624
1625 // Overload of getChildAs for const access with a non-pointer T typeid
1626 template <class T, typename = typename std::enable_if<!std::is_pointer<T>::value>::type>
1627 const T* getChildAs(const std::string& name, bool must_exist=true) const {
1628 return getChildAs<const T*>(name, must_exist);
1629 }
1630
1631 // Overload of getChildAs for non-const access with a non-pointer T type
1632 template <class T, typename = typename std::enable_if<std::is_pointer<T>::value>::type>
1633 T getChildAs(const std::string& name, bool must_exist=true) {
1634 static_assert(std::is_base_of<TreeNode, typename std::remove_pointer<T>::type>::value == true,
1635 "Cannot use getChildAs with a type argument that is not a subclass of " \
1636 "TreeNode. If the caller is looking for a resource, use " \
1637 "getChild(name)->getResource instead.");
1638 TreeNode* child = TreeNode::getChild(name, must_exist);
1639 T result = dynamic_cast<T>(child);
1640 if(result == nullptr){
1641 if(must_exist){
1642 throw SpartaException("Could not find child of \"")
1643 << getLocation() << "\" with the relative path \""
1644 << name << "\" that was of type: " << demangle(typeid(T).name())
1645 << ". Found node of type "<< demangle(typeid(*child).name());
1646 }
1647 return nullptr;
1648 }
1649 return result;
1650 }
1651
1652 // Overload of getChildAs for non-const access with a non-pointer T type
1653 template <class T, typename = typename std::enable_if<!std::is_pointer<T>::value>::type>
1654 T* getChildAs(const std::string& name, bool must_exist=true) {
1655 return getChildAs<T*>(name, must_exist);
1656 }
1657
1658
1670 template <class T,
1671 typename = typename std::enable_if<std::is_pointer<T>::value>::type,
1672 class ConstT = typename ptr_to_const_obj_ptr<T>::type>
1673 ConstT getAs() const {
1674 static_assert(std::is_base_of<TreeNode, typename std::remove_pointer<T>::type>::value == true,
1675 "Cannot use getAs with a type argument that is not a subclass of " \
1676 "TreeNode.");
1677 ConstT result = dynamic_cast<typename ptr_to_const_obj_ptr<T>::type>(this);
1678 if(result == nullptr){
1679 throw SpartaException("Could not get TreeNode \"")
1680 << getLocation() << "\" as type: const " << demangle(typeid(T).name());
1681 }
1682 return result;
1683 }
1684
1695 template <class T, typename = typename std::enable_if<!std::is_pointer<T>::value>::type>
1696 const T* getAs() const {
1697 return getAs<const T*>();
1698 }
1699
1710 template <class T, typename = typename std::enable_if<std::is_pointer<T>::value>::type>
1711 T getAs() {
1712 static_assert(std::is_base_of<TreeNode, typename std::remove_pointer<T>::type>::value == true,
1713 "Cannot use getAs with a type argument that is not a subclass of " \
1714 "TreeNode.");
1715 T result = dynamic_cast<T>(this);
1716 if(result == nullptr){
1717 throw SpartaException("Could not get TreeNode \"")
1718 << getLocation() << "\" as type: " << demangle(typeid(T).name());
1719 }
1720 return result;
1721 }
1722
1733 template <class T, typename = typename std::enable_if<!std::is_pointer<T>::value>::type>
1734 T* getAs() {
1735 return getAs<T*>();
1736 }
1737
1755 uint32_t findChildrenByTag(const std::string& tag,
1756 std::vector<TreeNode*>& results,
1757 int32_t max_depth=-1);
1758
1772 bool isDescendantOf(const TreeNode* ancestor,
1773 int32_t max_depth=-1) const;
1774
1778 std::string getLocation() const override final;
1779
1795 std::string getDisplayLocation() const;
1796
1803 std::string getExpectedLocation() const;
1804
1829 std::string renderSubtree(int32_t max_depth=-1,
1830 bool show_builtins=false,
1831 bool names_only=false,
1832 bool hide_hidden=false,
1833 bool(*leaf_filt_fxn)(const TreeNode*) = nullptr) const;
1834
1845 uint32_t getLevel() const;
1846
1852 template <typename T>
1853 uint32_t getRecursiveNodeCount() const {
1854 uint32_t count = dynamic_cast<const typename std::remove_const<typename std::remove_pointer<T>::type>::type*>(this) != nullptr;
1855 for(auto& child : children_){
1856 count += child->getRecursiveNodeCount<T>();
1857 }
1858 return count;
1859 }
1860
1874
1877
1881
1882 using ExtensionsBase = detail::ExtensionsBase;
1883
1893 void addExtensionFactory(const std::string & extension_name,
1894 std::function<ExtensionsBase*()> factory);
1895
1908 ExtensionsBase * getExtension(const std::string & extension_name, bool no_factory_ok=false);
1909
1919 const ExtensionsBase * getExtension(const std::string & extension_name) const;
1920
1925 template <typename T>
1926 T* getExtensionAs(const std::string & extension_name) {
1927 static_assert(std::is_base_of<ExtensionsBase, T>::value);
1928 auto ext = getExtension(extension_name);
1929 if (!ext) {
1930 return nullptr;
1931 }
1932
1933 auto ret = dynamic_cast<T*>(ext);
1934 if (!ret) {
1935 throw SpartaException("Could not downcast extension '")
1936 << extension_name << "' to " << typeid(T).name() << ". "
1937 << "Actual extension type is " << ext->getClassName() << ".";
1938 }
1939
1940 return ret;
1941 }
1942
1947 template <typename T>
1948 const T* getExtensionAs(const std::string & extension_name) const {
1949 static_assert(std::is_base_of<ExtensionsBase, T>::value);
1950 auto ext = getExtension(extension_name);
1951 if (!ext) {
1952 return nullptr;
1953 }
1954
1955 auto ret = dynamic_cast<const T*>(ext);
1956 if (!ret) {
1957 throw SpartaException("Could not downcast extension '")
1958 << extension_name << "' to " << typeid(T).name() << ". "
1959 << "Actual extension type is " << ext->getClassName() << ".";
1960 }
1961
1962 return ret;
1963 }
1964
1975 ExtensionsBase * getExtension();
1976
1990 const ExtensionsBase * getExtension() const;
1991
2005 ExtensionsBase * createExtension(const std::string & extension_name,
2006 bool replace=false);
2007
2025 ExtensionsBase * createExtension(bool replace=false);
2026
2030 ExtensionsBase * createExtension(const char* extension_name, bool replace = false);
2031
2044 template <typename Extension, typename... Args>
2045 Extension * addExtension(Args&&... args) {
2046 static_assert(std::is_base_of<ExtensionsBase, Extension>::value);
2047 if (hasExtension(Extension::NAME)) {
2048 throw SpartaException("Extension already exists: ") << Extension::NAME;
2049 }
2050
2051 std::shared_ptr<ExtensionsBase> ext(new Extension(std::forward<Args>(args)...));
2052 ext->setParameters(std::make_unique<ParameterSet>(nullptr));
2053 ext->postCreate();
2054
2055 addExtension_(Extension::NAME, ext);
2056 return dynamic_cast<Extension*>(ext.get());
2057 }
2058
2070 template <typename Extension, typename... Args>
2071 Extension * replaceExtension(Args&&... args) {
2072 static_assert(std::is_base_of<ExtensionsBase, Extension>::value);
2073 removeExtension(Extension::NAME);
2074 return addExtension<Extension, Args...>(std::forward<Args>(args)...);
2075 }
2076
2081 bool removeExtension(const std::string & extension_name);
2082
2086 bool hasExtension(const std::string & extension_name) const {
2087 return getExtension(extension_name) != nullptr;
2088 }
2089
2093 template <typename T>
2094 bool hasExtensionOfType(const std::string & extension_name) const noexcept {
2095 auto ext = getExtension(extension_name);
2096 if (!ext) {
2097 return false;
2098 }
2099
2100 return dynamic_cast<const T*>(ext) != nullptr;
2101 }
2102
2110 std::set<std::string> getAllInstantiatedExtensionNames() const;
2111
2119 std::set<std::string> getAllConfigExtensionNames() const;
2120
2126 size_t getNumExtensions() const {
2127 return getAllInstantiatedExtensionNames().size();
2128 }
2129
2133 std::map<std::string, const ExtensionsBase*> getAllExtensions() const {
2134 std::map<std::string, const ExtensionsBase*> extensions;
2135 for (const auto & ext_name : getAllInstantiatedExtensionNames()) {
2136 auto ext = getExtension(ext_name);
2137 sparta_assert(ext != nullptr);
2138 extensions[ext_name] = ext;
2139 }
2140 return extensions;
2141 }
2142
2146
2150
2186 void addChild(TreeNode* child, bool inherit_phase=true);
2187
2193 void addChild(TreeNode& child);
2194
2207 {
2208 incrementPrivacyLevel_(1);
2209 }
2210
2215 {
2216 is_scope_root_ = true;
2217 }
2218
2219
2223 bool isScopeRoot() const
2224 {
2225 return is_scope_root_;
2226 }
2227
2230
2239 const Clock* getClock() override{
2240 if(isFinalized()){
2241 return working_clock_;
2242 }else{
2243 // Search for clock
2244 TreeNode* n = this;
2245 while(n){
2246 const Clock* c = n->getLocalClock();
2247 if(c){
2248 return c;
2249 }
2250 n = n->getParent();
2251 }
2252 return nullptr;
2253 }
2254 }
2255
2259 const Clock* getClock() const {
2260 if(isFinalized()){
2261 return working_clock_;
2262 }else{
2263 // Search for clock
2264 const TreeNode* n = this;
2265 while(n){
2266 const Clock* c = n->getLocalClock();
2267 if(c){
2268 return c;
2269 }
2270 n = n->getParent();
2271 }
2272 return nullptr;
2273 }
2274 }
2275
2290 return clock_;
2291 }
2292
2296 const Clock* getLocalClock() const {
2297 return clock_;
2298 }
2299
2310 virtual void setClock(const Clock * clk);
2311
2315 Scheduler * getScheduler(const bool must_exist = false);
2316 const Scheduler * getScheduler(const bool must_exist = false) const;
2317
2340
2345
2361 virtual void addLink(TreeNode *node, const std::string &label);
2362
2376 virtual void activateLink(const std::string &label);
2377
2388 static std::string createSearchRegexPattern(const std::string& pat);
2389
2398 static bool hasWildcardCharacters(const std::string& name);
2399
2415 static std::string getNextName(const std::string& name, size_t& pos);
2416
2425 static bool matchesGlobLike(const std::string& pattern, const std::string& other);
2426
2427
2428 private:
2433 void lockdownParametersUtil_(){
2434 for(auto child : children_){
2435 child->lockdownParametersUtil_();
2436 }
2437 special_params_lockdown_ = true;
2438 }
2439
2445 static node_uid_type getNextNodeUID_();
2446
2454 static void trackParentlessNode_(TreeNode* node);
2455
2464 static void untrackParentlessNode_(TreeNode* node);
2465
2478 static void trackNode_(TreeNode* node);
2479
2501 static void untrackNode_(TreeNode* node) noexcept;
2502
2510 void informAddedChildSubtree_();
2511
2521 void informAddedChildAncestors_(TreeNode* des);
2522
2529 void incrementGetChildCount_(const std::string& name) const;
2530
2534
2562 virtual void setParent_(TreeNode* parent, bool inherit_phase);
2570 void addChild_(TreeNode* child, bool inherit_phase);
2584 void recursSetPhase_(TreePhase phase);
2585
2588
2592
2601 void getLocation_(std::stringstream& ss, bool anticipate_parent=false) const;
2602
2607 void getDisplayLocation_(std::stringstream& ss) const;
2608
2624 uint32_t renderSubtree_(std::stringstream& ss,
2625 uint32_t indent,
2626 int32_t max_depth,
2627 bool show_builtins,
2628 bool names_only,
2629 bool hide_hidden,
2630 bool(*leaf_filt_fxn)(const TreeNode*)) const;
2631
2641 virtual TreeNode*
2642 getImmediateChildByIdentity_(const std::string& name,
2643 bool must_exist=true)
2644 ;
2645
2646 // Overload of getImmediateChildByIdentity_
2648 virtual const TreeNode*
2649 getImmediateChildByIdentity_(const std::string& name,
2650 bool must_exist=true)
2651 const;
2659 void ensureNoParent_(const char* action);
2660
2665 OneWayBool<false> special_params_lockdown_;
2666
2669
2670 protected:
2671
2680 static bool identityMatchesPattern_(const std::string& ident,
2681 std::regex& expr,
2682 std::vector<std::string>& replacements);
2683
2684
2688 static bool identityMatchesPattern_(const std::string& ident,
2689 std::regex& expr);
2690
2710 static std::string getPreviousName_(const std::string& name,
2711 size_t& pos);
2712
2719 std::pair<uint32_t, std::string> recursGetDeepestMatchingPath_(const std::string& path,
2720 size_t name_pos) const;
2721
2752 void setExpectedParent_(const TreeNode* parent);
2753
2760
2775
2783
2791
2798 void enterConfig_() noexcept;
2799
2806
2813
2830
2852
2876 void dumpDebugContent_(std::ostream& out) const noexcept;
2877
2878
2887 void enterTeardown_() noexcept;
2888
2900 void verifyUniqueChildIdentifier_(const std::string& ident,
2901 bool ignore_group_collision=false);
2902
2923
2932
2938
2944
2953 return special_params_lockdown_;
2954 }
2955
2959
2968 {
2969 public:
2970
2971 typedef void (*stub_type)(void* obj,
2972 const TreeNode& origin,
2973 const TreeNode* obs_pt,
2974 const void* data); //, const std::type_info& tinfo);
2975
2976 delegate() :
2977 object_ptr(0),
2978 obs_point(0),
2979 name_ptr(0),
2980 stub_ptr(0),
2981 reveals_origin(true)
2982 { }
2983
2984 delegate(const delegate&& d) :
2985 object_ptr(d.object_ptr),
2986 obs_point(d.obs_point),
2987 name_ptr(d.name_ptr),
2988 stub_ptr(d.stub_ptr),
2989 reveals_origin(d.reveals_origin)
2990 { }
2991
2996 object_ptr = d.object_ptr;
2997 obs_point = d.obs_point;
2998 name_ptr = d.name_ptr;
2999 stub_ptr = d.stub_ptr;
3000 reveals_origin = d.reveals_origin;
3001
3002 return *this;
3003 }
3004
3005 delegate(const delegate& d) :
3006 object_ptr(d.object_ptr),
3007 obs_point(d.obs_point),
3008 name_ptr(d.name_ptr),
3009 stub_ptr(d.stub_ptr),
3010 reveals_origin(d.reveals_origin)
3011 { }
3012
3013
3022 template <class DataT, class T, void (T::*TMethod)(const TreeNode&,
3023 const TreeNode&,
3024 const DataT&)>
3025 constexpr bool equals(T* obj,
3026 const TreeNode& obs_pt,
3027 const std::string& name) const
3028 {
3029 return (stub_ptr == &method_stub<DataT, T, TMethod>)
3030 && (obj == object_ptr)
3031 && (&obs_pt == obs_point)
3032 && (name == *name_ptr);
3033 }
3034
3035 template <class DataT, class T, void (T::*TMethod)(const DataT&)>
3036 constexpr bool equals(T* obj,
3037 const TreeNode& obs_pt,
3038 const std::string& name) const
3039 {
3040 return (stub_ptr == &method_stub<DataT, T, TMethod>)
3041 && (obj == object_ptr)
3042 && (&obs_pt == obs_point)
3043 && (name == *name_ptr);
3044 }
3045
3046
3054 bool operator==(const delegate& d) const {
3055 return (stub_ptr == d.stub_ptr)
3056 && (object_ptr == d.object_ptr)
3057 && (obs_point == d.obs_point)
3058 && (name_ptr == d.name_ptr);
3059 }
3060
3079 bool observes(const TreeNode& obs_pt, const std::string* name) const
3080 {
3081 return (&obs_pt == obs_point)
3082 && TreeNode::notificationCategoryMatch(name_ptr, name);
3083 }
3084
3091 template <class DataT, class T, void (T::*TMethod)(const TreeNode&,
3092 const TreeNode&,
3093 const DataT&)>
3094 static delegate from_method(T* obj,
3095 const TreeNode& obs_pt,
3096 const std::string& name)
3097 {
3098 delegate d;
3099 d.object_ptr = obj;
3100 d.obs_point = &obs_pt;
3101 d.name_ptr = StringManager::getStringManager().internString(name);
3102 d.stub_ptr = &method_stub<DataT, T, TMethod>;
3103 return d;
3104 }
3105
3106 template <class DataT, class T, void (T::*TMethod)(const DataT&)>
3107 static delegate from_method(T* obj,
3108 const TreeNode& obs_pt,
3109 const std::string& name)
3110 {
3111 delegate d;
3112 d.object_ptr = obj;
3113 d.obs_point = &obs_pt;
3114 d.name_ptr = StringManager::getStringManager().internString(name);
3115 d.stub_ptr = &method_stub<DataT, T, TMethod>;
3116 d.reveals_origin = false;
3117 return d;
3118 }
3119
3123 template <typename DataT>
3124 void operator()(const TreeNode& origin,
3125 const DataT& data) const
3126 {
3127 (*stub_ptr)(object_ptr, origin, obs_point, &data); //, typeid(DataT));
3128 }
3129
3130 const std::string* getNameID() const {
3131 return name_ptr;
3132 }
3133
3134 const TreeNode* getObservationPoint() const {
3135 return obs_point;
3136 }
3137
3138 bool revealsOrigin() const { return reveals_origin; }
3139
3140 private:
3141
3148 template <class DataT, class T, void (T::*TMethod)(const TreeNode&,
3149 const TreeNode&,
3150 const DataT&)>
3151 static void method_stub(void* obj,
3152 const TreeNode& origin,
3153 const TreeNode* obs_pt,
3154 const void* data)
3155 {
3156 // Check type
3157 //sparta_assert(tinfo == typeid(DataT),
3158 // "Expected to invoke method delegate for data type \"" << typeid(DataT).name()
3159 // << "\" with incorrect data type \"" << typeid(tinfo).name() << "\"")
3160
3161 T* p = static_cast<T*>(obj);
3162 const DataT& d = *static_cast<const DataT*>(data);
3163 return (p->*TMethod)(origin, *obs_pt, d);
3164 }
3165
3166 template <class DataT, class T, void (T::*TMethod)(const DataT&)>
3167 static void method_stub(void* obj,
3168 const TreeNode& origin,
3169 const TreeNode* obs_pt,
3170 const void* data)
3171 {
3172 (void) origin;
3173 (void) obs_pt;
3174 T* p = static_cast<T*>(obj);
3175 const DataT& d = *static_cast<const DataT*>(data);
3176 return (p->*TMethod)(d);
3177 }
3178
3179 template <class DataT, void (*TMethod)(const TreeNode&,
3180 const TreeNode&,
3181 const DataT&)>
3182 static void method_stub(void* obj,
3183 const TreeNode& origin,
3184 const TreeNode* obs_pt,
3185 const void* data)
3186 {
3187 (void) obj;
3188 const DataT& d = *static_cast<const DataT*>(data);
3189 return (*TMethod)(origin, *obs_pt, d);
3190 }
3191
3192 template <class DataT, void (*TMethod)(const DataT&)>
3193 static void method_stub(void* obj,
3194 const TreeNode& origin,
3195 const TreeNode* obs_pt,
3196 const void* data)
3197 {
3198 (void) obj;
3199 (void) origin;
3200 (void) obs_pt;
3201 const DataT& d = *static_cast<const DataT*>(data);
3202 return (*TMethod)(d);
3203 }
3204
3209 void* object_ptr;
3210
3214 const TreeNode* obs_point;
3215
3220 const std::string* name_ptr;
3221
3225 stub_type stub_ptr;
3226
3232 bool reveals_origin = true;
3233 };
3234
3242
3243 const std::type_info* tinfo_;
3244 size_t hash_code_;
3245
3246 public:
3247
3248 type_info_container() = delete;
3249
3251 tinfo_(rhp.tinfo_),
3252 hash_code_(rhp.hash_code_)
3253 { }
3254
3255 type_info_container(const std::type_info& tinfo) :
3256 tinfo_(&tinfo),
3257 hash_code_(tinfo.hash_code())
3258 { }
3259
3260 bool operator==(const type_info_container& rhp) const {
3261 return hash_code_ == rhp.hash_code_;
3262 }
3263
3264 bool operator<(const type_info_container& rhp) const {
3265 return hash_code_ < rhp.hash_code_;
3266 }
3267
3268 const std::type_info& operator*() const {
3269 return *tinfo_;
3270 }
3271
3272 const std::type_info* get() const {
3273 return tinfo_;
3274 }
3275 };
3276
3280 typedef std::vector<delegate> DelegateVector;
3281
3285 typedef std::map<type_info_container, DelegateVector> NotificationObserverMap;
3286
3287
3296 template <typename DataT>
3298 const DataT& data,
3299 const std::string* name_id) {
3300 // Assure that this notification passes canGenerateNotification. It
3301 // is of no use to us if nodes are generating Notifications that no
3302 // observer can possible expect by examining the tree.
3303 // Skip this in production builds for performance
3304#ifndef NDEBUG
3305 sparta_assert(origin);
3306 sparta_assert(name_id);
3307 NotificationInfo info(origin, &typeid(DataT), name_id);
3308 if(!canGenerateNotification(info)){
3309 throw SpartaException("TreeNode ")
3310 << getLocation() << " posted a notification <"
3311 << origin->getLocation() << ", \"" << demangle(typeid(DataT).name())
3312 << "\", \"" << *name_id
3313 << "\"> which it did not properly announce through canGenerateNotification";
3314 }
3315#endif
3316 propagateNotification_(origin, data, name_id);
3317
3318 // Post to the global virtual node
3319 getVirtualGlobalNode()->propagateNotification_(origin, data, name_id);
3320 }
3321
3337 template <typename DataT, typename T, void (T::*TMethod)(const TreeNode&,
3338 const TreeNode&,
3339 const DataT&)>
3340 DelegateVector::iterator findDelegate_(DelegateVector& dvec,
3341 T* obj,
3342 const std::string& target_name) {
3343 const DelegateVector::const_iterator dend = dvec.end();
3344 DelegateVector::iterator d;
3345 for(d=dvec.begin(); d!=dend; ++d){
3346 if(d->equals<DataT, T, TMethod>(obj, *this, target_name)){
3347 break;
3348 }
3349 }
3350 return d;
3351 }
3352
3353 // Overload of findDelegate_ with one TMethod signature having a sole DataT parameter
3354 template <typename DataT, typename T, void (T::*TMethod)(const DataT&)>
3355 DelegateVector::iterator findDelegate_(DelegateVector& dvec,
3356 T* obj,
3357 const std::string& target_name) {
3358 const DelegateVector::const_iterator dend = dvec.end();
3359 DelegateVector::iterator d;
3360 for(d=dvec.begin(); d!=dend; ++d){
3361 if(d->equals<DataT, T, TMethod>(obj, *this, target_name)){
3362 break;
3363 }
3364 }
3365 return d;
3366 }
3367
3389 virtual void
3391 const std::vector<const std::string*>& name_ids,
3392 TreeNode* obs_node,
3393 const delegate* del,
3394 const bool allow_private);
3395
3408 virtual void
3410 const std::vector<const std::string*>& name_ids,
3411 TreeNode* obs_node,
3412 const delegate* del,
3413 const bool allow_private);
3414
3419 void
3421 const std::string& name,
3422 TreeNode* obs_node,
3423 const delegate* del,
3424 const bool private_only)
3425 {
3426 auto names = parseNotificationNameString(name);
3427 broadcastRegistrationForNotificationToChildren_(tinfo, names, obs_node, del,
3428 private_only);
3429 }
3430
3435 void
3437 const std::string& name,
3438 TreeNode* obs_node,
3439 const delegate* del,
3440 const bool private_only)
3441 {
3442 auto names = parseNotificationNameString(name);
3443 broadcastDeregistrationForNotificationToChildren_(tinfo, names, obs_node, del,
3444 private_only);
3445 }
3446
3455 template <typename DataT>
3457 const DataT& data,
3458 const std::string* name_id) {
3459 to_invoke->invokeDelegates_(this, data, name_id);
3460 }
3461
3462 private:
3463
3475 template <typename DataT>
3476 void invokeDelegates_(const TreeNode* origin,
3477 const DataT& data,
3478 const std::string* name_id) {
3479 auto itr = obs_local_.find(typeid(DataT));
3480 if(itr != obs_local_.end()){
3481 DelegateVector& observers = itr->second;
3482 for(delegate& d : observers) {
3483 // Invoke delegate if matched
3484 if(d.getNameID() == name_id
3485 || d.getNameID() == StringManager::getStringManager().EMPTY){
3486 d(*origin, data); // invoke
3487 }
3488 }
3489 }
3490 }
3491
3510 template <typename DataT>
3511 void propagateNotification_(const TreeNode* origin,
3512 const DataT& data,
3513 const std::string* name_id) {
3514
3515 invokeDelegates_<DataT>(origin, data, name_id);
3516
3517 if(getParent() != nullptr){
3518 getParent()->propagateNotification_(origin, data, name_id);
3519 }
3520 }
3521
3522 // Internal Notification System
3525
3526 public:
3527
3531
3539
3542 const std::type_info* _tinfo,
3543 const std::string* _name) :
3544 origin(_origin),
3545 tinfo(_tinfo),
3546 name(_name)
3547 {
3548 checkValid();
3549 }
3550
3553 origin(rhp.origin),
3554 tinfo(rhp.tinfo),
3555 name(rhp.name)
3556 {
3557 checkValid();
3558 }
3559
3562 origin(rhp.origin),
3563 tinfo(rhp.tinfo),
3564 name(rhp.name)
3565 {
3566 checkValid();
3567 }
3568
3571 rhp.checkValid();
3572 origin = rhp.origin;
3573 tinfo = rhp.tinfo;
3574 name = rhp.name;
3575 return *this;
3576 }
3577
3581 void checkValid() const {
3582 sparta_assert(origin);
3583 sparta_assert(tinfo);
3584 sparta_assert(name);
3585 }
3586
3591
3595 const std::type_info* tinfo;
3596
3600 const std::string* name;
3601 };
3602
3612 uint32_t getPossibleNotifications(std::vector<NotificationInfo>& infos) const;
3613
3619 void dumpPossibleNotifications(std::ostream& o) const noexcept;
3620
3649 template <typename DataT=ANY_TYPE>
3650 uint32_t locateNotificationSources(std::vector<TreeNode*>& nodes,
3651 const std::string& name="") {
3652 static_assert(std::is_same<
3653 typename std::remove_cv<
3654 typename std::remove_reference<
3655 typename std::remove_pointer<DataT>::type
3656 >::type
3657 >::type,
3658 DataT
3659 >::value,
3660 "DataT must NOT be a const, volatile, pointer, or reference"
3661 "type. It violates at least one of these restrictions");
3662
3663 auto& strmgr = StringManager::getStringManager();
3664 const std::string* name_id = strmgr.internString(name);
3665 return locateNotificationSources_<DataT>(nodes, name_id);
3666 }
3667
3675 template <typename DataT=ANY_TYPE>
3676 void dumpLocatedNotificationSources(std::ostream& o, const std::string& name=""){
3677 std::vector<TreeNode*> nodes;
3679 for(const TreeNode* n : nodes){
3680 o << n->stringize() << std::endl;
3681 }
3682 }
3683
3693 uint32_t getPossibleSubtreeNotifications(std::vector<NotificationInfo>& infos) const noexcept;
3699 void dumpPossibleSubtreeNotifications(std::ostream& o) const noexcept;
3700
3725 bool canGenerateNotification(const std::type_info& tinfo,
3726 const std::string* name) const;
3727
3737 bool canGenerateNotification(const std::type_info& tinfo,
3738 const std::string& name) const;
3739
3746
3772 bool canSubtreeGenerateNotification(const std::type_info& tinfo,
3773 const std::string* name) const;
3774
3784 bool canSubtreeGenerateNotification(const std::type_info& tinfo,
3785 const std::string& name) const;
3786
3791 bool canSubtreeGenerateNotifications(const std::type_info& tinfo,
3792 const std::vector<const std::string*>& names) const;
3793
3801 static std::vector<const std::string*> parseNotificationNameString(const std::string& csl);
3802
3876 template <typename DataT, typename T, void (T::*TMethod)(const TreeNode&, const TreeNode&, const DataT&)>
3877 void registerForNotification(T* obj, const std::string& name, bool ensure_possible=true)
3878 {
3879 registerForNotification_<DataT, T, TMethod>(obj, name, ensure_possible, false /*allow_private*/);
3880 }
3881
3882 // Overload which allows registration with a class member function accepting only a data argument
3883 template <typename DataT, typename T, void (T::*TMethod)(const DataT&)>
3884 void registerForNotification(T* obj, const std::string& name, bool ensure_possible=true)
3885 {
3886 registerForNotification_<DataT, T, TMethod>(obj, name, ensure_possible, false /*allow_private*/);
3887 }
3888
3898 template <typename DataT, typename T, void (T::*TMethod)(const TreeNode&, const TreeNode&, const DataT&)>
3899 void deregisterForNotification(T* obj, const std::string& name)
3900 {
3901 deregisterForNotification_<DataT, T, TMethod>(obj, name, false /* allow_private */);
3902 }
3903
3904 // Overload which allows deregistration with a class member function accepting only a data argument
3905 template <typename DataT, typename T, void (T::*TMethod)(const DataT&)>
3906 void deregisterForNotification(T* obj, const std::string& name)
3907 {
3908 deregisterForNotification_<DataT, T, TMethod>(obj, name, false /* allow_private */);
3909 }
3910
3918 bool hasObserversRegisteredForNotification(const std::type_info& tinfo,
3919 const std::string* name) const noexcept;
3920
3930 void getDelegatesRegisteredForNotification(const std::type_info& tinfo,
3931 const std::string* name,
3932 std::vector<delegate>& dels) noexcept;
3933
3940 static bool notificationCategoryMatch(const std::string* query_id,
3941 const std::string* node_id);
3942
3943 // Public Notification System
3946
3947 private:
3948
3952 void incrementPrivacyLevel_(uint32_t privacy_increment)
3953 {
3954 privacy_level_ += privacy_increment;
3955 for (auto n : children_)
3956 {
3957 n->incrementPrivacyLevel_(privacy_increment);
3958 }
3959 }
3960
3981 virtual void onAddingChild_(TreeNode* child) {
3982 (void) child;
3983 // No actions by default
3984 // Could throw to reject child
3985 }
3986
4009 virtual void onSettingParent_(const TreeNode* parent) const {
4010 (void) parent;
4011 // No actions by default
4012 // Could throw to reject parent
4013 }
4014
4031 virtual void onAddedAsChild_() noexcept {
4032 }
4033
4046 virtual void onDescendentSubtreeAdded_(TreeNode* des) noexcept {
4047 (void) des;
4048 }
4049
4056 virtual void onDestroyingParent_() noexcept {
4057 }
4058
4066 virtual void onDestroyingChild_(TreeNode* child) noexcept {
4067 (void) child;
4068 }
4069
4073 virtual void onConfiguring_() {;}
4074
4078 virtual void onBindTreeEarly_() {;}
4079
4083 virtual void onBindTreeLate_() {;}
4084
4113 virtual void onEnteringTeardown_() noexcept {
4114 }
4115
4130 void addChildNameMapping_(const std::string& name,
4131 TreeNode* child);
4132
4137 template <typename DataT>
4138 uint32_t locateNotificationSources_(std::vector<TreeNode*>& nodes, const std::string* name_id){
4139 uint32_t additions = 0;
4140
4141 const std::string* dummy;
4142 if(canGenerateNotification_(typeid(DataT), name_id, dummy)){
4143 nodes.push_back(this);
4144 ++additions;
4145 }
4146
4147 for(TreeNode* child : children_){
4148 additions += child->template locateNotificationSources_<DataT>(nodes, name_id);
4149 }
4150 return additions;
4151 }
4152
4178 virtual void notificationObserverAdded_(const std::type_info& tinfo,
4179 const std::string* name_id,
4180 TreeNode* obs_node,
4181 const delegate* del) {
4182 (void) tinfo;
4183 (void) name_id;
4184 (void) obs_node;
4185 (void) del;
4186 }
4187
4188
4207 virtual void notificationObserverRemoved_(const std::type_info& tinfo,
4208 const std::string* name_id,
4209 TreeNode* obs_node,
4210 const delegate* del) {
4211 (void) tinfo;
4212 (void) name_id;
4213 (void) obs_node;
4214 (void) del;
4215 }
4216
4245 virtual bool canGenerateNotification_(const std::type_info& tinfo,
4246 const std::string* name,
4247 const std::string*& match) const {
4248 (void) tinfo;
4249 (void) name;
4250 (void) match;
4251 return false;
4252 }
4253
4265 virtual void getPossibleNotifications_(std::vector<NotificationInfo>& infos) const {
4266 (void) infos;
4267 }
4268
4279 virtual void createResource_() {
4280 sparta_assert(isFinalizing()); // Must be in the finalizing phase
4281 }
4282
4293 virtual void validateNode_() const {
4294 }
4295
4303 bool canSeeChild_(const TreeNode* node) const
4304 {
4305 sparta_assert(node != nullptr);
4306 // If the node is not on the same privacy level then we can't see it.
4307 return (privacy_level_ == node->privacy_level_);
4308 }
4309 uint32_t findChildren_(const std::string& pattern,
4310 std::vector<TreeNode*>& results,
4311 std::vector<std::vector<std::string>>& replacements,
4312 bool allow_private);
4313
4317 uint32_t findChildren_(const std::string& pattern,
4318 std::vector<TreeNode*>& results,
4319 bool allow_private);
4324 template <typename DataT, typename T, void (T::*TMethod)(const TreeNode&, const TreeNode&, const DataT&)>
4325 void registerForNotification_(T* obj, const std::string& name, bool ensure_possible=true, bool allow_private=false)
4326 {
4327 (void)allow_private;
4328 const std::type_info& data_type = typeid(DataT);
4329 if(true == ensure_possible && false == canSubtreeGenerateNotification(data_type, name)){
4330 throw SpartaException("Cannot registerForNotification for data type \"")
4331 << demangle(typeid(DataT).name()) << "\" and name=\"" << name << "\" on node " << getLocation()
4332 << " with callback on \"" << demangle(typeid(T).name()) << "\""
4333 << "\" because this notification cannot possibly be generated by any descendant of this "
4334 << "node. Set ensure_possible=false to prevent this check if additional notification "
4335 << "source descendants are expected to be added. "
4336 << "It is possible the node generating the desired notification is in a private sub tree.";
4337 }
4338
4339 DelegateVector& observers = obs_local_[data_type]; // Create notification map
4340 if(findDelegate_<DataT, T, TMethod>(observers, obj, name) != observers.end()){
4341 throw SpartaException("Already observing a notification for data type \"")
4342 << demangle(typeid(DataT).name()) << "\" Name \"" << name << "\" on node " << getLocation()
4343 << " with callback on \"" << demangle(typeid(T).name()) << "\""
4344 << "\". Cannot register";
4345 }
4346
4347 delegate d = delegate::from_method<DataT, T, TMethod>(obj, *this, name);
4348 observers.push_back(std::move(d));
4349
4350 // Let children know
4351 broadcastRegistrationForNotificationListStringToChildren_(typeid(DataT), name, this, &observers.back(), allow_private);
4352 }
4353
4354 // Overload which allows registration with a class member function accepting only a data argument
4355 template <typename DataT, typename T, void (T::*TMethod)(const DataT&)>
4356 void registerForNotification_(T* obj, const std::string& name, bool ensure_possible=true, const bool allow_private=false)
4357 {
4358 (void)allow_private;
4359 const std::type_info& data_type = typeid(DataT);
4360 if(true == ensure_possible && false == canSubtreeGenerateNotification(data_type, name)){
4361 throw SpartaException("Cannot registerForNotification for data type \"")
4362 << demangle(typeid(DataT).name()) << "\" and name=\"" << name << "\" on node " << getLocation()
4363 << " with callback on \"" << demangle(typeid(T).name()) << "\""
4364 << "\" because this notification cannot possibly be generated by any descendant of this "
4365 << "node. Set ensure_possible=false to prevent this check if additional notification "
4366 << "source descendants are expected to be added. "
4367 << "It is possible the node generating the desired notification is in a private sub tree.";
4368 }
4369
4370 DelegateVector& observers = obs_local_[data_type]; // Create notification map
4371 if(findDelegate_<DataT, T, TMethod>(observers, obj, name) != observers.end()){
4372 throw SpartaException("Already observing a notification for data type \"")
4373 << demangle(typeid(DataT).name()) << "\" Name \"" << name << "\" on node " << getLocation()
4374 << " with callback on \"" << demangle(typeid(T).name()) << "\""
4375 << "\". Cannot register";
4376 }
4377
4378 delegate d = delegate::from_method<DataT, T, TMethod>(obj, *this, name);
4379 observers.push_back(std::move(d));
4380
4381 // Let children know
4382 broadcastRegistrationForNotificationListStringToChildren_(typeid(DataT), name, this, &observers.back(), allow_private);
4383 }
4384
4389 template <typename DataT, typename T, void (T::*TMethod)(const TreeNode&, const TreeNode&, const DataT&)>
4390 void deregisterForNotification_(T* obj, const std::string& name, const bool allow_private)
4391 {
4392 (void)allow_private;
4393 const std::type_info& data_type = typeid(DataT);
4394 auto itr = obs_local_.find(data_type);
4395 if(itr == obs_local_.end()){
4396 throw SpartaException("Not currently observing any notification for data type \"")
4397 << demangle(typeid(DataT).name()) << "\" Name \"" << name << "\" on node " << getLocation()
4398 << " with callback on \"" << demangle(typeid(T).name()) << "\" function \"" << TMethod
4399 << "\". Cannot deregister";
4400 }
4401 DelegateVector& observers = itr->second;
4402 DelegateVector::iterator d = findDelegate_<DataT, T, TMethod>(observers, obj, name);
4403 if(observers.end() == d){
4404 throw SpartaException("Not currently observing a notification for data type \"")
4405 << demangle(typeid(DataT).name()) << "\" Name \"" << name << "\" on node " << getLocation()
4406 << " . Attempted to deregister \"" << demangle(typeid(T).name()) << "\" function \"" << TMethod
4407 << "\". Cannot deregister";
4408 }
4409
4410 // Let children know that a delegate has been deregistered
4411 broadcastDeregistrationForNotificationListStringToChildren_(data_type, name, this, &(*d), allow_private);
4412
4413 observers.erase(d);
4414 }
4415 // Overload which allows deregistration with a class member function accepting only a data argument
4416 template <typename DataT, typename T, void (T::*TMethod)(const DataT&)>
4417 void deregisterForNotification_(T* obj, const std::string& name, const bool allow_private)
4418 {
4419 (void)allow_private;
4420 const std::type_info& data_type = typeid(DataT);
4421 auto itr = obs_local_.find(data_type);
4422 if(itr == obs_local_.end()){
4423 throw SpartaException("Not currently observing any notification for data type \"")
4424 << demangle(typeid(DataT).name()) << "\" Name \"" << name << "\" on node " << getLocation()
4425 << " with callback on \"" << demangle(typeid(T).name()) << "\" function \"" << TMethod
4426 << "\". Cannot deregister";
4427 }
4428 DelegateVector& observers = itr->second;
4429 DelegateVector::iterator d = findDelegate_<DataT, T, TMethod>(observers, obj, name);
4430 if(observers.end() == d){
4431 throw SpartaException("Not currently observing a notification for data type \"")
4432 << demangle(typeid(DataT).name()) << "\" Name \"" << name << "\" on node " << getLocation()
4433 << " . Attempted to deregister \"" << demangle(typeid(T).name()) << "\" function \"" << TMethod
4434 << "\". Cannot deregister";
4435 }
4436
4437 // Let children know that a delegate has been deregistered
4438 broadcastDeregistrationForNotificationListStringToChildren_(data_type, name, this, &(*d), allow_private);
4439
4440 observers.erase(d);
4441 }
4446 const TreeNode::ChildrenVector& getAllChildren_() const
4447 {
4448 return children_;
4449 }
4450
4454 template <typename T> T *getScopeRootImpl_(T *node) const;
4455
4459 TreeNode* getChild_(const std::string& name,
4460 bool must_exist,
4461 bool private_also);
4462
4464 const TreeNode* getChild_(const std::string& name,
4465 bool must_exist,
4466 bool private_also) const;
4467
4472 bool hasChild_(const std::string& name, bool private_also) const noexcept;
4473
4474 private:
4475
4482 void addExtension_(const std::string & extension_name, std::shared_ptr<ExtensionsBase> ext);
4483
4487 const node_uid_type node_uid_;
4488
4492 const std::string* name_;
4493
4497 const bool anon_;
4498
4504 const bool is_indexable_;
4505
4510 const std::string * const group_ptr_;
4511
4515 const group_idx_type group_idx_;
4516
4522 std::vector<const std::string*> tags_;
4523
4527 const std::string * const desc_ptr_;
4528
4541 const std::string* parent_loc_;
4542
4546 TreeNode* parent_;
4547
4552 bool is_attached_;
4553
4558 const Clock* clock_;
4559
4566 const Clock* working_clock_;
4567
4574 std::unordered_map<std::string, std::weak_ptr<ExtensionsBase>> cached_extensions_;
4575
4579
4584 mutable uint32_t num_children_finds_;
4585
4591 mutable uint32_t num_children_gets_;
4592
4595
4601 const TreeNode* expected_parent_;
4602
4606 bool is_builtin_;
4607
4612 bool is_hidden_;
4613
4617 AliasVector aliases_;
4618
4623 SharedPtr self_ptr_;
4624
4634 ChildrenVector children_;
4635
4640 ChildNameMapping names_;
4641
4648 NotificationObserverMap obs_local_;
4649
4654 bool is_expired_;
4655
4687 uint32_t privacy_level_ = 0;
4688
4700 bool is_scope_root_ = false;
4701
4708 class TreeNodeStatics {
4709 public:
4714 std::map<const TreeNode*, WeakPtr> parentless_map_;
4715
4728 std::map<const TreeNode*, WeakPtr> node_map_;
4729 };
4730 static TreeNodeStatics *statics_;
4731
4737 static node_uid_type next_node_uid_;
4738
4742 static TagsMap global_tags_map_;
4743
4748 static uint32_t teardown_errors_;
4749 };
4750
4752 template<class Ch,class Tr>
4753 inline std::basic_ostream<Ch,Tr>&
4754 operator<< (std::basic_ostream<Ch,Tr>& out, sparta::TreeNode const & tn) {
4755 out << tn.stringize();
4756 return out;
4757 }
4758
4760 inline std::ostream& operator<< (std::ostream& out, sparta::TreeNode const * tn) {
4761 if(nullptr == tn){
4762 out << "null";
4763 }else{
4764 out << tn->stringize();
4765 }
4766 return out;
4767 }
4768} // namespace sparta
Contains a number of ArchData pointers.
Basic Node framework in sparta device tree composite pattern.
Helpers for printing and populating vectors.
Object with a name which holds a Resource.
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.
Helpers for enforcing StaticInitialization order.
Container class for any number of ArchData pointers owned externally.
Manages building a clock tree.
A representation of simulated time.
Definition Clock.hpp:44
virtual bool isFinalizing() const
Is this node (and thus the entire tree above it) "finalized".
virtual bool isFinalized() const
Is this node (and thus the entire tree above it) "finalized".
TreePhase getPhase() const
Gets the trees current phase.
TreePhase
Current phase of tree construction (applies to node and entire tree).
@ TREE_CONFIGURING
Setting parameters, ports, and other pre-instantiation attributes. Hierarchy cannot be modified.
@ TREE_BUILDING
Setting up tree hierarchy only (initial state)
Information describing the type of validation being done.
Definition Resource.hpp:31
PhasedObject which can hold 0 or 1 Resource pointers to an associatedresource. Contains logic for set...
const T getResourceAs() const
Gets the resource contained by this node (if any) as the given type.
virtual std::string getResourceTypeRaw() const
Gets the typename of the resource that this node will eventually contain.
A class that lets you schedule events now and in the future.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
Static-initialization order controller.
static StringManager & getStringManager()
Returns the StringManager singleton.
const std::string *const EMPTY
Holds interned empty strings.
std::string * internString(const std::string &s)
Stores a string in shared space within this manager unless it is already stored.
Temporary delegate for notificaiton invokation until the implementation is working....
delegate & operator=(const delegate &d)
void operator()(const TreeNode &origin, const DataT &data) const
Invokes the delegate.
bool operator==(const delegate &d) const
Compares all fields in this delegate against another delegate.
bool observes(const TreeNode &obs_pt, const std::string *name) const
Compares against another delegate based on the observation point and name ignoring delegate callback ...
static delegate from_method(T *obj, const TreeNode &obs_pt, const std::string &name)
Creates delegate with given properties.
constexpr bool equals(T *obj, const TreeNode &obs_pt, const std::string &name) const
Compares against another delegate's information based on DataT, T, TMethod, observer pointer,...
Container for std::type_info.
Node in a composite tree representing a sparta Tree item.
Definition TreeNode.hpp:204
app::Simulation * getSimulation() const
Gets the simulation (if any) associated with this tree.
bool hasExtension(const std::string &extension_name) const
Check if this tree node has an extension by the given name.
virtual void broadcastRegistrationForNotificationToChildren_(const std::type_info &tinfo, const std::vector< const std::string * > &name_ids, TreeNode *obs_node, const delegate *del, const bool allow_private)
Recursively notifies children that the notification described is now (or still is) being observed at ...
static const uint32_t RENDER_SUBTREE_INDENT
Spaces to indent for each tree level in sparta::TreeNode::renderSubtree.
Definition TreeNode.hpp:361
static constexpr char NODE_NAME_VIRTUAL_GLOBAL[]
Node name for the virtual glopbal node.
Definition TreeNode.hpp:328
uint32_t findImmediateChildren_(std::regex &expr, std::vector< TreeNode * > &found, bool allow_private=false)
Variant of findImmediateChildren_ with no replacements vector.
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:302
node_uid_type getNodeUID() const
Gets the unique ID of this node.
bool hasObserversRegisteredForNotification(const std::type_info &tinfo, const std::string *name) const noexcept
Determines if this TreeNode (not descendants) has any observers for this type of event and name.
ConstT getAs() const
Retrieves this node after casting to type T.
const std::vector< const std::string * > & getTags() const
Gets the set of tags associated with this TreeNode.
std::string getLocation() const override final
bool canGenerateNotification(const std::type_info &tinfo, const std::string &name) const
Can this TreeNode generate a notification of the given type having the given name (does not require i...
ExtensionsBase * createExtension(const char *extension_name, bool replace=false)
std::map< std::string, const ExtensionsBase * > getAllExtensions() const
Get a map of extensions for this node.
Extension * addExtension(Args &&... args)
Add an extension, specifying the ExtensionsBase subclass type. Forward any arguments needed to your s...
const T * getExtensionAs(const std::string &extension_name) const
Get an extension, downcast to the given type.
static std::string getNextName(const std::string &name, size_t &pos)
Gets the next name between two '.' chars in a string starting at pos.
static const node_uid_type MAX_NODE_UID
Maximum value of node_uid_ before the framework throws an exception.
Definition TreeNode.hpp:308
void validateTree_()
Iterates the finalized tree and validates each node (e.g. ensures statistics can be evaluated)
ExtensionsBase * getExtension(const std::string &extension_name, bool no_factory_ok=false)
Get an extension object by type string. Returns nullptr if not found (unrecognized).
virtual uint32_t findImmediateChildren_(std::regex &expr, std::vector< const TreeNode * > &found, std::vector< std::vector< std::string > > &replacements, bool allow_private=false) const
Const-qualified variant of findImmediateChildren_.
void detachFromChildren_()
Removes a node from its children with the expectation this node will be immediately destroyed (i....
static const std::string DEBUG_DUMP_SECTION_DIVIDER
String dividing sections in a debug dump file.
Definition TreeNode.hpp:323
uint32_t getGroup(const std::string &group, std::vector< TreeNode * > &results)
Gets all nodes in a child group.
void dumpPossibleNotifications(std::ostream &o) const noexcept
Dumps a listing of the notifications which this node can generate to the ostream o.
static constexpr char NODE_NAME_NONE[]
Node name for anonymous node.
Definition TreeNode.hpp:318
static bool hasWildcardCharacters(const std::string &name)
Determines if a given node name has any wildcard characters which will be substituted in createSearch...
static constexpr char GROUP_NAME_BUILTIN[]
Reserved name for built-in nodes.
Definition TreeNode.hpp:369
const std::string & getDesc() const
Gets the description of this node.
TreeNode(TreeNode *parent, const std::string &name, const std::string &group, group_idx_type group_idx, const std::string &desc, bool is_indexable)
TreeNode full constructor. Initializes node and adds it as a child of an existing tree node (if paren...
virtual TreeNode * getRoot()
Gets farthest ancestor of this node.
size_t getNumExtensions() const
Get the number of extensions for this node.
void markHidden(bool hidden=true)
Marks this TreeNode hidden for the purposes of printint out nodes. This does not make the node inacce...
TreeNode(TreeNode *parent, const std::string &name, const std::string &desc)
TreeNode constructor with no group information.
uint32_t findChildren(const std::string &pattern, std::vector< TreeNode * > &results)
Version of findChildren with no replacements vector.
static constexpr char GROUP_NAME_NONE[]
Group name indicating that a node belongs to no group.
Definition TreeNode.hpp:313
void simulationTerminating_()
Hook to allow simulation resources to clean-up before simulation is ended.
static bool notificationCategoryMatch(const std::string *query_id, const std::string *node_id)
Checks if two notification categories match where one is an actual category.
void dumpPossibleSubtreeNotifications(std::ostream &o) const noexcept
Dumps a listing of the notifications which this node and its descendants can generate to the ostream ...
bool isIndexableByGroup() const
Returns whether this object is accessible through its parent's interfaces for getting children by gro...
const T getParentAs(bool must_exist=true) const
Retrieves a parent casted to type T* if this node has a parent.
Definition TreeNode.hpp:989
void enterTeardown_() noexcept
Recursively enter TREE_TEARDOWN phase while alerting nodes through onEnteringTeardown_ and alterting ...
virtual void addLink(TreeNode *node, const std::string &label)
Link this tree node with another tree node.
bool canSubtreeGenerateNotifications(const std::type_info &tinfo, const std::vector< const std::string * > &names) const
Checks to see if any a subtree can generate any of several notification names which may be patterns.
uint32_t findChildrenByTag(const std::string &tag, std::vector< TreeNode * > &results, int32_t max_depth=-1)
Finds a set of all children having the selected tag which this node or descendants of this node withi...
static const uint32_t TEARDOWN_ERROR_LIMIT
Number of teardown-phase-related messages that can be printed before the rest will be suppressed.
Definition TreeNode.hpp:405
void addExtensionFactory(const std::string &extension_name, std::function< ExtensionsBase *()> factory)
Add an extension factory to this tree node by its type (name).
static constexpr char LOCATION_NODE_SEPARATOR_EXPECTING
Separator character between node identifiers in a location string when the child is being attached to...
Definition TreeNode.hpp:345
static std::vector< const std::string * > parseNotificationNameString(const std::string &csl)
Parses a comma-separated list of notification names (or patterns) separated by commas and ignoring wh...
TreeNode * getChildAt(uint32_t idx) const
Gets a child at a position in the children vector in the order in which they were added to this node.
void addAlias(const std::string &alias)
Add a single alias for this node.
uint32_t getPossibleSubtreeNotifications(std::vector< NotificationInfo > &infos) const noexcept
Gets information on the possible notifications generated by this node and all its descendants.
std::shared_ptr< TreeNode > SharedPtr
Shared pointer to TreeNode. Acquire with WeakPtr::lock().
Definition TreeNode.hpp:275
const ExtensionsBase * getExtension(const std::string &extension_name) const
Get an extension object by type string. Returns nullptr if not found (unrecognized).
TreeNode(const TreeNode &)=delete
Not copy-constructable.
const TreeNode * getExpectedRoot() const
build-time equivalent to getRoot before an object is actually attached to a tree. This is a mainly a ...
void addChild(TreeNode &child)
AddChild by reference for convenience.
void broadcastDeregistrationForNotificationListStringToChildren_(const std::type_info &tinfo, const std::string &name, TreeNode *obs_node, const delegate *del, const bool private_only)
Symmetric oppostie of broadcastRegistrationForNotificationListStringToChildren_.
void broadcastRegistrationForNotificationListStringToChildren_(const std::type_info &tinfo, const std::string &name, TreeNode *obs_node, const delegate *del, const bool private_only)
Entry point to broadcastRegistrationForNotificationToChildren_ recursion. Breaks a name string.
ExtensionsBase * createExtension(const std::string &extension_name, bool replace=false)
Create an extension on demand. This is useful if you want to add an extension to a node that was not ...
virtual bool isAttached() const
Is this node part of a device tree with a proper RootTreeNode at the root.
Definition TreeNode.hpp:956
bool canSubtreeGenerateNotification(const std::type_info &tinfo, const std::string &name) const
Can this TreeNode or its descendants (of any distance) generate a notification of the given type havi...
bool isBuiltin() const
Is this node in the builtins group.
static const std::vector< std::pair< const char *, std::function< void(std::string &)> > > TREE_NODE_PATTERN_SUBS
List of pattern susbtitutions when creating a search pattern from a TreeNode name containing wildcard...
Definition TreeNode.hpp:411
void registerForNotification(T *obj, const std::string &name, bool ensure_possible=true)
Registers a callback method to listen for all notifications having the specified data type DataT and ...
void invokeDelegatesOn_(TreeNode *to_invoke, const DataT &data, const std::string *name_id)
Protected wrapper for invokeDelegates_ which allows a TreeNode to invoke delegates on another TreeNod...
TreeNode()=delete
Not default-constructable.
const ChildrenVector getChildren() const
Gets a vector of all children of this node in any group in the order in which they were added to this...
std::string renderSubtree(int32_t max_depth=-1, bool show_builtins=false, bool names_only=false, bool hide_hidden=false, bool(*leaf_filt_fxn)(const TreeNode *)=nullptr) const
Renders the subtree starting at this node as a string containing an indentation-based depth-first rep...
group_idx_type getGroupIndexMax(const std::string &group)
Gets the largest index of a node in the given group.
void verifyUniqueChildIdentifier_(const std::string &ident, bool ignore_group_collision=false)
Verifies that the given identifier is unique for all children of this node by comparing against names...
const std::string * getGroupNamePtr() const
Gets the group name string pointer (interned in StringManager singleton) of this node.
void detachFromParent_()
Removes a node from its parent with the expectation this node will be immediately destroyed (i....
const TreeNode * getChild(const std::string &name, bool must_exist=true) const
Overloaded const-qualified.
TreeNode(TreeNode &&rhp)
Move constructor.
uint32_t getLevel() const
Gets the level of this node. 0 is root.
uint32_t getChildren(std::vector< TreeNode * > &results, bool ignore_builtin_group=true, bool ignore_anonymous_nodes=true)
Gets all children of this node in the order in which they were added to this node.
sparta::TreeNode * findAncestorByName(const std::string &name)
Find ancestor by name.
void validatePostRun_(const PostRunValidationInfo &info) const
Hook to check the state of the simulator post-run and throw exceptions if something is incorrect.
bool isScopeRoot() const
void validateGroup(const std::string &name, const std::string &group, group_idx_type idx)
Validates the given group name string and group index for this TreeNode. Does not consider context (e...
static constexpr char LOCATION_NODE_SEPARATOR_ATTACHED
Separator character between node identifiers in a location string when the child is attached to the p...
Definition TreeNode.hpp:336
virtual void broadcastDeregistrationForNotificationToChildren_(const std::type_info &tinfo, const std::vector< const std::string * > &name_ids, TreeNode *obs_node, const delegate *del, const bool allow_private)
Recursively notifies children that the notification described has lost one particular observer (del) ...
DelegateVector::iterator findDelegate_(DelegateVector &dvec, T *obj, const std::string &target_name)
Finds a delegate associated with the given type T, object pointer, DataT, and TMethod within a Deleva...
static const std::map< const TreeNode *, WeakPtr > & getAllNodes()
Gets the vector of all TreeNodes currently known to be constructed.
void enterFinalizing_()
Recursively enter TREE_FINALIZING phase.
bool hasTag(const std::string &tag) const
Does this node have a specific tag (by name)
void validateDesc(const std::string &desc)
Validates the given description string for this TreeNode.
Scheduler * getScheduler(const bool must_exist=false)
Get the scheduler associated with this tree node's root.
const std::string * getNamePtr() const
Gets the name of this node's string pointer which isinterned in StringManager)
void addChild(TreeNode *child, bool inherit_phase=true)
Adds a TreeNode to this node as a child.
uint32_t group_idx_type
Index within a group.
Definition TreeNode.hpp:260
TreeNode(TreeNode *parent, const std::string &name, const std::string &group, group_idx_type group_idx, const std::string &desc)
TreeNode constructor with no is_indexable parameter [defaults to true].
bool areParametersLocked_() const
This method informs whether the tree is past the lockdown phase for all LOCKED and HIDDEN parameters....
const TreeNode * getScopeRoot() const
static bool matchesGlobLike(const std::string &pattern, const std::string &other)
Determine if a glob-like search pattern matches some other string.
static std::string getPreviousName_(const std::string &name, size_t &pos)
Gets the previous name between two '.' chars in a string starting at pos.
void postPropagatingNotification_(const TreeNode *origin, const DataT &data, const std::string *name_id)
Starts a notification propagating up the tree.
void enterFinalized_()
Recursively enter TREE_FINALIZED phase.
static const uint64_t CHILD_GET_THRESHOLD
Threshold for number of getChild calls after finalization before a warning message is printed about f...
Definition TreeNode.hpp:399
const Clock * getLocalClock()
Gets the clock associated directly with this Node. This is useful for configuration and simulator deb...
const AliasVector & getAliases() const
Gets a vector of all aliases of this node.
bool hasTag(const std::string *interned_tag_name) const
Does this node have a specific tag (by string pointer interned with StringManager)....
ExtensionsBase * getExtension()
Get an extension without needing to specify any particular extension name. If no extensions exist,...
std::string getDeepestMatchingPath(const std::string &path) const
Gets the deepest whole node location starting from this node that matches some part of path starting ...
void deregisterForNotification(T *obj, const std::string &name)
Removes at most one registration made with registerForNotification.
std::multimap< std::string, TreeNode * > ChildNameMapping
Mapping of names, aliases, and groups to individual child nodes within one node. This must be in a de...
Definition TreeNode.hpp:255
bool canGenerateNotification(const std::type_info &tinfo, const std::string *name) const
Can this TreeNode generate a notification of the given type having the given name or pattern.
std::weak_ptr< TreeNode > WeakPtr
Weak pointer to a TreeNode. Acquire with getWeakPtr.
Definition TreeNode.hpp:265
virtual void activateLink(const std::string &label)
Activate one of the links added with addLink.
TreeNode(TreeNode *parent, const std::string &group, group_idx_type group_idx, const std::string &desc)
TreeNode constructor for anonymous node with group information.
std::vector< std::string > AliasVector
Vector of aliases (other names for this node)
Definition TreeNode.hpp:247
void finalizeTree_()
Recursively create resources based on tree configuration. enter_finalize_ should be invoked after thi...
virtual uint32_t findImmediateChildren_(std::regex &expr, std::vector< TreeNode * > &found, std::vector< std::vector< std::string > > &replacements, bool allow_private=false)
Finds immediate children with some identity (name or alias) matching a regex.
WeakPtr getWeakPtr()
Gets a weak pointer to this TreeNode. This weak pointer is guaranteed to expire when this TreeNode is...
const ExtensionsBase * getExtension() const
Get an extension without needing to specify any particular extension name. If no extensions exist,...
static constexpr char LOCATION_NODE_SEPARATOR_UNATTACHED
Separator character preceding a node identifiers in a location string when that node has no parent an...
Definition TreeNode.hpp:354
TreeNode(const std::string &name, const std::string &group, group_idx_type group_idx, const std::string &desc)
TreeNode constructor with no parent Node.
static TreeNode * getVirtualGlobalNode()
Gets the virtual global node singleton. This node can have no parent and no children....
virtual void setClock(const Clock *clk)
Assigns a clock to this node. This clock will then be accessed by any descendant which has no assigne...
std::string getDisplayLocation() const
Returns the location of this node in the device tree which might not be usable for navigating the dev...
std::pair< uint32_t, std::string > recursGetDeepestMatchingPath_(const std::string &path, size_t name_pos) const
Finds the deepest node path mathing the input path. Implements getDeepestMatchingPath.
uint32_t locateNotificationSources(std::vector< TreeNode * > &nodes, const std::string &name="")
Gets all possible notification info from NotificationSources within this node's subtree.
void getDelegatesRegisteredForNotification(const std::type_info &tinfo, const std::string *name, std::vector< delegate > &dels) noexcept
Gets the vector of delegates registered for a notification on this TreeNode.
std::vector< const std::string * > getIdentifiers() const
Gets all the identifiers for this node (excluding groups)
bool isExpired() const
Is this expired (i.e. has it been the rhp of a move constructor)
virtual TreeNode * getParent()
Gets immediate parent of this node if one exists.
Definition TreeNode.hpp:964
bool isDescendantOf(const TreeNode *ancestor, int32_t max_depth=-1) const
Determines if this node is a descendant of the specified ancestor node within some number of generati...
void dumpDebugContent_(std::ostream &out) const noexcept
Allows resources to write out detailed textual debugging information about the node....
void lockdownParameters()
Method to put the device tree in lockdown phase. All LOCKED and HIDDEN parameters are frozen after th...
Definition TreeNode.hpp:429
static std::string createSearchRegexPattern(const std::string &pat)
Compute a regex pattern for a node child path containing any number of wildcard characters (not a dot...
group_idx_type getGroupIdx() const
Gets the group index of this node.
uint32_t getRecursiveNodeCount() const
Recursively gets the count of a nodes that are a subclass of a particular type (determined by dynamic...
const ConstT getChildAs(const std::string &name, bool must_exist=true) const
Retrieves a child that is castable to T with the given dotted path.
static bool identityMatchesPattern_(const std::string &ident, std::regex &expr, std::vector< std::string > &replacements)
Performs pattern matching on a identity string.
virtual std::string stringize(bool pretty=false) const
Create a string representation of this node.
Definition TreeNode.hpp:722
const T * getAs() const
Retrieves this node after casting to type const T.
std::vector< delegate > DelegateVector
Vector of delegates representing a list of observers to notify.
std::map< const std::string *, std::vector< TreeNode * > > TagsMap
Map of strings (interned in StringManager) tags to TreeNodes.
Definition TreeNode.hpp:280
std::map< type_info_container, DelegateVector > NotificationObserverMap
Map of delegate vectors containing all observers.
void stringizeTags(std::stringstream &ss) const
Render tags to a string in the form: " tags:[tag0, tag1]" If there are any tags. The leading space ma...
Definition TreeNode.hpp:742
const std::string & getName() const override
Gets the name of this node.
void enterConfig_() noexcept
Recursively enter TREE_CONFIGURING phase.
sparta::TreeNode * findAncestorByTag(const std::string &tag)
Find the first ancestor with a particular tag.
uint32_t getNumChildren() const
Gets the number of children that this node has including those in the sparta builtins group.
TreeNode * getChild(const std::string &name, bool must_exist=true)
Retrieves a child with this dotted path name.
Extension * replaceExtension(Args &&... args)
Replace an extension, specifying the ExtensionsBase subclass type. This api is similar to addExtensio...
void addTags(const std::vector< std::string > &v)
Adds each elements of a vector of tags to this node.
bool removeExtension(const std::string &extension_name)
Remove an extension by its name. Returns true if successful, false if the extension was not found.
bool hasExtensionOfType(const std::string &extension_name) const noexcept
Check if this tree node has an extension by the given name and type.
T * getExtensionAs(const std::string &extension_name)
Get an extension, downcast to the given type.
uint32_t getGroupSize(const std::string &group)
Gets the number of nodes in a group.
bool canGenerateNotification(const NotificationInfo &info) const
Can this TreeNode generate a notification of the given NotificationInfo.
uint32_t getChildrenOfType(std::vector< T * > &results) const
return all the children matching a particular type using dynamic cast.
void validateName(const std::string &nm)
Validates the given name string for this TreeNode. Does not consider context (e.g....
bool isHidden() const
Is this TreeNode supposed to be hidden during tree printouts This value does not have to be respected...
std::string getExpectedLocation() const
build-time equivalent to getLocation before an object is actually attached to a tree....
T getAs()
Retrieves this node after casting to type const T.
static std::string formatAllNodes()
Prints the list of all TreeNodes currently known to be constructed.
TreeNode * getScopeRoot()
void removeChildForTeardown_(TreeNode *child)
"Removes" the given child by invoking onDestroyingChild_ then removing this child from the children_ ...
bool hasImmediateChild(const TreeNode *n) const noexcept
Determines if the node n is an immediate child of this node.
void addAliases(const AliasVector &v)
Adds each element of a vector of aliases to this node..
static const uint64_t CHILD_FIND_THRESHOLD
Threshold for number of findChildren calls after finalization before a warning message is printed abo...
Definition TreeNode.hpp:391
void addTag(const std::string &tag)
Adds single tag to this node.
TreeNode(const std::string &name, const std::string &desc)
TreeNode constructor with no parent node or group information.
bool canSubtreeGenerateNotification(const std::type_info &tinfo, const std::string *name) const
Can this TreeNode or its descendants (of any distance) generate a notification of the given type havi...
std::set< std::string > getAllConfigExtensionNames() const
Get a list of extension names found for this node in all arch/config/extension files.
void bindTreeLate_()
Recursively invoke TreeNode::onBindTreeEarly_ and Resource::onBindTreeLate_ (in that order for each n...
uint32_t getChildrenIdentifiers(std::vector< std::string > &idents, bool ignore_builtin_group=true) const
Gets all child identifiers, aliases, group names, and group-aliases which can be used to refer to a c...
static bool isNodeConstructed(const TreeNode *)
Is a given node constructed?
virtual ~TreeNode()
Virtual destructor.
uint32_t findChildren(const std::string &pattern, std::vector< TreeNode * > &results, std::vector< std::vector< std::string > > &replacements)
Finds all children starting at this node with a given pattern relative to this node by matching names...
sparta::TreeNode * findAncestorByType()
Find ancestor by type.
T * getAs()
Retrieves this node after casting to type const T.
TreeNode & operator=(const TreeNode &)=delete
Not assign-constructable.
const std::string & getGroup() const
Gets the group name of this node.
bool isAnonymous() const
Is this node anonymous.
const Clock * getClock() override
Walks up parents (starting with self) until a parent with an associated local clock is found,...
void bindTreeEarly_()
Recursively invoke TreeNode::onBindTreeEarly_ and Resource::onBindTreeEarly_ (in that order for each ...
T * findAncestorResourceByType()
Find an ancestor's resource with a certain type.
void setExpectedParent_(const TreeNode *parent)
Tracks a node as an expected parent without actually adding this node as a child. This is used almost...
uint32_t findImmediateChildren_(std::regex &expr, std::vector< const TreeNode * > &found, bool allow_private=false) const
Variant of const-qualified findImmediateChildren_ with no replacements vector.
void makeSubtreePrivate()
Make the entire subtree private.
std::vector< TreeNode * > ChildrenVector
Vector of TreeNode children.
Definition TreeNode.hpp:242
uint32_t getPossibleNotifications(std::vector< NotificationInfo > &infos) const
Gets information on the possible notifications generated by this node (excludes children).
ConstWeakPtr getWeakPtr() const
Const variant of getWeakPtr.
uint64_t node_uid_type
Type of unique-identifier assigned to every node.
Definition TreeNode.hpp:237
void removeFromParentForTeardown_(TreeNode *parent)
Protected Wrapper for getParent()->removeChildForTeardown_ which allows subclases of TreeNode to indi...
bool locationMatchesPattern(const std::string &pattern, const TreeNode *pat_loc) const
Determines if the given pattern (which may contain wildcards) can describe this node.
ExtensionsBase * createExtension(bool replace=false)
Create an extension on demand without needing to specify any particular extension name....
void dumpLocatedNotificationSources(std::ostream &o, const std::string &name="")
Retrieves the relevant NotificationSources from locateNotificationSources and prints them to the outp...
std::set< std::string > getAllInstantiatedExtensionNames() const
Get a list of extension names for all instantiated extensions on this TreeNode.
std::weak_ptr< const TreeNode > ConstWeakPtr
Weak pointer to a const TreeNode. Acquire with getWeakPtr.
Definition TreeNode.hpp:270
static bool identityMatchesPattern_(const std::string &ident, std::regex &expr)
Variant of identityMatchesPattern_ with no replacements vector.
bool hasChild(const std::string &name) const noexcept
Determines if a child can be found with the given dotted path.
Virtual global node for all device trees in a single simulation. This node acts a potential notificat...
Simulator which builds a sparta DeviceTree.
Macros for handling exponential backoff.
std::ostream & operator<<(std::ostream &o, const SimulationInfo &info)
ostream insertion operator for SimulationInfo
std::string demangle(const std::string &name) noexcept
Demangles a C++ symbol.
Definition Utils.hpp:214
Type for indicating that ANY notification source type should be included in a search performed by loc...
Definition TreeNode.hpp:289
Notification type/name information.
const TreeNode * origin
TreeNode from which this notification can be generated.
NotificationInfo(const NotificationInfo &rhp)
Copy constructor.
void checkValid() const
Ensures the node contains valid data.
NotificationInfo(const NotificationInfo &&rhp)
Move constructor.
const std::string * name
String interned by StringManager. Must not be nullptr.
NotificationInfo & operator=(const NotificationInfo &rhp)
Assignment operator.
const std::type_info * tinfo
Type from typeid() on notification DataT.
NotificationInfo(const TreeNode *_origin, const std::type_info *_tinfo, const std::string *_name)
Basic constructor.