10#include <unordered_map>
12#include "sparta/utils/Utils.hpp"
114 parent->
addChild(
this, inherit_phase);
123 bool validateIndependently(std::string& err_names)
const
126 for(
const ParameterPairs::value_type& p :
keys_){
128 if(!p.second->validateIndependently(temp)){
129 err_names += p.second->stringize();
139 bool validateDependencies(
const TreeNode* node, std::string& err_names)
const
142 for(
const ParameterPairs::value_type& p :
keys_){
144 if(!p.second->validateDependencies(node, temp)){
145 err_names += p.second->stringize();
159 for(ParameterPairs::value_type & pair :
keys_) {
160 pair.second->resetReadCount_();
168 for(ParameterPairs::value_type & pair :
keys_) {
169 pair.second->resetWriteCount_();
184 std::stringstream errors;
186 for(
const ParameterPairs::value_type& p :
keys_){
187 if(not p.second->isReadOrIgnored()){
191 errors << p.second->getName();
198 <<
getLocation() <<
"\" have not been read: " << errors.str()
199 <<
" . A sparta::Resource must read all of its parameters or explicitly "
217 for(
const ParameterPairs::value_type& p :
keys_){
224 std::stringstream ss;
230 uint32_t col_sizes[] = {0, 0, 0, 0, 0};
231 for(
const ParameterPairs::value_type& p :
keys_){
232 col_sizes[0] = std::max<uint32_t>(p.second->getTypeName().size(), col_sizes[0]);
233 col_sizes[1] = std::max<uint32_t>(p.second->getName().size(), col_sizes[1]);
234 col_sizes[2] = std::max<uint32_t>(p.second->getValueAsString().size(), col_sizes[2]);
235 col_sizes[3] = std::max<uint32_t>(p.second->getDefaultAsString().size(), col_sizes[3]);
236 col_sizes[4] = std::max<uint32_t>(
numDecDigits(p.second->getReadCount()), col_sizes[4]);
240 for(
const ParameterPairs::value_type& p :
keys_){
247 << std::setiosflags(std::ios::left) << std::setw(col_sizes[0]) << pb->
getTypeName() <<
" "
248 << std::setiosflags(std::ios::left) << std::setw(col_sizes[1]) << pb->
getName() <<
" : "
249 << std::setiosflags(std::ios::left) << std::setw(col_sizes[2]) << pb->
getValueAsString() <<
", def="
250 << std::setiosflags(std::ios::left) << std::setw(col_sizes[3]) << pb->
getDefaultAsString()
251 <<
" read: " << std::setiosflags(std::ios::left) << std::setw(col_sizes[4]) << pb->
getReadCount()
261 virtual std::string
stringize(
bool pretty=
false)
const override {
263 std::stringstream ss;
287 ParameterPairs::const_iterator itr =
keys_.find(name);
288 if(
keys_.end() == itr){
290 << name <<
"\". No parameter by that name exists.";
314 template <
class ContentT>
319 throw SpartaException(
"Found parameter \"") << name <<
"\" in ParameterSet "
320 <<
getLocation() <<
" but Parameter was not of requested type \""
321 <<
demangle(
typeid(ContentT).name()) <<
"\". Was instead \""
328 template <
class ContentT>
333 throw SpartaException(
"Found parameter \"") << name <<
"\" in ParameterSet "
334 <<
getLocation() <<
" but Parameter was not of requested type \""
335 <<
demangle(
typeid(ContentT).name()) <<
"\". Was instead \""
352 return getChildAs<ParameterBase>(name, must_exist);
359 return getChildAs<ParameterBase>(name, must_exist);
364 std::vector<ParameterBase*>& results) {
365 std::vector<TreeNode*> nodes;
367 uint32_t num_found = 0;
371 results.push_back(p);
374 std::cerr <<
"Could not cast node " << node->
getLocation() <<
" to ParameterBase*" << std::endl;
384 ParameterPairs::const_iterator itr =
keys_.find(name);
385 return itr !=
keys_.end();
405 std::vector<const ParameterBase *> params)
const
407 for(
const auto & param : params)
411 bool hit_modifier =
false;
412 for(
const auto & it :
params_) {
415 << it->getName() <<
"' cannot modifiy '" << param->getName()
416 <<
"' because '" << it->getName()
417 <<
"' does not come before '" << param->getName()
418 <<
"' in the parameter set");
419 return (hit_modifier ==
true);
421 if(it == modifying_param) {
437 if(locking_params_stack_.empty()) {
441 "Parameter '" << locking_params_stack_.back()->getName()
442 <<
"' cannot modify itself in it's own callback!");
457 return (locking_params_stack_.empty() ==
false);
480 keys_(0), key_iter_(ParameterPairs::iterator())
485 keys_(keys), key_iter_(itr)
491 keys_(rhp.keys_), key_iter_(rhp.key_iter_)
497 key_iter_ = rhp.key_iter_;
523 if(key_iter_ == keys_->end()){
526 return key_iter_->second;
533 if(key_iter_ == keys_->end()){
536 return key_iter_->second;
540 return key_iter_ == rhp.key_iter_;
544 return key_iter_ != rhp.key_iter_;
560 std::vector<std::string> getNames()
const {
561 std::vector<std::string> result;
562 for(ParameterPairs::value_type
const & pair :
keys_){
563 result.push_back(pair.first);
569 ParameterPairs::iterator itr =
keys_.begin();
574 ParameterPairs::iterator itr =
keys_.end();
579 ParameterPairs::const_iterator itr =
keys_.begin();
584 ParameterPairs::const_iterator itr =
keys_.end();
588 size_t getNumParameters()
const {
741#define PARAMETER(type, name, def, doc) \
742 sparta::Parameter<type> name {#name, def, doc, __this_ps};
744#define LOCKED_PARAMETER(type, name, def, doc) \
745 sparta::Parameter<type> name {#name, def, doc, sparta::Parameter<type>::ParameterAttribute::LOCKED, __this_ps};
747#define VOLATILE_LOCKED_PARAMETER(type, name, def, doc) \
748 sparta::Parameter<type> name {#name, def, doc, sparta::Parameter<type>::ParameterAttribute::LOCKED, __this_ps, true};
750#define HIDDEN_PARAMETER(type, name, def, doc) \
751 sparta::Parameter<type> name {#name, def, doc, sparta::Parameter<type>::ParameterAttribute::HIDDEN, __this_ps};
753#define VOLATILE_HIDDEN_PARAMETER(type, name, def, doc) \
754 sparta::Parameter<type> name {#name, def, doc, sparta::Parameter<type>::ParameterAttribute::HIDDEN, __this_ps, true};
761#define VOLATILE_PARAMETER(type, name, def, doc) \
762 sparta::Parameter<type> name {#name, def, doc, __this_ps, true};
774#define PARAMETER_CTORDEFAULT(type, name, doc) \
775 class _Parameter_##name : public sparta::Parameter<type> \
778 typedef type value_type; \
781 _Parameter_##name() = delete; \
784 _Parameter_##name(ParameterSet* ps, type default_value) : \
785 sparta::Parameter<type>(#name, default_value, doc) \
787 sparta_assert(ps, "Must construct Parameter " #name " with valid ParameterSet"); \
788 this->addToSet_(ps); \
791 void operator=(const value_type& v) { \
792 sparta::Parameter<value_type>::operator=(v); \
796 _Parameter_##name name;
Individual Parameter interface base class, container class, and global helpers methods.
#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.
static TypeToTypeNameMap GBL_type_name_map
Map of internal typeid names to C++ names.
Boolean with a default capable of being changed to the opposite value only. it can never set to the d...
Non-templated base class for generic parameter access and iteration.
const T getValueAs() const
Gets the value of this ParameterBase as a templated type T if this parameter actually contains a valu...
virtual bool isVisibilityAllowed() const =0
Query if this parameter is safe to be displayed via prints, dumps. A parameter should not be displaye...
virtual std::string getDefaultAsString() const =0
Gets the default value of this Parameter as a string.
uint32_t getReadCount() const
Number of times this Parameter has been read after initialization or after the last write (or explici...
virtual std::string getValueAsString() const =0
Gets the current value of this Parameter as a string.
virtual const std::string getTypeName() const =0
Gets the compiler-independent readable type string of the value currently held.
Exception indicating a misconfigured Parameter or invalid Parameter access.
Non-const iterator for ParameterSet.
Generic container of Parameters.
virtual void onAddedAsChild_() noexcept override
When added to the tree after construction, try and pull values for paraemters. Finalize the Parameter...
ParameterBase * getParameter(const std::string &name, bool must_exist=true)
Gets a TreeNode by exact path relative to this node.
ParameterSet()=delete
Default constructor disabled.
void readValuesFromVirtualTree_()
Read values for every parameter of possible from the virtual parameter tree if possible.
void lock(const ParameterBase *lock_param)
Lock the parameter set for parameter to parameter updates.
void resetWriteCounts()
Reset the write-count on all Parameters in this set.
std::unordered_map< std::string, ParameterBase * > ParameterPairs
Mapping of parameter names to parameters (for fast lookup by name)
static constexpr char PARAMETER_SET_NODE_TAG[]
Tag added to ParameterSet nodes.
const T getParameterValueAs(const std::string &name) const
Finds a parameter and gets its value as the templated type.
void addParameter_(sparta::ParameterBase *p)
Add a parameter to the parameter set. \temp This will be removed.
uint32_t readAllParamValuesFromVirtualTree()
Read values for all parameters from the virtual tree and re-write them to the parameters the way SPAR...
void operator=(const ParameterSet &rhp)=delete
Copying disabled. Do not override.
ParameterSetIterator< ParameterPairs::const_iterator > const_iterator
Const iterator.
bool readValueFromVirtualTree_(const ParameterTree *arch_pt, const ParameterTree *pt, sparta::ParameterBase *p)
Read a value for a single parameter from the virtual tree and write it to the selected parameter....
uint32_t readVolatileParamValuesFromVirtualTree()
Read values for each volatile parameter from the virtual tree and re-write them to the parameters the...
const ParameterBase * getParameter(const std::string &name, bool must_exist=true) const
Const qualified variant of getParameter.
static constexpr char NODE_NAME[]
Name of any ParameterSet TreeNode.
ParameterVector params_
Straight vector of registered parameters.
bool hasParameter(const std::string &name) const
Determines whether this ParameterSet has the parameter with the given name.
virtual std::string dumpList() const
Print out a friendly set of parameters.
bool canParameterBeModifiedByLockingParam(const ParameterBase *modify_e)
Determine if the given parameter can be modified by a parameter currently writing to the parameter se...
bool isLocked() const
Is the parameter set currently locked by another parameter.
bool hasModificationPermission(const ParameterBase *modifying_param, std::vector< const ParameterBase * > params) const
Determine if the given parameter can safely modify the given list of parameters.
const ParameterTree * getSimArchParameterTree_()
Find the virtual architectural parameter tree for this node's tree from its root.
uint32_t findParameters(const std::string &name, std::vector< ParameterBase * > &results)
virtual ~ParameterSet()=default
Destructor.
void ignoreAll() const
Increments the read count of each Parameter in this set.
ParameterPairs keys_
Map of name (key) to ParameterBase* instances.
ParameterSet(TreeNode *parent)
Constructor.
std::vector< sparta::ParameterBase * > ParameterVector
Vector of ParameterBase pointers.
const ParameterTree * getSimParameterTree_()
Find the virtual parameter tree for this node's tree from its root.
ParameterSet * __this_ps
Pointer to this ParameterSet accessible in class declaration scope. This is used By parameters to att...
ParameterSetIterator< ParameterPairs::iterator > iterator
Non-const iterator.
const Parameter< ContentT > & getParameterAs(const std::string &name) const
Retrieves a sparta::Parameter<ContentT> reference from this parameter set.
virtual std::string stringize(bool pretty=false) const override
Create a string representation of this node.
ParameterSet(const ParameterSet &rhp)=delete
Copying disabled. Do not override.
virtual void onAddingChild_(TreeNode *child) override
React to child registration.
static uint32_t getNumBoundTypes()
Gets the number of bound types.
void verifyAllRead() const
Checks the read-count on all Parameters. If any have no been read at least once since last write (sin...
void unlock(const ParameterBase *unlock_param)
Unlock the parameter set for parameter to parameter updates.
void resetReadCounts()
Reset the read-count on all Parameters in this set.
Virtual Parameter Tree. This represents a tree of parameters read from some source but does not neces...
Parameter instance, templated to contain only a specific type.
TreePhase getPhase() const
Gets the trees current phase.
@ TREE_FINALIZING
Tree is being finalized, but has not completed (maybe errors?)
Used to construct and throw a standard C++ exception. Inherits from std::exception.
Node in a composite tree representing a sparta Tree item.
static const group_idx_type GROUP_IDX_NONE
GroupIndex indicating that a node has no group index because it belongs to no group.
std::string getLocation() const override final
static constexpr char GROUP_NAME_BUILTIN[]
Reserved name for built-in nodes.
void addChild(TreeNode *child, bool inherit_phase=true)
Adds a TreeNode to this node as a child.
const std::string & getName() const override
Gets the name of this node.
void addTag(const std::string &tag)
Adds single tag to this node.
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...
Macros for handling exponential backoff.
uint32_t numDecDigits(uint32_t val)
Gets number of decimal digits in a uint32_t.
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.