0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Properties.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
27 #ifndef Common_Properties_h
28 #define Common_Properties_h
29 
30 #include <vector>
31 #include <string>
32 #include <boost/version.hpp>
33 #include <boost/any.hpp>
34 
35 // Required declarations for custom validators *before* including the header,
36 // otherwise no overloading would happen in standard conforming compilers.
37 
39 namespace boost {
40 
42 namespace program_options {
43 
44 using namespace std;
45 
46 typedef std::vector<std::string> Strings;
47 typedef std::vector<int64_t> Int64s;
48 
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);
53 
54 // pre 1.35 vector<T> validate doesn't pickup user defined validate for T
55 #if BOOST_VERSION < 103500
56 template<typename T>
57 void validate(boost::any& v, const vector<string> &s, std::vector<T>*, int);
58 #endif
59 
60 }} // namespace boost::program_options
61 
62 #include <Common/Error.h>
63 
64 #include <boost/program_options.hpp>
65 
66 
67 namespace boost { namespace program_options {
68 
69 using namespace std;
70 
71 #if BOOST_VERSION < 103500
72 
75 template<typename T>
76 void validate(boost::any& v, const vector<string> &s, std::vector<T>*, int) {
77  if (v.empty())
78  v = boost::any(std::vector<T>());
79 
80  std::vector<T>* tv = boost::any_cast< std::vector<T> >(&v);
81  assert(NULL != tv);
82 
83  for (unsigned i = 0; i < s.size(); ++i) {
84  try {
85  // so we can pick up user defined validate for T here
86  boost::any a;
87  vector<string> sv;
88  sv.push_back(s[i]);
89  validate(a, sv, (T*)0, 0);
90  tv->push_back(boost::any_cast<T>(a));
91  }
92  catch (const bad_lexical_cast &/*e*/) {
93  boost::throw_exception(invalid_option_value(s[i]));
94  }
95  }
96 }
97 #endif // < boost 1.35
98 
99 }} // namespace boost::program_options
100 
101 // convenience/abbreviated accessors
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); }
139 
140 namespace Hypertable {
141 
146 namespace Po = boost::program_options;
147 
148 typedef std::vector<String> Strings;
149 typedef std::vector<int64_t> Int64s;
150 typedef std::vector<double> Doubles;
151 
152 namespace Property {
153 
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;
160 
161 // Abbrs for some common types
162 inline Po::typed_value<bool> *boo(bool *v = 0) {
163  return Po::value<bool>(v);
164 }
165 
166 inline Po::typed_value<String> *str(String *v = 0) {
167  return Po::value<String>(v);
168 }
169 
170 inline Po::typed_value<Strings> *strs(Strings *v = 0) {
171  return Po::value<Strings>(v);
172 }
173 
174 inline Po::typed_value<uint16_t> *i16(uint16_t *v = 0) {
175  return Po::value<uint16_t>(v);
176 }
177 
178 inline Po::typed_value<int32_t> *i32(int32_t *v = 0) {
179  return Po::value<int32_t>(v);
180 }
181 
182 inline Po::typed_value<int64_t> *i64(int64_t *v = 0) {
183  return Po::value<int64_t>(v);
184 }
185 
186 inline Po::typed_value<Int64s> *i64s(Int64s *v = 0) {
187  return Po::value<Int64s>(v);
188 }
189 
190 inline Po::typed_value<double> *f64(double *v = 0) {
191  return Po::value<double>(v);
192 }
193 
194 inline Po::typed_value<Doubles> *f64s(Doubles *v = 0) {
195  return Po::value<Doubles>(v);
196 }
197 
198 } // namespace Property
199 
200 typedef Po::options_description PropertiesDesc;
201 typedef Po::positional_options_description PositionalDesc;
202 
208 class Properties {
209  typedef Po::variable_value Value;
210  typedef Po::variables_map Map;
211  typedef std::pair<Map::iterator, bool> InsRet;
212  typedef std::map<String, String> AliasMap;
213 
214 public:
217  : m_need_alias_sync(false) {
218  }
219 
227  Properties(const String &filename, const PropertiesDesc &desc,
228  bool allow_unregistered = false)
229  : m_need_alias_sync(false) {
230  load(filename, desc, allow_unregistered);
231  }
232 
241  void load(const String &filename, const PropertiesDesc &desc,
242  bool allow_unregistered = false);
243 
256  void parse_args(int argc, char *argv[], const PropertiesDesc &desc,
257  const PropertiesDesc *hidden = 0, const PositionalDesc *p = 0,
258  bool allow_unregistered = false);
259 
271  void parse_args(const std::vector<String> &args, const PropertiesDesc &desc,
272  const PropertiesDesc *hidden = 0, const PositionalDesc *p = 0,
273  bool allow_unregistered = false);
274 
278  void notify();
279 
286  template <typename T>
287  T get(const String &name) const {
288  try {
289  return m_map[name].template as<T>();
290  }
291  catch (std::exception &e) {
292  HT_THROWF(Error::CONFIG_GET_ERROR, "getting value of '%s': %s",
293  name.c_str(), e.what());
294  }
295  }
296 
306  template <typename T>
307  T get(const String &name, const T &default_value) const {
308  try {
309  Map::const_iterator it = m_map.find(name);
310 
311  if (it != m_map.end())
312  return (*it).second.template as<T>();
313 
314  return default_value;
315  }
316  catch (std::exception &e) {
317  HT_THROWF(Error::CONFIG_GET_ERROR, "getting value of '%s': %s",
318  name.c_str(), e.what());
319  }
320  }
321 
327  const boost::any &operator[](const String &name) const {
328  return m_map[name].value();
329  }
330 
337  bool defaulted(const String &name) const {
338  return m_map[name].defaulted();
339  }
340 
346  bool has(const String &name) const {
347  return m_map.count(name);
348  }
349 
351 
352 
360  InsRet add(const String &name, const boost::any &v, bool defaulted = false) {
361  m_need_alias_sync = true;
362  return m_map.insert(Map::value_type(name, Value(v, defaulted)));
363  }
364 
372  void set(const String &name, const boost::any &v, bool defaulted = false) {
373  InsRet r = add(name, v, defaulted);
374 
375  if (!r.second)
376  (*r.first).second = Value(v, defaulted);
377  }
378 
384  void remove(const String &name) {
385  m_map.erase(name);
386  }
387 
398  void alias(const String &primary, const String &secondary,
399  bool overwrite = false);
400 
408  void sync_aliases();
409 
415  void get_names(std::vector<String> &names) {
416  for (Map::const_iterator it = m_map.begin(); it != m_map.end(); it++)
417  names.push_back((*it).first);
418  }
419 
426  void print(std::ostream &out, bool include_default = false);
427 
434  static String to_str(const boost::any &a);
435 
436 private:
439 
441  Map m_map;
442 
444  AliasMap m_alias_map;
445 };
446 
447  typedef std::shared_ptr<Properties> PropertiesPtr;
448 
459 public:
465  SubProperties(PropertiesPtr &props, const String &prefix)
466  : m_props(props), m_prefix(prefix) {
467  }
468 
474  String full_name(const String &name) const {
475  return m_prefix + name;
476  }
477 
483  template <typename T>
484  T get(const String &name) const {
485  return m_props->get<T>(full_name(name));
486  }
487 
494  template <typename T>
495  T get(const String &name, const T &default_value) const {
496  return m_props->get(full_name(name), default_value);
497  }
498 
505  bool defaulted(const String &name) const {
506  return m_props->defaulted(full_name(name));
507  }
508 
515  bool has(const String &name) const {
516  return m_props->has(full_name(name));
517  }
518 
520 
521 private:
523  PropertiesPtr m_props;
524 
526  String m_prefix;
527 };
528 
531 }
532 
533 #endif // Common_Properties_h
std::vector< double > Doubles
Definition: Properties.h:150
std::vector< int64_t > Int64s
Definition: Properties.h:47
Po::variables_map Map
Definition: Properties.h:210
static String filename
Definition: Config.cc:48
static bool allow_unregistered
Definition: Config.cc:50
Boost library.
Definition: Properties.cc:39
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
Po::positional_options_description PositionalDesc
Definition: Properties.h:201
Properties(const String &filename, const PropertiesDesc &desc, bool allow_unregistered=false)
Constructor; load properties from a filename.
Definition: Properties.h:227
void get_names(std::vector< String > &names)
Returns all property names.
Definition: Properties.h:415
Po::typed_value< Int64s > * i64s(Int64s *v=0)
Definition: Properties.h:186
Po::variable_value Value
Definition: Properties.h:209
Po::typed_value< uint16_t > * i16(uint16_t *v=0)
Definition: Properties.h:174
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
bool default_value(int var_code)
Returns default value for given variable.
Po::typed_value< Doubles > * f64s(Doubles *v=0)
Definition: Properties.h:194
std::vector< String > Strings
Definition: Properties.h:148
const uint64_t MiB
Definition: Properties.h:157
STL namespace.
std::vector< int64_t > Int64s
Definition: Properties.h:149
void add(const Key &key, uint8_t flag, const void *value, uint32_t value_len, TableMutatorAsync *value_index_mutator, TableMutatorAsync *qualifier_index_mutator)
Definition: IndexTables.cc:34
void validate(boost::any &v, const Strings &values,::int64_t *, int)
Definition: Properties.cc:41
const uint64_t KiB
Definition: Properties.h:155
Properties()
Default constructor; creates an empty set.
Definition: Properties.h:216
const uint64_t G
Definition: Properties.h:158
Po::typed_value< int64_t > * i64(int64_t *v=0)
Definition: Properties.h:182
Po::typed_value< int32_t > * i32(int32_t *v=0)
Definition: Properties.h:178
std::shared_ptr< Properties > PropertiesPtr
Definition: Properties.h:447
AliasMap m_alias_map
A map with all aliases.
Definition: Properties.h:444
Manages a collection of program options.
Definition: Properties.h:208
const uint64_t K
Definition: Properties.h:154
bool defaulted(const String &name) const
Check whether a property has a default value.
Definition: Properties.h:337
Po::options_description PropertiesDesc
Definition: Properties.h:200
Po::typed_value< bool > * boo(bool *v=0)
Definition: Properties.h:162
bool defaulted(const String &name)
Check if a configuration value is defaulted.
Definition: Config.h:67
void parse_args(int argc, char *argv[])
Initialization helper; parses the argc/argv parameters into properties, reads the configuration file...
Definition: Config.cc:567
SubProperties(PropertiesPtr &props, const String &prefix)
Constructor.
Definition: Properties.h:465
bool has(const String &name) const
Check whether a property exists.
Definition: Properties.h:346
std::vector< std::string > Strings
Definition: Properties.h:46
bool has(const String &name) const
Check whether a sub-property exists.
Definition: Properties.h:515
Po::typed_value< Strings > * strs(Strings *v=0)
Definition: Properties.h:170
Hypertable definitions
Helper class to access parts of the properties.
Definition: Properties.h:458
void sync_aliases()
Sync alias values.
Definition: Config.cc:611
void set(const String &name, const boost::any &v, bool defaulted=false)
Set a property in the map, create if not found.
Definition: Properties.h:372
#define HT_THROWF(_code_, _fmt_,...)
Definition: Error.h:490
Map m_map
The map containing all properties.
Definition: Properties.h:441
bool m_need_alias_sync
Whether the aliases need to be synced.
Definition: Properties.h:438
const boost::any & operator[](const String &name) const
Get the underlying boost::any value of 'name'.
Definition: Properties.h:327
const uint64_t M
Definition: Properties.h:156
Program options.
Definition: Properties.cc:39
Po::typed_value< double > * f64(double *v=0)
Definition: Properties.h:190
std::pair< Map::iterator, bool > InsRet
Definition: Properties.h:211
void alias(const String &cmdline_opt, const String &file_opt, bool overwrite)
Setup command line option alias for config file option.
Definition: Config.cc:607
#define HT_PROPERTIES_ABBR_ACCESSORS(_const_)
Definition: Properties.h:102
Error codes, Exception handling, error logging.
String full_name(const String &name) const
Returns the full name of a sub-property.
Definition: Properties.h:474
bool defaulted(const String &name) const
Check whether a sub-property has a default value.
Definition: Properties.h:505
const uint64_t GiB
Definition: Properties.h:159
std::map< String, String > AliasMap
Definition: Properties.h:212