Scorum
config_api.cpp
Go to the documentation of this file.
2 
3 #include <fc/exception/exception.hpp>
4 
5 #include <boost/algorithm/string.hpp>
6 
7 namespace scorum {
8 
9 const std::string config_api::default_api_name = "api";
10 
11 config_api::configs_by_api_type config_api::_instances_by_api = config_api::configs_by_api_type();
12 
13 config_api::config_api(const std::string& api_name)
14  : _api_name(api_name)
15  , max_blockchain_history_depth(_max_blockchain_history_depth)
16  , max_blocks_history_depth(_max_blocks_history_depth)
17  , max_budgets_list_size(_max_budgets_list_size)
18  , max_discussions_list_size(_max_discussions_list_size)
19  , lookup_limit(_lookup_limit)
20  , tags_to_analize_count(_tags_to_analize_count)
21  , max_timestamp_range_in_s(_max_timestamp_range_in_s)
22 {
23 }
24 
26  : _api_name(config.api_name())
27  , _max_blockchain_history_depth(config._max_blockchain_history_depth)
28  , _max_blocks_history_depth(config._max_blocks_history_depth)
29  , _max_budgets_list_size(config._max_budgets_list_size)
30  , _max_discussions_list_size(config._max_discussions_list_size)
31  , _lookup_limit(config._lookup_limit)
32  , _tags_to_analize_count(config._tags_to_analize_count)
33  , _max_timestamp_range_in_s(config._max_timestamp_range_in_s)
34  , max_blockchain_history_depth(_max_blockchain_history_depth)
35  , max_blocks_history_depth(_max_blocks_history_depth)
36  , max_budgets_list_size(_max_budgets_list_size)
37  , max_discussions_list_size(_max_discussions_list_size)
38  , lookup_limit(_lookup_limit)
39  , tags_to_analize_count(_tags_to_analize_count)
40  , max_timestamp_range_in_s(_max_timestamp_range_in_s)
41 {
42 }
43 
44 namespace bpo = boost::program_options;
45 
46 #define STR(field) BOOST_PP_STRINGIZE(field)
47 
48 bpo::options_description config_api::get_options_descriptions() const
49 {
50  bpo::options_description result;
51  bpo::options_description_easy_init options(&result);
52  get_option_description<uint32_t>(options, STR(max_blockchain_history_depth));
53  get_option_description<uint32_t>(options, STR(max_blocks_history_depth));
54  get_option_description<uint32_t>(options, STR(max_budgets_list_size));
55  get_option_description<uint32_t>(options, STR(max_discussions_list_size));
56  get_option_description<uint32_t>(options, STR(lookup_limit));
57  get_option_description<uint32_t>(options, STR(tags_to_analize_count));
58  get_option_description<uint32_t>(options, STR(max_timestamp_range_in_s));
59  return result;
60 }
61 
62 void config_api::set_options(const bpo::variables_map& options)
63 {
64  config_api clean_config(api_name());
65 
66  set_option<uint32_t>(options, clean_config._max_blockchain_history_depth, STR(max_blockchain_history_depth));
67  set_option<uint32_t>(options, clean_config._max_blocks_history_depth, STR(max_blocks_history_depth));
68  set_option<uint32_t>(options, clean_config._max_budgets_list_size, STR(max_budgets_list_size));
69  set_option<uint32_t>(options, clean_config._max_discussions_list_size, STR(max_discussions_list_size));
70  set_option<uint32_t>(options, clean_config._lookup_limit, STR(lookup_limit));
71  set_option<uint32_t>(options, clean_config._tags_to_analize_count, STR(tags_to_analize_count));
72  set_option<uint32_t>(options, clean_config._max_timestamp_range_in_s, STR(max_timestamp_range_in_s));
73 
74  // recreate to reset constants
75  config_api::_instances_by_api[api_name()].reset(new config_api(clean_config));
76 }
77 
78 template <typename MemberType>
79 void config_api::get_option_description(bpo::options_description_easy_init& options, const char* member_name) const
80 {
81  options(get_option_name(member_name, api_name()).c_str(), bpo::value<MemberType>(),
82  get_option_description(member_name).c_str());
83 }
84 
85 template <typename MemberType>
86 void config_api::set_option(const bpo::variables_map& options, MemberType& member, const char* member_name)
87 {
88  if (options.count(get_option_name(member_name, api_name())))
89  {
90  member = options.at(get_option_name(member_name, api_name())).as<MemberType>();
91  }
92  else if (options.count(get_option_name(member_name, default_api_name)))
93  {
94  member = options.at(get_option_name(member_name, default_api_name)).as<MemberType>();
95  }
96 }
97 
98 std::string config_api::get_option_name(const char* field, const std::string& api_name) const
99 {
100  std::string result = api_name;
101  result += "-";
102  result += field;
103  return boost::algorithm::replace_all_copy(result, "_", "-");
104 }
105 
106 std::string config_api::get_option_description(const char* field) const
107 {
108  std::string result = boost::algorithm::replace_all_copy(std::string(field), "_", " ");
109  result += " option for ";
110  result += api_name();
111  return result;
112 }
113 
114 config_api& get_api_config(std::string api_name)
115 {
116  if (api_name.empty())
117  api_name = config_api::default_api_name;
118 
119  auto config_it = config_api::_instances_by_api.find(api_name);
120  if (config_api::_instances_by_api.end() == config_it)
121  {
122  config_it
123  = config_api::_instances_by_api.emplace(api_name, std::unique_ptr<config_api>(new config_api(api_name)))
124  .first;
125  }
126  return (*config_it->second);
127 }
128 }
void set_options(const boost::program_options::variables_map &options)
Definition: config_api.cpp:62
const uint32_t & max_timestamp_range_in_s
Definition: config_api.hpp:32
boost::program_options::options_description get_options_descriptions() const
Definition: config_api.cpp:48
const uint32_t & max_blocks_history_depth
Definition: config_api.hpp:27
const std::string & api_name() const
Definition: config_api.hpp:37
const uint32_t & tags_to_analize_count
Definition: config_api.hpp:31
const uint32_t & max_budgets_list_size
Definition: config_api.hpp:28
const uint32_t & lookup_limit
Definition: config_api.hpp:30
const uint32_t & max_discussions_list_size
Definition: config_api.hpp:29
const uint32_t & max_blockchain_history_depth
Definition: config_api.hpp:26
#define STR(field)
Definition: config_api.cpp:46
Definition: asset.cpp:15
config_api & get_api_config(std::string api_name)
Definition: config_api.cpp:114