12 #ifndef SST_CORE_PARAM_H
13 #define SST_CORE_PARAM_H
15 #include "sst/core/output.h"
16 #include "sst/core/from_string.h"
26 #include "sst/core/threadsafe.h"
28 #include "sst/core/serialization/serializable.h"
29 #include "sst/core/serialization/serializer.h"
31 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')) { \
59 do { x++; } while ( *x && *x != ')' ); \
62 if ( *x != 'd' ) goto NO_VARIABLE; \
65 while ( *y && isdigit(*y) ) y++; \
74 if (
'\0' == *x )
return false;
78 if ( *x < *y )
return true;
82 if ( !(*x) && (*y) )
return true;
103 inline T convert_value(
const std::string& key,
const std::string& val)
const {
105 return SST::Core::from_string<T>(val);
107 catch (
const std::invalid_argument& e ) {
108 std::string msg =
"Params::find(): No conversion for value: key = " + key +
", value = " + val +
109 ". Original error: " + e.what();
110 std::invalid_argument t(msg);
129 inline T find_impl(
const std::string& k, T default_value,
bool &found)
const {
132 const std::string& value = getString(k,found);
134 return default_value;
137 return convert_value<T>(k,value);
152 inline T find_impl(
const std::string& k,
const std::string& default_value,
bool &found)
const {
154 const std::string& value = getString(k,found);
157 return SST::Core::from_string<T>(default_value);
159 catch (
const std::invalid_argument& e ) {
160 std::string msg =
"Params::find(): Invalid default value specified: key = " + k +
", value = " + default_value +
161 ". Original error: " + e.what();
162 std::invalid_argument t(msg);
169 return SST::Core::from_string<T>(value);
171 catch (
const std::invalid_argument& e ) {
172 std::string msg =
"Params::find(): No conversion for value: key = " + k +
", value = " + value +
173 ". Original error: " + e.what();
174 std::invalid_argument t(msg);
180 typedef std::map<uint32_t, std::string>::const_iterator const_iterator;
182 const std::string& getString(
const std::string& name,
bool& found)
const;
184 void getArrayTokens(
const std::string& value, std::vector<std::string>& tokens)
const;
197 bool enableVerify(
bool enable) {
bool old = verify_enabled; verify_enabled = enable;
return old; }
272 typename std::enable_if<not std::is_same<std::string,T>::value, T>::type
273 find(
const std::string& k, T default_value,
bool &found)
const {
274 return find_impl<T>(k,default_value,found);
288 T
find(
const std::string& k,
const std::string& default_value,
bool &found)
const {
289 return find_impl<T>(k,default_value,found);
303 typename std::enable_if<std::is_same<bool,T>::value, T>::type
304 find(
const std::string& k,
const char* default_value,
bool &found )
const {
305 if (
nullptr == default_value ) {
306 return find_impl<T>(k,
static_cast<T
>(0), found);
308 return find_impl<T>(k, std::string(default_value), found);
320 T
find(
const std::string& k, T default_value )
const {
322 return find_impl<T>(k, default_value, tmp);
335 T
find(
const std::string& k,
const std::string& default_value )
const {
337 return find_impl<T>(k, default_value, tmp);
351 typename std::enable_if<std::is_same<bool,T>::value, T>::type
352 find(
const std::string& k,
const char* default_value )
const {
354 if (
nullptr == default_value ) {
355 return find_impl<T>(k,
static_cast<T
>(0), tmp);
357 return find_impl<T>(k, std::string(default_value), tmp);
368 T
find(
const std::string& k)
const {
370 T default_value = T();
371 return find_impl<T>(k, default_value, tmp);
385 typename std::enable_if<not std::is_same<bool, T>::value, T>::type
386 find(
const std::string& k,
bool &found)
const {
387 T default_value = T();
388 return find_impl<T>(k, default_value, found);
457 std::string value = getString(k,found);
458 if ( !found )
return;
462 if ( value.front() !=
'[' || value.back() !=
']' ) {
463 vec.push_back(convert_value<T>(k,value));
467 value = value.substr(1,value.size()-2);
470 std::vector<std::string> tokens;
471 getArrayTokens(value,tokens);
475 for (
auto& val : tokens ) {
476 vec.push_back(convert_value<T>(k,val));
490 std::string value = getString(k,found);
491 if ( !found )
return false;
493 if( (value.find(
"[") == std::string::npos) ||
494 (value.find(
"]") == std::string::npos) ){
501 void print_all_params(std::ostream &os,
const std::string& prefix =
"")
const;
517 void insert(
const std::string& key,
const std::string& value,
bool overwrite =
true);
533 std::set<std::string>
getKeys()
const;
554 Params find_prefix_params(
const std::string& prefix)
const __attribute__ ((deprecated(
"Params::find_prefix_params() is deprecated and will be removed in SST 12. Please use the new Params::get_scoped_params() function. As of SST 12, only a \".\" will be allowed as a scoping delimiter.")));
556 Params find_scoped_params(
const std::string& scope,
const char* delims =
".:") const __attribute__ ((deprecated("
Params::find_scoped_params() is deprecated and will be removed in SST 12. Please use the new
Params::
get_scoped_params() function. As of SST 12, only a \".\" will be allowed as a scoping delimiter.")));
609 void serialize_order(SST::Core::Serialization::
serializer &ser) override;
610 ImplementSerializable(SST::
Params)
615 std::map<uint32_t, std::
string> my_data;
616 std::vector<std::map<uint32_t, std::
string>*> data;
619 static
bool g_verify_enabled;
621 static uint32_t getKey(const std::
string& str);
629 static const std::
string& getParamName(uint32_t
id);
633 friend
int ::main(
int argc,
char *argv[]);
635 static std::map<std::
string, uint32_t> keyMap;
636 static std::vector<std::
string> keyMapReverse;
637 static SST::Core::ThreadSafe::
Spinlock keyLock;
638 static SST::Core::ThreadSafe::
Spinlock globalLock;
639 static uint32_t nextKeyID;
642 static std::map<std::
string,std::map<uint32_t,std::
string> > global_params;
648 #define SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(type) \
650 type Params::find(const std::string& k, type default_value, bool &found) const; \
652 type Params::find(const std::string& k, const std::string& default_value, bool &found) const; \
654 type Params::find(const std::string& k, type default_value ) const; \
656 type Params::find(const std::string& k, const std::string& default_value ) const; \
658 type Params::find(const std::string& k) const;
661 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(int32_t)
662 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(uint32_t)
663 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(int64_t)
664 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(uint64_t)
665 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
bool)
666 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
float)
667 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
double)
668 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
UnitAlgebra)
673 std::
string Params::
find<std::
string>(const std::
string& k, const std::
string& default_value,
bool &found) const;
680 #endif //SST_CORE_PARAMS_H
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file...
Definition: output.h:54
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
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:453
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:320
Params find_prefix_params(const std::string &prefix) const
Returns a new parameter object with parameters that match the specified prefix.
Definition: params.cc:209
void pushAllowedKeys(const KeySet_t &keys)
Definition: params.cc:259
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:273
bool contains(const key_type &k) const
Search the container for a particular key.
Definition: params.cc:250
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:229
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:368
static void enableVerify()
Enable, on a global scale, parameter verification.
Definition: params.h:204
Definition: serializable.h:109
bool empty() const
Returns true if the Params is empty.
Definition: params.cc:47
void clear()
Erases all elements, including deleting reference to global param sets.
Definition: params.cc:79
Params()
Create a new, empty Params.
Definition: params.cc:52
bool enableVerify(bool enable)
Enable or disable parameter verification on an instance of Params.
Definition: params.h:197
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:335
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:386
Definition: threadsafe.h:127
size_t size() const
Returns the size of the Params.
Definition: params.cc:41
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:304
Parameter store.
Definition: params.h:44
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:342
void popAllowedKeys()
Removes the most recent set of keys considered allowed.
Definition: params.cc:265
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:288
std::set< key_type, KeyCompare > KeySet_t
Definition: params.h:188
std::string key_type
Definition: params.h:187
void verifyParam(const key_type &k) const
Definition: params.cc:274
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:335
std::set< std::string > getKeys() const
Get all the keys contained in the Params object.
Definition: params.cc:165
Params & operator=(const Params &old)
Assignment operator.
Definition: params.cc:69
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:140
Performs Unit math in full precision.
Definition: unitAlgebra.h:107
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:352
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:488
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