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
815
821 void resetReadCount_() const {
822 reads_ = 0;
823 }
824
832 writes_ = 0;
833 }
834
838 void ignore_() const {
839 ignored_ = true;
840 }
841
846
860 // Writing is illegal if the owning node already has a resource.
861 // This is not how this SHOULD work - all configuration should be
862 // done during configuration. However, dynamically created nodes
863 // need to be configured
864 //TreeNode* pset = getParent();
865 //sparta_assert(// pset != nullptr, "Cannot write a parameter that does not belong to a ParameterSet");
866 //TreeNode* owner = pset->getParent();
867 //if(owner){
868 // if(owner->getPhase() > TREE_CONFIGURING
869 // && owner->hasResource()){
870 // throw SpartaException("Cannot write to Parameter ")
871 // << getLocation() << " because the associated TreeNode ("
872 // << owner->getLocation() << ") has already created a resource";
873 // }
874 //}
875
876 // Write-after read is prohibited
877 if(isVolatile() == false && reads_ > 0){
878 throw SpartaException("Cannot write parameter ") << getLocation()
879 << " after reading it unless it is a volatile parameter";
880 }
881
882 // Writing is illegal once tree is configured
883 if(getPhase() > TREE_FINALIZED){
884 throw SpartaException("Cannot write to Parameter ")
885 << getLocation() << " because it is already finalized";
886 }
887
889 ++writes_;
890 }
891
897
902 void logAssignedValue_() const;
903
907 static void logCurrentBackTrace_();
908
916
920 friend class ParameterSet;
921
925 sparta::SpartaHandler modifier_callback_;
926
931 mutable bool ignored_;
932
936 std::string string_quote_;
937
938 private:
939
943 std::string name_;
944
948 std::string desc_;
949
953 uint32_t writes_;
954
959 mutable uint32_t reads_;
960
964 std::vector<const ParameterBase *> associated_params_;
965
969 bool is_volatile_;
970
973
974 }; // class ParameterBase
975
992 template<class ValueType>
994 {
996 std::function<bool(ValueType&, const sparta::TreeNode*)> callback_;
997
998 std::string name_;
999
1000 public:
1001
1002 template<class T, bool (T::*TMethod)(ValueType&, const sparta::TreeNode*)>
1003 static ValidationCheckCallback<ValueType> from_method(T* obj,
1004 const std::string& name)
1005 {
1006 (void) obj;
1008 vcc.callback_ = std::bind(TMethod, obj, std::placeholders::_1, std::placeholders::_2);
1009 return vcc;
1010
1011 // This is not valid because the template parameter TMethod cannot be deduced
1012 //return ValidationCheckCallback<ValueType>(obj, name);
1013 }
1014
1028 /*
1029 template<class T, bool (T::*TMethod)(ValueType)>
1030 ValidationCheckCallback(T* obj, const std::string& name) :
1031 callback_(std::bind(TMethod, obj, std::placeholders::_1)),
1032 name_(name)
1033 {
1034 validateName(name_);
1035 }*/
1036
1038 ValidationCheckCallback(bool (*method)(ValueType&, const sparta::TreeNode*),
1039 const std::string& name) :
1040 callback_(std::bind(method, std::placeholders::_1, std::placeholders::_2)),
1041 name_(name)
1042 {
1043 validateName(name_);
1044 }
1045
1048 callback_(std::bind(&ValidationCheckCallback<ValueType>::doNothing_, std::placeholders::_1, std::placeholders::_2)),
1049 name_("<uninitialized>")
1050 {
1051 validateName(name_);
1052 }
1053
1055 ValidationCheckCallback(const std::string& name) :
1056 callback_(std::bind(&ValidationCheckCallback<ValueType>::doNothing_, std::placeholders::_1, std::placeholders::_2)),
1057 name_(name)
1058 {
1059 validateName(name_);
1060 }
1061
1064 callback_(rhp.callback_),
1065 name_(rhp.name_)
1066 {
1067 validateName(name_);
1068 }
1069
1070 void operator=(const ValidationCheckCallback<ValueType>& rhp)
1071 {
1072 callback_ = rhp.callback_;
1073 name_ = rhp.name_;
1074 }
1075
1076 std::string getName() const
1077 {
1078 return name_;
1079 }
1080
1082 bool operator()(ValueType& val, const TreeNode* node) const
1083 {
1084 return callback_(val, node);
1085 }
1086
1089 static void validateName(const std::string& nm)
1090 {
1091 if(nm.find(',') != std::string::npos){
1092 ParameterException ex("ValidationCheckCallback name \"");
1093 ex << nm << "\" contains a comma, which is not permitted";
1094 throw ex;
1095 }
1096 }
1097
1098 private:
1099
1101 static bool doNothing_(ValueType& val, const TreeNode* node)
1102 {
1103 (void) val;
1104 (void) node;
1105 return true;
1106 }
1107 };
1108
1117 template <typename ValueType>
1119 {
1120 public:
1121
1126 enum class ParameterAttribute : std::uint8_t {
1127 DEFAULT = 0,
1128 __FIRST = DEFAULT,
1129 LOCKED = 1,
1130 HIDDEN = 2,
1131 __LAST
1132 };
1133
1134 private:
1135 OneWayBool<false> default_override_;
1136 ParameterAttribute param_attr_ {ParameterAttribute::DEFAULT};
1137 ValueType def_val_;
1138 ValueType val_;
1139 sparta::utils::DisplayBase disp_base_;
1140 std::vector<ValidationCheckCallback<ValueType> > bounds_;
1141 std::vector<ValidationCheckCallback<ValueType> > dependencies_;
1142
1143 public:
1145 using type = ValueType;
1146 using value_type = ValueType; // only for passing regression
1147
1160 Parameter(const std::string& name,
1161 const ValueType& def,
1162 const std::string& doc,
1163 bool isvolatile = false) :
1164 ParameterBase(name, doc),
1165 def_val_(def),
1166 val_(def),
1167 disp_base_(sparta::utils::BASE_DEC)
1168 {
1169 //std::cout << name << " = " << typeid(ValueType).name() << std::endl;
1170
1172
1173 if(isvolatile){
1174 setIsVolatile();
1175 }
1176 }
1177
1183 Parameter(const std::string& name,
1184 const ValueType& def,
1185 const std::string& doc,
1186 ParameterSet * ps,
1187 bool isvolatile = false) :
1188 Parameter (name, def, doc, isvolatile)
1189 {
1190 sparta_assert(ps, "Must construct parameter " << name << " with valid ParameterSet");
1191 addToSet_(ps);
1192 }
1193
1194 Parameter(const std::string& name,
1195 const ValueType& def,
1196 const std::string& doc,
1197 const ParameterAttribute& attr,
1198 ParameterSet* ps,
1199 bool isvolatile = false) :
1200 Parameter(name, def, doc, isvolatile)
1201 {
1202 sparta_assert(ps, "Must construct parameter " << name << " with valid ParameterSet");
1203 param_attr_ = attr;
1204 if(param_attr_ == ParameterAttribute::HIDDEN) {
1205 markHidden(true);
1206 }
1207 addToSet_(ps);
1208 }
1209
1211 virtual ~Parameter() = default;
1212
1230 template<class T, bool (T::*TMethod)(ValueType&, const sparta::TreeNode*)>
1231 void addDependentValidationCallback(T* obj, const std::string& name)
1232 {
1233 dependencies_.push_back(ValidationCheckCallback<ValueType>::template from_method<T, TMethod>(obj, name));
1234
1235 // Not allowed: Cannot resolve template arguments
1236 //dependencies_.push_back(ValidationCheckCallback<ValueType>(obj, name));
1237 }
1238
1259 void addDependentValidationCallback(bool (*method)(ValueType&, const sparta::TreeNode*),
1260 const std::string& name)
1261 {
1262 dependencies_.push_back(ValidationCheckCallback<ValueType>(method, name));
1263 }
1264
1265
1270 virtual const std::string getTypeName() const override final{
1271 return Parameter::template getTypeName_<ValueType>();
1272 }
1273
1276 ValueType getDefault() const {
1277 return def_val_;
1278 }
1279
1286 virtual std::string getDefaultAsString() const override final {
1287 return sparta::utils::stringize_value(def_val_, disp_base_, string_quote_);
1288 }
1289
1297 std::string getValueAsString() const override final {
1299 return sparta::utils::stringize_value(val_, disp_base_, "\"");
1300 }
1301 return sparta::utils::stringize_value(val_, disp_base_, string_quote_);
1302 }
1303
1312 std::string getValueAsStringAt(size_t idx, bool peek=false) const override final {
1313 return Parameter::template getValueAsStringAt_<ValueType, ValueType>(idx, peek);
1314 }
1315
1321 std::string getItemValueFromString(const std::vector<uint32_t>& indices,
1322 bool peek=false) const override final {
1323 if(indices.size() == 0){
1324 // No indices given. This must be a non-vector type
1325 return getFinalVectorItemValueFromString_(val_, indices, 0, peek);
1326 }
1327
1328 // Handle if this is a vector
1329 return getVectorItemValueFromString_(val_, indices, 0, peek);
1330 }
1331
1338 uint32_t getVectorSizeAt(const std::vector<uint32_t>& indices,
1339 bool peek=false) const override final {
1340 if(indices.size() == 0){
1341 // No indices given. This must be a non-vector type
1342 return getFinalVectorSize_(val_, indices, 0, peek);
1343 }
1344
1345 // Handle if this is a vector
1346 return getVectorSize_(val_, indices, 0, peek);
1347 }
1348
1353 operator const ValueType&() const {
1354 return getValue();
1355 }
1356
1361 const ValueType& operator()() const {
1362 return getValue();
1363 }
1364
1380 void ignore() const {
1381 ignore_();
1382 }
1383
1394 void unread() const {
1395 unread_();
1396 }
1397
1403 const ValueType& getValue() const {
1405 return getValue_();
1406 }
1407
1420 const ValueType& peekValue() const {
1421 return getValue_();
1422 }
1423
1427 double getDoubleValue() const override final {
1428 return getDoubleValue_<ValueType>();
1429 }
1430
1438 virtual size_t getNumValues(bool peek=false) const override final {
1439 return Parameter::template getNumValues_<ValueType, ValueType>(peek);
1440 }
1441
1446 virtual bool isVector() const override final {
1448 }
1449
1450 // Implements ParameterBase::getDimensionality
1451 virtual uint32_t getDimensionality() const override final {
1452 return Dimensionality<ValueType>::value;
1453 }
1454
1459 virtual ParameterBase::const_iterator begin() const override final{
1460 return ParameterBase::const_iterator(this, 0);
1461 }
1462
1467 virtual ParameterBase::const_iterator end() const override final{
1469 }
1470
1475 template <class T>
1476 bool operator==(const Parameter<T>& rhp) const {
1477 return getValue() == rhp.getValue();
1478 }
1479
1484 template <class T>
1485 bool operator!=(const Parameter<T>& rhp) const {
1486 return getValue() != rhp.getValue();
1487 }
1488
1493 bool operator>(const Parameter<type>& rhp) const {
1494 return getValue() > rhp.getValue();
1495 }
1496
1501 template <class T>
1502 bool operator>=(const Parameter<T>& rhp) const {
1503 return getValue() >= rhp.getValue();
1504 }
1505
1510 bool operator<(const Parameter<type>& rhp) const {
1511 return getValue() < rhp.getValue();
1512 }
1513
1518 bool operator<=(const Parameter<type>& rhp) const {
1519 return getValue() <= rhp.getValue();
1520 }
1521
1523 template <class T>
1524 typename std::enable_if<!std::is_base_of<ParameterBase, T>::value, bool>::type
1525 operator==(const T rhp) const {
1526 return getValue() == (ValueType)rhp;
1527 }
1528
1530 template <class T>
1531 typename std::enable_if<!std::is_base_of<ParameterBase, T>::value, bool>::type
1532 operator!=(const T rhp) const {
1533 return getValue() != (ValueType)rhp;
1534 }
1535
1537 template <class T>
1538 typename std::enable_if<!std::is_base_of<ParameterBase, T>::value, bool>::type
1539 operator>(const T rhp) const {
1540 return getValue() > (ValueType)rhp;
1541 }
1542
1544 template <class T>
1545 typename std::enable_if<!std::is_base_of<ParameterBase, T>::value, bool>::type
1546 operator>=(const T rhp) const {
1547 return getValue() >= (ValueType)rhp;
1548 }
1549
1551 template <class T>
1552 typename std::enable_if<!std::is_base_of<ParameterBase, T>::value, bool>::type
1553 operator<(const T rhp) const {
1554 return getValue() < (ValueType)rhp;
1555 }
1556
1558 template <class T>
1559 typename std::enable_if<!std::is_base_of<ParameterBase, T>::value, bool>::type
1560 operator<=(const T rhp) const {
1561 return getValue() <= (ValueType)rhp;
1562 }
1563
1576 void operator=(const Parameter& p) = delete;
1577
1590 void operator=(const ValueType& v) {
1591 checkModificationPermission_();
1592 // if the simulator was setup using --read-final-config
1593 // we do not allow the simulator to override values
1594 // so just return out and warn that we are skipping this.
1595 if (usingFinalConfig_() && (false == isHidden()))
1596 {
1597 static bool been_warned = false;
1598 if(!been_warned) {
1599 std::cout << "WARNING: A simulator override for parameter " << getLocation()
1600 << " (using operator=) was performed and ignored. This is because"
1601 << " the simulator is using --read-final-config. This is your"
1602 << " first and last warning." << std::endl;
1603 been_warned = true;
1604 }
1605 return;
1606 }
1607
1609 ValueType old_val = val_;
1610 val_ = v;
1612 try {
1614 }
1615 catch(sparta::SpartaException & e) {
1616 val_ = old_val;
1617 throw;
1618 }
1619 }
1620
1621 virtual bool isDefaultOverridden() const override final {
1622 return default_override_;
1623 }
1624
1625 virtual void overrideDefaultFromString(const std::string& str) override final {
1626 Parameter::template overrideDefaultFromString_<ValueType, ValueType>(str);
1628 "Cannot override default on parameter if read count is > 0. "
1629 "Problem on parameter " << getLocation());
1630 default_override_ = true;
1631 }
1632
1633 virtual void overrideDefaultFromStringVector(const std::vector<std::string>& vec) override final {
1634 Parameter::template overrideDefaultFromStringVector_<ValueType, ValueType>(vec);
1636 "Cannot override default on parameter if read count is > 0. "
1637 "Problem on parameter " << getLocation());
1638 default_override_ = true;
1639 }
1640
1641 virtual void overrideDefaultItemValueFromString(const std::vector<uint32_t>& indices,
1642 const std::string& str) override final {
1643 if(indices.size() == 0){
1644 // No indices given. This must be a non-vector type
1645 Parameter::template overrideDefaultFromString_<ValueType, ValueType>(str);
1646 }else{
1647 // handle if this is a vector
1648 const bool incr_write_count = false;
1649 setVectorItemValueFromString_(def_val_, indices, 0, str, incr_write_count);
1650 }
1651 default_override_ = true;
1652 }
1653
1654 virtual void overrideDefaultResizeVectorsFromString(const std::vector<uint32_t>& indices) override final {
1655 if(indices.size() == 0){
1656 return; // No effect
1657 }
1658
1659 resizeVectorsFromString_(def_val_, indices, 0);
1660 }
1661
1662 virtual void overrideDefaultClearVectorValue() override final {
1663 clearVectorValue_(def_val_);
1664 }
1665
1666
1667 virtual void restoreValueFromDefaultImpl_() override final {
1668 checkModificationPermission_();
1670 "Parameter " << getLocation() << " must not have been read when restoring "
1671 "a value from the default. This is a write");
1672
1674
1675 val_ = def_val_;
1676 }
1677
1678 virtual bool equals(const ParameterBase &other) override final {
1679 return *this == dynamic_cast<const Parameter<ValueType> &>(other);
1680 }
1681
1682 virtual void resizeVectorsFromString(const std::vector<uint32_t>& indices) override final {
1683 if(indices.size() == 0){
1684 return; // No effect
1685 }
1686
1687 resizeVectorsFromString_(val_, indices, 0);
1688 }
1689
1690 virtual void clearVectorValue() override final {
1691 clearVectorValue_(val_);
1692 }
1693
1699 checkModificationPermission_();
1700 sparta::utils::DisplayBase old = disp_base_;
1701 disp_base_ = base;
1702 return old;
1703 }
1704
1710 return disp_base_;
1711 }
1712
1713 bool validateIndependently(std::string& err_names) const override
1714 {
1715 (void) err_names;
1716
1717 // \todo Implement independent validation
1718 //sparta_assert(0, "sparta::Parameter::validateIndependently is unimplemented");
1719
1720 return true;
1721 }
1722
1733 bool validateDependencies(const TreeNode* node, std::string& err_names) const override
1734 {
1735 bool success = true;
1736 ValueType val = getValue_(); // Does not count as a read
1737 for(const ValidationCheckCallback<ValueType>& vcb : dependencies_){
1738 if(!vcb(val, node)){
1739 err_names += vcb.getName() + ",";
1740 success = false;
1741 }
1742 }
1743 return success;
1744 }
1745
1758 template <class U>
1760 return Parameter::template operator_insert_<U, ValueType>(e);
1761 }
1762
1768 virtual bool isVisibilityAllowed() const override{
1769 return ((param_attr_ == ParameterAttribute::HIDDEN) and
1770 (this->areParametersLocked_())) ? false : true;
1771 }
1772
1773 protected:
1774
1775 template <class U, class C1>
1776 typename std::enable_if<is_vector<C1>::value, Parameter<ValueType>&>::type
1777 operator_insert_(U e) {
1778 checkModificationPermission_();
1780 val_.push_back((typename ValueType::value_type) e);
1781 return *this;
1782 }
1783
1784 virtual void setValueFromStringImpl_(const std::string& str, bool poke=false) override final {
1785 Parameter::template setValueFromString_<ValueType, ValueType>(str, poke);
1786 }
1787
1788 virtual void setValueFromStringVectorImpl_(const std::vector<std::string>& vec, bool poke=false) override final {
1789 Parameter::template setValueFromStringVector_<ValueType, ValueType>(vec, poke);
1790 }
1791
1792 virtual void setItemValueFromStringImpl_(const std::vector<uint32_t>& indices,
1793 const std::string& str) override final {
1794 if(indices.size() == 0){
1795 // No indices given. This must be a non-vector type
1796 Parameter::template setValueFromString_<ValueType, ValueType>(str);
1797 }else{
1798 // handle if this is a vector
1799 const bool incr_write_count = true;
1800 setVectorItemValueFromString_(val_, indices, 0, str, incr_write_count);
1801 }
1802 }
1803
1805 const ValueType& getValue_() const {
1806 return val_;
1807 }
1808
1809 template <typename T>
1810 typename std::enable_if<std::is_arithmetic<T>::value, double>::type
1811 getDoubleValue_() const {
1812 return val_;
1813 }
1814
1815 template <typename T>
1816 typename std::enable_if<!std::is_arithmetic<T>::value, double>::type
1817 getDoubleValue_() const {
1818 throw SpartaException("Cannot get 'double' type value from parameter ")
1819 << getLocation() << " which is of type " << getTypeName();
1820 }
1821
1822 private:
1827 void checkModificationPermission_() const{
1828 if(this->areParametersLocked_() and
1829 param_attr_ != ParameterAttribute::DEFAULT){
1830 throw ParameterException("Modifying special parameters after Lockdown phase is disallowed.");
1831 }
1832 }
1833
1834 // Multi-dimensional parameter indexed writes
1835
1855 template <typename T>
1856 typename std::enable_if<is_vector<typename std::remove_reference<T>::type>::value>::type
1857 setVectorItemValueFromString_(T& vec,
1858 const std::vector<uint32_t>& indices,
1859 uint32_t index_level,
1860 const std::string& str,
1861 bool increment_write_count) {
1862 checkModificationPermission_();
1863 assert(indices.size() > 0);
1864
1865 uint32_t idx = indices.at(index_level);
1866 if(idx >= vec.size()){
1867 vec.resize(idx+1);
1868 }
1869
1870 if(indices.size() - 1 == index_level){
1871 // Final index should place a value in a vector
1872 // Note that vec is a vector
1873 typename T::reference to_set = vec.at(idx);
1874 setFinalVectorItemValueFromString_(to_set, indices, index_level, str);
1875 if(increment_write_count){
1877 }
1878 }else{
1879 // Non-final index should select a vector and recurse until the
1880 // last level of nested vectors is reached
1881 typename T::reference next_vec = vec.at(idx);
1882 setVectorItemValueFromString_(next_vec, indices, index_level+1, str, increment_write_count);
1883 }
1884 }
1885
1891 template <typename T>
1892 typename std::enable_if<!is_vector<typename std::remove_reference<T>::type>::value>::type
1893 setVectorItemValueFromString_(T& vec,
1894 const std::vector<uint32_t>& indices,
1895 uint32_t index_level,
1896 const std::string& str,
1897 bool increment_write_count) {
1898 checkModificationPermission_();
1899 (void) vec;
1900 (void) index_level;
1901 (void) str;
1902 (void) increment_write_count;
1903
1904 throw ParameterException("Cannot set value from string on parameter \"")
1905 << getName() << "\" which is of type \""
1906 << getTypeName() << "\" with indices: " << indices << " (" << indices.size()
1907 << " levels)" << " because this type only has " << index_level << " dimensions";
1908 }
1909
1916 template <typename T>
1917 typename std::enable_if<is_vector<typename std::remove_reference<T>::type>::value>::type
1918 setFinalVectorItemValueFromString_(T& to_set,
1919 const std::vector<uint32_t>& indices,
1920 uint32_t index_level,
1921 const std::string& str) {
1922 checkModificationPermission_();
1923 (void) to_set;
1924 (void) str;
1925
1926 throw ParameterException("Cannot set value from string on parameter \"")
1927 << getName() << "\" which is of type \""
1928 << getTypeName() << "\" with indices: " << indices << " (" << indices.size()
1929 << " levels)" << " because this type has more than " << index_level << " dimensions";
1930 }
1931
1945 template <typename T>
1946 typename std::enable_if<!is_vector<typename std::remove_reference<T>::type>::value>::type
1947 setFinalVectorItemValueFromString_(T& to_set,
1948 const std::vector<uint32_t>& indices,
1949 uint32_t index_level,
1950 const std::string& str) {
1951 checkModificationPermission_();
1952 sparta_assert(indices.size() == 0 || indices.size() - 1 == index_level);
1953
1954 size_t end_pos;
1955 to_set = smartLexicalCast<typename bit_reference_to_bool<T>::type>(this, str, end_pos);
1956 }
1957
1958
1959 // Multi-dimensional parameter indexed enlargement
1960
1977 template <typename T>
1978 typename std::enable_if<is_vector<typename std::remove_reference<T>::type>::value>::type
1979 resizeVectorsFromString_(T& vec,
1980 const std::vector<uint32_t>& indices,
1981 uint32_t index_level) {
1982 checkModificationPermission_();
1983 if(index_level == indices.size()){
1984 return; // Done. No further effect
1985 }
1986
1987 uint32_t idx = indices.at(index_level);
1988 if(idx >= vec.size()){
1989 vec.resize(idx+1);
1990 }
1991
1992 if(index_level < indices.size()){
1993 // Recurs to next index_level
1994 typename T::reference next_vec = vec.at(idx);
1995 resizeVectorsFromString_(next_vec, indices, index_level+1);
1996 }
1997 }
1998
2005 template <typename T>
2006 typename std::enable_if<!is_vector<typename std::remove_reference<T>::type>::value>::type
2007 resizeVectorsFromString_(T& vec,
2008 const std::vector<uint32_t>& indices,
2009 uint32_t index_level) {
2010 checkModificationPermission_();
2011 (void) vec;
2012 (void) index_level;
2013
2014 throw ParameterException("Cannot resize a vector in parameter \"")
2015 << getName() << "\" which is of type \""
2016 << getTypeName() << "\" to contain indices: " << indices << " (" << indices.size()
2017 << " levels)" << " because this type only has " << getDimensionality()
2018 << " dimensions. Therefore this index "
2019 "would be within a vector of scalars and this method has no idea with what "
2020 "value to initialize the new elements of said vector. Ues an indices vector "
2021 "with less than " << getDimensionality() << " elements";
2022 }
2023
2024 // Vector Clearing
2025
2033 template <typename T>
2034 static
2035 typename std::enable_if<is_vector<typename std::remove_reference<T>::type>::value>::type
2036 clearVectorValue_(T& vec) {
2037 vec.clear();
2038 }
2039
2046 template <typename T>
2047 static
2048 typename std::enable_if<!is_vector<typename std::remove_reference<T>::type>::value>::type
2049 clearVectorValue_(T& vec) {
2050 (void) vec;
2051
2052 // No effect
2053 }
2054
2055
2056 // Multi-dimensional parameter indexed reads
2057
2073 template <typename T>
2074 typename std::enable_if<is_vector<typename std::remove_reference<T>::type>::value, std::string>::type
2075 getVectorItemValueFromString_(const T& vec,
2076 const std::vector<uint32_t>& indices,
2077 uint32_t index_level,
2078 bool peek) const {
2079 assert(indices.size() > 0);
2080
2081 uint32_t idx = indices.at(index_level);
2082 if(idx >= vec.size()){
2083 throw ParameterException("Cannot get item from parameter \"")
2084 << getName() << "\" as a vector which is of type \""
2085 << getTypeName() << "\" with indices: " << indices << " (" << indices.size()
2086 << " levels)" << " because this type has only " << vec.size()
2087 << " elements at the vector located by indices[" << index_level << "]";
2088 }
2089
2090 if(indices.size() - 1 == index_level){
2091 // Final index should place a value in a vector
2092 // Note that vec is a vector
2093 typename T::const_reference to_get = vec.at(idx);
2094 return getFinalVectorItemValueFromString_(to_get, indices, index_level, peek);
2095 }else{
2096 // Non-final index should select a vector and recurse
2097 typename T::const_reference next_vec = vec.at(idx);
2098 return getVectorItemValueFromString_(next_vec, indices, index_level+1, peek);
2099 }
2100 }
2101
2107 template <typename T>
2108 typename std::enable_if<!is_vector<typename std::remove_reference<T>::type>::value, std::string>::type
2109 getVectorItemValueFromString_(const T& vec,
2110 const std::vector<uint32_t>& indices,
2111 uint32_t index_level,
2112 bool peek) const {
2113 (void) vec;
2114 (void) index_level;
2115 (void) peek;
2116
2117 throw ParameterException("Cannot get item from parameter \"")
2118 << getName() << "\" which is of type \""
2119 << getTypeName() << "\" with indices: " << indices << " (" << indices.size()
2120 << " levels)" << " because this type only has " << index_level << " dimensions";
2121 }
2122
2129 template <typename T>
2130 typename std::enable_if<is_vector<typename std::remove_reference<T>::type>::value, std::string>::type
2131 getFinalVectorItemValueFromString_(const T& to_get,
2132 const std::vector<uint32_t>& indices,
2133 uint32_t index_level,
2134 bool peek) const {
2135 (void) to_get;
2136 (void) peek;
2137
2138 throw ParameterException("Cannot get value from string on parameter \"")
2139 << getName() << "\" which is of type \""
2140 << getTypeName() << "\" with indices: " << indices << " (" << indices.size()
2141 << " levels)" << " because this type has more than " << index_level << " dimensions";
2142 }
2143
2158 template <typename T>
2159 typename std::enable_if<!is_vector<typename std::remove_reference<T>::type>::value, std::string>::type
2160 getFinalVectorItemValueFromString_(const T& to_get,
2161 const std::vector<uint32_t>& indices,
2162 uint32_t index_level,
2163 bool peek) const {
2164 sparta_assert(indices.size() == 0 || indices.size() - 1 == index_level);
2165
2166 if(!peek){
2168 }
2169
2170 return sparta::utils::stringize_value(to_get, disp_base_, string_quote_);
2171 }
2172
2173
2174 // Nested Vector size queries
2175
2194 template <typename T>
2195 typename std::enable_if<is_vector<typename std::remove_reference<T>::type>::value, uint32_t>::type
2196 getVectorSize_(const T& vec,
2197 const std::vector<uint32_t>& indices,
2198 uint32_t index_level,
2199 bool peek) const
2200 {
2201 sparta_assert(indices.size() > 0);
2202
2203 uint32_t idx = indices.at(index_level);
2204 if(idx >= vec.size()){
2205 throw ParameterException("Cannot get size of vector from parameter \"")
2206 << getName() << "\" as a vector which is of type \""
2207 << getTypeName() << "\" with indices: " << indices << " (" << indices.size()
2208 << " levels)" << " because this type has only " << vec.size()
2209 << " elements at the vector located by indices[" << index_level << "]";
2210 }
2211
2212 if(indices.size() - 1 == index_level){
2213 // Final index should place a value in a vector
2214 // Note that vec is a vector
2215 typename T::const_reference to_get = vec.at(idx);
2216 return getFinalVectorSize_(to_get, indices, index_level, peek);
2217 }else{
2218 // Non-final index should select a vector and recurse
2219 typename T::const_reference next_vec = vec.at(idx);
2220 return getVectorSize_(next_vec, indices, index_level+1, peek);
2221 }
2222 }
2223
2229 template <typename T>
2230 typename std::enable_if<!is_vector<typename std::remove_reference<T>::type>::value, uint32_t>::type
2231 getVectorSize_(const T& vec,
2232 const std::vector<uint32_t>& indices,
2233 uint32_t index_level,
2234 bool peek) const {
2235 (void) vec;
2236 (void) index_level;
2237 (void) peek;
2238
2239 throw ParameterException("Cannot get size of vector from parameter \"")
2240 << getName() << "\" which is of type \""
2241 << getTypeName() << "\" with indices: " << indices << " (" << indices.size()
2242 << " levels)" << " because this type only has " << index_level << " dimensions";
2243 }
2244
2250 template <typename T>
2251 typename std::enable_if<!is_vector<typename std::remove_reference<T>::type>::value, uint32_t>::type
2252 getFinalVectorSize_(const T& to_get,
2253 const std::vector<uint32_t>& indices,
2254 uint32_t index_level,
2255 bool peek) const {
2256 (void) to_get;
2257 (void) peek;
2258
2259 throw ParameterException("Cannot get size of vector from parameter \"")
2260 << getName() << "\" which is of type \""
2261 << getTypeName() << "\" with indices: " << indices << " (" << indices.size()
2262 << " levels)" << " because the in dimension " << index_level
2263 << " is a a scalar (not a vector)";
2264 }
2265
2277 template <typename T>
2278 typename std::enable_if<is_vector<typename std::remove_reference<T>::type>::value, uint32_t>::type
2279 getFinalVectorSize_(const T& to_get,
2280 const std::vector<uint32_t>& indices,
2281 uint32_t index_level,
2282 bool peek) const
2283 {
2284 sparta_assert(indices.size() == 0 || indices.size() - 1 == index_level);
2285
2286 if(!peek){
2287 incrementReadCount_(); // This is considered a read because this information can be useful
2288 }
2289
2290 return to_get.size();
2291 }
2292
2293
2294 // Type string computation
2295
2296 template <class T>
2297 typename std::enable_if<is_vector<T>::value, std::string>::type
2298 getTypeName_() const {
2299 std::stringstream ss;
2300 ss << "std::vector<";
2301 ss << getTypeName_<typename T::value_type>();
2302 ss << ">";
2303 return ss.str();
2304 }
2305
2306 template <class T>
2307 typename std::enable_if<!is_vector<T>::value, std::string>::type
2308 getTypeName_() const {
2311 }else{
2312 return demangle(typeid(T).name());
2313 }
2314 }
2315
2316
2317 // Vector value writes
2318 template <class T, class C1>
2319 typename std::enable_if<is_vector<C1>::value >::type
2320 setValueFromString_(const std::string& str, bool poke=false) {
2321 checkModificationPermission_();
2322 (void) str;
2323 (void) poke;
2324 throw ParameterException("Cannot set value from string on parameter \"")
2325 << getName() << "\" which is a vector type \""
2326 << getTypeName() << "\"";
2327 }
2328
2329 // Scalar value writes
2330 template <class T, class C1>
2331 typename std::enable_if<!is_vector<C1>::value >::type
2332 setValueFromString_(const std::string& str, bool poke=false) {
2333 checkModificationPermission_();
2334 ValueType tmp;
2335
2336 size_t end_pos;
2337 tmp = smartLexicalCast<ValueType>(this, str, end_pos);
2338
2339 if (!poke) {
2341 }
2342
2343 ValueType old_val = val_;
2344 val_ = tmp;
2345 try {
2347 }
2348 catch(sparta::SpartaException & e) {
2349 val_ = old_val;
2350 throw;
2351 }
2352 }
2353
2354
2355 // Vector value writes for DEFAULT value
2356 template <class T, class C1>
2357 typename std::enable_if<is_vector<C1>::value >::type
2358 overrideDefaultFromString_(const std::string& str) {
2359 checkModificationPermission_();
2360 (void) str;
2361 throw ParameterException("Cannot set default from string on parameter \"")
2362 << getName() << "\" which is a vector type \""
2363 << getTypeName() << "\"";
2364 }
2365
2366 // Scalar value writes for DEFAULT value
2367 template <class T, class C1>
2368 typename std::enable_if<!is_vector<C1>::value >::type
2369 overrideDefaultFromString_(const std::string& str) {
2370 checkModificationPermission_();
2371 ValueType tmp;
2372
2373 size_t end_pos;
2374 tmp = smartLexicalCast<ValueType>(this, str, end_pos);
2375 def_val_ = tmp;
2376 }
2377
2378
2379 // 1-d vector writes
2380
2381 template <class T, class C1> // C1=ValueType
2382 typename std::enable_if<!is_vector<C1>::value >::type
2383 setValueFromStringVector_(const std::vector<std::string>& vec, bool poke=false) {
2384 checkModificationPermission_();
2385 (void) vec;
2386 (void) poke;
2387 throw ParameterException("Cannot directly set value from string vector on parameter \"")
2388 << getName() << "\" which is a scalar (or string) type \""
2389 << getTypeName() << "\"";
2390 }
2391
2392 template <class T, class C1> // C1=ValueType
2393 typename std::enable_if<is_vector<C1>::value && !is_vector<typename C1::value_type>::value>::type
2394 setValueFromStringVector_(const std::vector<std::string>& vec, bool poke=false) {
2395 checkModificationPermission_();
2396 ValueType tmpvec;
2397 for(const std::string& s : vec){
2398 size_t end_pos;
2399 tmpvec.push_back(smartLexicalCast<typename ValueType::value_type>(this, s, end_pos));
2400 }
2401 if (!poke) {
2403 }
2404 ValueType old_val = val_;
2405 val_ = tmpvec;
2406 try {
2408 }
2409 catch(sparta::SpartaException & e) {
2410 val_ = old_val;
2411 throw;
2412 }
2413 }
2414
2415 template <class T, class C1> // C1=ValueType
2416 typename std::enable_if<is_vector<C1>::value && is_vector<typename C1::value_type>::value>::type
2417 setValueFromStringVector_(const std::vector<std::string>& vec, bool poke=false) {
2418 checkModificationPermission_();
2419 (void) vec;
2420 (void) poke;
2421 throw ParameterException("Cannot directly set value from string vector on parameter \"")
2422 << getName() << "\" which has " << getDimensionality() << " dimensions. Type is \""
2423 << getTypeName() << "\". Only 1-dimensional parameters can be set using this method";
2424 }
2425
2426
2427 // 1-d vector writes for DEFAULT value
2428
2429 template <class T, class C1> // C1=ValueType
2430 typename std::enable_if<!is_vector<C1>::value >::type
2431 overrideDefaultFromStringVector_(const std::vector<std::string>& vec) {
2432 checkModificationPermission_();
2433 (void) vec;
2434
2435 throw ParameterException("Cannot directly override default value from string vector on parameter \"")
2436 << getName() << "\" which is a scalar (or string) type \""
2437 << getTypeName() << "\"";
2438 }
2439
2440 template <class T, class C1> // C1=ValueType
2441 typename std::enable_if<is_vector<C1>::value && !is_vector<typename C1::value_type>::value>::type
2442 overrideDefaultFromStringVector_(const std::vector<std::string>& vec) {
2443 checkModificationPermission_();
2444 ValueType tmpvec;
2445 for(const std::string& s : vec){
2446 size_t end_pos;
2447 tmpvec.push_back(smartLexicalCast<typename ValueType::value_type>(this, s, end_pos));
2448 }
2449 def_val_ = tmpvec;
2450 }
2451
2452 template <class T, class C1> // C1=ValueType
2453 typename std::enable_if<is_vector<C1>::value && is_vector<typename C1::value_type>::value>::type
2454 overrideDefaultFromStringVector_(const std::vector<std::string>& vec) {
2455 checkModificationPermission_();
2456 (void) vec;
2457
2458 throw ParameterException("Cannot directly set value from string vector on parameter \"")
2459 << getName() << "\" which has " << getDimensionality() << " dimensions. Type is \""
2460 << getTypeName() << "\". Only 1-dimensional parameters can be set using this method";
2461 }
2462
2463
2464 // 1-d vector reads
2465
2466 template <class T, class C1> // C1=ValueType
2467 typename std::enable_if<!is_vector<C1>::value, std::string>::type
2468 getValueAsStringAt_(size_t idx, bool peek) const {
2469 if(idx != 0){
2470 ParameterException ex("Cannot get value as string at index other than 0 on parameter \"");
2471 ex << getName() << "\" which is a scalar (or string) type \""
2472 << getTypeName() << "\"";
2473 throw ex;
2474 }
2475 if(!peek){
2477 }
2478 return getValueAsString();
2479 }
2480
2481 template <class T, class C1> // C1=ValueType
2482 typename std::enable_if<is_vector<C1>::value, std::string>::type
2483 getValueAsStringAt_(size_t idx, bool peek) const {
2484 if(!peek){
2486 }
2487 return sparta::utils::stringize_value(val_.at(idx), disp_base_, string_quote_);
2488 }
2489
2490
2491 // 1-d vector inspection
2492
2493 template <class T, class C1> // C1=ValueType
2494 typename std::enable_if<!is_vector<C1>::value, size_t>::type
2495 getNumValues_(bool peek) const {
2496 (void) peek;
2497 // DO NOT count this as a read access. Nothing can be deduced about
2498 // the parameter from this information since this is a scalar
2499 return 1;
2500 }
2501
2502 template <class T, class C1> // C1=ValueType
2503 typename std::enable_if<is_vector<C1>::value, size_t>::type
2504 getNumValues_(bool peek) const {
2505 // Getting number of values counts as reading since it *might* be
2506 // informative
2507 if(!peek){
2509 }
2510 return val_.size();
2511 }
2512
2513 // Parameter Dimensionality
2514
2518 template<typename T, typename Enable = void>
2519 struct Dimensionality {
2520 };
2521
2527 template<typename T>
2528 struct Dimensionality<T, typename std::enable_if<is_vector<T>::value>::type> {
2529 enum { value = Dimensionality<typename T::value_type>::value + 1};
2530 };
2531
2535 template<typename T>
2536 struct Dimensionality<T, typename std::enable_if<!is_vector<T>::value>::type> {
2537 enum { value = 0 }; // T is a scalar type, with 0 dimensions
2538 };
2539
2540 }; // class Parameter
2541
2542
2543 // Implementation
2544
2545 template <class T>
2546 inline const T ParameterBase::getValueAs() const {
2547 const Parameter<T>* p = dynamic_cast<const Parameter<T>*>(this);
2548 if(nullptr == p){
2549 throw ParameterException("Cannot get value from Parameter \"")
2550 << getName() << "\" as a " << demangle(typeid(T).name())
2551 << " because it is internally a " << getTypeName()
2552 << ". getValueAs must be exact";
2553 }
2554 return *p;
2555 }
2556
2557 template <typename T>
2559 const std::string& s,
2560 size_t& end_pos,
2561 bool allow_recursion,
2562 bool allow_prefix)
2563 {
2564 try{
2565 return utils::smartLexicalCast<T>(s, end_pos, allow_recursion, allow_prefix);
2566 }catch(SpartaException& ex){
2567 ex << " in parameter " << p->getLocation();
2568 throw;
2569 }
2570 }
2571
2572} // 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.
static const std::string & lookupTypeName()
Determines if there is a known compiler-independent typename for type T.
Definition KeyValue.hpp:231
static bool hasTypeNameFor()
Determines if there is a known compiler-independent typename for type T.
Definition KeyValue.hpp:216
Boolean with a default capable of being changed to the opposite value only. it can never set to the d...
Definition Utils.hpp:366
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...
friend class detail::ExtensionsBase
incrementReadCount_() needed by TreeNode::ExtensionsBase
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 ...
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.
class sparta::ParameterBase::ParameterValueIterator const_iterator
Generic value iterator for a SINGLE parameter which represents values ONLY as std::string.
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:204
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:722
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:214
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:247
static const bool value
'value' field used by enable_if construct.
Definition Utils.hpp:253