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;
90 typedef std::map<uint32_t, std::string>::const_iterator const_iterator;
94 typedef std::set<key_type, KeyCompare>
KeySet_t;
101 bool enableVerify(
bool enable) {
bool old = verify_enabled; verify_enabled = enable;
return old; }
111 size_t size()
const {
return data.size(); }
113 bool empty()
const {
return data.empty(); }
117 Params() : data(), verify_enabled(true) { }
120 Params(
const Params& old) : data(old.data), allowedKeys(old.allowedKeys), verify_enabled(old.verify_enabled) { }
132 verify_enabled = old.verify_enabled;
133 allowedKeys = old.allowedKeys;
150 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);
195 T
find(
const std::string &k, std::string default_value,
bool &found)
const {
197 const_iterator i = data.find(getKey(k));
198 if (i == data.end()) {
201 return SST::Core::from_string<T>(default_value);
203 catch (
const std::invalid_argument& e ) {
204 std::string msg =
"Params::find(): Invalid default value specified: key = " + k +
", value = " + i->second +
205 ". Original error: " + e.what();
206 std::invalid_argument t(msg);
212 return SST::Core::from_string<T>(i->second);
214 catch (
const std::invalid_argument& e ) {
215 std::string msg =
"Params::find(): No conversion for value: key = " + k +
", value = " + i->second +
216 ". Original error: " + e.what();
217 std::invalid_argument t(msg);
232 T
find(
const std::string &k, T default_value )
const {
234 return find<T>(k, default_value, tmp);
247 T
find(
const std::string &k, std::string default_value )
const {
249 return find<T>(k, default_value, tmp);
260 T
find(
const std::string &k)
const {
262 T default_value = T();
263 return find(k, default_value, tmp);
275 T
find(
const std::string &k,
bool &found)
const {
276 T default_value = T();
277 return find(k, default_value, found);
292 void find_array(
const key_type &k, std::vector<T>& vec)
const {
294 const_iterator i = data.find(getKey(k));
295 if ( i == data.end()) {
298 std::string value = i->second;
302 if( (value.find(
"[") == std::string::npos) ||
303 (value.find(
"]") == std::string::npos) ){
305 "Params::find_array(): Invalid formatting: String must be enclosed by brackets [str]";
306 std::invalid_argument t(msg);
309 value = value.substr(0,value.size()-1);
310 value = value.substr(1);
312 std::stringstream ss(value);
316 getline( ss, substr,
',' );
319 vec.push_back(SST::Core::from_string<T>(substr));
321 catch (
const std::invalid_argument& e ) {
322 std::string msg =
"Params::find(): No conversion for value: key = " + k +
", value = " + substr +
323 ". Oringal error: " + e.what();
324 std::invalid_argument t(msg);
332 for (const_iterator i = data.begin() ; i != data.end() ; ++i) {
333 os << prefix <<
"key=" << keyMapReverse[i->first] <<
", value=" << i->second << std::endl;
338 for (const_iterator i = data.begin() ; i != data.end() ; ++i) {
339 out.
output(
"%s%s = %s\n", prefix.c_str(), keyMapReverse[i->first].c_str(), i->second.c_str());
347 void insert(std::string key, std::string value,
bool overwrite =
true) {
349 data[getKey(key)] = value;
352 uint32_t
id = getKey(key);
353 data.insert(std::make_pair(
id, value));
358 data.insert(params.data.begin(), params.data.end());
361 std::set<std::string> getKeys()
const {
362 std::set<std::string> ret;
363 for (const_iterator i = data.begin() ; i != data.end() ; ++i) {
364 ret.insert(keyMapReverse[i->first]);
375 for (const_iterator i = data.begin() ; i != data.end() ; ++i) {
376 std::string key = keyMapReverse[i->first].substr(0, prefix.length());
378 ret.
insert(keyMapReverse[i->first].substr(prefix.length()), i->second);
381 ret.allowedKeys = allowedKeys;
393 return data.find(getKey(k)) != data.end();
401 allowedKeys.push_back(keys);
408 allowedKeys.pop_back();
416 if ( !g_verify_enabled || !verify_enabled )
return;
418 for ( std::vector<KeySet_t>::const_reverse_iterator ri = allowedKeys.rbegin() ; ri != allowedKeys.rend() ; ++ri ) {
419 if ( ri->find(k) != ri->end() )
return;
422 #ifdef USE_PARAM_WARNINGS 424 outXX.
output(CALL_INFO,
"Warning: Parameter \"%s\" is undocumented.\n", k.c_str());
436 return keyMapReverse[id];
447 std::map<uint32_t, std::string> data;
448 std::vector<KeySet_t> allowedKeys;
450 static bool g_verify_enabled;
452 uint32_t getKey(
const std::string &str)
const 454 std::lock_guard<SST::Core::ThreadSafe::Spinlock> lock(keyLock);
455 std::map<std::string, uint32_t>::iterator i = keyMap.find(str);
456 if ( i == keyMap.end() ) {
462 uint32_t getKey(
const std::string &str)
464 std::lock_guard<SST::Core::ThreadSafe::Spinlock> lock(keyLock);
465 std::map<std::string, uint32_t>::iterator i = keyMap.find(str);
466 if ( i == keyMap.end() ) {
467 uint32_t
id = nextKeyID++;
468 keyMap.insert(std::make_pair(str,
id));
469 keyMapReverse.push_back(str);
470 assert(keyMapReverse.size() == nextKeyID);
477 friend int ::main(
int argc,
char *argv[]);
479 static std::map<std::string, uint32_t> keyMap;
480 static std::vector<std::string> keyMapReverse;
482 static uint32_t nextKeyID;
489 #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:120
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
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:232
void pushAllowedKeys(const KeySet_t &keys)
Definition: params.h:400
bool contains(const key_type &k)
Definition: params.h:392
Params find_prefix_params(std::string prefix) const
Returns a new parameter object with parameters that match the specified prefix.
Definition: params.h:372
void insert(std::string key, std::string value, bool overwrite=true)
Add a key value pair into the param object.
Definition: params.h:347
Params & operator=(const Params &old)
Assignment operator.
Definition: params.h:130
static void enableVerify()
Enable, on a global scale, parameter verification.
Definition: params.h:108
size_t count(const key_type &k)
Finds the number of elements with given key.
Definition: params.h:150
Definition: serializable.h:109
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:292
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:195
void clear()
Erases all elements.
Definition: params.h:140
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
static const std::string & getParamName(uint32_t id)
Given a Parameter Key ID, return the Name of the matching parameter.
Definition: params.h:434
Params()
Create a new, empty Params.
Definition: params.h:117
bool enableVerify(bool enable)
Enable or disable parameter verification on an instance of Params.
Definition: params.h:101
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
Definition: threadsafe.h:127
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:247
Parameter store.
Definition: params.h:45
void popAllowedKeys()
Removes the most recent set of keys considered allowed.
Definition: params.h:407
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:275
std::set< key_type, KeyCompare > KeySet_t
Definition: params.h:94
std::string key_type
Definition: params.h:93
bool empty() const
Returns true if the Params is empty.
Definition: params.h:113
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:260
void print_all_params(std::ostream &os, std::string prefix="") const
Print all key/value parameter pairs to specified ostream.
Definition: params.h:331
size_t size() const
Returns the size of the Params.
Definition: params.h:111
void verifyParam(const key_type &k) const
Definition: params.h:415