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[]);
48 struct KeyCompare : std::binary_function<std::string, std::string, bool>
50 bool operator()(
const std::string& X,
const std::string& Y)
const
52 const char *x = X.c_str();
53 const char *y = Y.c_str();
55 #define EAT_VAR(A, B) \
57 if ( *x == '%' && (*(x+1) == '(' || *(x+1) == 'd')) { \
61 do { x++; } while ( *x && *x != ')' ); \
64 if ( *x != 'd' ) goto NO_VARIABLE; \
67 while ( *y && isdigit(*y) ) y++; \
76 if (
'\0' == *x )
return false;
80 if ( *x < *y )
return true;
84 if ( !(*x) && (*y) )
return true;
91 typedef std::map<uint32_t, std::string>::const_iterator const_iterator;
95 typedef std::set<key_type, KeyCompare>
KeySet_t;
102 bool enableVerify(
bool enable) {
bool old = verify_enabled; verify_enabled = enable;
return old; }
112 size_t size()
const {
return data.size(); }
114 bool empty()
const {
return data.empty(); }
118 Params() : data(), verify_enabled(true) { }
121 Params(
const Params& old) : data(old.data), allowedKeys(old.allowedKeys), verify_enabled(old.verify_enabled) { }
134 verify_enabled = old.verify_enabled;
135 allowedKeys = old.allowedKeys;
156 size_t count(
const key_type& k) {
return data.count(getKey(k)); }
164 T
find(
const std::string &k, T default_value,
bool &found)
const {
166 const_iterator i = data.find(getKey(k));
167 if (i == data.end()) {
169 return default_value;
173 return SST::Core::from_string<T>(i->second);
175 catch (
const std::invalid_argument& e ) {
176 std::string msg =
"Params::find(): No conversion for value: key = " + k +
", value = " + i->second +
177 ". Oringal error: " + e.what();
178 std::invalid_argument t(msg);
189 T
find(
const std::string &k, T default_value )
const {
191 return find(k, default_value, tmp);
198 T
find(
const std::string &k)
const {
200 T default_value = T();
201 return find(k, default_value, tmp);
209 T
find(
const std::string &k,
bool &found)
const {
210 T default_value = T();
211 return find(k, default_value, found);
222 void find_array(
const key_type &k, std::vector<T>& vec)
const {
224 const_iterator i = data.find(getKey(k));
225 if ( i == data.end()) {
228 std::string value = i->second;
231 value = value.substr(0,value.size()-1);
232 value = value.substr(1);
234 std::stringstream ss(value);
238 getline( ss, substr,
',' );
241 vec.push_back(SST::Core::from_string<T>(substr));
243 catch (
const std::invalid_argument& e ) {
244 std::string msg =
"Params::find(): No conversion for value: key = " + k +
", value = " + substr +
245 ". Oringal error: " + e.what();
246 std::invalid_argument t(msg);
257 __attribute__ ((deprecated))
258 int64_t
find_integer(const key_type &k,
long default_value,
bool &found)
const {
259 return find<int64_t>(k,default_value,found);
266 __attribute__ ((deprecated))
267 int64_t
find_integer(const key_type &k,
long default_value = -1)
const {
268 return find<int64_t>(k,default_value);
278 __attribute__ ((deprecated))
280 find_array<int64_t>(k,vec);
288 __attribute__ ((deprecated))
289 double
find_floating(const key_type& k,
double default_value,
bool &found)
const {
290 return find<double>(k,default_value,found);
297 __attribute__ ((deprecated))
298 double
find_floating(const key_type& k,
double default_value = -1.0)
const {
299 return find<double>(k,default_value);
309 __attribute__ ((deprecated))
311 find_array<double>(k,vec);
319 __attribute__ ((deprecated))
321 return find<std::string>(k,default_value,found);
328 __attribute__ ((deprecated))
330 return find<std::string>(k,default_value);
340 __attribute__ ((deprecated))
342 find_array<std::string>(k,vec);
347 for (const_iterator i = data.begin() ; i != data.end() ; ++i) {
348 os << prefix <<
"key=" << keyMapReverse[i->first] <<
", value=" << i->second << std::endl;
356 void insert(std::string key, std::string value,
bool overwrite =
true) {
358 data[getKey(key)] = value;
361 uint32_t
id = getKey(key);
362 data.insert(std::make_pair(
id, value));
367 data.insert(params.data.begin(), params.data.end());
370 std::set<std::string> getKeys()
const {
371 std::set<std::string> ret;
372 for (const_iterator i = data.begin() ; i != data.end() ; ++i) {
373 ret.insert(keyMapReverse[i->first]);
384 for (const_iterator i = data.begin() ; i != data.end() ; ++i) {
385 std::string key = keyMapReverse[i->first].substr(0, prefix.length());
387 ret.
insert(keyMapReverse[i->first].substr(prefix.length()), i->second);
390 ret.allowedKeys = allowedKeys;
402 return data.find(getKey(k)) != data.end();
410 allowedKeys.push_back(keys);
417 allowedKeys.pop_back();
425 if ( !g_verify_enabled || !verify_enabled )
return;
427 for ( std::vector<KeySet_t>::const_reverse_iterator ri = allowedKeys.rbegin() ; ri != allowedKeys.rend() ; ++ri ) {
428 if ( ri->find(k) != ri->end() )
return;
431 #ifdef USE_PARAM_WARNINGS
433 outXX.
output(CALL_INFO,
"Warning: Parameter \"%s\" is undocumented.\n", k.c_str());
445 return keyMapReverse[id];
456 std::map<uint32_t,
std::
string> data;
457 std::vector<KeySet_t> allowedKeys;
459 static
bool g_verify_enabled;
461 uint32_t getKey(const
std::
string &str)
const
463 std::lock_guard<SST::Core::ThreadSafe::Spinlock> lock(keyLock);
464 std::map<std::string, uint32_t>::iterator i = keyMap.find(str);
465 if ( i == keyMap.end() ) {
471 uint32_t getKey(
const std::string &str)
473 std::lock_guard<SST::Core::ThreadSafe::Spinlock> lock(keyLock);
474 std::map<std::string, uint32_t>::iterator i = keyMap.find(str);
475 if ( i == keyMap.end() ) {
476 uint32_t
id = nextKeyID++;
477 keyMap.insert(std::make_pair(str,
id));
478 keyMapReverse.push_back(str);
479 assert(keyMapReverse.size() == nextKeyID);
486 friend int ::main(
int argc,
char *argv[]);
488 static std::map<std::string, uint32_t> keyMap;
489 static std::vector<std::string> keyMapReverse;
491 static uint32_t nextKeyID;
498 #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:121
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:222
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:189
void pushAllowedKeys(const KeySet_t &keys)
Definition: params.h:409
bool contains(const key_type &k)
Definition: params.h:401
Params find_prefix_params(std::string prefix) const
Returns a new parameter object with parameters that match the specified prefix.
Definition: params.h:381
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:320
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:279
void insert(std::string key, std::string value, bool overwrite=true)
Add a key value pair into the param object.
Definition: params.h:356
Params & operator=(const Params &old)
Map assignment operator.
Definition: params.h:132
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:198
static void enableVerify()
Enable, on a global scale, parameter verification.
Definition: params.h:109
void print_all_params(std::ostream &os, std::string prefix="") const
Print all key/value parameter pairs to specified ostream.
Definition: params.h:346
size_t count(const key_type &k)
Finds the number of elements with given key.
Definition: params.h:156
Definition: serializable.h:108
bool empty() const
Returns true if the Params is empty.
Definition: params.h:114
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:258
void clear()
Erases all elements in a map.
Definition: params.h:145
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:341
static const std::string & getParamName(uint32_t id)
Given a Parameter Key ID, return the Name of the matching parameter.
Definition: params.h:443
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:310
Params()
Create a new, empty Params.
Definition: params.h:118
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:164
bool enableVerify(bool enable)
Enable or disable parameter verification on an instance of Params.
Definition: params.h:102
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:289
Definition: threadsafe.h:101
size_t size() const
Returns the size of the Params.
Definition: params.h:112
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:209
Parameter store.
Definition: params.h:46
void popAllowedKeys()
Removes the most recent set of keys considered allowed.
Definition: params.h:416
std::set< key_type, KeyCompare > KeySet_t
Definition: params.h:95
std::string key_type
Definition: params.h:94
void verifyParam(const key_type &k) const
Definition: params.h:424