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>
30 #include <sst/core/output.h>
32 int main(
int argc,
char *argv[]);
47 struct KeyCompare : std::binary_function<std::string, std::string, bool>
49 bool operator()(
const std::string& X,
const std::string& Y)
const
51 const char *x = X.c_str();
52 const char *y = Y.c_str();
54 #define EAT_VAR(A, B) \
56 if ( *x == '%' && (*(x+1) == '(' || *(x+1) == 'd')) { \
60 do { x++; } while ( *x && *x != ')' ); \
63 if ( *x != 'd' ) goto NO_VARIABLE; \
66 while ( *y && isdigit(*y) ) y++; \
75 if (
'\0' == *x )
return false;
79 if ( *x < *y )
return true;
83 if ( !(*x) && (*y) )
return true;
103 inline T find_impl(
const std::string &k, T default_value,
bool &found)
const {
106 const std::string& value = getString(k,found);
110 return default_value;
116 return SST::Core::from_string<T>(value);
118 catch (
const std::invalid_argument& e ) {
119 std::string msg =
"Params::find(): No conversion for value: key = " + k +
", value = " + value +
120 ". Original error: " + e.what();
121 std::invalid_argument t(msg);
138 inline T find_impl(
const std::string &k, std::string default_value,
bool &found)
const {
140 const std::string& value = getString(k,found);
143 return SST::Core::from_string<T>(default_value);
145 catch (
const std::invalid_argument& e ) {
146 std::string msg =
"Params::find(): Invalid default value specified: key = " + k +
", value = " + default_value +
147 ". Original error: " + e.what();
148 std::invalid_argument t(msg);
155 return SST::Core::from_string<T>(value);
157 catch (
const std::invalid_argument& e ) {
158 std::string msg =
"Params::find(): No conversion for value: key = " + k +
", value = " + value +
159 ". Original error: " + e.what();
160 std::invalid_argument t(msg);
166 typedef std::map<uint32_t, std::string>::const_iterator const_iterator;
168 const std::string& getString(
const std::string& name,
bool& found)
const;
179 bool enableVerify(
bool enable) {
bool old = verify_enabled; verify_enabled = enable;
return old; }
237 typename std::enable_if<not std::is_same<std::string,T>::value, T>::type
238 find(
const std::string &k, T default_value,
bool &found)
const {
239 return find_impl<T>(k,default_value,found);
253 T
find(
const std::string &k, std::string default_value,
bool &found)
const {
254 return find_impl<T>(k,default_value,found);
268 typename std::enable_if<std::is_same<bool,T>::value, T>::type
269 find(
const std::string &k,
const char* default_value,
bool &found )
const {
270 if ( 0 == default_value ) {
271 return find_impl<T>(k,
static_cast<T
>(0), found);
273 return find_impl<T>(k, std::string(default_value), found);
285 T
find(
const std::string &k, T default_value )
const {
287 return find_impl<T>(k, default_value, tmp);
300 T
find(
const std::string &k, std::string default_value )
const {
302 return find_impl<T>(k, default_value, tmp);
316 typename std::enable_if<std::is_same<bool,T>::value, T>::type
317 find(
const std::string &k,
const char* default_value )
const {
319 if ( 0 == default_value ) {
320 return find_impl<T>(k,
static_cast<T
>(0), tmp);
322 return find_impl<T>(k, std::string(default_value), tmp);
333 T
find(
const std::string &k)
const {
335 T default_value = T();
336 return find_impl<T>(k, default_value, tmp);
350 typename std::enable_if<not std::is_same<bool, T>::value, T>::type
351 find(
const std::string &k,
bool &found)
const {
352 T default_value = T();
353 return find_impl<T>(k, default_value, found);
376 std::string value = getString(k,found);
377 if ( !found )
return;
381 if( (value.find(
"[") == std::string::npos) ||
382 (value.find(
"]") == std::string::npos) ){
384 "Params::find_array(): Invalid formatting: String must be enclosed by brackets [str]";
385 std::invalid_argument t(msg);
388 value = value.substr(0,value.size()-1);
389 value = value.substr(1);
391 std::stringstream ss(value);
395 getline( ss, substr,
',' );
398 vec.push_back(SST::Core::from_string<T>(substr));
400 catch (
const std::invalid_argument& e ) {
401 std::string msg =
"Params::find(): No conversion for value: key = " + k +
", value = " + substr +
402 ". Original error: " + e.what();
403 std::invalid_argument t(msg);
417 void insert(std::string key, std::string value,
bool overwrite =
true);
421 std::set<std::string> getKeys()
const;
428 Params find_scoped_params(
const std::string& scope,
const char* delims =
".:")
const;
466 std::map<uint32_t, std::
string> data;
469 static
bool g_verify_enabled;
471 uint32_t getKey(const std::
string &str) const;
472 uint32_t getKey(const std::
string &str);
475 friend
int ::main(
int argc,
char *argv[]);
477 static std::map<std::
string, uint32_t> keyMap;
478 static std::vector<std::
string> keyMapReverse;
479 static SST::Core::ThreadSafe::
Spinlock keyLock;
480 static uint32_t nextKeyID;
488 #define SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(type) \
490 type Params::find(const std::string &k, type default_value, bool &found) const; \
492 type Params::find(const std::string &k, std::string default_value, bool &found) const; \
494 type Params::find(const std::string &k, type default_value ) const; \
496 type Params::find(const std::string &k, std::string default_value ) const; \
498 type Params::find(const std::string &k) const;
501 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(int32_t)
502 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(uint32_t)
503 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(int64_t)
504 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(uint64_t)
505 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
bool)
506 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
float)
507 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
double)
508 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
UnitAlgebra)
513 std::
string Params::
find<std::
string>(const std::
string &k, std::
string default_value,
bool &found) const;
520 #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:368
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:285
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
T find(const std::string &k, 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:253
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:238
void insert(std::string key, std::string value, bool overwrite=true)
Add a key value pair into the param object.
Definition: params.cc:99
T find(const std::string &k, std::string default_value) const
Find a Parameter value in the set, and return its value as a type T.
Definition: params.h:300
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:333
static void enableVerify()
Enable, on a global scale, parameter verification.
Definition: params.h:186
void print_all_params(std::ostream &os, std::string prefix="") const
Print all key/value parameter pairs to specified ostream.
Definition: params.cc:82
size_t count(const key_type &k)
Finds the number of elements with given key.
Definition: params.cc:76
Definition: serializable.h:110
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:179
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:351
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:269
Parameter store.
Definition: params.h:45
void popAllowedKeys()
Removes the most recent set of keys considered allowed.
Definition: params.cc:185
std::set< key_type, KeyCompare > KeySet_t
Definition: params.h:172
std::string key_type
Definition: params.h:171
void verifyParam(const key_type &k) const
Definition: params.cc:191
Params & operator=(const Params &old)
Assignment operator.
Definition: params.cc:61
Performs Unit math in full precision.
Definition: unitAlgebra.h:104
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:317