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;
301 value = value.substr(0,value.size()-1);
302 value = value.substr(1);
304 std::stringstream ss(value);
308 getline( ss, substr,
',' );
311 vec.push_back(SST::Core::from_string<T>(substr));
313 catch (
const std::invalid_argument& e ) {
314 std::string msg =
"Params::find(): No conversion for value: key = " + k +
", value = " + substr +
315 ". Oringal error: " + e.what();
316 std::invalid_argument t(msg);
324 for (const_iterator i = data.begin() ; i != data.end() ; ++i) {
325 os << prefix <<
"key=" << keyMapReverse[i->first] <<
", value=" << i->second << std::endl;
330 for (const_iterator i = data.begin() ; i != data.end() ; ++i) {
331 out.
output(
"%s%s = %s\n", prefix.c_str(), keyMapReverse[i->first].c_str(), i->second.c_str());
339 void insert(std::string key, std::string value,
bool overwrite =
true) {
341 data[getKey(key)] = value;
344 uint32_t
id = getKey(key);
345 data.insert(std::make_pair(
id, value));
350 data.insert(params.data.begin(), params.data.end());
353 std::set<std::string> getKeys()
const {
354 std::set<std::string> ret;
355 for (const_iterator i = data.begin() ; i != data.end() ; ++i) {
356 ret.insert(keyMapReverse[i->first]);
367 for (const_iterator i = data.begin() ; i != data.end() ; ++i) {
368 std::string key = keyMapReverse[i->first].substr(0, prefix.length());
370 ret.
insert(keyMapReverse[i->first].substr(prefix.length()), i->second);
373 ret.allowedKeys = allowedKeys;
385 return data.find(getKey(k)) != data.end();
393 allowedKeys.push_back(keys);
400 allowedKeys.pop_back();
408 if ( !g_verify_enabled || !verify_enabled )
return;
410 for ( std::vector<KeySet_t>::const_reverse_iterator ri = allowedKeys.rbegin() ; ri != allowedKeys.rend() ; ++ri ) {
411 if ( ri->find(k) != ri->end() )
return;
414 #ifdef USE_PARAM_WARNINGS
416 outXX.
output(CALL_INFO,
"Warning: Parameter \"%s\" is undocumented.\n", k.c_str());
428 return keyMapReverse[id];
439 std::map<uint32_t,
std::
string> data;
440 std::vector<KeySet_t> allowedKeys;
442 static
bool g_verify_enabled;
444 uint32_t getKey(const
std::
string &str)
const
446 std::lock_guard<SST::Core::ThreadSafe::Spinlock> lock(keyLock);
447 std::map<std::string, uint32_t>::iterator i = keyMap.find(str);
448 if ( i == keyMap.end() ) {
454 uint32_t getKey(
const std::string &str)
456 std::lock_guard<SST::Core::ThreadSafe::Spinlock> lock(keyLock);
457 std::map<std::string, uint32_t>::iterator i = keyMap.find(str);
458 if ( i == keyMap.end() ) {
459 uint32_t
id = nextKeyID++;
460 keyMap.insert(std::make_pair(str,
id));
461 keyMapReverse.push_back(str);
462 assert(keyMapReverse.size() == nextKeyID);
469 friend int ::main(
int argc,
char *argv[]);
471 static std::map<std::string, uint32_t> keyMap;
472 static std::vector<std::string> keyMapReverse;
474 static uint32_t nextKeyID;
481 #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
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, T default_value) const
Find a Parameter value in the set, and return its value as a type T.
Definition: params.h:232
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 pushAllowedKeys(const KeySet_t &keys)
Definition: params.h:392
bool contains(const key_type &k)
Definition: params.h:384
Params find_prefix_params(std::string prefix) const
Returns a new parameter object with parameters that match the specified prefix.
Definition: params.h:364
void insert(std::string key, std::string value, bool overwrite=true)
Add a key value pair into the param object.
Definition: params.h:339
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
Params & operator=(const Params &old)
Assignment operator.
Definition: params.h:130
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
static void enableVerify()
Enable, on a global scale, parameter verification.
Definition: params.h:108
void print_all_params(std::ostream &os, std::string prefix="") const
Print all key/value parameter pairs to specified ostream.
Definition: params.h:323
size_t count(const key_type &k)
Finds the number of elements with given key.
Definition: params.h:150
Definition: serializable.h:109
bool empty() const
Returns true if the Params is empty.
Definition: params.h:113
void clear()
Erases all elements.
Definition: params.h:140
static const std::string & getParamName(uint32_t id)
Given a Parameter Key ID, return the Name of the matching parameter.
Definition: params.h:426
Params()
Create a new, empty Params.
Definition: params.h:117
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:101
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
Definition: threadsafe.h:116
size_t size() const
Returns the size of the Params.
Definition: params.h:111
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
Parameter store.
Definition: params.h:45
void popAllowedKeys()
Removes the most recent set of keys considered allowed.
Definition: params.h:399
std::set< key_type, KeyCompare > KeySet_t
Definition: params.h:94
std::string key_type
Definition: params.h:93
void verifyParam(const key_type &k) const
Definition: params.h:407