12#ifndef SST_CORE_PARAM_H
13#define SST_CORE_PARAM_H
15#include "sst/core/from_string.h"
16#include "sst/core/serialization/serializable.h"
17#include "sst/core/serialization/serializer.h"
18#include "sst/core/threadsafe.h"
38int main(
int argc,
char* argv[]);
69 bool operator()(
const std::string& X,
const std::string& Y)
const
71 const char* x = X.c_str();
72 const char* y = Y.c_str();
74#define EAT_VAR(A, B) \
76 if ( *x == '%' && (*(x + 1) == '(' || *(x + 1) == 'd') ) { \
82 } while ( *x && *x != ')' ); \
85 if ( *x != 'd' ) goto NO_VARIABLE; \
88 while ( *y && isdigit(*y) ) \
98 if (
'\0' == *x )
return false;
103 if ( *x < *y )
return true;
106 }
while ( *x && *y );
107 if ( !(*x) && (*y) )
return true;
127 inline T convert_value(
const std::string& key,
const std::string& val)
const
130 return SST::Core::from_string<T>(val);
132 catch (
const std::invalid_argument& e ) {
133 std::string msg =
"Params::find(): No conversion for value: key = " + key +
", value = " + val +
134 ". Original error: " + e.what();
135 std::invalid_argument t(msg);
154 inline T find_impl(
const std::string& k, T default_value,
bool& found)
const
158 const std::string& value = getString(k, found);
160 return default_value;
163 return convert_value<T>(k, value);
178 inline T find_impl(
const std::string& k,
const std::string& default_value,
bool& found)
const
181 const std::string& value = getString(k, found);
184 return SST::Core::from_string<T>(default_value);
186 catch (
const std::invalid_argument& e ) {
187 std::string msg =
"Params::find(): Invalid default value specified: key = " + k +
188 ", value = " + default_value +
". Original error: " + e.what();
189 std::invalid_argument t(msg);
196 return SST::Core::from_string<T>(value);
198 catch (
const std::invalid_argument& e ) {
199 std::string msg =
"Params::find(): No conversion for value: key = " + k +
", value = " + value +
200 ". Original error: " + e.what();
201 std::invalid_argument t(msg);
207 using const_iterator = std::map<uint32_t, std::string>::const_iterator;
209 const std::string& getString(
const std::string& name,
bool& found)
const;
234 void cleanToken(std::string& token)
const;
240 void getDelimitedTokens(
const std::string& value,
char delim, std::vector<std::string>& tokens)
const;
255 bool old = verify_enabled;
256 verify_enabled = enable;
267 g_verify_enabled =
true;
334 std::enable_if_t<!std::is_same_v<std::string, T>, T>
find(
const std::string& k, T default_value,
bool& found)
const
336 return find_impl<T>(k, default_value, found);
350 T
find(
const std::string& k,
const std::string& default_value,
bool& found)
const
352 return find_impl<T>(k, default_value, found);
366 std::enable_if_t<std::is_same_v<bool, T>, T>
find(
367 const std::string& k,
const char* default_value,
bool& found)
const
369 if (
nullptr == default_value ) {
370 return find_impl<T>(k,
static_cast<T
>(0), found);
372 return find_impl<T>(k, std::string(default_value), found);
384 T
find(
const std::string& k, T default_value)
const
387 return find_impl<T>(k, default_value, tmp);
400 T
find(
const std::string& k,
const std::string& default_value)
const
403 return find_impl<T>(k, default_value, tmp);
417 std::enable_if_t<std::is_same_v<bool, T>, T>
find(
const std::string& k,
const char* default_value)
const
420 if (
nullptr == default_value ) {
421 return find_impl<T>(k,
static_cast<T
>(0), tmp);
423 return find_impl<T>(k, std::string(default_value), tmp);
434 T
find(
const std::string& k)
const
437 T default_value = T();
438 return find_impl<T>(k, default_value, tmp);
452 std::enable_if_t<!std::is_same_v<bool, T>, T>
find(
const std::string& k,
bool& found)
const
454 T default_value = T();
455 return find_impl<T>(k, default_value, found);
525 std::string value = getString(k, found);
526 if ( !found )
return;
530 if ( value.front() !=
'[' || value.back() !=
']' ) {
531 vec.push_back(convert_value<T>(k, value));
535 value = value.substr(1, value.size() - 2);
538 std::vector<std::string> tokens;
539 getDelimitedTokens(value,
',', tokens);
542 for (
auto& str : tokens ) {
548 for (
auto& val : tokens ) {
549 vec.push_back(convert_value<T>(k, val));
620 std::string value = getString(k, found);
621 if ( !found )
return;
626 if ( value.find(
"set([") == 0 ) {
631 value = value.substr(4, value.length() - 5);
633 value[value.length() - 1] =
'}';
639 if ( value.front() !=
'{' || value.back() !=
'}' ) {
640 set.insert(convert_value<T>(k, value));
644 value = value.substr(1, value.size() - 2);
647 std::vector<std::string> tokens;
648 getDelimitedTokens(value,
',', tokens);
651 for (
auto& str : tokens ) {
657 for (
auto& val : tokens ) {
658 set.insert(convert_value<T>(k, val));
726 template <
class keyT,
class valT>
732 std::string value = getString(k, found);
733 if ( !found )
return;
736 if ( value.front() !=
'{' || value.back() !=
'}' ) {
737 std::string msg =
"Invalid format for parameter to be parsed as a map: " + value;
740 throw std::invalid_argument(msg);
743 value = value.substr(1, value.size() - 2);
746 std::vector<std::string> tokens;
747 getDelimitedTokens(value,
',', tokens);
751 std::vector<std::string> kvpair;
752 for (
auto& x : tokens ) {
754 getDelimitedTokens(x,
':', kvpair);
756 if ( kvpair.size() != 2 ) {
757 std::string msg =
"Invalid format for map key/value pair: " + x;
760 throw std::invalid_argument(msg);
764 cleanToken(kvpair[0]);
765 cleanToken(kvpair[1]);
768 map[convert_value<keyT>(k, kvpair[0])] = convert_value<valT>(k, kvpair[1]);
783 std::string value = getString(k, found);
784 if ( !found )
return false;
786 if ( (value.find(
"[") == std::string::npos) || (value.find(
"]") == std::string::npos) ) {
793 void print_all_params(std::ostream& os,
const std::string& prefix =
"")
const;
797 std::string
toString(
const std::string& prefix =
"")
const;
809 void insert(
const std::string& key,
const std::string& value,
bool overwrite =
true);
824 std::set<std::string>
getKeys()
const;
877 void verifyParam(
const key_type& k)
const;
887 void addSharedParamSet(
const std::string& set);
901 static void insert_shared(
902 const std::string& set,
const key_type& key,
const key_type& value,
bool overwrite =
true);
912 static std::map<std::string, std::string> getSharedParamSet(
const std::string& name);
921 static std::vector<std::string> getSharedParamSetNames();
930 std::vector<std::string> getLocalKeys()
const;
940 std::vector<std::string> getSubscribedSharedParamSets()
const;
948 void verifyKey(
const key_type& k)
const;
952 std::map<uint32_t, std::string> my_data;
953 std::vector<std::map<uint32_t, std::string>*> data;
954 std::vector<KeySet_t> allowedKeys;
956 static bool g_verify_enabled;
958 static uint32_t getKey(
const std::string& str);
965 static const std::string& getParamName(uint32_t
id);
968 friend int ::main(
int argc,
char* argv[]);
972 static std::map<std::string, uint32_t> keyMap;
973 static std::vector<std::string> keyMapReverse;
974 static std::recursive_mutex keyLock;
976 static uint32_t nextKeyID;
978 static std::map<std::string, std::map<uint32_t, std::string>> shared_params;
984#define SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(type) \
986 type Params::find(const std::string& k, type default_value, bool& found) const; \
988 type Params::find(const std::string& k, const std::string& default_value, bool& found) const; \
990 type Params::find(const std::string& k, type default_value) const; \
992 type Params::find(const std::string& k, const std::string& default_value) const; \
994 type Params::find(const std::string& k) const;
997 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(int32_t)
998 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(uint32_t)
999 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(int64_t)
1000 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(uint64_t)
1001 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
bool)
1002 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
float)
1003 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
double)
1004 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(UnitAlgebra)
Represents the configuration of a generic component.
Definition configComponent.h:83
A Configuration Graph A graph representing Components and Links.
Definition configGraph.h:76
Class that represents a PortModule in ConfigGraph.
Definition configComponent.h:50
Outputs configuration data to a specified file path.
Definition configGraphOutput.h:62
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition serializer.h:43
Definition threadsafe.h:138
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file.
Definition output.h:58
Parameter store.
Definition params.h:65
void popAllowedKeys()
Removes the most recent set of keys considered allowed.
Definition params.cc:252
void clear()
Erases all elements, including deleting reference to shared param sets.
Definition params.cc:86
void print_all_params(std::ostream &os, const std::string &prefix="") const
Print all key/value parameter pairs to specified ostream.
Definition params.cc:105
std::set< std::string > getKeys() const
Get all the keys contained in the Params object.
Definition params.cc:198
Params()
Create a new, empty Params.
Definition params.cc:58
std::enable_if_t< std::is_same_v< bool, T >, T > find(const std::string &k, const char *default_value) const
Find a Parameter value in the set, and return its value as a type T.
Definition params.h:417
T find(const std::string &k) const
Find a Parameter value in the set, and return its value as a type T.
Definition params.h:434
bool contains(const key_type &k) const
Search the container for a particular key.
Definition params.cc:233
void find_array(const key_type &k, std::vector< T > &vec) const
Find a Parameter value in the set, and return its value as a vector of T's.
Definition params.h:520
bool enableVerify(bool enable)
Enable or disable parameter verification on an instance of Params.
Definition params.h:253
std::string key_type
Definition params.h:243
Params & operator=(const Params &old)
Assignment operator.
Definition params.cc:75
Params get_scoped_params(const std::string &scope) const
Returns a new parameter object with parameters that match the specified scoped prefix (scopes are sep...
Definition params.cc:211
void pushAllowedKeys(const std::vector< std::string > &keys)
Definition params.cc:242
std::enable_if_t<!std::is_same_v< std::string, T >, T > find(const std::string &k, T default_value, bool &found) const
Find a Parameter value in the set, and return its value as a type T.
Definition params.h:334
T find(const std::string &k, T default_value) const
Find a Parameter value in the set, and return its value as a type T.
Definition params.h:384
static void enableVerify()
Enable, on a global scale, parameter verification.
Definition params.h:265
T find(const std::string &k, const std::string &default_value) const
Find a Parameter value in the set, and return its value as a type T.
Definition params.h:400
void find_map(const key_type &k, std::map< keyT, valT > &map) const
Find a Parameter value in the set, and return its value as a map<keyT,valT>.
Definition params.h:727
std::string toString(const std::string &prefix="") const
Return a string version of all key/value parameter pairs.
Definition params.cc:147
bool empty() const
Returns true if the Params is empty.
Definition params.cc:53
size_t size() const
Returns the size of the Params.
Definition params.cc:47
size_t count(const key_type &k) const
Finds the number of elements with given key.
Definition params.cc:94
void find_set(const key_type &k, std::set< T > &set) const
Find a Parameter value in the set, and return its value as a set of T's.
Definition params.h:615
bool is_value_array(const key_type &k) const
Checks to see if the value associated with the given key is considered to be an array.
Definition params.h:780
std::enable_if_t< std::is_same_v< bool, T >, T > find(const std::string &k, const char *default_value, bool &found) const
Find a Parameter value in the set, and return its value as a type T.
Definition params.h:366
std::set< key_type, KeyCompare > KeySet_t
Definition params.h:244
T find(const std::string &k, const std::string &default_value, bool &found) const
Find a Parameter value in the set, and return its value as a type T.
Definition params.h:350
void insert(const std::string &key, const std::string &value, bool overwrite=true)
Add a key/value pair into the param object.
Definition params.cc:171
std::enable_if_t<!std::is_same_v< bool, T >, T > find(const std::string &k, bool &found) const
Find a Parameter value in the set, and return its value as a type T.
Definition params.h:452
Base class for Model Generation.
Definition sstmodel.h:32
Main control class for a SST Simulation.
Definition simulation.h:121