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"
32int main(
int argc,
char* argv[]);
62 bool operator()(
const std::string& X,
const std::string& Y)
const
64 const char* x = X.c_str();
65 const char* y = Y.c_str();
67#define EAT_VAR(A, B) \
69 if ( *x == '%' && (*(x + 1) == '(' || *(x + 1) == 'd') ) { \
75 } while ( *x && *x != ')' ); \
78 if ( *x != 'd' ) goto NO_VARIABLE; \
81 while ( *y && isdigit(*y) ) \
91 if (
'\0' == *x )
return false;
96 if ( *x < *y )
return true;
100 if ( !(*x) && (*y) )
return true;
120 inline T convert_value(
const std::string& key,
const std::string& val)
const
123 return SST::Core::from_string<T>(val);
125 catch (
const std::invalid_argument& e ) {
126 std::string msg =
"Params::find(): No conversion for value: key = " + key +
", value = " + val +
127 ". Original error: " + e.what();
128 std::invalid_argument t(msg);
147 inline T find_impl(
const std::string& k, T default_value,
bool& found)
const
151 const std::string& value = getString(k, found);
153 return default_value;
156 return convert_value<T>(k, value);
171 inline T find_impl(
const std::string& k,
const std::string& default_value,
bool& found)
const
174 const std::string& value = getString(k, found);
177 return SST::Core::from_string<T>(default_value);
179 catch (
const std::invalid_argument& e ) {
180 std::string msg =
"Params::find(): Invalid default value specified: key = " + k +
181 ", value = " + default_value +
". Original error: " + e.what();
182 std::invalid_argument t(msg);
189 return SST::Core::from_string<T>(value);
191 catch (
const std::invalid_argument& e ) {
192 std::string msg =
"Params::find(): No conversion for value: key = " + k +
", value = " + value +
193 ". Original error: " + e.what();
194 std::invalid_argument t(msg);
200 using const_iterator = std::map<uint32_t, std::string>::const_iterator;
202 const std::string& getString(
const std::string& name,
bool& found)
const;
227 void cleanToken(std::string& token)
const;
233 void getDelimitedTokens(
const std::string& value,
char delim, std::vector<std::string>& tokens)
const;
248 bool old = verify_enabled;
249 verify_enabled = enable;
260 g_verify_enabled =
true;
327 std::enable_if_t<!std::is_same_v<std::string, T>, T>
find(
const std::string& k, T default_value,
bool& found)
const
329 return find_impl<T>(k, default_value, found);
343 T
find(
const std::string& k,
const std::string& default_value,
bool& found)
const
345 return find_impl<T>(k, default_value, found);
359 std::enable_if_t<std::is_same_v<bool, T>, T>
find(
360 const std::string& k,
const char* default_value,
bool& found)
const
362 if (
nullptr == default_value ) {
363 return find_impl<T>(k,
static_cast<T
>(0), found);
365 return find_impl<T>(k, std::string(default_value), found);
377 T
find(
const std::string& k, T default_value)
const
380 return find_impl<T>(k, default_value, tmp);
393 T
find(
const std::string& k,
const std::string& default_value)
const
396 return find_impl<T>(k, default_value, tmp);
410 std::enable_if_t<std::is_same_v<bool, T>, T>
find(
const std::string& k,
const char* default_value)
const
413 if (
nullptr == default_value ) {
414 return find_impl<T>(k,
static_cast<T
>(0), tmp);
416 return find_impl<T>(k, std::string(default_value), tmp);
427 T
find(
const std::string& k)
const
430 T default_value = T();
431 return find_impl<T>(k, default_value, tmp);
445 std::enable_if_t<!std::is_same_v<bool, T>, T>
find(
const std::string& k,
bool& found)
const
447 T default_value = T();
448 return find_impl<T>(k, default_value, found);
518 std::string value = getString(k, found);
519 if ( !found )
return;
523 if ( value.front() !=
'[' || value.back() !=
']' ) {
524 vec.push_back(convert_value<T>(k, value));
528 value = value.substr(1, value.size() - 2);
531 std::vector<std::string> tokens;
532 getDelimitedTokens(value,
',', tokens);
535 for (
auto& str : tokens ) {
541 for (
auto& val : tokens ) {
542 vec.push_back(convert_value<T>(k, val));
613 std::string value = getString(k, found);
614 if ( !found )
return;
619 if ( value.find(
"set([") == 0 ) {
624 value = value.substr(4, value.length() - 5);
626 value[value.length() - 1] =
'}';
632 if ( value.front() !=
'{' || value.back() !=
'}' ) {
633 set.insert(convert_value<T>(k, value));
637 value = value.substr(1, value.size() - 2);
640 std::vector<std::string> tokens;
641 getDelimitedTokens(value,
',', tokens);
644 for (
auto& str : tokens ) {
650 for (
auto& val : tokens ) {
651 set.insert(convert_value<T>(k, val));
719 template <
class keyT,
class valT>
725 std::string value = getString(k, found);
726 if ( !found )
return;
729 if ( value.front() !=
'{' || value.back() !=
'}' ) {
730 std::string msg =
"Invalid format for parameter to be parsed as a map: " + value;
733 throw std::invalid_argument(msg);
736 value = value.substr(1, value.size() - 2);
739 std::vector<std::string> tokens;
740 getDelimitedTokens(value,
',', tokens);
744 std::vector<std::string> kvpair;
745 for (
auto& x : tokens ) {
747 getDelimitedTokens(x,
':', kvpair);
749 if ( kvpair.size() != 2 ) {
750 std::string msg =
"Invalid format for map key/value pair: " + x;
753 throw std::invalid_argument(msg);
757 cleanToken(kvpair[0]);
758 cleanToken(kvpair[1]);
761 map[convert_value<keyT>(k, kvpair[0])] = convert_value<valT>(k, kvpair[1]);
776 std::string value = getString(k, found);
777 if ( !found )
return false;
779 if ( (value.find(
"[") == std::string::npos) || (value.find(
"]") == std::string::npos) ) {
786 void print_all_params(std::ostream& os,
const std::string& prefix =
"")
const;
790 std::string
toString(
const std::string& prefix =
"")
const;
802 void insert(
const std::string& key,
const std::string& value,
bool overwrite =
true);
817 std::set<std::string>
getKeys()
const;
869 void verifyParam(
const key_type& k)
const;
879 void addSharedParamSet(
const std::string& set);
893 static void insert_shared(
894 const std::string& set,
const key_type& key,
const key_type& value,
bool overwrite =
true);
904 static std::map<std::string, std::string> getSharedParamSet(
const std::string& name);
913 static std::vector<std::string> getSharedParamSetNames();
922 std::vector<std::string> getLocalKeys()
const;
932 std::vector<std::string> getSubscribedSharedParamSets()
const;
940 void verifyKey(
const key_type& k)
const;
944 std::map<uint32_t, std::string> my_data;
945 std::vector<std::map<uint32_t, std::string>*> data;
946 std::vector<KeySet_t> allowedKeys;
948 static bool g_verify_enabled;
950 static uint32_t getKey(
const std::string& str);
957 static const std::string& getParamName(uint32_t
id);
960 friend int ::main(
int argc,
char* argv[]);
964 static std::map<std::string, uint32_t> keyMap;
965 static std::vector<std::string> keyMapReverse;
968 static uint32_t nextKeyID;
970 static std::map<std::string, std::map<uint32_t, std::string>> shared_params;
976#define SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(type) \
978 type Params::find(const std::string& k, type default_value, bool& found) const; \
980 type Params::find(const std::string& k, const std::string& default_value, bool& found) const; \
982 type Params::find(const std::string& k, type default_value) const; \
984 type Params::find(const std::string& k, const std::string& default_value) const; \
986 type Params::find(const std::string& k) const;
989 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(int32_t)
990 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(uint32_t)
991 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(int64_t)
992 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(uint64_t)
993 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
bool)
994 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
float)
995 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
double)
996 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(UnitAlgebra)
Represents the configuration of a generic component.
Definition configGraph.h:263
A Configuration Graph A graph representing Components and Links.
Definition configGraph.h:450
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:45
Definition threadsafe.h:132
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file.
Definition output.h:54
Parameter store.
Definition params.h:58
void popAllowedKeys()
Removes the most recent set of keys considered allowed.
Definition params.cc:243
void clear()
Erases all elements, including deleting reference to shared param sets.
Definition params.cc:84
void print_all_params(std::ostream &os, const std::string &prefix="") const
Print all key/value parameter pairs to specified ostream.
Definition params.cc:103
std::set< std::string > getKeys() const
Get all the keys contained in the Params object.
Definition params.cc:191
Params()
Create a new, empty Params.
Definition params.cc:56
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:410
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:427
bool contains(const key_type &k) const
Search the container for a particular key.
Definition params.cc:224
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:513
bool enableVerify(bool enable)
Enable or disable parameter verification on an instance of Params.
Definition params.h:246
std::string key_type
Definition params.h:236
Params & operator=(const Params &old)
Assignment operator.
Definition params.cc:73
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:203
void pushAllowedKeys(const std::vector< std::string > &keys)
Definition params.cc:233
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:327
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:377
static void enableVerify()
Enable, on a global scale, parameter verification.
Definition params.h:258
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:393
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:720
std::string toString(const std::string &prefix="") const
Return a string version of all key/value parameter pairs.
Definition params.cc:143
bool empty() const
Returns true if the Params is empty.
Definition params.cc:51
size_t size() const
Returns the size of the Params.
Definition params.cc:45
size_t count(const key_type &k) const
Finds the number of elements with given key.
Definition params.cc:92
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:608
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:773
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:359
std::set< key_type, KeyCompare > KeySet_t
Definition params.h:237
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:343
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:166
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:445
Base class for Model Generation.
Definition sstmodel.h:30
Main control class for a SST Simulation.
Definition simulation_impl.h:87