12#include "sparta/simulation/ParameterTree.hpp"
21#include <boost/lexical_cast.hpp>
30 void setFeatureValue(
const std::string & name,
const unsigned int value) {
32 boost::lexical_cast<std::string>(value));
35 unsigned int getFeatureValue(
const std::string & feature_name)
const {
36 auto feature = feature_values_.
tryGet(feature_name);
37 if (feature ==
nullptr || !feature->hasValue()) {
40 return std::stoi(feature->getValue());
43 bool isFeatureValueSet(
const std::string & feature_name)
const {
44 return (feature_values_.
tryGet(feature_name) !=
nullptr);
108 std::ifstream fin(yaml_opts_fname);
110 std::cout <<
" [feature] Warning: No options file named '"
111 << yaml_opts_fname <<
"' found." << std::endl;
115 std::vector<std::vector<std::string>> str_vectors;
116 utils::split_lines_around_tokens(fin, str_vectors,
":");
117 for (
auto & strvec : str_vectors) {
119 std::string & option_name = strvec[0];
120 std::string & option_value = strvec[1];
121 option_name = sparta::utils::strip_whitespace(option_name);
122 option_value = sparta::utils::strip_whitespace(option_value);
123 feature_options_.
set(option_name, option_value,
false);
147 template <
typename T,
148 typename =
typename std::enable_if<
149 std::is_trivial<T>::value, T>::type>
151 const std::string & option_name,
152 const T default_value)
const
154 auto option = feature_options_.
tryGet(option_name);
155 if (option ==
nullptr || !option->hasValue()) {
156 return default_value;
159 const std::string & opt_value_str = option->getValue();
161 const double opt_value = boost::lexical_cast<double>(opt_value_str);
162 return static_cast<T
>(opt_value);
163 }
catch (
const boost::bad_lexical_cast &) {
177 return default_value;
183 template <
typename T,
184 typename =
typename std::enable_if<
185 std::is_same<T, std::string>::value, T>::type>
187 const std::string & option_name,
188 const T default_value)
const
190 auto option = feature_options_.
tryGet(option_name);
191 if (option ==
nullptr || !option->hasValue()) {
192 return default_value;
194 return option->getValue();
205 const std::string & yaml_opts_fname)
207 auto iter = feature_options_.find(feature_name);
208 if (iter == feature_options_.end()) {
210 opts->setOptionsFromYamlFile(yaml_opts_fname);
211 feature_options_[feature_name] = std::move(opts);
214 iter->second->setOptionsFromYamlFile(yaml_opts_fname);
231 auto iter = feature_options_.find(feature_name);
232 if (iter != feature_options_.end()) {
233 return iter->second.get();
235 return &null_feature_options_;
240 std::unordered_map<std::string, std::unique_ptr<FeatureOptions>> feature_options_;
241 FeatureOptions null_feature_options_;
250typename std::enable_if<
251 std::is_same<T, app::FeatureConfiguration>::value,
255 const std::string & feature_name,
256 const unsigned int feature_value)
258 return cfg.getFeatureValue(feature_name) == feature_value;
264typename std::enable_if<
268 const std::string & feature_name,
269 const unsigned int feature_value)
272 (feature_value == 0);
278typename std::enable_if<
279 std::is_same<T, app::FeatureConfiguration>::value,
283 const std::string & feature_name)
285 return cfg.getFeatureValue(feature_name) > 0;
292typename std::enable_if<
297 const std::string & feature_name)
305typename std::enable_if<
306 std::is_same<T, app::FeatureConfiguration>::value,
307const app::FeatureConfiguration::FeatureOptions*>::type
309 const std::string & feature_name)
311 return cfg.getFeatureOptions(feature_name);
317typename std::enable_if<
319const app::FeatureConfiguration::FeatureOptions*>::type
322 const std::string & feature_name)
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Exception class for all of Sparta.
void setValue(const std::string &val, bool required=true, const std::string &origin="")
Set a value on this node directly.
Virtual Parameter Tree. This represents a tree of parameters read from some source but does not neces...
Node * create(const std::string &path, bool required=false)
Add a node to the tree, with proper priority.
bool set(const std::string &path, const std::string &value, bool required, const std::string &origin="")
Add a parameter to the tree, replacing any existing parameter.
const Node * tryGet(const std::string &path, const bool must_be_leaf=true) const
Try to get a node if it exists. Returns nullptr it it does not.
void setOptionsFromYamlFile(const std::string &yaml_opts_fname)
std::string getOptionValue(const std::string &option_name, const T default_value) const
T getOptionValue(const std::string &option_name, const T default_value) const
Collection of named feature values.
const FeatureOptions * getFeatureOptions(const std::string &feature_name) const
void setFeatureOptionsFromFile(const std::string &feature_name, const std::string &yaml_opts_fname)
Macros for handling exponential backoff.
std::enable_if< std::is_same< T, app::FeatureConfiguration >::value, bool >::type IsFeatureValueEqualTo(const T &cfg, const std::string &feature_name, const unsigned int feature_value)
std::enable_if< std::is_same< T, app::FeatureConfiguration >::value, constapp::FeatureConfiguration::FeatureOptions * >::type GetFeatureOptions(const T &cfg, const std::string &feature_name)
std::enable_if< std::is_same< T, app::FeatureConfiguration >::value, bool >::type IsFeatureValueEnabled(const T &cfg, const std::string &feature_name)