27 #ifndef Common_Properties_h
28 #define Common_Properties_h
32 #include <boost/version.hpp>
33 #include <boost/any.hpp>
42 namespace program_options {
49 void validate(boost::any &v,
const vector<string> &values, ::int64_t *,
int);
50 void validate(boost::any &v,
const vector<string> &values, ::int32_t *,
int);
51 void validate(boost::any &v,
const vector<string> &values, ::uint16_t *,
int);
52 void validate(boost::any &v,
const vector<string> &values,
double *,
int);
55 #if BOOST_VERSION < 103500
57 void validate(boost::any& v,
const vector<string> &s, std::vector<T>*,
int);
64 #include <boost/program_options.hpp>
67 namespace boost {
namespace program_options {
71 #if BOOST_VERSION < 103500
76 void validate(boost::any& v,
const vector<string> &s, std::vector<T>*,
int) {
78 v = boost::any(std::vector<T>());
80 std::vector<T>* tv = boost::any_cast< std::vector<T> >(&v);
83 for (
unsigned i = 0; i < s.size(); ++i) {
90 tv->push_back(boost::any_cast<T>(a));
92 catch (
const bad_lexical_cast &) {
93 boost::throw_exception(invalid_option_value(s[i]));
97 #endif // < boost 1.35
102 #define HT_PROPERTIES_ABBR_ACCESSORS(_const_) \
103 inline bool get_bool(const String &name) _const_ { \
104 return get<bool>(name); } \
105 inline String get_str(const String &name) _const_ { \
106 return get<String>(name); } \
107 inline Strings get_strs(const String &name) _const_ { \
108 return get<Strings>(name); } \
109 inline uint16_t get_i16(const String &name) _const_ { \
110 return get<uint16_t>(name); } \
111 inline int32_t get_i32(const String &name) _const_ { \
112 return get<int32_t>(name); } \
113 inline int64_t get_i64(const String &name) _const_ { \
114 return get<int64_t>(name); } \
115 inline Int64s get_i64s(const String &name) _const_ { \
116 return get<Int64s>(name); } \
117 inline double get_f64(const String &name) _const_ { \
118 return get<double>(name); } \
119 inline Doubles get_f64s(const String &name) _const_ { \
120 return get<Doubles>(name); } \
121 inline bool get_bool(const String &name, bool default_value) _const_ { \
122 return get(name, default_value); } \
123 inline String get_str(const String &name, const String &default_value) \
124 _const_ { return get(name, default_value); } \
125 inline Strings get_strs(const String &name, const Strings &default_value) \
126 _const_ { return get(name, default_value); } \
127 inline uint16_t get_i16(const String &name, uint16_t default_value) \
128 _const_ { return get(name, default_value); } \
129 inline int32_t get_i32(const String &name, int32_t default_value) _const_ { \
130 return get(name, default_value); } \
131 inline int64_t get_i64(const String &name, int64_t default_value) _const_ { \
132 return get(name, default_value); } \
133 inline Int64s get_i64s(const String &name, const Int64s &default_value) \
134 _const_ { return get(name, default_value); } \
135 inline double get_f64(const String &name, double default_value) _const_ { \
136 return get(name, default_value); } \
137 inline Doubles get_f64s(const String &name, const Doubles &default_value) \
138 _const_ { return get(name, default_value); }
154 const uint64_t
K = 1000;
155 const uint64_t
KiB = 1024;
156 const uint64_t
M = K * 1000;
157 const uint64_t
MiB = KiB * 1024;
158 const uint64_t
G = M * 1000;
159 const uint64_t
GiB = MiB * 1024;
162 inline Po::typed_value<bool> *
boo(
bool *v = 0) {
163 return Po::value<bool>(v);
167 return Po::value<String>(v);
170 inline Po::typed_value<Strings> *
strs(Strings *v = 0) {
171 return Po::value<Strings>(v);
174 inline Po::typed_value<uint16_t> *
i16(uint16_t *v = 0) {
175 return Po::value<uint16_t>(v);
178 inline Po::typed_value<int32_t> *
i32(int32_t *v = 0) {
179 return Po::value<int32_t>(v);
182 inline Po::typed_value<int64_t> *
i64(int64_t *v = 0) {
183 return Po::value<int64_t>(v);
186 inline Po::typed_value<Int64s> *
i64s(Int64s *v = 0) {
187 return Po::value<Int64s>(v);
190 inline Po::typed_value<double> *
f64(
double *v = 0) {
191 return Po::value<double>(v);
194 inline Po::typed_value<Doubles> *
f64s(Doubles *v = 0) {
195 return Po::value<Doubles>(v);
210 typedef Po::variables_map
Map;
211 typedef std::pair<Map::iterator, bool>
InsRet;
217 : m_need_alias_sync(false) {
229 : m_need_alias_sync(false) {
256 void parse_args(
int argc,
char *argv[],
const PropertiesDesc &desc,
257 const PropertiesDesc *hidden = 0,
const PositionalDesc *p = 0,
271 void parse_args(
const std::vector<String> &args,
const PropertiesDesc &desc,
272 const PropertiesDesc *hidden = 0,
const PositionalDesc *p = 0,
286 template <
typename T>
289 return m_map[name].template as<T>();
291 catch (std::exception &e) {
293 name.c_str(), e.what());
306 template <
typename T>
309 Map::const_iterator it = m_map.find(name);
311 if (it != m_map.end())
312 return (*it).second.template as<T>();
316 catch (std::exception &e) {
318 name.c_str(), e.what());
328 return m_map[name].value();
338 return m_map[name].defaulted();
347 return m_map.count(name);
361 m_need_alias_sync =
true;
362 return m_map.insert(Map::value_type(name, Value(v,
defaulted)));
399 bool overwrite =
false);
416 for (Map::const_iterator it = m_map.begin(); it != m_map.end(); it++)
417 names.push_back((*it).first);
426 void print(std::ostream &out,
bool include_default =
false);
434 static String to_str(
const boost::any &a);
466 : m_props(props), m_prefix(prefix) {
475 return m_prefix + name;
483 template <
typename T>
485 return m_props->get<T>(full_name(name));
494 template <
typename T>
506 return m_props->defaulted(full_name(name));
516 return m_props->has(full_name(name));
523 PropertiesPtr m_props;
533 #endif // Common_Properties_h
std::vector< double > Doubles
std::vector< int64_t > Int64s
static bool allow_unregistered
std::string String
A String is simply a typedef to std::string.
Po::positional_options_description PositionalDesc
Properties(const String &filename, const PropertiesDesc &desc, bool allow_unregistered=false)
Constructor; load properties from a filename.
void get_names(std::vector< String > &names)
Returns all property names.
Po::typed_value< Int64s > * i64s(Int64s *v=0)
Po::typed_value< uint16_t > * i16(uint16_t *v=0)
Po::typed_value< String > * str(String *v=0)
bool default_value(int var_code)
Returns default value for given variable.
Po::typed_value< Doubles > * f64s(Doubles *v=0)
std::vector< String > Strings
std::vector< int64_t > Int64s
void add(const Key &key, uint8_t flag, const void *value, uint32_t value_len, TableMutatorAsync *value_index_mutator, TableMutatorAsync *qualifier_index_mutator)
void validate(boost::any &v, const Strings &values,::int64_t *, int)
Properties()
Default constructor; creates an empty set.
Po::typed_value< int64_t > * i64(int64_t *v=0)
Po::typed_value< int32_t > * i32(int32_t *v=0)
std::shared_ptr< Properties > PropertiesPtr
AliasMap m_alias_map
A map with all aliases.
Manages a collection of program options.
bool defaulted(const String &name) const
Check whether a property has a default value.
Po::options_description PropertiesDesc
Po::typed_value< bool > * boo(bool *v=0)
bool defaulted(const String &name)
Check if a configuration value is defaulted.
void parse_args(int argc, char *argv[])
Initialization helper; parses the argc/argv parameters into properties, reads the configuration file...
SubProperties(PropertiesPtr &props, const String &prefix)
Constructor.
bool has(const String &name) const
Check whether a property exists.
std::vector< std::string > Strings
bool has(const String &name) const
Check whether a sub-property exists.
Po::typed_value< Strings > * strs(Strings *v=0)
Helper class to access parts of the properties.
void sync_aliases()
Sync alias values.
void set(const String &name, const boost::any &v, bool defaulted=false)
Set a property in the map, create if not found.
#define HT_THROWF(_code_, _fmt_,...)
Map m_map
The map containing all properties.
bool m_need_alias_sync
Whether the aliases need to be synced.
const boost::any & operator[](const String &name) const
Get the underlying boost::any value of 'name'.
Po::typed_value< double > * f64(double *v=0)
std::pair< Map::iterator, bool > InsRet
void alias(const String &cmdline_opt, const String &file_opt, bool overwrite)
Setup command line option alias for config file option.
#define HT_PROPERTIES_ABBR_ACCESSORS(_const_)
Error codes, Exception handling, error logging.
String full_name(const String &name) const
Returns the full name of a sub-property.
bool defaulted(const String &name) const
Check whether a sub-property has a default value.
std::map< String, String > AliasMap