The Sparta Modeling Framework
Loading...
Searching...
No Matches
StatisticsHierRootNodes.hpp
1// <StatisticsHierRootNodes> -*- C++ -*-
2
3#pragma once
4
5#include <memory>
6#include <string>
7#include <unordered_map>
8
9namespace sparta {
10namespace statistics {
11
17template <class StorageT>
19{
20public:
21 virtual ~StatisticsHierRootNodes() {}
22
24 void addHierarchyRoot(const std::string & storage_name,
25 std::shared_ptr<StorageT> & root)
26 {
27 sparta_assert(root != nullptr, "Unexpected null statistics hierarchy root encountered");
28 if (roots_.count(storage_name) > 0) {
29 throw SpartaException("Statistic hiearchy with root named '") << storage_name
30 << "' already exists in this statistics set";
31 }
32 roots_[storage_name] = root;
33 }
34
36 std::vector<std::string> getRootNames() const {
37 std::set<std::string> names;
38 for (const auto & root : roots_) {
39 names.insert(root.first);
40 }
41 const std::vector<std::string> more_names = getLazyLoadedRootNames_();
42 names.insert(more_names.begin(), more_names.end());
43 return std::vector<std::string>(names.begin(), names.end());
44 }
45
50 void mapRootNameToReportFilename(const std::string & root_name,
51 const std::string & report_filename) const
52 {
53 root_names_to_report_filenames_[root_name] = report_filename;
54 }
55
59 StorageT * getRootByName(const std::string & root_name)
60 {
61 onNamedRootRequest_(root_name);
62 auto iter = roots_.find(root_name);
63 if (iter == roots_.end()) {
64 //Check if this root exists by a different name, for
65 //example the name passed in was 'out_csv' but its name
66 //in our hash is 'out.csv'
67 utils::ValidValue<std::string> report_filename =
68 getReportFilenameForRoot_(root_name);
69 if (report_filename.isValid()) {
70 return getRootByName(report_filename);
71 } else {
72 return nullptr;
73 }
74 }
75 return iter->second.get();
76 }
77
78private:
79 utils::ValidValue<std::string> getReportFilenameForRoot_(
80 const std::string & root_name) const
81 {
82 utils::ValidValue<std::string> report_filename;
83 auto iter = root_names_to_report_filenames_.find(root_name);
84 if (iter != root_names_to_report_filenames_.end()) {
85 report_filename = iter->second;
86 }
87 return report_filename;
88 }
89
90 virtual void onNamedRootRequest_(const std::string & root_name) {
91 (void) root_name;
92 }
93
94 virtual std::vector<std::string> getLazyLoadedRootNames_() const {
95 return {};
96 }
97
98 mutable std::unordered_map<
99 std::string,
100 std::string> root_names_to_report_filenames_;
101
102 std::unordered_map<
103 std::string,
104 std::shared_ptr<StorageT>> roots_;
105};
106
107} // namespace sparta
108} // namespace statistics
109
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
Utility class that holds onto statistics node hierarchies, accessible by a name that you choose.
void mapRootNameToReportFilename(const std::string &root_name, const std::string &report_filename) const
std::vector< std::string > getRootNames() const
Access the root node names in this set.
StorageT * getRootByName(const std::string &root_name)
void addHierarchyRoot(const std::string &storage_name, std::shared_ptr< StorageT > &root)
Append a statistics hierarchy root node to this set.
Provides a wrapper around a value to ensure that the value is assigned.
bool isValid() const
Is this value valid.
Macros for handling exponential backoff.