The Sparta Modeling Framework
Loading...
Searching...
No Matches
Parameter.hpp
Go to the documentation of this file.
1// <Parameter> -*- C++ -*-
2
3
4#pragma once
5
6#include <cassert>
7#include <cstddef>
8#include <cstdint>
9#include <iostream>
10#include <ios>
11#include <iomanip>
12#include <string>
13#include <ostream>
14#include <stdexcept>
15#include <vector>
16#include <functional>
17#include <type_traits>
18#include <typeinfo>
19
21#include "sparta/utils/Utils.hpp"
25#include "sparta/utils/KeyValue.hpp"
29#include "sparta/utils/VectorUtils.hpp"
31
38namespace sparta
39{
40 class ParameterBase;
41 class ParameterSet;
42
46 template <typename T>
47 inline T smartLexicalCast(const ParameterBase* p,
48 const std::string& s,
49 size_t& end_pos,
50 bool allow_recursion=true,
51 bool allow_prefix=true);
52
58 {
59 public:
60
62 ParameterException() = default;
63
66
71 ParameterException(const std::string & reason) :
72 SpartaException(reason)
73 { }
74
78 virtual ~ParameterException() noexcept {}
79
83 template<class T>
84 ParameterException & operator<<(const T & msg) {
86 return *this;
87 }
88 };
89
114 class ParameterBase : public TreeNode
115 {
116 public:
117
121 static constexpr char PARAMETER_NODE_TAG[] = "SPARTA_Parameter";
122
139 {
140 public:
141
150 ParameterValueIterator(const ParameterBase* pb, size_t idx) :
151 p_(pb),
152 idx_(idx)
153 {
154 sparta_assert(pb);
155 }
156
162 p_(rhp.p_),
163 idx_(rhp.idx_)
164 { }
165
175 return (idx_ == rhp.idx_) && (p_ == rhp.p_);
176 }
177
187 return !(*this == rhp);
188 }
189
197 const std::string operator*() const {
198 if(idx_ >= p_->getNumValues()){
199 throw ParameterException("Tried to dereference invalid ParameterBase iterator");
200 }
201 return p_->getValueAsStringAt(idx_);
202 }
203
211 if(idx_ >= p_->getNumValues()){
212 throw ParameterException("Tried to increment past end of ParameterBase iterator");
213 }
214 ++idx_;
215 return *this;
216 }
217
225 (void) i;
226 ParameterValueIterator current = *this;
227 ++(*this);
228 return current;
229 }
230
231 private:
232 const ParameterBase* p_;
233 size_t idx_;
234
236
237
246 ParameterBase(const std::string & name,
247 const std::string & desc) :
248 TreeNode(name, desc),
249 modifier_callback_(name.c_str()),
250 ignored_(false),
251 string_quote_(""),
252 name_(name),
253 desc_(desc),
254 writes_(0),
255 reads_(0),
256 is_volatile_(false)
257 {
259 addTag(PARAMETER_NODE_TAG); // Tag for quick searching
260 }
261
265 virtual ~ParameterBase() {}
266
272 sparta_assert(getPhase() <= TREE_FINALIZED, "Cannot set volatile state on a Parameter after finalization");
273 sparta_assert(getReadCount() == 0, "Cannot set volatile state on a Parameter after it has been read");
274 is_volatile_ = true;
275 }
276
280 bool isVolatile() const {return is_volatile_;}
281
286 virtual const std::string getTypeName() const = 0;
287
295 virtual std::string getDefaultAsString() const = 0;
296
303 virtual bool isDefault() const {
305 return true;
306 }
307 return false;
308 }
309
317 virtual std::string getValueAsString() const = 0;
318
336 virtual std::string getValueAsStringAt(size_t idx, bool peek=false) const = 0;
337
338
349 virtual std::string getItemValueFromString(const std::vector<uint32_t>& indices,
350 bool peek=false) const = 0;
351
355 std::string peekItemValueFromString(const std::vector<uint32_t>& indices) const {
356 return getItemValueFromString(indices, true);
357 }
358
374 template <class T>
375 const T getValueAs() const;
376
383 virtual bool isVector() const = 0;
384
385
393 virtual uint32_t getDimensionality() const = 0;
394
408 virtual uint32_t getVectorSizeAt(const std::vector<uint32_t>& indices,
409 bool peek=false) const = 0;
410
414 uint32_t peekVectorSizeAt(const std::vector<uint32_t>& indices) const {
415 return getVectorSizeAt(indices, true);
416 }
417
430 virtual size_t getNumValues(bool peek=false) const = 0;
431
435 size_t peekNumValues() const {
436 return getNumValues(true);
437 }
438
443 virtual double getDoubleValue() const = 0;
444
451 virtual const_iterator begin() const = 0;
452
459 virtual const_iterator end() const = 0;
460
461
477 virtual bool isDefaultOverridden() const = 0;
478
490 virtual void overrideDefaultFromString(const std::string& val) = 0;
491
503 virtual void overrideDefaultFromStringVector(const std::vector<std::string>& val) = 0;
504
512 virtual void overrideDefaultItemValueFromString(const std::vector<uint32_t>& indices,
513 const std::string& str) = 0;
514
521 virtual void overrideDefaultResizeVectorsFromString(const std::vector<uint32_t>& indices) = 0;
522
529
536
550 void setValueFromString(const std::string& str, bool poke=false);
551
568 void setValueFromStringVector(const std::vector<std::string>& str, bool poke=false);
569
587 void setItemValueFromString(const std::vector<uint32_t>& indices,
588 const std::string& str);
589
594 virtual bool equals(const ParameterBase &other) = 0;
595
604 virtual void resizeVectorsFromString(const std::vector<uint32_t>& indices) = 0;
605
611 virtual void clearVectorValue() = 0;
612
623 virtual std::string stringize(bool pretty=false) const override {
624 (void) pretty;
625 std::stringstream ss;
626 ss << "[" << TreeNode::stringize(pretty) << "]<param " << getTypeName() << " " << getName() << "="
627 << getValueAsString() << ", def=" << getDefaultAsString() << ", write=" << getWriteCount()
628 << " read: " << getReadCount() << " ignored: " << isIgnored();
629 if(isVolatile()){
630 ss << " VOLATILE";
631 }
632 ss << ">";
633 return ss.str();
634 }
635
644 virtual bool validateIndependently(std::string& err_names) const = 0;
645
657 virtual bool validateDependencies(const TreeNode* node,
658 std::string& err_names) const = 0;
659
682 void associateParametersForModification(std::vector<const ParameterBase *> params,
683 const sparta::SpartaHandler& modifier_callback);
684
688
697 uint32_t getWriteCount() const {
698 return writes_;
699 }
700
712 uint32_t getReadCount() const {
713 return reads_;
714 }
715
720 bool isReadOrIgnored() const {
721 return ignored_ || reads_ > 0;
722 }
723
728 bool isIgnored() const {
729 return ignored_;
730 }
731
737 virtual bool supportsCompression() const {
738 return false;
739 }
740
746 virtual bool isVisibilityAllowed() const = 0;
747
755 std::string setStringQuote(const std::string& s) {
756 auto old = string_quote_;
757 string_quote_ = s;
758 return old;
759 }
760
761 protected:
762
767
771 virtual void setValueFromStringImpl_(const std::string& s, bool poke=false) = 0;
772
776 virtual void setValueFromStringVectorImpl_(const std::vector<std::string>& str, bool poke=false) = 0;
777
781 virtual void setItemValueFromStringImpl_(const std::vector<uint32_t>& indices,
782 const std::string& str) = 0;
783
790 void unread_() const {
792 ignored_ = false;
793 }
794
801
807 void incrementReadCount_() const {
808 ++reads_;
809 }
810
816 void resetReadCount_() const {
817 reads_ = 0;
818 }
819
827 writes_ = 0;
828 }
829
833 void ignore_() const {
834 ignored_ = true;
835 }
836
841
855 // Writing is illegal if the owning node already has a resource.
856 // This is not how this SHOULD work - all configuration should be
857 // done during configuration. However, dynamically created nodes
858 // need to be configured
859 //TreeNode* pset = getParent();
860 //sparta_assert(// pset != nullptr, "Cannot write a parameter that does not belong to a ParameterSet");
861 //TreeNode* owner = pset->getParent();
862 //if(owner){
863 // if(owner->getPhase() > TREE_CONFIGURING
864 // && owner->hasResource()){
865 // throw SpartaException("Cannot write to Parameter ")
866 // << getLocation() << " because the associated TreeNode ("
867 // << owner->getLocation() << ") has already created a resource";
868 // }
869 //}
870
871 // Write-after read is prohibited
872 if(isVolatile() == false && reads_ > 0){
873 throw SpartaException("Cannot write parameter ") << getLocation()
874 << " after reading it unless it is a volatile parameter";
875 }
876
877 // Writing is illegal once tree is configured
878 if(getPhase() > TREE_FINALIZED){
879 throw SpartaException("Cannot write to Parameter ")
880 << getLocation() << " because it is already finalized";
881 }
882
884 ++writes_;
885 }
886
892
897 void logAssignedValue_() const;
898
902 static void logCurrentBackTrace_();
903
911
915 friend class ParameterSet;
916
920 sparta::SpartaHandler modifier_callback_;
921
926 mutable bool ignored_;
927
931 std::string string_quote_;
932
933 private:
934
938 std::string name_;
939
943 std::string desc_;
944
948 uint32_t writes_;
949
954 mutable uint32_t reads_;
955
959 std::vector<const ParameterBase *> associated_params_;
960
964 bool is_volatile_;
965
968
969 }; // class ParameterBase
970
987 template<class ValueType>
989 {
991 std::function<bool(ValueType&, const sparta::TreeNode*)> callback_;
992
993 std::string name_;
994
995 public:
996
997 template<class T, bool (T::*TMethod)(ValueType&, const sparta::TreeNode*)>
998 static ValidationCheckCallback<ValueType> from_method(T* obj,
999 const std::string& name)
1000 {
1001 (void) obj;
1003 vcc.callback_ = std::bind(TMethod, obj, std::placeholders::_1, std::placeholders::_2);
1004 return vcc;
1005
1006 // This is not valid because the template parameter TMethod cannot be deduced
1007 //return ValidationCheckCallback<ValueType>(obj, name);
1008 }
1009
1023 /*
1024 template<class T, bool (T::*TMethod)(ValueType)>
1025 ValidationCheckCallback(T* obj, const std::string& name) :
1026 callback_(std::bind(TMethod, obj, std::placeholders::_1)),
1027 name_(name)
1028 {
1029 validateName(name_);
1030 }*/
1031
1033 ValidationCheckCallback(bool (*method)(ValueType&, const sparta::TreeNode*),
1034 const std::string& name) :
1035 callback_(std::bind(method, std::placeholders::_1, std::placeholders::_2)),
1036 name_(name)
1037 {
1038 validateName(name_);
1039 }
1040
1043 callback_(std::bind(&ValidationCheckCallback<ValueType>::doNothing_, std::placeholders::_1, std::placeholders::_2)),
1044 name_("<uninitialized>")
1045 {
1046 validateName(name_);
1047 }
1048
1050 ValidationCheckCallback(const std::string& name) :
1051 callback_(std::bind(&ValidationCheckCallback<ValueType>::doNothing_, std::placeholders::_1, std::placeholders::_2)),
1052 name_(name)
1053 {
1054 validateName(name_);
1055 }
1056
1059 callback_(rhp.callback_),
1060 name_(rhp.name_)
1061 {
1062 validateName(name_);
1063 }
1064
1065 void operator=(const ValidationCheckCallback<ValueType>& rhp)
1066 {
1067 callback_ = rhp.callback_;
1068 name_ = rhp.name_;
1069 }
1070
1071 std::string getName() const
1072 {
1073 return name_;
1074 }
1075
1077 bool operator()(ValueType& val, const TreeNode* node) const
1078 {
1079 return callback_(val, node);
1080 }
1081
1084 static void validateName(const std::string& nm)
1085 {
1086 if(nm.find(',') != std::string::npos){
1087 ParameterException ex("ValidationCheckCallback name \"");
1088 ex << nm << "\" contains a comma, which is not permitted";
1089 throw ex;
1090 }
1091 }
1092
1093 private:
1094
1096 static bool doNothing_(ValueType& val, const TreeNode* node)
1097 {
1098 (void) val;
1099 (void) node;
1100 return true;
1101 }
1102 };
1103
1112 template <typename ValueType>
1114 {
1115 public:
1116
1121 enum class ParameterAttribute : std::uint8_t {
1122 DEFAULT = 0,
1123 __FIRST = DEFAULT,
1124 LOCKED = 1,
1125 HIDDEN = 2,
1126 __LAST
1127 };
1128
1129 private:
1130 OneWayBool<false> default_override_;
1131 ParameterAttribute param_attr_ {ParameterAttribute::DEFAULT};
1132 ValueType def_val_;
1133 ValueType val_;
1134 sparta::utils::DisplayBase disp_base_;
1135 std::vector<ValidationCheckCallback<ValueType> > bounds_;
1136 std::vector<ValidationCheckCallback<ValueType> > dependencies_;
1137
1138 public:
1140 using type = ValueType;
1141 using value_type = ValueType; // only for passing regression
1142
1155 Parameter(const std::string& name,
1156 const ValueType& def,
1157 const std::string& doc,
1158 bool isvolatile = false) :
1159 ParameterBase(name, doc),
1160 def_val_(def),
1161 val_(def),
1162 disp_base_(sparta::utils::BASE_DEC)
1163 {
1164 //std::cout << name << " = " << typeid(ValueType).name() << std::endl;
1165
1167
1168 if(isvolatile){
1169 setIsVolatile();
1170 }
1171 }
1172
1178 Parameter(const std::string& name,
1179 const ValueType& def,
1180 const std::string& doc,
1181 ParameterSet * ps,
1182 bool isvolatile = false) :
1183 Parameter (name, def, doc, isvolatile)
1184 {
1185 sparta_assert(ps, "Must construct parameter " << name << " with valid ParameterSet");
1186 addToSet_(ps);
1187 }
1188
1189 Parameter(const std::string& name,
1190 const ValueType& def,
1191 const std::string& doc,
1192 const ParameterAttribute& attr,
1193 ParameterSet* ps,
1194 bool isvolatile = false) :
1195 Parameter(name, def, doc, isvolatile)
1196 {
1197 sparta_assert(ps, "Must construct parameter " << name << " with valid ParameterSet");
1198 param_attr_ = attr;
1199 if(param_attr_ == ParameterAttribute::HIDDEN) {
1200 markHidden(true);
1201 }
1202 addToSet_(ps);
1203 }
1204
1206 virtual ~Parameter() = default;
1207
1225 template<class T, bool (T::*TMethod)(ValueType&, const sparta::TreeNode*)>
1226 void addDependentValidationCallback(T* obj, const std::string& name)
1227 {
1228 dependencies_.push_back(ValidationCheckCallback<ValueType>::template from_method<T, TMethod>(obj, name));
1229
1230 // Not allowed: Cannot resolve template arguments
1231 //dependencies_.push_back(ValidationCheckCallback<ValueType>(obj, name));
1232 }
1233
1254 void addDependentValidationCallback(bool (*method)(ValueType&, const sparta::TreeNode*),
1255 const std::string& name)
1256 {
1257 dependencies_.push_back(ValidationCheckCallback<ValueType>(method, name));
1258 }
1259
1260
1265 virtual const std::string getTypeName() const override final{
1266 return Parameter::template getTypeName_<ValueType>();
1267 }
1268
1271 ValueType getDefault() const {
1272 return def_val_;
1273 }
1274
1281 virtual std::string getDefaultAsString() const override final {
1282 return sparta::utils::stringize_value(def_val_, disp_base_, string_quote_);
1283 }
1284
1292 std::string getValueAsString() const override final {
1294 return sparta::utils::stringize_value(val_, disp_base_, "\"");
1295 }
1296 return sparta::utils::stringize_value(val_, disp_base_, string_quote_);
1297 }
1298
1307 std::string getValueAsStringAt(size_t idx, bool peek=false) const override final {
1308 return Parameter::template getValueAsStringAt_<ValueType, ValueType>(idx, peek);
1309 }
1310
1316 std::string getItemValueFromString(const std::vector<uint32_t>& indices,
1317 bool peek=false) const override final {
1318 if(indices.size() == 0){
1319 // No indices given. This must be a non-vector type
1320 return getFinalVectorItemValueFromString_(val_, indices, 0, peek);
1321 }
1322
1323 // Handle if this is a vector
1324 return getVectorItemValueFromString_(val_, indices, 0, peek);
1325 }
1326
1333 uint32_t getVectorSizeAt(const std::vector<uint32_t>& indices,
1334 bool peek=false) const override final {
1335 if(indices.size() == 0){
1336 // No indices given. This must be a non-vector type
1337 return getFinalVectorSize_(val_, indices, 0, peek);
1338 }
1339
1340 // Handle if this is a vector
1341 return getVectorSize_(val_, indices, 0, peek);
1342 }
1343
1348 operator const ValueType&() const {
1349 return getValue();
1350 }
1351
1356 const ValueType& operator()() const {
1357 return getValue();
1358 }
1359
1375 void ignore() const {
1376 ignore_();
1377 }
1378
1389 void unread() const {
1390 unread_();
1391 }
1392
1398 const ValueType& getValue() const {
1400 return getValue_();
1401 }
1402
1415 const ValueType& peekValue() const {
1416 return getValue_();
1417 }
1418
1422 double getDoubleValue() const override final {
1423 return getDoubleValue_<ValueType>();
1424 }
1425
1433 virtual size_t getNumValues(bool peek=false) const override final {
1434 return Parameter::template getNumValues_<ValueType, ValueType>(peek);
1435 }
1436
1441 virtual bool isVector() const override final {
1443 }
1444
1445 // Implements ParameterBase::getDimensionality
1446 virtual uint32_t getDimensionality() const override final {
1447 return Dimensionality<ValueType>::value;
1448 }
1449
1454 virtual ParameterBase::const_iterator begin() const override final{
1455 return ParameterBase::const_iterator(this, 0);
1456 }
1457
1462 virtual ParameterBase::const_iterator end() const override final{
1464 }
1465
1470 template <class T>
1471 bool operator==(const Parameter<T>& rhp) const {
1472 return getValue() == rhp.getValue();
1473 }
1474
1479 template <class T>
1480 bool operator!=(const Parameter<T>& rhp) const {
1481 return getValue() != rhp.getValue();
1482 }
1483
1488 bool operator>(const Parameter<type>& rhp) const {
1489 return getValue() > rhp.getValue();
1490 }
1491
1496 template <class T>
1497 bool operator>=(const Parameter<T>& rhp) const {
1498 return getValue() >= rhp.getValue();
1499 }
1500
1505 bool operator<(const Parameter<type>& rhp) const {
1506 return getValue() < rhp.getValue();
1507 }
1508
1513 bool operator<=(const Parameter<type>& rhp) const {
1514 return getValue() <= rhp.getValue();
1515 }
1516
1518 template <class T>
1519 typename std::enable_if<!std::is_base_of<ParameterBase, T>::value, bool>::type
1520 operator==(const T rhp) const {
1521 return getValue() == (ValueType)rhp;
1522 }
1523
1525 template <class T>
1526 typename std::enable_if<!std::is_base_of<ParameterBase, T>::value, bool>::type
1527 operator!=(const T rhp) const {
1528 return getValue() != (ValueType)rhp;
1529 }
1530
1532 template <class T>
1533 typename std::enable_if<!std::is_base_of<ParameterBase, T>::value, bool>::type
1534 operator>(const T rhp) const {
1535 return getValue() > (ValueType)rhp;
1536 }
1537
1539 template <class T>
1540 typename std::enable_if<!std::is_base_of<ParameterBase, T>::value, bool>::type
1541 operator>=(const T rhp) const {
1542 return getValue() >= (ValueType)rhp;
1543 }
1544
1546 template <class T>
1547 typename std::enable_if<!std::is_base_of<ParameterBase, T>::value, bool>::type
1548 operator<(const T rhp) const {
1549 return getValue() < (ValueType)rhp;
1550 }
1551
1553 template <class T>
1554 typename std::enable_if<!std::is_base_of<ParameterBase, T>::value, bool>::type
1555 operator<=(const T rhp) const {
1556 return getValue() <= (ValueType)rhp;
1557 }
1558
1571 void operator=(const Parameter& p) = delete;
1572
1585 void operator=(const ValueType& v) {
1586 checkModificationPermission_();
1587 // if the simulator was setup using --read-final-config
1588 // we do not allow the simulator to override values
1589 // so just return out and warn that we are skipping this.
1590 if (usingFinalConfig_() && (false == isHidden()))
1591 {
1592 static bool been_warned = false;
1593 if(!been_warned) {
1594 std::cout << "WARNING: A simulator override for parameter " << getLocation()
1595 << " (using operator=) was performed and ignored. This is because"
1596 << " the simulator is using --read-final-config. This is your"
1597 << " first and last warning." << std::endl;
1598 been_warned = true;
1599 }
1600 return;
1601 }
1602
1604 ValueType old_val = val_;
1605 val_ = v;
1607 try {
1609 }
1610 catch(sparta::SpartaException & e) {
1611 val_ = old_val;
1612 throw;
1613 }
1614 }
1615
1616 virtual bool isDefaultOverridden() const override final {
1617 return default_override_;
1618 }
1619
1620 virtual void overrideDefaultFromString(const std::string& str) override final {
1621 Parameter::template overrideDefaultFromString_<ValueType, ValueType>(str);
1623 "Cannot override default on parameter if read count is > 0. "
1624 "Problem on parameter " << getLocation());
1625 default_override_ = true;
1626 }
1627
1628 virtual void overrideDefaultFromStringVector(const std::vector<std::string>& vec) override final {
1629 Parameter::template overrideDefaultFromStringVector_<ValueType, ValueType>(vec);
1631 "Cannot override default on parameter if read count is > 0. "
1632 "Problem on parameter " << getLocation());
1633 default_override_ = true;
1634 }
1635
1636 virtual void overrideDefaultItemValueFromString(const std::vector<uint32_t>& indices,
1637 const std::string& str) override final {
1638 if(indices.size() == 0){
1639 // No indices given. This must be a non-vector type
1640 Parameter::template overrideDefaultFromString_<ValueType, ValueType>(str);
1641 }else{
1642 // handle if this is a vector
1643 const bool incr_write_count = false;
1644 setVectorItemValueFromString_(def_val_, indices, 0, str, incr_write_count);
1645 }
1646 default_override_ = true;
1647 }
1648
1649 virtual void overrideDefaultResizeVectorsFromString(const std::vector<uint32_t>& indices) override final {
1650 if(indices.size() == 0){
1651 return; // No effect
1652 }
1653
1654 resizeVectorsFromString_(def_val_, indices, 0);
1655 }
1656
1657 virtual void overrideDefaultClearVectorValue() override final {
1658 clearVectorValue_(def_val_);
1659 }
1660
1661
1662 virtual void restoreValueFromDefaultImpl_() override final {
1663 checkModificationPermission_();
1665 "Parameter " << getLocation() << " must not have been read when restoring "
1666 "a value from the default. This is a write");
1667
1669
1670 val_ = def_val_;
1671 }
1672
1673 virtual bool equals(const ParameterBase &other) override final {
1674 return *this == dynamic_cast<const Parameter<ValueType> &>(other);
1675 }
1676
1677 virtual void resizeVectorsFromString(const std::vector<uint32_t>& indices) override final {
1678 if(indices.size() == 0){
1679 return; // No effect
1680 }
1681
1682 resizeVectorsFromString_(val_, indices, 0);
1683 }
1684
1685 virtual void clearVectorValue() override final {
1686 clearVectorValue_(val_);
1687 }
1688
1694 checkModificationPermission_();
1695 sparta::utils::DisplayBase old = disp_base_;
1696 disp_base_ = base;
1697 return old;
1698 }
1699
1705 return disp_base_;
1706 }
1707
1708 bool validateIndependently(std::string& err_names) const override
1709 {
1710 (void) err_names;
1711
1712 // \todo Implement independent validation
1713 //sparta_assert(0, "sparta::Parameter::validateIndependently is unimplemented");
1714
1715 return true;
1716 }
1717
1728 bool validateDependencies(const TreeNode* node, std::string& err_names) const override
1729 {
1730 bool success = true;
1731 ValueType val = getValue_(); // Does not count as a read
1732 for(const ValidationCheckCallback<ValueType>& vcb : dependencies_){
1733 if(!vcb(val, node)){
1734 err_names += vcb.getName() + ",";
1735 success = false;
1736 }
1737 }
1738 return success;
1739 }
1740
1753 template <class U>
1755 return Parameter::template operator_insert_<U, ValueType>(e);
1756 }
1757
1763 virtual bool isVisibilityAllowed() const override{
1764 return ((param_attr_ == ParameterAttribute::HIDDEN) and
1765 (this->areParametersLocked_())) ? false : true;
1766 }
1767
1768 protected:
1769
1770 template <class U, class C1>
1771 typename std::enable_if<is_vector<C1>::value, Parameter<ValueType>&>::type
1772 operator_insert_(U e) {
1773 checkModificationPermission_();
1775 val_.push_back((typename ValueType::value_type) e);
1776 return *this;
1777 }
1778
1779 virtual void setValueFromStringImpl_(const std::string& str, bool poke=false) override final {
1780 Parameter::template setValueFromString_<ValueType, ValueType>(str, poke);
1781 }
1782
1783 virtual void setValueFromStringVectorImpl_(const std::vector<std::string>& vec, bool poke=false) override final {
1784 Parameter::template setValueFromStringVector_<ValueType, ValueType>(vec, poke);
1785 }
1786
1787 virtual void setItemValueFromStringImpl_(const std::vector<uint32_t>& indices,
1788 const std::string& str) override final {
1789 if(indices.size() == 0){
1790 // No indices given. This must be a non-vector type
1791 Parameter::template setValueFromString_<ValueType, ValueType>(str);
1792 }else{
1793 // handle if this is a vector
1794 const bool incr_write_count = true;
1795 setVectorItemValueFromString_(val_, indices, 0, str, incr_write_count);
1796 }
1797 }
1798
1800 const ValueType& getValue_() const {
1801 return val_;
1802 }
1803
1804 template <typename T>
1805 typename std::enable_if<std::is_arithmetic<T>::value, double>::type
1806 getDoubleValue_() const {
1807 return val_;
1808 }
1809
1810 template <typename T>
1811 typename std::enable_if<!std::is_arithmetic<T>::value, double>::type
1812 getDoubleValue_() const {
1813 throw SpartaException("Cannot get 'double' type value from parameter ")
1814 << getLocation() << " which is of type " << getTypeName();
1815 }
1816
1817 private:
1822 void checkModificationPermission_() const{
1823 if(this->areParametersLocked_() and
1824 param_attr_ != ParameterAttribute::DEFAULT){
1825 throw ParameterException("Modifying special parameters after Lockdown phase is disallowed.");
1826 }
1827 }
1828
1829 // Multi-dimensional parameter indexed writes
1830
1850 template <typename T>
1851 typename std::enable_if<is_vector<typename std::remove_reference<T>::type>::value>::type
1852 setVectorItemValueFromString_(T& vec,
1853 const std::vector<uint32_t>& indices,
1854 uint32_t index_level,
1855 const std::string& str,
1856 bool increment_write_count) {
1857 checkModificationPermission_();
1858 assert(indices.size() > 0);
1859
1860 uint32_t idx = indices.at(index_level);
1861 if(idx >= vec.size()){
1862 vec.resize(idx+1);
1863 }
1864
1865 if(indices.size() - 1 == index_level){
1866 // Final index should place a value in a vector
1867 // Note that vec is a vector
1868 typename T::reference to_set = vec.at(idx);
1869 setFinalVectorItemValueFromString_(to_set, indices, index_level, str);
1870 if(increment_write_count){
1872 }
1873 }else{
1874 // Non-final index should select a vector and recurse until the
1875 // last level of nested vectors is reached
1876 typename T::reference next_vec = vec.at(idx);
1877 setVectorItemValueFromString_(next_vec, indices, index_level+1, str, increment_write_count);
1878 }
1879 }
1880
1886 template <typename T>
1887 typename std::enable_if<!is_vector<typename std::remove_reference<T>::type>::value>::type
1888 setVectorItemValueFromString_(T& vec,
1889 const std::vector<uint32_t>& indices,
1890 uint32_t index_level,
1891 const std::string& str,
1892 bool increment_write_count) {
1893 checkModificationPermission_();
1894 (void) vec;
1895 (void) index_level;
1896 (void) str;
1897 (void) increment_write_count;
1898
1899 throw ParameterException("Cannot set value from string on parameter \"")
1900 << getName() << "\" which is of type \""
1901 << getTypeName() << "\" with indices: " << indices << " (" << indices.size()
1902 << " levels)" << " because this type only has " << index_level << " dimensions";
1903 }
1904
1911 template <typename T>
1912 typename std::enable_if<is_vector<typename std::remove_reference<T>::type>::value>::type
1913 setFinalVectorItemValueFromString_(T& to_set,
1914 const std::vector<uint32_t>& indices,
1915 uint32_t index_level,
1916 const std::string& str) {
1917 checkModificationPermission_();
1918 (void) to_set;
1919 (void) str;
1920
1921 throw ParameterException("Cannot set value from string on parameter \"")
1922 << getName() << "\" which is of type \""
1923 << getTypeName() << "\" with indices: " << indices << " (" << indices.size()
1924 << " levels)" << " because this type has more than " << index_level << " dimensions";
1925 }
1926
1940 template <typename T>
1941 typename std::enable_if<!is_vector<typename std::remove_reference<T>::type>::value>::type
1942 setFinalVectorItemValueFromString_(T& to_set,
1943 const std::vector<uint32_t>& indices,
1944 uint32_t index_level,
1945 const std::string& str) {
1946 checkModificationPermission_();
1947 assert(indices.size() == 0 || indices.size() - 1 == index_level);
1948
1949 size_t end_pos;
1950 to_set = smartLexicalCast<typename bit_reference_to_bool<T>::type>(this, str, end_pos);
1951 }
1952
1953
1954 // Multi-dimensional parameter indexed enlargement
1955
1972 template <typename T>
1973 typename std::enable_if<is_vector<typename std::remove_reference<T>::type>::value>::type
1974 resizeVectorsFromString_(T& vec,
1975 const std::vector<uint32_t>& indices,
1976 uint32_t index_level) {
1977 checkModificationPermission_();
1978 if(index_level == indices.size()){
1979 return; // Done. No further effect
1980 }
1981
1982 uint32_t idx = indices.at(index_level);
1983 if(idx >= vec.size()){
1984 vec.resize(idx+1);
1985 }
1986
1987 if(index_level < indices.size()){
1988 // Recurs to next index_level
1989 typename T::reference next_vec = vec.at(idx);
1990 resizeVectorsFromString_(next_vec, indices, index_level+1);
1991 }
1992 }
1993
2000 template <typename T>
2001 typename std::enable_if<!is_vector<typename std::remove_reference<T>::type>::value>::type
2002 resizeVectorsFromString_(T& vec,
2003 const std::vector<uint32_t>& indices,
2004 uint32_t index_level) {
2005 checkModificationPermission_();
2006 (void) vec;
2007 (void) index_level;
2008
2009 throw ParameterException("Cannot resize a vector in parameter \"")
2010 << getName() << "\" which is of type \""
2011 << getTypeName() << "\" to contain indices: " << indices << " (" << indices.size()
2012 << " levels)" << " because this type only has " << getDimensionality()
2013 << " dimensions. Therefore this index "
2014 "would be within a vector of scalars and this method has no idea with what "
2015 "value to initialize the new elements of said vector. Ues an indices vector "
2016 "with less than " << getDimensionality() << " elements";
2017 }
2018
2019 // Vector Clearing
2020
2028 template <typename T>
2029 static
2030 typename std::enable_if<is_vector<typename std::remove_reference<T>::type>::value>::type
2031 clearVectorValue_(T& vec) {
2032 vec.clear();
2033 }
2034
2041 template <typename T>
2042 static
2043 typename std::enable_if<!is_vector<typename std::remove_reference<T>::type>::value>::type
2044 clearVectorValue_(T& vec) {
2045 (void) vec;
2046
2047 // No effect
2048 }
2049
2050
2051 // Multi-dimensional parameter indexed reads
2052
2068 template <typename T>
2069 typename std::enable_if<is_vector<typename std::remove_reference<T>::type>::value, std::string>::type
2070 getVectorItemValueFromString_(const T& vec,
2071 const std::vector<uint32_t>& indices,
2072 uint32_t index_level,
2073 bool peek) const {
2074 assert(indices.size() > 0);
2075
2076 uint32_t idx = indices.at(index_level);
2077 if(idx >= vec.size()){
2078 throw ParameterException("Cannot get item from parameter \"")
2079 << getName() << "\" as a vector which is of type \""
2080 << getTypeName() << "\" with indices: " << indices << " (" << indices.size()
2081 << " levels)" << " because this type has only " << vec.size()
2082 << " elements at the vector located by indices[" << index_level << "]";
2083 }
2084
2085 if(indices.size() - 1 == index_level){
2086 // Final index should place a value in a vector
2087 // Note that vec is a vector
2088 typename T::const_reference to_get = vec.at(idx);
2089 return getFinalVectorItemValueFromString_(to_get, indices, index_level, peek);
2090 }else{
2091 // Non-final index should select a vector and recurse
2092 typename T::const_reference next_vec = vec.at(idx);
2093 return getVectorItemValueFromString_(next_vec, indices, index_level+1, peek);
2094 }
2095 }
2096
2102 template <typename T>
2103 typename std::enable_if<!is_vector<typename std::remove_reference<T>::type>::value, std::string>::type
2104 getVectorItemValueFromString_(const T& vec,
2105 const std::vector<uint32_t>& indices,
2106 uint32_t index_level,
2107 bool peek) const {
2108 (void) vec;
2109 (void) index_level;
2110 (void) peek;
2111
2112 throw ParameterException("Cannot get item from parameter \"")
2113 << getName() << "\" which is of type \""
2114 << getTypeName() << "\" with indices: " << indices << " (" << indices.size()
2115 << " levels)" << " because this type only has " << index_level << " dimensions";
2116 }
2117
2124 template <typename T>
2125 typename std::enable_if<is_vector<typename std::remove_reference<T>::type>::value, std::string>::type
2126 getFinalVectorItemValueFromString_(const T& to_get,
2127 const std::vector<uint32_t>& indices,
2128 uint32_t index_level,
2129 bool peek) const {
2130 (void) to_get;
2131 (void) peek;
2132
2133 throw ParameterException("Cannot get value from string on parameter \"")
2134 << getName() << "\" which is of type \""
2135 << getTypeName() << "\" with indices: " << indices << " (" << indices.size()
2136 << " levels)" << " because this type has more than " << index_level << " dimensions";
2137 }
2138
2153 template <typename T>
2154 typename std::enable_if<!is_vector<typename std::remove_reference<T>::type>::value, std::string>::type
2155 getFinalVectorItemValueFromString_(const T& to_get,
2156 const std::vector<uint32_t>& indices,
2157 uint32_t index_level,
2158 bool peek) const {
2159 assert(indices.size() == 0 || indices.size() - 1 == index_level);
2160
2161 if(!peek){
2163 }
2164
2165 return sparta::utils::stringize_value(to_get, disp_base_, string_quote_);
2166 }
2167
2168
2169 // Nested Vector size queries
2170
2189 template <typename T>
2190 typename std::enable_if<is_vector<typename std::remove_reference<T>::type>::value, uint32_t>::type
2191 getVectorSize_(const T& vec,
2192 const std::vector<uint32_t>& indices,
2193 uint32_t index_level,
2194 bool peek) const {
2195 assert(indices.size() > 0);
2196
2197 uint32_t idx = indices.at(index_level);
2198 if(idx >= vec.size()){
2199 throw ParameterException("Cannot get size of vector from parameter \"")
2200 << getName() << "\" as a vector which is of type \""
2201 << getTypeName() << "\" with indices: " << indices << " (" << indices.size()
2202 << " levels)" << " because this type has only " << vec.size()
2203 << " elements at the vector located by indices[" << index_level << "]";
2204 }
2205
2206 if(indices.size() - 1 == index_level){
2207 // Final index should place a value in a vector
2208 // Note that vec is a vector
2209 typename T::const_reference to_get = vec.at(idx);
2210 return getFinalVectorSize_(to_get, indices, index_level, peek);
2211 }else{
2212 // Non-final index should select a vector and recurse
2213 typename T::const_reference next_vec = vec.at(idx);
2214 return getVectorSize_(next_vec, indices, index_level+1, peek);
2215 }
2216 }
2217
2223 template <typename T>
2224 typename std::enable_if<!is_vector<typename std::remove_reference<T>::type>::value, uint32_t>::type
2225 getVectorSize_(const T& vec,
2226 const std::vector<uint32_t>& indices,
2227 uint32_t index_level,
2228 bool peek) const {
2229 (void) vec;
2230 (void) index_level;
2231 (void) peek;
2232
2233 throw ParameterException("Cannot get size of vector from parameter \"")
2234 << getName() << "\" which is of type \""
2235 << getTypeName() << "\" with indices: " << indices << " (" << indices.size()
2236 << " levels)" << " because this type only has " << index_level << " dimensions";
2237 }
2238
2244 template <typename T>
2245 typename std::enable_if<!is_vector<typename std::remove_reference<T>::type>::value, uint32_t>::type
2246 getFinalVectorSize_(const T& to_get,
2247 const std::vector<uint32_t>& indices,
2248 uint32_t index_level,
2249 bool peek) const {
2250 (void) to_get;
2251 (void) peek;
2252
2253 throw ParameterException("Cannot get size of vector from parameter \"")
2254 << getName() << "\" which is of type \""
2255 << getTypeName() << "\" with indices: " << indices << " (" << indices.size()
2256 << " levels)" << " because the in dimension " << index_level
2257 << " is a a scalar (not a vector)";
2258 }
2259
2271 template <typename T>
2272 typename std::enable_if<is_vector<typename std::remove_reference<T>::type>::value, uint32_t>::type
2273 getFinalVectorSize_(const T& to_get,
2274 const std::vector<uint32_t>& indices,
2275 uint32_t index_level,
2276 bool peek) const {
2277 assert(indices.size() == 0 || indices.size() - 1 == index_level);
2278
2279 if(!peek){
2280 incrementReadCount_(); // This is considered a read because this information can be useful
2281 }
2282
2283 return to_get.size();
2284 }
2285
2286
2287 // Type string computation
2288
2289 template <class T>
2290 typename std::enable_if<is_vector<T>::value, std::string>::type
2291 getTypeName_() const {
2292 std::stringstream ss;
2293 ss << "std::vector<";
2294 ss << getTypeName_<typename T::value_type>();
2295 ss << ">";
2296 return ss.str();
2297 }
2298
2299 template <class T>
2300 typename std::enable_if<!is_vector<T>::value, std::string>::type
2301 getTypeName_() const {
2302 if(KeyValue::hasTypeNameFor<T>()){
2303 return KeyValue::lookupTypeName<T>();
2304 }else{
2305 return demangle(typeid(T).name());
2306 }
2307 }
2308
2309
2310 // Vector value writes
2311 template <class T, class C1>
2312 typename std::enable_if<is_vector<C1>::value >::type
2313 setValueFromString_(const std::string& str, bool poke=false) {
2314 checkModificationPermission_();
2315 (void) str;
2316 (void) poke;
2317 throw ParameterException("Cannot set value from string on parameter \"")
2318 << getName() << "\" which is a vector type \""
2319 << getTypeName() << "\"";
2320 }
2321
2322 // Scalar value writes
2323 template <class T, class C1>
2324 typename std::enable_if<!is_vector<C1>::value >::type
2325 setValueFromString_(const std::string& str, bool poke=false) {
2326 checkModificationPermission_();
2327 ValueType tmp;
2328
2329 size_t end_pos;
2330 tmp = smartLexicalCast<ValueType>(this, str, end_pos);
2331
2332 if (!poke) {
2334 }
2335
2336 ValueType old_val = val_;
2337 val_ = tmp;
2338 try {
2340 }
2341 catch(sparta::SpartaException & e) {
2342 val_ = old_val;
2343 throw;
2344 }
2345 }
2346
2347
2348 // Vector value writes for DEFAULT value
2349 template <class T, class C1>
2350 typename std::enable_if<is_vector<C1>::value >::type
2351 overrideDefaultFromString_(const std::string& str) {
2352 checkModificationPermission_();
2353 (void) str;
2354 throw ParameterException("Cannot set default from string on parameter \"")
2355 << getName() << "\" which is a vector type \""
2356 << getTypeName() << "\"";
2357 }
2358
2359 // Scalar value writes for DEFAULT value
2360 template <class T, class C1>
2361 typename std::enable_if<!is_vector<C1>::value >::type
2362 overrideDefaultFromString_(const std::string& str) {
2363 checkModificationPermission_();
2364 ValueType tmp;
2365
2366 size_t end_pos;
2367 tmp = smartLexicalCast<ValueType>(this, str, end_pos);
2368 def_val_ = tmp;
2369 }
2370
2371
2372 // 1-d vector writes
2373
2374 template <class T, class C1> // C1=ValueType
2375 typename std::enable_if<!is_vector<C1>::value >::type
2376 setValueFromStringVector_(const std::vector<std::string>& vec, bool poke=false) {
2377 checkModificationPermission_();
2378 (void) vec;
2379 (void) poke;
2380 throw ParameterException("Cannot directly set value from string vector on parameter \"")
2381 << getName() << "\" which is a scalar (or string) type \""
2382 << getTypeName() << "\"";
2383 }
2384
2385 template <class T, class C1> // C1=ValueType
2386 typename std::enable_if<is_vector<C1>::value && !is_vector<typename C1::value_type>::value>::type
2387 setValueFromStringVector_(const std::vector<std::string>& vec, bool poke=false) {
2388 checkModificationPermission_();
2389 ValueType tmpvec;
2390 for(const std::string& s : vec){
2391 size_t end_pos;
2392 tmpvec.push_back(smartLexicalCast<typename ValueType::value_type>(this, s, end_pos));
2393 }
2394 if (!poke) {
2396 }
2397 ValueType old_val = val_;
2398 val_ = tmpvec;
2399 try {
2401 }
2402 catch(sparta::SpartaException & e) {
2403 val_ = old_val;
2404 throw;
2405 }
2406 }
2407
2408 template <class T, class C1> // C1=ValueType
2409 typename std::enable_if<is_vector<C1>::value && is_vector<typename C1::value_type>::value>::type
2410 setValueFromStringVector_(const std::vector<std::string>& vec, bool poke=false) {
2411 checkModificationPermission_();
2412 (void) vec;
2413 (void) poke;
2414 throw ParameterException("Cannot directly set value from string vector on parameter \"")
2415 << getName() << "\" which has " << getDimensionality() << " dimensions. Type is \""
2416 << getTypeName() << "\". Only 1-dimensional parameters can be set using this method";
2417 }
2418
2419
2420 // 1-d vector writes for DEFAULT value
2421
2422 template <class T, class C1> // C1=ValueType
2423 typename std::enable_if<!is_vector<C1>::value >::type
2424 overrideDefaultFromStringVector_(const std::vector<std::string>& vec) {
2425 checkModificationPermission_();
2426 (void) vec;
2427
2428 throw ParameterException("Cannot directly override default value from string vector on parameter \"")
2429 << getName() << "\" which is a scalar (or string) type \""
2430 << getTypeName() << "\"";
2431 }
2432
2433 template <class T, class C1> // C1=ValueType
2434 typename std::enable_if<is_vector<C1>::value && !is_vector<typename C1::value_type>::value>::type
2435 overrideDefaultFromStringVector_(const std::vector<std::string>& vec) {
2436 checkModificationPermission_();
2437 ValueType tmpvec;
2438 for(const std::string& s : vec){
2439 size_t end_pos;
2440 tmpvec.push_back(smartLexicalCast<typename ValueType::value_type>(this, s, end_pos));
2441 }
2442 def_val_ = tmpvec;
2443 }
2444
2445 template <class T, class C1> // C1=ValueType
2446 typename std::enable_if<is_vector<C1>::value && is_vector<typename C1::value_type>::value>::type
2447 overrideDefaultFromStringVector_(const std::vector<std::string>& vec) {
2448 checkModificationPermission_();
2449 (void) vec;
2450
2451 throw ParameterException("Cannot directly set value from string vector on parameter \"")
2452 << getName() << "\" which has " << getDimensionality() << " dimensions. Type is \""
2453 << getTypeName() << "\". Only 1-dimensional parameters can be set using this method";
2454 }
2455
2456
2457 // 1-d vector reads
2458
2459 template <class T, class C1> // C1=ValueType
2460 typename std::enable_if<!is_vector<C1>::value, std::string>::type
2461 getValueAsStringAt_(size_t idx, bool peek) const {
2462 if(idx != 0){
2463 ParameterException ex("Cannot get value as string at index other than 0 on parameter \"");
2464 ex << getName() << "\" which is a scalar (or string) type \""
2465 << getTypeName() << "\"";
2466 throw ex;
2467 }
2468 if(!peek){
2470 }
2471 return getValueAsString();
2472 }
2473
2474 template <class T, class C1> // C1=ValueType
2475 typename std::enable_if<is_vector<C1>::value, std::string>::type
2476 getValueAsStringAt_(size_t idx, bool peek) const {
2477 if(!peek){
2479 }
2480 return sparta::utils::stringize_value(val_.at(idx), disp_base_, string_quote_);
2481 }
2482
2483
2484 // 1-d vector inspection
2485
2486 template <class T, class C1> // C1=ValueType
2487 typename std::enable_if<!is_vector<C1>::value, size_t>::type
2488 getNumValues_(bool peek) const {
2489 (void) peek;
2490 // DO NOT count this as a read access. Nothing can be deduced about
2491 // the parameter from this information since this is a scalar
2492 return 1;
2493 }
2494
2495 template <class T, class C1> // C1=ValueType
2496 typename std::enable_if<is_vector<C1>::value, size_t>::type
2497 getNumValues_(bool peek) const {
2498 // Getting number of values counts as reading since it *might* be
2499 // informative
2500 if(!peek){
2502 }
2503 return val_.size();
2504 }
2505
2506 // Parameter Dimensionality
2507
2511 template<typename T, typename Enable = void>
2512 struct Dimensionality {
2513 };
2514
2520 template<typename T>
2521 struct Dimensionality<T, typename std::enable_if<is_vector<T>::value>::type> {
2522 enum { value = Dimensionality<typename T::value_type>::value + 1};
2523 };
2524
2528 template<typename T>
2529 struct Dimensionality<T, typename std::enable_if<!is_vector<T>::value>::type> {
2530 enum { value = 0 }; // T is a scalar type, with 0 dimensions
2531 };
2532
2533 }; // class Parameter
2534
2535
2536 // Implementation
2537
2538 template <class T>
2539 inline const T ParameterBase::getValueAs() const {
2540 const Parameter<T>* p = dynamic_cast<const Parameter<T>*>(this);
2541 if(nullptr == p){
2542 throw ParameterException("Cannot get value from Parameter \"")
2543 << getName() << "\" as a " << demangle(typeid(T).name())
2544 << " because it is internally a " << getTypeName()
2545 << ". getValueAs must be exact";
2546 }
2547 return *p;
2548 }
2549
2550 template <typename T>
2552 const std::string& s,
2553 size_t& end_pos,
2554 bool allow_recursion,
2555 bool allow_prefix)
2556 {
2557 try{
2558 return utils::smartLexicalCast<T>(s, end_pos, allow_recursion, allow_prefix);
2559 }catch(SpartaException& ex){
2560 ex << " in parameter " << p->getLocation();
2561 throw;
2562 }
2563 }
2564
2565} // namespace sparta
String-to-value helpers and string formatting helpers.
Basic Node framework in sparta device tree composite pattern.
Helpers for printing and populating vectors.
std::string stringize_value(const std::vector< T > &v, DisplayBase base=BASE_DEC, const std::string &string_quote="")
Converting a vector of intrinsic types (and std::string) to a string.
Definition Printing.hpp:80
DisplayBase
Numeric display options used by Parameter printing routines.
Definition Printing.hpp:27
Smart lexical casting supporting prefixes, separator ignoring, and suffixes.
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.
File that contains the macro used to generate the class callbacks.
Basic Node framework in sparta device tree composite pattern.
Boolean with a default capable of being changed to the opposite value only. it can never set to the d...
Definition Utils.hpp:355
Generic value iterator for a SINGLE parameter which represents values ONLY as std::string.
bool operator!=(const ParameterValueIterator &rhp)
Inequality test.
bool operator==(const ParameterValueIterator &rhp)
Equality test.
ParameterValueIterator(const ParameterBase *pb, size_t idx)
Full Constructor.
ParameterValueIterator(const ParameterValueIterator &rhp)
Copy Constructor.
const std::string operator*() const
Dereference operator.
const ParameterValueIterator & operator++()
Preincrement operator.
const ParameterValueIterator operator++(int i)
Postincrement operator.
Non-templated base class for generic parameter access and iteration.
virtual void overrideDefaultFromStringVector(const std::vector< std::string > &val)=0
Sets the default value of this vector parameter for architecture baseline configuration purposes.
void resetReadCount_() const
Resets the number of reads that wil be reported by getReadCount.
void restoreValueFromDefault()
Attempts to restore the devalue value of this parameter.
virtual std::string stringize(bool pretty=false) const override
Render description of this parameter as a string.
void incrementReadCount_() const
Increment the number of reads that will be reported by getReadCount.
virtual std::string getItemValueFromString(const std::vector< uint32_t > &indices, bool peek=false) const =0
Gets the current value of a single element within this parameter of the parameter as if this paramete...
virtual bool isVector() const =0
Determines whether this Parameter is a vector or a scalar Parameter.
virtual void clearVectorValue()=0
If the parameter is a vector type, clears the value so that it becomes an empty vector (regardless of...
virtual size_t getNumValues(bool peek=false) const =0
Gets the number of elements contained in this Parameter as a Vector.
virtual bool supportsCompression() const
Scalar parameters may compress well, but we cannot really make a strong enough determination without ...
bool isVolatile() const
Is this a volatile parmaeter?
virtual void resizeVectorsFromString(const std::vector< uint32_t > &indices)=0
Attempt to resize a vector nested within this parameter to contain the vector indicated by indices.
const T getValueAs() const
Gets the value of this ParameterBase as a templated type T if this parameter actually contains a valu...
std::string peekItemValueFromString(const std::vector< uint32_t > &indices) const
Wrapper for getItemValueFromString with peek=true.
static void logCurrentBackTrace_()
Log the current backtrace to the global parameters logger.
virtual void overrideDefaultClearVectorValue()=0
If the parameter is a vector type, clears the default value so that it becomes an empty vector (regar...
virtual std::string getValueAsStringAt(size_t idx, bool peek=false) const =0
Gets the current value of this Parameter as a string at a particular index as if this Parameter were ...
class sparta::ParameterBase::ParameterValueIterator const_iterator
Generic value iterator for a SINGLE parameter which represents values ONLY as std::string.
virtual void restoreValueFromDefaultImpl_()=0
Implements restoreValueFromDefault.
void setValueFromString(const std::string &str, bool poke=false)
Attempts to assign a value to this non-vector Parameter from a string.
uint32_t getWriteCount() const
Number of times this Parameter has been written after initialization.
size_t peekNumValues() const
Wrapper for getNumValues with peek=true.
virtual bool equals(const ParameterBase &other)=0
Returns true if the value of this equals other.
virtual double getDoubleValue() const =0
Gets the value of this parameter as a double. \thwo Exception if underlying parameter type is not num...
void associateParametersForModification(std::vector< const ParameterBase * > params, const sparta::SpartaHandler &modifier_callback)
Associate a parameter with this parameter for future modification.
void setItemValueFromString(const std::vector< uint32_t > &indices, const std::string &str)
Attempts to assign a value to this nested vector Parameter from a string at a position within the vec...
virtual const_iterator begin() const =0
Gets a beginning const_iterator for values of this Parameter.
virtual uint32_t getDimensionality() const =0
Determines the number of dimensions of this Parameter. A scalar has 0 dimensions. A parameter of type...
void incrementWriteCount_()
Increments the number of writes that will be reported by getWriteCount.
virtual bool isDefaultOverridden() const =0
Has the default value (NOT the current value) for parameter been overridden in any way (including par...
bool isReadOrIgnored() const
Is this parameter ignored or read at least once (barring any reset of the read count or ignore flag)
virtual ~ParameterBase()
Destructor.
virtual bool validateIndependently(std::string &err_names) const =0
Performs validation independently of all other Parameters.
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.
sparta::SpartaHandler modifier_callback_
Modifier callback called when the parameter is written.
virtual void setValueFromStringVectorImpl_(const std::vector< std::string > &str, bool poke=false)=0
Implements setValueFromStringVectorImpl_.
void setIsVolatile()
Set volatile flag (allows write after read)
void resetWriteCount_()
Resets the number of writes that wil be reported by getWriteCount.
ParameterBase(const std::string &name, const std::string &desc)
Constructor.
virtual void overrideDefaultFromString(const std::string &val)=0
Sets the default value of this non-vector parameter for architecture baseline configuration purposes.
void logLoadedDefaultValue_() const
Log the default loaded to this parameter to the global parameters logger for debugging.
virtual void overrideDefaultItemValueFromString(const std::vector< uint32_t > &indices, const std::string &str)=0
Partially override the default default value in some element at an n-dimensional array specified.
bool isIgnored() const
Has this parameter been ignored (without having read count reset after)
void unread_() const
Mark this parameter as unread and unignored This is used or preloading defaults to parameters and the...
virtual uint32_t getVectorSizeAt(const std::vector< uint32_t > &indices, bool peek=false) const =0
Determines the size of a vector contained by this parameter at the location specified by indices.
virtual void setValueFromStringImpl_(const std::string &s, bool poke=false)=0
Implements setValueFromString.
virtual void overrideDefaultResizeVectorsFromString(const std::vector< uint32_t > &indices)=0
Override the default value by clearing the possibly-nested vector (if this parameter is a vector)....
void logAssignedValue_() const
Log the most recently assigned value given to this parameter to the global parameters logger for debu...
void setValueFromStringVector(const std::vector< std::string > &str, bool poke=false)
Attempts to assign a value to this vector Parameter from a string.
virtual bool validateDependencies(const TreeNode *node, std::string &err_names) const =0
Performs validation based on other Parameters in the Device Tree.
bool usingFinalConfig_()
Ask the simulator if we are using a final config.
void invokeModifierCB_()
Invoke any register callbacks for this parameter. These callbacks are used by Parameters to make modi...
uint32_t peekVectorSizeAt(const std::vector< uint32_t > &indices) const
Wrapper of getVectorSizeAt with peek=true.
std::string string_quote_
The quote sequence for printing strings. Defaults to empty string.
virtual const_iterator end() const =0
Gets an ending const_iterator for values of this Parameter.
void addToSet_(ParameterSet *ps)
Add this parameter to a set - an action which is protected and requires the friendship that the Param...
uint32_t getReadCount() const
Number of times this Parameter has been read after initialization or after the last write (or explici...
virtual void setItemValueFromStringImpl_(const std::vector< uint32_t > &indices, const std::string &str)=0
Implements setItemValueFromStringImpl_.
virtual bool isDefault() const
Is this parameter's current value the default value.
bool ignored_
Has this parameter been ignored. Resettable. Mutable so that it can be accessed from a const Parmeter...
static constexpr char PARAMETER_NODE_TAG[]
Tag added to Parameter nodes.
void ignore_() const
Flag as ignored. See Parameter<T>::ignore.
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.
std::string setStringQuote(const std::string &s)
Set the quote sequence for printing strings.
Exception indicating a misconfigured Parameter or invalid Parameter access.
Definition Parameter.hpp:58
ParameterException(const std::string &reason)
Construct with a default string.
Definition Parameter.hpp:71
ParameterException(const ParameterException &)=default
Default copy (rule of 3)
ParameterException()=default
Default construction (rule of 3)
ParameterException & operator<<(const T &msg)
Wrapper around SpartaException::operator<<.
Definition Parameter.hpp:84
virtual ~ParameterException() noexcept
Destructor.
Definition Parameter.hpp:78
Generic container of Parameters.
Parameter instance, templated to contain only a specific type.
sparta::utils::DisplayBase getNumericDisplayBase() const
Gets the numeric base for displaying the value of this parameter.
void operator=(const Parameter &p)=delete
Copy assignment - deleted explicitly.
virtual void overrideDefaultResizeVectorsFromString(const std::vector< uint32_t > &indices) override final
Override the default value by clearing the possibly-nested vector (if this parameter is a vector)....
ValueType getDefault() const
Returns the default value.
std::enable_if<!std::is_base_of< ParameterBase, T >::value, bool >::type operator==(const T rhp) const
Increments read count.
void operator=(const ValueType &v)
Assigns the specified value to this parameter.
virtual void restoreValueFromDefaultImpl_() override final
Implements restoreValueFromDefault.
void addDependentValidationCallback(T *obj, const std::string &name)
Adds dependency callback to a class member function.
virtual bool isVisibilityAllowed() const override
Query if this parameter is safe to be displayed via prints, dumps. A parameter should not be displaye...
virtual void setItemValueFromStringImpl_(const std::vector< uint32_t > &indices, const std::string &str) override final
Implements setItemValueFromStringImpl_.
virtual void overrideDefaultItemValueFromString(const std::vector< uint32_t > &indices, const std::string &str) override final
Partially override the default default value in some element at an n-dimensional array specified.
uint32_t getVectorSizeAt(const std::vector< uint32_t > &indices, bool peek=false) const override final
Get the size of a nested vector within the parameter located by indices.
std::string getValueAsStringAt(size_t idx, bool peek=false) const override final
Treats this parameter as a vector and gets the value as a string at a specific index....
const ValueType & getValue() const
Gets the current value of this Parameter.
bool validateDependencies(const TreeNode *node, std::string &err_names) const override
Invokes all validation callbacks for a particular node in the device tree and returns true if none of...
ParameterAttribute
ParameterAttribute enum class which describes special attributes of this parameter.
virtual void resizeVectorsFromString(const std::vector< uint32_t > &indices) override final
Attempt to resize a vector nested within this parameter to contain the vector indicated by indices.
virtual void setValueFromStringImpl_(const std::string &str, bool poke=false) override final
Implements setValueFromString.
virtual ParameterBase::const_iterator end() const override final
Get begin iterator.
virtual std::string getDefaultAsString() const override final
Returns the default value as a string, even if type is a vector.
virtual uint32_t getDimensionality() const override final
Determines the number of dimensions of this Parameter. A scalar has 0 dimensions. A parameter of type...
virtual bool isDefaultOverridden() const override final
Has the default value (NOT the current value) for parameter been overridden in any way (including par...
virtual const std::string getTypeName() const override final
Gets the human-readable name of this parameter's type.
bool operator<=(const Parameter< type > &rhp) const
Compares two Parameter objects by value.
std::enable_if<!std::is_base_of< ParameterBase, T >::value, bool >::type operator!=(const T rhp) const
Increments read count.
virtual bool isVector() const override final
Is this parameter a vector?
virtual void clearVectorValue() override final
If the parameter is a vector type, clears the value so that it becomes an empty vector (regardless of...
virtual ParameterBase::const_iterator begin() const override final
Get begin iterator.
const ValueType & peekValue() const
Gets the current value of this Parameter without incrementing the read count. This should be used whe...
std::enable_if<!std::is_base_of< ParameterBase, T >::value, bool >::type operator>=(const T rhp) const
Increments read count.
void ignore() const
Marks this parameter as ignored.
const ValueType & getValue_() const
Iternal getValue_ wrapper which does not increment the read counter.
virtual void setValueFromStringVectorImpl_(const std::vector< std::string > &vec, bool poke=false) override final
Implements setValueFromStringVectorImpl_.
virtual bool equals(const ParameterBase &other) override final
Returns true if the value of this equals other.
Parameter< ValueType > & operator<<(U e)
Helper for constructing vectors.
std::string getItemValueFromString(const std::vector< uint32_t > &indices, bool peek=false) const override final
Override from ParameterBase.
const ValueType & operator()() const
Gets the value currently held by this Parameter.
Parameter(const std::string &name, const ValueType &def, const std::string &doc, bool isvolatile=false)
Construct a parameter.
double getDoubleValue() const override final
Cast value to double if possible. Throw if not.
virtual size_t getNumValues(bool peek=false) const override final
Gets the number of values in this Parameter.
void unread() const
Mark this parameter as unread and unignored.
Parameter(const std::string &name, const ValueType &def, const std::string &doc, ParameterSet *ps, bool isvolatile=false)
Constructor used by the PARAMETER macro.
virtual void overrideDefaultFromStringVector(const std::vector< std::string > &vec) override final
Sets the default value of this vector parameter for architecture baseline configuration purposes.
bool operator>=(const Parameter< T > &rhp) const
Compares two Parameter objects by value.
sparta::utils::DisplayBase setNumericDisplayBase(sparta::utils::DisplayBase base)
Set the numeric base for displaying the value of this parameter.
virtual void overrideDefaultClearVectorValue() override final
If the parameter is a vector type, clears the default value so that it becomes an empty vector (regar...
virtual ~Parameter()=default
Destructor.
std::string getValueAsString() const override final
Returns value as a string, even if type is a vector.
bool operator<(const Parameter< type > &rhp) const
Compares two Parameter objects by value.
bool operator!=(const Parameter< T > &rhp) const
Compares two Parameter objects by value.
ValueType type
Type held by this parameter. This cannot change at run-time.
virtual void overrideDefaultFromString(const std::string &str) override final
Sets the default value of this non-vector parameter for architecture baseline configuration purposes.
bool validateIndependently(std::string &err_names) const override
Performs validation independently of all other Parameters.
bool operator==(const Parameter< T > &rhp) const
Compares two Parameter objects by value.
void addDependentValidationCallback(bool(*method)(ValueType &, const sparta::TreeNode *), const std::string &name)
Adds dependency callback via a global function or lambda.
bool operator>(const Parameter< type > &rhp) const
Compares two Parameter objects by value.
std::enable_if<!std::is_base_of< ParameterBase, T >::value, bool >::type operator>(const T rhp) const
Increments read count.
TreePhase getPhase() const
Gets the trees current phase.
@ TREE_FINALIZED
Tree and all resources have been instantiated. No more configuration/connection allowed.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
SpartaException & operator<<(const T &msg)
Append additional information to the message.
Node in a composite tree representing a sparta Tree item.
Definition TreeNode.hpp:205
std::string getLocation() const override final
void markHidden(bool hidden=true)
Marks this TreeNode hidden for the purposes of printint out nodes. This does not make the node inacce...
bool areParametersLocked_() const
This method informs whether the tree is past the lockdown phase for all LOCKED and HIDDEN parameters....
virtual std::string stringize(bool pretty=false) const
Create a string representation of this node.
Definition TreeNode.hpp:723
const std::string & getName() const override
Gets the name of this node.
bool isHidden() const
Is this TreeNode supposed to be hidden during tree printouts This value does not have to be respected...
void addTag(const std::string &tag)
Adds single tag to this node.
Delegate for Parameter validation.
ValidationCheckCallback(bool(*method)(ValueType &, const sparta::TreeNode *), const std::string &name)
Construct delegate with a class member pointer.
ValidationCheckCallback()
Construct with no functionality.
bool operator()(ValueType &val, const TreeNode *node) const
Invoke callback to check given value at the indicated position in the device tree.
static void validateName(const std::string &nm)
ValidationCheckCallback(const std::string &name)
Construct with no functionality and a name.
ValidationCheckCallback(const ValidationCheckCallback< ValueType > &rhp)
Copy Constructor.
Macros for handling exponential backoff.
T smartLexicalCast(const ParameterBase *p, const std::string &s, size_t &end_pos, bool allow_recursion=true, bool allow_prefix=true)
smartLexicalCast wrapper with parameter information added to exceptions
std::string demangle(const std::string &name) noexcept
Demangles a C++ symbol.
Definition Utils.hpp:203
void bind(Bus *p1, Bus *p2)
Bind two buses together.
Definition Bus.hpp:333
Templated for determining if ValueType is std::vector for use in metaprogramming constructs....
Definition Utils.hpp:236
static const bool value
'value' field used by enable_if construct.
Definition Utils.hpp:242