SST 16.0.0
Structural Simulation Toolkit
params.h
1// Copyright 2009-2026 NTESS. Under the terms
2// of Contract DE-NA0003525 with NTESS, the U.S.
3// Government retains certain rights in this software.
4//
5// Copyright (c) 2009-2026, NTESS
6// All rights reserved.
7//
8// This file is part of the SST software package. For license
9// information, see the LICENSE file in the top level directory of the
10// distribution.
11
12#ifndef SST_CORE_PARAM_H
13#define SST_CORE_PARAM_H
14
15#include "sst/core/from_string.h"
16#include "sst/core/serialization/serializable.h"
17#include "sst/core/serialization/serializer.h"
18#include "sst/core/threadsafe.h"
19
20#include <cassert>
21#include <cstddef>
22#include <cstdint>
23#include <inttypes.h>
24#include <iostream>
25#include <map>
26#include <mutex>
27#include <ostream>
28#include <set>
29#include <sstream>
30#include <stack>
31#include <stdexcept>
32#include <stdlib.h>
33#include <string>
34#include <type_traits>
35#include <utility>
36#include <vector>
37
38int main(int argc, char* argv[]);
39
40namespace SST {
41
42class ConfigGraph;
43class ConfigComponent;
45class Output;
47
48namespace Core {
50} // namespace Core
51
52/**
53 * Parameter store.
54 *
55 * Stores key-value pairs as std::strings and provides
56 * a templated find method for finding values and converting
57 * them to arbitrary types (@see find()).
58 *
59 * NOTE: Params objects should only be used for simulation
60 * initialization, a Params object should not be used as part of an
61 * event, as serialization of Params objects only works correctly as
62 * part of ConfigGraph serialization.
63 */
64class Params
65{
66private:
67 struct KeyCompare
68 {
69 bool operator()(const std::string& X, const std::string& Y) const
70 {
71 const char* x = X.c_str();
72 const char* y = Y.c_str();
73
74#define EAT_VAR(A, B) \
75 do { \
76 if ( *x == '%' && (*(x + 1) == '(' || *(x + 1) == 'd') ) { \
77 /* We need to eat off some tokens */ \
78 ++x; \
79 if ( *x == '(' ) { \
80 do { \
81 x++; \
82 } while ( *x && *x != ')' ); \
83 x++; /* *x should now == 'd' */ \
84 } \
85 if ( *x != 'd' ) goto NO_VARIABLE; \
86 x++; /* Finish eating the variable */ \
87 /* Now, eat of digits of Y */ \
88 while ( *y && isdigit(*y) ) \
89 y++; \
90 } \
91 } while ( 0 )
92
93 do {
94 EAT_VAR(x, y);
95 EAT_VAR(y, x);
96NO_VARIABLE:
97 if ( *x == *y ) {
98 if ( '\0' == *x ) return false;
99 x++;
100 y++;
101 }
102 else {
103 if ( *x < *y ) return true;
104 return false;
105 }
106 } while ( *x && *y );
107 if ( !(*x) && (*y) ) return true;
108 return false;
109
110#undef EAT_VAR
111 }
112 };
113
114 /** Private utility function to convert a value to the specified
115 * type and check for errors. Key is passed only in case an error
116 * message needs to be generated.
117 *
118 * Type T must be either a basic numeric type (including bool) ,
119 * a std::string, or a class that has a constructor with a std::string
120 * as its only parameter. This class uses SST::Core::from_string to
121 * do the conversion.
122 * @param k - Parameter name
123 * @throw std::invalid_argument If value in (key, value) can't be
124 * converted to type T, an invalid_argument exception is thrown.
125 */
126 template <class T>
127 inline T convert_value(const std::string& key, const std::string& val) const
128 {
129 try {
130 return SST::Core::from_string<T>(val);
131 }
132 catch ( const std::invalid_argument& e ) {
133 std::string msg = "Params::find(): No conversion for value: key = " + key + ", value = " + val +
134 ". Original error: " + e.what();
135 std::invalid_argument t(msg);
136 throw t;
137 }
138 }
139
140 /** Private utility function to find a Parameter value in the set,
141 * and return its value as a type T.
142 *
143 * Type T must be either a basic numeric type (including bool) ,
144 * a std::string, or a class that has a constructor with a std::string
145 * as its only parameter. This class uses SST::Core::from_string to
146 * do the conversion.
147 * @param k - Parameter name
148 * @param default_value - Default value to return if parameter isn't found
149 * @param found - set to true if the the parameter was found
150 * @throw std::invalid_argument If value in (key, value) can't be
151 * converted to type T, an invalid_argument exception is thrown.
152 */
153 template <class T>
154 inline T find_impl(const std::string& k, T default_value, bool& found) const
155 {
156 verifyKey(k);
157 // const_iterator i = data.find(getKey(k));
158 const std::string& value = getString(k, found);
159 if ( !found ) {
160 return default_value;
161 }
162 else {
163 return convert_value<T>(k, value);
164 }
165 }
166
167 /** Find a Parameter value in the set, and return its value as a type T.
168 * Type T must be either a basic numeric type (including bool) ,
169 * a std::string, or a class that has a constructor with a std::string
170 * as its only parameter. This class uses SST::Core::from_string to
171 * do the conversion.
172 * @param k - Parameter name
173 * @param default_value - Default value to return if parameter isn't found,
174 * specified as a string
175 * @param found - set to true if the the parameter was found
176 */
177 template <class T>
178 inline T find_impl(const std::string& k, const std::string& default_value, bool& found) const
179 {
180 verifyKey(k);
181 const std::string& value = getString(k, found);
182 if ( !found ) {
183 try {
184 return SST::Core::from_string<T>(default_value);
185 }
186 catch ( const std::invalid_argument& e ) {
187 std::string msg = "Params::find(): Invalid default value specified: key = " + k +
188 ", value = " + default_value + ". Original error: " + e.what();
189 std::invalid_argument t(msg);
190 throw t;
191 }
192 }
193 else {
194 found = true;
195 try {
196 return SST::Core::from_string<T>(value);
197 }
198 catch ( const std::invalid_argument& e ) {
199 std::string msg = "Params::find(): No conversion for value: key = " + k + ", value = " + value +
200 ". Original error: " + e.what();
201 std::invalid_argument t(msg);
202 throw t;
203 }
204 }
205 }
206
207 using const_iterator = std::map<uint32_t, std::string>::const_iterator; /*!< Const Iterator type */
208
209 const std::string& getString(const std::string& name, bool& found) const;
210
211 /**
212 Private function to clean up a token. It will remove leading
213 and trailing whitespace, leading and trailing quotes (single or
214 double), and the backslash (\‍) character before escaped quotes
215 of the same type as a leading quote.
216
217 The format of the items depends on where they came from. The
218 string for the items are generated by calling the repr()
219 function on them. For strings, this means they will typically
220 be enclosed in single quotes. It is possible that they end up
221 enclosed in double quotes if the string itself contains a
222 single quote. For strings which contain both single and double
223 quotes, the repr() will create a single quoted string with all
224 internal single quotes escaped with '\'. Also, any string that
225 starts with a quote character, must end with the same quote
226 character.
227
228 Examples of strings using double and/or single quotes:
229 'This is "a" test' -> This is "a" test
230 "This is 'a' test" -> This is 'a' test
231 'This "is \'a\'" test' -> This "is 'a'" test
232 'This "is \"a\"" test' -> This "is \"a\"" test
233 */
234 void cleanToken(std::string& token) const;
235
236 /**
237 Private function for creating tokens from a delimited list.
238 Delimiters inside of quotes (single or double) are ignored.
239 */
240 void getDelimitedTokens(const std::string& value, char delim, std::vector<std::string>& tokens) const;
241
242public:
243 using key_type = std::string; /*!< Type of key (string) */
244 using KeySet_t = std::set<key_type, KeyCompare>; /*!< Type of a set of keys */
245
246 /**
247 * Enable or disable parameter verification on an instance
248 * of Params. Useful when generating a new set of Params to
249 * pass off to a module.
250 *
251 * @return returns the previous state of the flag
252 */
253 bool enableVerify(bool enable)
254 {
255 bool old = verify_enabled;
256 verify_enabled = enable;
257 return old;
258 }
259
260 /**
261 * Enable, on a global scale, parameter verification. Used
262 * after construction of the config graph so that warnings are
263 * not generated during construction.
264 */
265 static void enableVerify()
266 {
267 g_verify_enabled = true;
268 };
269
270 /**
271 * Returns the size of the Params. This will count both local and
272 * shared params.
273 *
274 * @return number of key/value pairs in this Params object
275 */
276 size_t size() const;
277 /**
278 * Returns true if the Params is empty. Checks both local and
279 * shared param sets.
280 *
281 * @return true if this Params object is empty, false otherwise
282 */
283 bool empty() const;
284
285 /** Create a new, empty Params */
286 Params();
287
288 /** Create a copy of a Params object */
289 Params(const Params& old);
290
291 ~Params() {}
292
293 /**
294 * @brief Assignment operator.
295 * @param old Param to be copied
296 *
297 * All the elements of old are copied, This will also copy
298 * over any references to shared param sets
299 */
300 Params& operator=(const Params& old);
301
302 /**
303 * Erases all elements, including deleting reference to shared
304 * param sets.
305 */
306 void clear();
307
308 /**
309 * @brief Finds the number of elements with given key.
310 *
311 * The call will check both local and shared params, but will
312 * still only report one instance if the given key is found in
313 * both the local and shared param sets.
314 *
315 * @param k Key of (key, value) pairs to be located.
316 * @return Number of elements with specified key
317 * (either 1 or 0).
318 *
319 */
320 size_t count(const key_type& k) const;
321
322 /** Find a Parameter value in the set, and return its value as a type T.
323 * Type T must be either a basic numeric type (including bool) ,
324 * a std::string, or a class that has a constructor with a std::string
325 * as its only parameter. This class uses SST::Core::from_string to
326 * do the conversion.
327 * @param k - Parameter name
328 * @param default_value - Default value to return if parameter isn't found
329 * @param found - set to true if the the parameter was found
330 * @throw std::invalid_argument If value in (key, value) can't be
331 * converted to type T, an invalid_argument exception is thrown.
332 */
333 template <class T>
334 std::enable_if_t<!std::is_same_v<std::string, T>, T> find(const std::string& k, T default_value, bool& found) const
335 {
336 return find_impl<T>(k, default_value, found);
337 }
338
339 /** Find a Parameter value in the set, and return its value as a type T.
340 * Type T must be either a basic numeric type (including bool) ,
341 * a std::string, or a class that has a constructor with a std::string
342 * as its only parameter. This class uses SST::Core::from_string to
343 * do the conversion.
344 * @param k - Parameter name
345 * @param default_value - Default value to return if parameter isn't found,
346 * specified as a string
347 * @param found - set to true if the the parameter was found
348 */
349 template <class T>
350 T find(const std::string& k, const std::string& default_value, bool& found) const
351 {
352 return find_impl<T>(k, default_value, found);
353 }
354
355 /** Find a Parameter value in the set, and return its value as a type T.
356 * This version of find is only enabled for bool. This
357 * is required because a string literal will be preferentially
358 * cast to a bool rather than a string. This ensures that
359 * find<bool> works correctly for string literals. This class uses
360 * SST::Core::from_string to do the conversion.
361 * @param k - Parameter name
362 * @param default_value - Default value to return if parameter isn't found,
363 * specified as a string literal
364 */
365 template <class T>
366 std::enable_if_t<std::is_same_v<bool, T>, T> find(
367 const std::string& k, const char* default_value, bool& found) const
368 {
369 if ( nullptr == default_value ) {
370 return find_impl<T>(k, static_cast<T>(0), found);
371 }
372 return find_impl<T>(k, std::string(default_value), found);
373 }
374
375 /** Find a Parameter value in the set, and return its value as a type T.
376 * Type T must be either a basic numeric type (including bool),
377 * a std::string, or a class that has a constructor with a std::string
378 * as its only parameter. This class uses SST::Core::from_string to
379 * do the conversion.
380 * @param k - Parameter name
381 * @param default_value - Default value to return if parameter isn't found
382 */
383 template <class T>
384 T find(const std::string& k, T default_value) const
385 {
386 bool tmp;
387 return find_impl<T>(k, default_value, tmp);
388 }
389
390 /** Find a Parameter value in the set, and return its value as a type T.
391 * Type T must be either a basic numeric type (including bool) ,
392 * a std::string, or a class that has a constructor with a std::string
393 * as its only parameter. This class uses SST::Core::from_string to
394 * do the conversion.
395 * @param k - Parameter name
396 * @param default_value - Default value to return if parameter isn't found,
397 * specified as a string
398 */
399 template <class T>
400 T find(const std::string& k, const std::string& default_value) const
401 {
402 bool tmp;
403 return find_impl<T>(k, default_value, tmp);
404 }
405
406 /** Find a Parameter value in the set, and return its value as a type T.
407 * This version of find is only enabled for bool. This
408 * is required because a string literal will be preferentially
409 * cast to a bool rather than a string. This ensures that
410 * find<bool> works correctly for string literals.This class uses
411 * SST::Core::from_string to do the conversion.
412 * @param k - Parameter name
413 * @param default_value - Default value to return if parameter isn't found,
414 * specified as a string literal
415 */
416 template <class T>
417 std::enable_if_t<std::is_same_v<bool, T>, T> find(const std::string& k, const char* default_value) const
418 {
419 bool tmp;
420 if ( nullptr == default_value ) {
421 return find_impl<T>(k, static_cast<T>(0), tmp);
422 }
423 return find_impl<T>(k, std::string(default_value), tmp);
424 }
425
426 /** Find a Parameter value in the set, and return its value as a type T.
427 * Type T must be either a basic numeric type (including bool) ,
428 * a std::string, or a class that has a constructor with a std::string
429 * as its only parameter. This class uses SST::Core::from_string to
430 * do the conversion.
431 * @param k - Parameter name
432 */
433 template <class T>
434 T find(const std::string& k) const
435 {
436 bool tmp;
437 T default_value = T();
438 return find_impl<T>(k, default_value, tmp);
439 }
440
441 /** Find a Parameter value in the set, and return its value as a
442 * type T. Type T must be either a basic numeric type , a
443 * std::string, or a class that has a constructor with a
444 * std::string as its only parameter. This version of find is not
445 * enabled for bool as it conflicts with find<bool>(string key, bool
446 * default_value). This class uses SST::Core::from_string to do
447 * the conversion.
448 * @param k - Parameter name
449 * @param found - set to true if the the parameter was found
450 */
451 template <class T>
452 std::enable_if_t<!std::is_same_v<bool, T>, T> find(const std::string& k, bool& found) const
453 {
454 T default_value = T();
455 return find_impl<T>(k, default_value, found);
456 }
457
458 /** Find a Parameter value in the set, and return its value as a
459 * vector of T's. The array will be appended to
460 * the end of the vector.
461 *
462 * Type T must be either a basic numeric type (including bool) , a
463 * std::string, or a class that has a constructor with a
464 * std::string as its only parameter. This class uses
465 * SST::Core::from_string to do the conversion. The values in the
466 * array must be enclosed in square brackets ( [] ), and be comma
467 * separated (commas in double or single quotes will not be
468 * considered a delimiter). If there are no square brackets, the
469 * entire string will be considered one value and a single item
470 * will be added to the vector.
471 *
472 * More details about parsing the values out of the string:
473 *
474 * Parses a string representing an array of tokens. It is
475 * tailored to the types strings you get when passing a python
476 * list as the param string. When you call addParam() on a python
477 * list in the input file, it will call the str() function on the
478 * list, which creates a string with the following format:
479 * [item1, item2, item3]
480 *
481 * The format of the items depends on where they came from. The
482 * string for the items are generated by calling the repr()
483 * function on them. For strings, this means they will typically
484 * be enclosed in single quotes. It is possible that they end up
485 * enclosed in double quotes if the string itself contains a
486 * single quote. For strings which contain both single and double
487 * quotes, the repr() will create a single quoted string with all
488 * internal single quotes escaped with '\'. Most other items used
489 * in SST do not enclose the string in quotes, though any string
490 * that contains a comma would need to be enclosed in quotes,
491 * since the comma is the delimiter character used. This is not
492 * done automatically, so if you have something that generates a
493 * commma in the string created by repr(), you may need to create
494 * an array string manually. Also, any string that starts with a
495 * quote character, must end with the same quote character.
496 *
497 * Tokens are generated by splitting the string on commas that are
498 * not within quotes (double or single). All whitespace at the
499 * beginning and end of a token is ignored (unless inside quotes).
500 * Once the tokens are generated, any quoted string will have the
501 * front and back quotes removed. The '\' for any escaped quote
502 * of the same type as the front and back is also removed.
503 *
504 * Examples:
505 *
506 * These will produce the same results:
507 * [1, 2, 3, 4, 5]
508 * ['1', '2', '3', '4', '5']
509 *
510 * Examples of strings using double and/or single quotes:
511 * 'This is "a" test' -> This is "a" test
512 * "This is 'a' test" -> This is 'a' test
513 * 'This "is \'a\'" test' -> This "is 'a'" test
514 * 'This "is \"a\"" test' -> This "is \"a\"" test
515 *
516 * @param k - Parameter name
517 * @param vec - vector to append array items to
518 */
519 template <class T>
520 void find_array(const key_type& k, std::vector<T>& vec) const
521 {
522 verifyKey(k);
523
524 bool found = false;
525 std::string value = getString(k, found);
526 if ( !found ) return;
527 // If string starts with [ and ends with ], it is considered
528 // an array. Otherwise, it is considered a single
529 // value.
530 if ( value.front() != '[' || value.back() != ']' ) {
531 vec.push_back(convert_value<T>(k, value));
532 return;
533 }
534
535 value = value.substr(1, value.size() - 2);
536
537 // Get the tokens for the array
538 std::vector<std::string> tokens;
539 getDelimitedTokens(value, ',', tokens);
540
541 // Clean up the tokens
542 for ( auto& str : tokens ) {
543 cleanToken(str);
544 }
545
546 // Convert each token into the proper type and put in output
547 // vector
548 for ( auto& val : tokens ) {
549 vec.push_back(convert_value<T>(k, val));
550 }
551 }
552
553 /** Find a Parameter value in the set, and return its value as a
554 * set of T's. The items will be added to the passed in set.
555 *
556 * Type T must be either a basic numeric type (including bool), a
557 * std::string, or a class that has a constructor with a
558 * std::string as its only parameter. This class uses
559 * SST::Core::from_string to do the conversion. The values in the
560 * set must be enclosed in curly braces ( {} ), and be comma
561 * separated (commas in double or single quotes will not be
562 * considered a delimiter). If there are no curly braces, the
563 * entire string will be considered one value and a single item
564 * will be added to the set.
565 *
566 * More details about parsing the values out of the string:
567 *
568 * Parses a string representing a set of tokens. It is
569 * tailored to the types strings you get when passing a python
570 * set as the param string. When you call addParam() on a python
571 * set in the input file, it will call the str() function on the
572 * set, which creates a string with the following format:
573 * {item1, item2, item3}
574 *
575 * The format of the items depends on where they came from. The
576 * string for the items are generated by calling the repr()
577 * function on them. For strings, this means they will typically
578 * be enclosed in single quotes. It is possible that they end up
579 * enclosed in double quotes if the string itself contains a
580 * single quote. For strings which contain both single and double
581 * quotes, the repr() will create a single quoted string with all
582 * internal single quotes escaped with '\'. Most other items used
583 * in SST do not enclose the string in quotes, though any string
584 * that contains a comma would need to be enclosed in quotes,
585 * since the comma is the delimiter character used. This is not
586 * done automatically, so if you have something that generates a
587 * commma in the string created by repr(), you may need to create
588 * an array string manually. Also, any string that starts with a
589 * quote character, must end with the same quote character.
590 *
591 * Tokens are generated by splitting the string on commas that are
592 * not within quotes (double or single). All whitespace at the
593 * beginning and end of a token is ignored (unless inside quotes).
594 * Once the tokens are generated, any quoted string will have the
595 * front and back quotes removed. The '\' for any escaped quote
596 * of the same type as the front and back is also removed.
597 *
598 * Examples:
599 *
600 * These will produce the same results:
601 * {1, 2, 3, 4, 5}
602 * {'1', '2', '3', '4', '5'}
603 *
604 * Examples of strings using double and/or single quotes:
605 * 'This is "a" test' -> This is "a" test
606 * "This is 'a' test" -> This is 'a' test
607 * 'This "is \'a\'" test' -> This "is 'a'" test
608 * 'This "is \"a\"" test' -> This "is \"a\"" test
609 *
610 * @param k - Parameter name
611 * @param set - set to add items to (new items will overwrite old
612 * items)
613 */
614 template <class T>
615 void find_set(const key_type& k, std::set<T>& set) const
616 {
617 verifyKey(k);
618
619 bool found = false;
620 std::string value = getString(k, found);
621 if ( !found ) return;
622
623 // Python 2 seems to have a slightly different format when
624 // str() is called on sets: set([a, b, c]). We need to change
625 // this to the expected format.
626 if ( value.find("set([") == 0 ) {
627 // Need to convert formats
628
629 // take a substring with space at beginning and end to add
630 // {}
631 value = value.substr(4, value.length() - 5);
632 value[0] = '{';
633 value[value.length() - 1] = '}';
634 }
635
636 // If string starts with { and ends with }, it is considered
637 // a set. Otherwise, it is considered a single
638 // value.
639 if ( value.front() != '{' || value.back() != '}' ) {
640 set.insert(convert_value<T>(k, value));
641 return;
642 }
643
644 value = value.substr(1, value.size() - 2);
645
646 // Get the tokens for the array
647 std::vector<std::string> tokens;
648 getDelimitedTokens(value, ',', tokens);
649
650 // Clean up the tokens
651 for ( auto& str : tokens ) {
652 cleanToken(str);
653 }
654
655 // Convert each token into the proper type and put in output
656 // vector
657 for ( auto& val : tokens ) {
658 set.insert(convert_value<T>(k, val));
659 }
660 }
661
662 /** Find a Parameter value in the set, and return its value as a
663 * map<keyT,valT>. The items will be added to the passed in map.
664 *
665 * Types keyT and valT must be either a basic numeric type
666 * (including bool), a std::string, or a class that has a
667 * constructor with a std::string as its only parameter. This
668 * class uses SST::Core::from_string to do the conversion. The
669 * values in the map must be enclosed in curly braces ( {} ), the
670 * key/value pairs must be comma separated (commas in double or
671 * single quotes will not be considered a delimiter) and the key
672 * and value in the pairs must be separated by a colon (colons in
673 * double or single quotes will not be considered a delimiter).
674 * If there are no curly braces, the function will throw an
675 * std::invalid_argument exception without adding anything to the
676 * map.
677 *
678 * More details about parsing the values out of the string:
679 *
680 * Parses a string representing a set of tokens. It is
681 * tailored to the types strings you get when passing a python
682 * set as the param string. When you call addParam() on a python
683 * set in the input file, it will call the str() function on the
684 * set, which creates a string with the following format:
685 * {key1 : value1, key2 : value2, key3 : value3}
686 *
687 * The format of each item depends on where it came from. The
688 * string for the items are generated by calling the repr()
689 * function on them. For strings, this means they will typically
690 * be enclosed in single quotes. It is possible that they end up
691 * enclosed in double quotes if the string itself contains a
692 * single quote. For strings which contain both single and double
693 * quotes, the repr() will create a single quoted string with all
694 * internal single quotes escaped with '\'. Most other items used
695 * in SST do not enclose the string in quotes, though any string
696 * that contains a comma would need to be enclosed in quotes,
697 * since the comma is the delimiter character used. This is not
698 * done automatically, so if you have something that generates a
699 * commma in the string created by repr(), you may need to create
700 * an array string manually. Also, any string that starts with a
701 * quote character, must end with the same quote character.
702 *
703 * Tokens are generated by splitting the string on commas that are
704 * not within quotes (double or single). All whitespace at the
705 * beginning and end of a token is ignored (unless inside quotes).
706 * Once the tokens are generated, any quoted string will have the
707 * front and back quotes removed. The '\' for any escaped quote
708 * of the same type as the front and back is also removed.
709 *
710 * Examples:
711 *
712 * These will produce the same results:
713 * {"one" : 1, "two" : 2, "three" : 3, "Four" : 4}
714 * {"one" :'1', "two" : '2', "three" : '3', "four" : '4'}
715 *
716 * Examples of strings using double and/or single quotes:
717 * 'This is "a" test' -> This is "a" test
718 * "This is 'a' test" -> This is 'a' test
719 * 'This "is \'a\'" test' -> This "is 'a'" test
720 * 'This "is \"a\"" test' -> This "is \"a\"" test
721 *
722 * @param k - Parameter name
723 * @param map - map to add items to (new items will overwrite old
724 * items)
725 */
726 template <class keyT, class valT>
727 void find_map(const key_type& k, std::map<keyT, valT>& map) const
728 {
729 verifyKey(k);
730
731 bool found = false;
732 std::string value = getString(k, found);
733 if ( !found ) return;
734 // If string starts with { and ends with }, it is considered
735 // a map. Otherwise, we will throw an exception.
736 if ( value.front() != '{' || value.back() != '}' ) {
737 std::string msg = "Invalid format for parameter to be parsed as a map: " + value;
738
739 // Throw exception
740 throw std::invalid_argument(msg);
741 }
742
743 value = value.substr(1, value.size() - 2);
744
745 // Get the tokens
746 std::vector<std::string> tokens;
747 getDelimitedTokens(value, ',', tokens);
748
749 // Now, we need to convert each of these to a key/value pair
750 // separated by a colon
751 std::vector<std::string> kvpair;
752 for ( auto& x : tokens ) {
753 kvpair.clear();
754 getDelimitedTokens(x, ':', kvpair);
755
756 if ( kvpair.size() != 2 ) {
757 std::string msg = "Invalid format for map key/value pair: " + x;
758
759 // throw exception
760 throw std::invalid_argument(msg);
761 }
762
763 // Clean up the tokens
764 cleanToken(kvpair[0]);
765 cleanToken(kvpair[1]);
766
767 // Insert into map, overwriting what might already be there
768 map[convert_value<keyT>(k, kvpair[0])] = convert_value<valT>(k, kvpair[1]);
769 }
770 }
771
772 /** Checks to see if the value associated with the given key is
773 * considered to be an array. A value is considered to be an
774 * array if it is enclosed in square brackets ([]). No whitespace
775 * before or after the brackets is allowed.
776 *
777 * @param k - Parameter name
778 * @return true if value is an array as described above, false otherwise.
779 */
780 bool is_value_array(const key_type& k) const
781 {
782 bool found = false;
783 std::string value = getString(k, found);
784 if ( !found ) return false;
785 // String should start with [ and end with ]
786 if ( (value.find("[") == std::string::npos) || (value.find("]") == std::string::npos) ) {
787 return false;
788 }
789 return true;
790 }
791
792 /** Print all key/value parameter pairs to specified ostream */
793 void print_all_params(std::ostream& os, const std::string& prefix = "") const;
794 /** Print all key/value parameter pairs to specified ostream */
795 void print_all_params(Output& out, const std::string& prefix = "") const;
796 /** Return a string version of all key/value parameter pairs */
797 std::string toString(const std::string& prefix = "") const;
798
799 /**
800 * Add a key/value pair into the param object.
801 *
802 * @param key key to add to the map
803 *
804 * @param value value to add to the map
805 *
806 * @param overwrite controls whether the key/value pair will
807 * overwrite an existing pair in the set
808 */
809 void insert(const std::string& key, const std::string& value, bool overwrite = true);
810
811 /**
812 * Add contents of input Params object to current Params object.
813 * This will also add any pointers to shared param sets after the
814 * existing pointers to shared param sets in this object.
815 *
816 * @param params Params object that should added to current object
817 */
818 void insert(const Params& params);
819
820 /**
821 Get all the keys contained in the Params object. This will
822 give both local and shared params.
823 */
824 std::set<std::string> getKeys() const;
825
826 /**
827 * Returns a new parameter object with parameters that match the
828 * specified scoped prefix (scopes are separated with "." The
829 * keys will be stripped of the "scope." prefix.
830 *
831 * Function will search both local and shared params, but all
832 * params will be copied into the local space of the new Params
833 * object.
834 *
835 * @param scope Scope to search (anything prefixed with "scope."
836 * will be included in the return Params object
837 *
838 * @return New Params object with the found scoped params.
839 */
840 Params get_scoped_params(const std::string& scope) const;
841
842 /**
843 * Search the container for a particular key. This will search
844 * both local and shared params.
845 *
846 * @param k Key to search for
847 * @return True if the params contains the key, false otherwise
848 */
849 bool contains(const key_type& k) const;
850
851 /**
852 * @param keys Set of keys to consider valid to add to the stack
853 * of legal keys
854 */
855 void pushAllowedKeys(const std::vector<std::string>& keys);
856
857 /**
858 * Removes the most recent set of keys considered allowed
859 */
860 void popAllowedKeys();
861
862 void serialize_order(SST::Core::Serialization::serializer& ser);
863
864private:
865 //// Functions used by model descriptions and config graph
866 //// outputters (classes that use it are friended below)
867 friend class SST::ConfigGraph;
868 friend class SST::ConfigComponent;
869 friend class SST::ConfigPortModule;
870 friend class SST::Core::ConfigGraphOutput;
871 friend class SST::SSTModelDescription;
872
873 /**
874 * @param k Key to check for validity
875 * @return True if the key is considered allowed
876 */
877 void verifyParam(const key_type& k) const;
878
879 /**
880 * Adds a shared param set to be looked at in this Params object
881 * if the key isn't found locally. It will search the shared sets
882 * in the order they were inserted and return immediately after
883 * finding the key in one of the sets.
884 *
885 * @param set set to add to the search list for this object
886 */
887 void addSharedParamSet(const std::string& set);
888
889 /**
890 * Adds a key/value pair to the specified shared set
891 *
892 * @param set shared set to add the key/value pair to
893 *
894 * @param key key to add to the map
895 *
896 * @param value value to add to the map
897 *
898 * @param overwrite controls whether the key/value pair will
899 * overwrite an existing pair in the set
900 */
901 static void insert_shared(
902 const std::string& set, const key_type& key, const key_type& value, bool overwrite = true);
903
904 /**
905 * Get a named shared parameter set.
906 *
907 * @param name Name of the set to get
908 *
909 * @return returns a copy of the reqeusted shared param set
910 *
911 */
912 static std::map<std::string, std::string> getSharedParamSet(const std::string& name);
913
914 /**
915 * Get a vector of the names of available shared parameter sets.
916 *
917 * @return returns a vector of the names of available shared param
918 * sets
919 *
920 */
921 static std::vector<std::string> getSharedParamSetNames();
922
923 /**
924 * Get a vector of the local keys
925 *
926 * @return returns a vector of the local keys in this Params
927 * object
928 *
929 */
930 std::vector<std::string> getLocalKeys() const;
931
932 /**
933 * Get a vector of the shared param sets this Params object is
934 * subscribed to
935 *
936 * @return returns a vector of the shared param sets his Params
937 * object is subscribed to
938 *
939 */
940 std::vector<std::string> getSubscribedSharedParamSets() const;
941
942
943 // Private functions used by Params
944 /**
945 * @param k Key to check for validity
946 * @return True if the key is considered allowed
947 */
948 void verifyKey(const key_type& k) const;
949
950
951 // Private data
952 std::map<uint32_t, std::string> my_data;
953 std::vector<std::map<uint32_t, std::string>*> data;
954 std::vector<KeySet_t> allowedKeys;
955 bool verify_enabled;
956 static bool g_verify_enabled;
957
958 static uint32_t getKey(const std::string& str);
959
960 /**
961 * Given a Parameter Key ID, return the Name of the matching parameter
962 * @param id Key ID to look up
963 * @return String name of the parameter
964 */
965 static const std::string& getParamName(uint32_t id);
966
967 /* Friend main() because it broadcasts the maps */
968 friend int ::main(int argc, char* argv[]);
969 /* Friend simulation because it checkpoints the maps */
970 friend class Simulation;
971
972 static std::map<std::string, uint32_t> keyMap;
973 static std::vector<std::string> keyMapReverse;
974 static std::recursive_mutex keyLock;
975 static SST::Core::ThreadSafe::Spinlock sharedLock;
976 static uint32_t nextKeyID;
977
978 static std::map<std::string, std::map<uint32_t, std::string>> shared_params;
979};
980
981#if 0
982 class UnitAlgebra;
983
984#define SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(type) \
985 template <> \
986 type Params::find(const std::string& k, type default_value, bool& found) const; \
987 template <> \
988 type Params::find(const std::string& k, const std::string& default_value, bool& found) const; \
989 template <> \
990 type Params::find(const std::string& k, type default_value) const; \
991 template <> \
992 type Params::find(const std::string& k, const std::string& default_value) const; \
993 template <> \
994 type Params::find(const std::string& k) const;
995
996
997 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(int32_t)
998 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(uint32_t)
999 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(int64_t)
1000 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(uint64_t)
1001 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(bool)
1002 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(float)
1003 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(double)
1004 SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(UnitAlgebra)
1005
1006 // std::string has to be special cased because of signature conflicts
1007 // SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(std::string)
1008 template<>
1009 std::string Params::find<std::string>(const std::string& k, const std::string& default_value, bool &found) const;
1010#endif
1011
1012} // namespace SST
1013
1014#endif // SST_CORE_PARAMS_H
Represents the configuration of a generic component.
Definition configComponent.h:83
A Configuration Graph A graph representing Components and Links.
Definition configGraph.h:76
Class that represents a PortModule in ConfigGraph.
Definition configComponent.h:50
Outputs configuration data to a specified file path.
Definition configGraphOutput.h:62
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition serializer.h:43
Definition threadsafe.h:138
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file.
Definition output.h:58
Parameter store.
Definition params.h:65
void popAllowedKeys()
Removes the most recent set of keys considered allowed.
Definition params.cc:252
void clear()
Erases all elements, including deleting reference to shared param sets.
Definition params.cc:86
void print_all_params(std::ostream &os, const std::string &prefix="") const
Print all key/value parameter pairs to specified ostream.
Definition params.cc:105
std::set< std::string > getKeys() const
Get all the keys contained in the Params object.
Definition params.cc:198
Params()
Create a new, empty Params.
Definition params.cc:58
std::enable_if_t< std::is_same_v< bool, T >, T > 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:417
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:434
bool contains(const key_type &k) const
Search the container for a particular key.
Definition params.cc:233
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:520
bool enableVerify(bool enable)
Enable or disable parameter verification on an instance of Params.
Definition params.h:253
std::string key_type
Definition params.h:243
Params & operator=(const Params &old)
Assignment operator.
Definition params.cc:75
Params get_scoped_params(const std::string &scope) const
Returns a new parameter object with parameters that match the specified scoped prefix (scopes are sep...
Definition params.cc:211
void pushAllowedKeys(const std::vector< std::string > &keys)
Definition params.cc:242
std::enable_if_t<!std::is_same_v< std::string, T >, 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:334
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:384
static void enableVerify()
Enable, on a global scale, parameter verification.
Definition params.h:265
T find(const std::string &k, const std::string &default_value) const
Find a Parameter value in the set, and return its value as a type T.
Definition params.h:400
void find_map(const key_type &k, std::map< keyT, valT > &map) const
Find a Parameter value in the set, and return its value as a map<keyT,valT>.
Definition params.h:727
std::string toString(const std::string &prefix="") const
Return a string version of all key/value parameter pairs.
Definition params.cc:147
bool empty() const
Returns true if the Params is empty.
Definition params.cc:53
size_t size() const
Returns the size of the Params.
Definition params.cc:47
size_t count(const key_type &k) const
Finds the number of elements with given key.
Definition params.cc:94
void find_set(const key_type &k, std::set< T > &set) const
Find a Parameter value in the set, and return its value as a set of T's.
Definition params.h:615
bool is_value_array(const key_type &k) const
Checks to see if the value associated with the given key is considered to be an array.
Definition params.h:780
std::enable_if_t< std::is_same_v< bool, T >, T > 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:366
std::set< key_type, KeyCompare > KeySet_t
Definition params.h:244
T find(const std::string &k, const 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:350
void insert(const std::string &key, const std::string &value, bool overwrite=true)
Add a key/value pair into the param object.
Definition params.cc:171
std::enable_if_t<!std::is_same_v< bool, T >, 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:452
Base class for Model Generation.
Definition sstmodel.h:32
Main control class for a SST Simulation.
Definition simulation.h:121