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;
102 inline T find_impl(
const std::string& k, T default_value,
bool &found)
const {
105 const std::string& value = getString(k,found);
109 return default_value;
115 return SST::Core::from_string<T>(value);
117 catch (
const std::invalid_argument& e ) {
118 std::string msg =
"Params::find(): No conversion for value: key = " + k +
", value = " + value +
119 ". Original error: " + e.what();
120 std::invalid_argument t(msg);
137 inline T find_impl(
const std::string& k,
const std::string& default_value,
bool &found)
const {
139 const std::string& value = getString(k,found);
142 return SST::Core::from_string<T>(default_value);
144 catch (
const std::invalid_argument& e ) {
145 std::string msg =
"Params::find(): Invalid default value specified: key = " + k +
", value = " + default_value +
146 ". Original error: " + e.what();
147 std::invalid_argument t(msg);
154 return SST::Core::from_string<T>(value);
156 catch (
const std::invalid_argument& e ) {
157 std::string msg =
"Params::find(): No conversion for value: key = " + k +
", value = " + value +
158 ". Original error: " + e.what();
159 std::invalid_argument t(msg);
165 typedef std::map<uint32_t, std::string>::const_iterator const_iterator;
167 const std::string& getString(
const std::string& name,
bool& found)
const;
178 bool enableVerify(
bool enable) {
bool old = verify_enabled; verify_enabled = enable;
return old; }
236 typename std::enable_if<not std::is_same<std::string,T>::value, T>::type
237 find(
const std::string& k, T default_value,
bool &found)
const {
238 return find_impl<T>(k,default_value,found);
252 T
find(
const std::string& k,
const std::string& default_value,
bool &found)
const {
253 return find_impl<T>(k,default_value,found);
267 typename std::enable_if<std::is_same<bool,T>::value, T>::type
268 find(
const std::string& k,
const char* default_value,
bool &found )
const {
269 if (
nullptr == default_value ) {
270 return find_impl<T>(k,
static_cast<T
>(0), found);
272 return find_impl<T>(k, std::string(default_value), found);
284 T
find(
const std::string& k, T default_value )
const {
286 return find_impl<T>(k, default_value, tmp);
299 T
find(
const std::string& k,
const std::string& default_value )
const {
301 return find_impl<T>(k, default_value, tmp);
315 typename std::enable_if<std::is_same<bool,T>::value, T>::type
316 find(
const std::string& k,
const char* default_value )
const {
318 if (
nullptr == default_value ) {
319 return find_impl<T>(k,
static_cast<T
>(0), tmp);
321 return find_impl<T>(k, std::string(default_value), tmp);
332 T
find(
const std::string& k)
const {
334 T default_value = T();
335 return find_impl<T>(k, default_value, tmp);
349 typename std::enable_if<not std::is_same<bool, T>::value, T>::type
350 find(
const std::string& k,
bool &found)
const {
351 T default_value = T();
352 return find_impl<T>(k, default_value, found);
375 std::string value = getString(k,found);
376 if ( !found )
return;
380 if( (value.find(
"[") == std::string::npos) ||
381 (value.find(
"]") == std::string::npos) ){
383 "Params::find_array(): Invalid formatting: String must be enclosed by brackets [str]";
384 std::invalid_argument t(msg);
387 value = value.substr(0,value.size()-1);
388 value = value.substr(1);
390 std::stringstream ss(value);
394 getline( ss, substr,
',' );
397 vec.push_back(SST::Core::from_string<T>(substr));
399 catch (
const std::invalid_argument& e ) {
400 std::string msg =
"Params::find(): No conversion for value: key = " + k +
", value = " + substr +
401 ". Original error: " + e.what();
402 std::invalid_argument t(msg);
409 void print_all_params(std::ostream &os,
const std::string& prefix =
"")
const;
416 void insert(
const std::string& key,
const std::string& value,
bool overwrite =
true);
420 std::set<std::string> getKeys()
const;
427 Params find_scoped_params(
const std::string& scope,
const char* delims =
".:")
const;
465 std::map<uint32_t, std::
string> data;
468 static
bool g_verify_enabled;
470 uint32_t getKey(const std::
string& str) const;
471 uint32_t getKey(const std::
string& str);
474 friend
int ::main(
int argc,
char *argv[]);
476 static std::map<std::
string, uint32_t> keyMap;
477 static std::vector<std::
string> keyMapReverse;
478 static SST::Core::ThreadSafe::
Spinlock keyLock;
479 static uint32_t nextKeyID;
487 #define SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(type) \
489 type Params::find(const std::string& k, type default_value, bool &found) const; \
491 type Params::find(const std::string& k, const std::string& default_value, bool &found) const; \
493 type Params::find(const std::string& k, type default_value ) const; \
495 type Params::find(const std::string& k, const std::string& default_value ) const; \
497 type Params::find(const std::string& k) const;
500 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(int32_t)
501 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(uint32_t)
502 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(int64_t)
503 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(uint64_t)
504 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
bool)
505 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
float)
506 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
double)
507 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
UnitAlgebra)
512 std::
string Params::
find<std::
string>(const std::
string& k, const std::
string& default_value,
bool &found) const;
519 #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:367
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:284
Params find_prefix_params(const std::string &prefix) const
Returns a new parameter object with parameters that match the specified prefix.
Definition: params.cc:156
void pushAllowedKeys(const KeySet_t &keys)
Definition: params.cc:179
bool contains(const key_type &k)
Definition: params.cc:173
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:237
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:332
static void enableVerify()
Enable, on a global scale, parameter verification.
Definition: params.h:185
size_t count(const key_type &k)
Finds the number of elements with given key.
Definition: params.cc:76
Definition: serializable.h:109
bool empty() const
Returns true if the Params is empty.
Definition: params.cc:44
static const std::string & getParamName(uint32_t id)
Given a Parameter Key ID, return the Name of the matching parameter.
Definition: params.cc:206
void clear()
Erases all elements.
Definition: params.cc:69
Params()
Create a new, empty Params.
Definition: params.cc:49
bool enableVerify(bool enable)
Enable or disable parameter verification on an instance of Params.
Definition: params.h:178
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:299
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:350
Definition: threadsafe.h:127
size_t size() const
Returns the size of the Params.
Definition: params.cc:38
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:268
Parameter store.
Definition: params.h:44
void popAllowedKeys()
Removes the most recent set of keys considered allowed.
Definition: params.cc:185
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:252
std::set< key_type, KeyCompare > KeySet_t
Definition: params.h:171
std::string key_type
Definition: params.h:170
void verifyParam(const key_type &k) const
Definition: params.cc:191
Params & operator=(const Params &old)
Assignment operator.
Definition: params.cc:61
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:99
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:316
void print_all_params(std::ostream &os, const std::string &prefix="") const
Print all key/value parameter pairs to specified ostream.
Definition: params.cc:82