12 #ifndef SST_CORE_PARAM_H
13 #define SST_CORE_PARAM_H
15 #include "sst/core/from_string.h"
16 #include "sst/core/output.h"
17 #include "sst/core/serialization/serializable.h"
18 #include "sst/core/serialization/serializer.h"
19 #include "sst/core/threadsafe.h"
30 int main(
int argc,
char* argv[]);
46 struct KeyCompare : std::binary_function<std::string, std::string, bool>
48 bool operator()(
const std::string& X,
const std::string& Y)
const
50 const char* x = X.c_str();
51 const char* y = Y.c_str();
53 #define EAT_VAR(A, B) \
55 if ( *x == '%' && (*(x + 1) == '(' || *(x + 1) == 'd') ) { \
61 } while ( *x && *x != ')' ); \
64 if ( *x != 'd' ) goto NO_VARIABLE; \
67 while ( *y && isdigit(*y) ) \
77 if (
'\0' == *x )
return false;
82 if ( *x < *y )
return true;
86 if ( !(*x) && (*y) )
return true;
106 inline T convert_value(
const std::string& key,
const std::string& val)
const
109 return SST::Core::from_string<T>(val);
111 catch (
const std::invalid_argument& e ) {
112 std::string msg =
"Params::find(): No conversion for value: key = " + key +
", value = " + val +
113 ". Original error: " + e.what();
114 std::invalid_argument t(msg);
133 inline T find_impl(
const std::string& k, T default_value,
bool& found)
const
137 const std::string& value = getString(k, found);
138 if ( !found ) {
return default_value; }
140 return convert_value<T>(k, value);
155 inline T find_impl(
const std::string& k,
const std::string& default_value,
bool& found)
const
158 const std::string& value = getString(k, found);
161 return SST::Core::from_string<T>(default_value);
163 catch (
const std::invalid_argument& e ) {
164 std::string msg =
"Params::find(): Invalid default value specified: key = " + k +
165 ", value = " + default_value +
". Original error: " + e.what();
166 std::invalid_argument t(msg);
173 return SST::Core::from_string<T>(value);
175 catch (
const std::invalid_argument& e ) {
176 std::string msg =
"Params::find(): No conversion for value: key = " + k +
", value = " + value +
177 ". Original error: " + e.what();
178 std::invalid_argument t(msg);
184 typedef std::map<uint32_t, std::string>::const_iterator const_iterator;
186 const std::string& getString(
const std::string& name,
bool& found)
const;
188 void getArrayTokens(
const std::string& value, std::vector<std::string>& tokens)
const;
203 bool old = verify_enabled;
204 verify_enabled = enable;
279 typename std::enable_if<not std::is_same<std::string, T>::value, T>::type
280 find(
const std::string& k, T default_value,
bool& found)
const
282 return find_impl<T>(k, default_value, found);
296 T
find(
const std::string& k,
const std::string& default_value,
bool& found)
const
298 return find_impl<T>(k, default_value, found);
312 typename std::enable_if<std::is_same<bool, T>::value, T>::type
313 find(
const std::string& k,
const char* default_value,
bool& found)
const
315 if (
nullptr == default_value ) {
return find_impl<T>(k,
static_cast<T
>(0), found); }
316 return find_impl<T>(k, std::string(default_value), found);
328 T
find(
const std::string& k, T default_value)
const
331 return find_impl<T>(k, default_value, tmp);
344 T
find(
const std::string& k,
const std::string& default_value)
const
347 return find_impl<T>(k, default_value, tmp);
361 typename std::enable_if<std::is_same<bool, T>::value, T>::type
362 find(
const std::string& k,
const char* default_value)
const
365 if (
nullptr == default_value ) {
return find_impl<T>(k,
static_cast<T
>(0), tmp); }
366 return find_impl<T>(k, std::string(default_value), tmp);
377 T
find(
const std::string& k)
const
380 T default_value = T();
381 return find_impl<T>(k, default_value, tmp);
395 typename std::enable_if<not std::is_same<bool, T>::value, T>::type
find(
const std::string& k,
bool& found)
const
397 T default_value = T();
398 return find_impl<T>(k, default_value, found);
468 std::string value = getString(k, found);
469 if ( !found )
return;
473 if ( value.front() !=
'[' || value.back() !=
']' ) {
474 vec.push_back(convert_value<T>(k, value));
478 value = value.substr(1, value.size() - 2);
481 std::vector<std::string> tokens;
482 getArrayTokens(value, tokens);
486 for (
auto& val : tokens ) {
487 vec.push_back(convert_value<T>(k, val));
502 std::string value = getString(k, found);
503 if ( !found )
return false;
505 if ( (value.find(
"[") == std::string::npos) || (value.find(
"]") == std::string::npos) ) {
return false; }
510 void print_all_params(std::ostream& os,
const std::string& prefix =
"")
const;
524 void insert(
const std::string& key,
const std::string& value,
bool overwrite =
true);
539 std::set<std::string>
getKeys()
const;
561 "Params::find_prefix_params() is deprecated and will be removed in SST 12. Please use the new "
562 "Params::get_scoped_params() function. As of SST 12, only a \".\" will be allowed as a scoping delimiter.")));
564 Params find_scoped_params(
const std::string& scope,
const char* delims =
".:") const __attribute__((deprecated(
565 "
Params::find_scoped_params() is deprecated and will be removed in SST 12. Please use the new "
619 void serialize_order(SST::Core::Serialization::
serializer& ser) override;
620 ImplementSerializable(SST::
Params)
623 std::map<uint32_t, std::
string> my_data;
624 std::vector<std::map<uint32_t, std::
string>*> data;
627 static
bool g_verify_enabled;
629 static uint32_t getKey(const std::
string& str);
637 static const std::
string& getParamName(uint32_t
id);
640 friend
int ::main(
int argc,
char* argv[]);
642 static std::map<std::
string, uint32_t> keyMap;
643 static std::vector<std::
string> keyMapReverse;
644 static SST::Core::ThreadSafe::
Spinlock keyLock;
645 static SST::Core::ThreadSafe::
Spinlock globalLock;
646 static uint32_t nextKeyID;
648 static std::map<std::
string, std::map<uint32_t, std::
string>> global_params;
654 #define SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(type) \
656 type Params::find(const std::string& k, type default_value, bool& found) const; \
658 type Params::find(const std::string& k, const std::string& default_value, bool& found) const; \
660 type Params::find(const std::string& k, type default_value) const; \
662 type Params::find(const std::string& k, const std::string& default_value) const; \
664 type Params::find(const std::string& k) const;
667 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(int32_t)
668 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(uint32_t)
669 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(int64_t)
670 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(uint64_t)
671 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
bool)
672 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
float)
673 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
double)
674 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
UnitAlgebra)
679 std::
string Params::
find<std::
string>(const std::
string& k, const std::
string& default_value,
bool &found) const;
684 #endif // SST_CORE_PARAMS_H
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file...
Definition: output.h:51
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:34
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:463
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:328
Params find_prefix_params(const std::string &prefix) const
Returns a new parameter object with parameters that match the specified prefix.
Definition: params.cc:204
void pushAllowedKeys(const KeySet_t &keys)
Definition: params.cc:250
bool contains(const key_type &k) const
Search the container for a particular key.
Definition: params.cc:241
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:222
size_t count(const key_type &k) const
Finds the number of elements with given key.
Definition: params.cc:88
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:377
static void enableVerify()
Enable, on a global scale, parameter verification.
Definition: params.h:213
Definition: serializable.h:118
bool empty() const
Returns true if the Params is empty.
Definition: params.cc:49
void clear()
Erases all elements, including deleting reference to global param sets.
Definition: params.cc:80
Params()
Create a new, empty Params.
Definition: params.cc:54
bool enableVerify(bool enable)
Enable or disable parameter verification on an instance of Params.
Definition: params.h:201
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:344
std::enable_if< not std::is_same< bool, T >::value, T >::type 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:395
Definition: threadsafe.h:121
std::enable_if< std::is_same< bool, T >::value, T >::type 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:313
size_t size() const
Returns the size of the Params.
Definition: params.cc:43
Parameter store.
Definition: params.h:43
static void insert_global(const std::string &set, const key_type &key, const key_type &value, bool overwrite=true)
Adds a key/value pair to the specified global set.
Definition: params.cc:331
void popAllowedKeys()
Removes the most recent set of keys considered allowed.
Definition: params.cc:256
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:296
std::set< key_type, KeyCompare > KeySet_t
Definition: params.h:192
std::string key_type
Definition: params.h:191
void verifyParam(const key_type &k) const
Definition: params.cc:265
void addGlobalParamSet(const std::string &set)
Adds a global param set to be looked at in this Params object if the key isn't found locally...
Definition: params.cc:325
std::set< std::string > getKeys() const
Get all the keys contained in the Params object.
Definition: params.cc:162
Params & operator=(const Params &old)
Assignment operator.
Definition: params.cc:69
std::enable_if< not std::is_same< std::string, T >::value, T >::type 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:280
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:139
std::enable_if< std::is_same< bool, T >::value, T >::type 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:362
Performs Unit math in full precision.
Definition: unitAlgebra.h:106
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:499
void print_all_params(std::ostream &os, const std::string &prefix="") const
Print all key/value parameter pairs to specified ostream.
Definition: params.cc:99