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;
89 typedef std::map<uint32_t, std::string>::const_iterator const_iterator;
93 typedef std::set<key_type, KeyCompare>
KeySet_t;
100 bool enableVerify(
bool enable) {
bool old = verify_enabled; verify_enabled = enable;
return old; }
110 size_t size()
const {
return data.size(); }
112 bool empty()
const {
return data.empty(); }
116 Params() : data(), verify_enabled(true) { }
119 Params(
const Params& old) : data(old.data), allowedKeys(old.allowedKeys), verify_enabled(old.verify_enabled) { }
131 verify_enabled = old.verify_enabled;
132 allowedKeys = old.allowedKeys;
149 size_t count(
const key_type& k) {
return data.count(getKey(k)); }
163 T
find(
const std::string &k, T default_value,
bool &found)
const {
165 const_iterator i = data.find(getKey(k));
166 if (i == data.end()) {
168 return default_value;
172 return SST::Core::from_string<T>(i->second);
174 catch (
const std::invalid_argument& e ) {
175 std::string msg =
"Params::find(): No conversion for value: key = " + k +
", value = " + i->second +
176 ". Oringal error: " + e.what();
177 std::invalid_argument t(msg);
194 T
find(
const std::string &k, std::string default_value,
bool &found)
const {
196 const_iterator i = data.find(getKey(k));
197 if (i == data.end()) {
200 return SST::Core::from_string<T>(default_value);
202 catch (
const std::invalid_argument& e ) {
203 std::string msg =
"Params::find(): Invalid default value specified: key = " + k +
", value = " + i->second +
204 ". Original error: " + e.what();
205 std::invalid_argument t(msg);
211 return SST::Core::from_string<T>(i->second);
213 catch (
const std::invalid_argument& e ) {
214 std::string msg =
"Params::find(): No conversion for value: key = " + k +
", value = " + i->second +
215 ". Original error: " + e.what();
216 std::invalid_argument t(msg);
231 T
find(
const std::string &k, T default_value )
const {
233 return find<T>(k, default_value, tmp);
246 T
find(
const std::string &k, std::string default_value )
const {
248 return find<T>(k, default_value, tmp);
259 T
find(
const std::string &k)
const {
261 T default_value = T();
262 return find(k, default_value, tmp);
274 T
find(
const std::string &k,
bool &found)
const {
275 T default_value = T();
276 return find(k, default_value, found);
291 void find_array(
const key_type &k, std::vector<T>& vec)
const {
293 const_iterator i = data.find(getKey(k));
294 if ( i == data.end()) {
297 std::string value = i->second;
300 value = value.substr(0,value.size()-1);
301 value = value.substr(1);
303 std::stringstream ss(value);
307 getline( ss, substr,
',' );
310 vec.push_back(SST::Core::from_string<T>(substr));
312 catch (
const std::invalid_argument& e ) {
313 std::string msg =
"Params::find(): No conversion for value: key = " + k +
", value = " + substr +
314 ". Oringal error: " + e.what();
315 std::invalid_argument t(msg);
326 __attribute__ ((deprecated))
327 int64_t
find_integer(const key_type &k,
long default_value,
bool &found)
const {
328 return find<int64_t>(k,default_value,found);
335 __attribute__ ((deprecated))
336 int64_t
find_integer(const key_type &k,
long default_value = -1)
const {
337 return find<int64_t>(k,default_value);
347 __attribute__ ((deprecated))
349 find_array<int64_t>(k,vec);
357 __attribute__ ((deprecated))
358 double
find_floating(const key_type& k,
double default_value,
bool &found)
const {
359 return find<double>(k,default_value,found);
366 __attribute__ ((deprecated))
367 double
find_floating(const key_type& k,
double default_value = -1.0)
const {
368 return find<double>(k,default_value);
378 __attribute__ ((deprecated))
380 find_array<double>(k,vec);
388 __attribute__ ((deprecated))
390 return find<std::string>(k,default_value,found);
397 __attribute__ ((deprecated))
399 return find<std::string>(k,default_value);
409 __attribute__ ((deprecated))
411 find_array<std::string>(k,vec);
416 for (const_iterator i = data.begin() ; i != data.end() ; ++i) {
417 os << prefix <<
"key=" << keyMapReverse[i->first] <<
", value=" << i->second << std::endl;
425 void insert(std::string key, std::string value,
bool overwrite =
true) {
427 data[getKey(key)] = value;
430 uint32_t
id = getKey(key);
431 data.insert(std::make_pair(
id, value));
436 data.insert(params.data.begin(), params.data.end());
439 std::set<std::string> getKeys()
const {
440 std::set<std::string> ret;
441 for (const_iterator i = data.begin() ; i != data.end() ; ++i) {
442 ret.insert(keyMapReverse[i->first]);
453 for (const_iterator i = data.begin() ; i != data.end() ; ++i) {
454 std::string key = keyMapReverse[i->first].substr(0, prefix.length());
456 ret.
insert(keyMapReverse[i->first].substr(prefix.length()), i->second);
459 ret.allowedKeys = allowedKeys;
471 return data.find(getKey(k)) != data.end();
479 allowedKeys.push_back(keys);
486 allowedKeys.pop_back();
494 if ( !g_verify_enabled || !verify_enabled )
return;
496 for ( std::vector<KeySet_t>::const_reverse_iterator ri = allowedKeys.rbegin() ; ri != allowedKeys.rend() ; ++ri ) {
497 if ( ri->find(k) != ri->end() )
return;
500 #ifdef USE_PARAM_WARNINGS
502 outXX.
output(CALL_INFO,
"Warning: Parameter \"%s\" is undocumented.\n", k.c_str());
514 return keyMapReverse[id];
525 std::map<uint32_t,
std::
string> data;
526 std::vector<KeySet_t> allowedKeys;
528 static
bool g_verify_enabled;
530 uint32_t getKey(const
std::
string &str)
const
532 std::lock_guard<SST::Core::ThreadSafe::Spinlock> lock(keyLock);
533 std::map<std::string, uint32_t>::iterator i = keyMap.find(str);
534 if ( i == keyMap.end() ) {
540 uint32_t getKey(
const std::string &str)
542 std::lock_guard<SST::Core::ThreadSafe::Spinlock> lock(keyLock);
543 std::map<std::string, uint32_t>::iterator i = keyMap.find(str);
544 if ( i == keyMap.end() ) {
545 uint32_t
id = nextKeyID++;
546 keyMap.insert(std::make_pair(str,
id));
547 keyMapReverse.push_back(str);
548 assert(keyMapReverse.size() == nextKeyID);
555 friend int ::main(
int argc,
char *argv[]);
557 static std::map<std::string, uint32_t> keyMap;
558 static std::vector<std::string> keyMapReverse;
560 static uint32_t nextKeyID;
567 #endif //SST_CORE_PARAMS_H
Output object provides consistant method for outputing data to stdout, stderr and/or sst debug file...
Definition: output.h:54
Params(const Params &old)
Create a copy of a Params object.
Definition: params.h:119
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:291
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:231
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:194
void pushAllowedKeys(const KeySet_t &keys)
Definition: params.h:478
bool contains(const key_type &k)
Definition: params.h:470
Params find_prefix_params(std::string prefix) const
Returns a new parameter object with parameters that match the specified prefix.
Definition: params.h:450
std::string find_string(const key_type &k, std::string default_value, bool &found) const
Find a Parameter value in the set, and return its value.
Definition: params.h:389
void find_integer_array(const key_type &k, std::vector< int64_t > &vec) const
Find a Parameter value in the set, and return its value as a vector of integers.
Definition: params.h:348
void insert(std::string key, std::string value, bool overwrite=true)
Add a key value pair into the param object.
Definition: params.h:425
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:246
Params & operator=(const Params &old)
Assignment operator.
Definition: params.h:129
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:259
static void enableVerify()
Enable, on a global scale, parameter verification.
Definition: params.h:107
void print_all_params(std::ostream &os, std::string prefix="") const
Print all key/value parameter pairs to specified ostream.
Definition: params.h:415
size_t count(const key_type &k)
Finds the number of elements with given key.
Definition: params.h:149
Definition: serializable.h:108
bool empty() const
Returns true if the Params is empty.
Definition: params.h:112
int64_t find_integer(const key_type &k, long default_value, bool &found) const
Find a Parameter value in the set, and return its value as an integer.
Definition: params.h:327
void clear()
Erases all elements.
Definition: params.h:139
void find_string_array(const key_type &k, std::vector< std::string > &vec) const
Find a Parameter value in the set, and return its value as a vector of strings.
Definition: params.h:410
static const std::string & getParamName(uint32_t id)
Given a Parameter Key ID, return the Name of the matching parameter.
Definition: params.h:512
void find_floating_array(const key_type &k, std::vector< double > &vec) const
Find a Parameter value in the set, and return its value as a vector of floats.
Definition: params.h:379
Params()
Create a new, empty Params.
Definition: params.h:116
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:163
bool enableVerify(bool enable)
Enable or disable parameter verification on an instance of Params.
Definition: params.h:100
void output(uint32_t line, const char *file, const char *func, const char *format,...) const
Output the message with formatting as specified by the format parameter.
Definition: output.h:184
double find_floating(const key_type &k, double default_value, bool &found) const
Find a Parameter value in the set, and return its value as a double.
Definition: params.h:358
Definition: threadsafe.h:101
size_t size() const
Returns the size of the Params.
Definition: params.h:110
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:274
Parameter store.
Definition: params.h:44
void popAllowedKeys()
Removes the most recent set of keys considered allowed.
Definition: params.h:485
std::set< key_type, KeyCompare > KeySet_t
Definition: params.h:93
std::string key_type
Definition: params.h:92
void verifyParam(const key_type &k) const
Definition: params.h:493