0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Config.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.
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 
28 #ifndef Common_Config_h
29 #define Common_Config_h
30 
31 #include <Common/Logger.h>
32 #include <Common/Meta.h>
33 #include <Common/Properties.h>
34 
35 #include <mutex>
36 
37 namespace Hypertable { namespace Config {
38 
43  using namespace Property;
45 
47  extern std::recursive_mutex rec_mutex;
48 
51 
57  inline bool has(const String &name) {
58  HT_ASSERT(properties);
59  return properties->has(name);
60  }
61 
67  inline bool defaulted(const String &name) {
68  HT_ASSERT(properties);
69  return properties->defaulted(name);
70  }
71 
81  template <typename T>
82  T get(const String &name) {
83  HT_ASSERT(properties);
84  return properties->get<T>(name);
85  }
86 
95  template <typename T>
96  T get(const String &name, const T &default_value) {
97  HT_ASSERT(properties);
98  return properties->get<T>(name, default_value);
99  }
100 
103  HT_PROPERTIES_ABBR_ACCESSORS(BOOST_PP_EMPTY())
104 
105 
111  Desc &cmdline_desc(const char *usage=nullptr);
112 
117  void cmdline_desc(const Desc &desc);
118 
122  Desc &cmdline_hidden_desc();
123 
128 
134  Desc &file_desc(const char *usage = NULL);
135 
140  void file_desc(const Desc &desc);
141 
149  struct Policy {
150  static void init_options() { }
151  static void init() { }
152  static void on_init_error(Exception &e) {
153  HT_ERROR_OUT << e << HT_END;
154  std::exit(EXIT_FAILURE);
155  }
156  static void cleanup() { }
157  };
158 
166  static void init_options();
167  static void init();
168  };
169 
173  template <class CarT, class CdrT>
174  struct Cons {
175  static void init_options() {
176  CarT::init_options();
177  CdrT::init_options();
178  }
179  static void init() {
180  CarT::init();
181  CdrT::init();
182  }
183  static void on_init_error(Exception &e) {
184  CarT::on_init_error(e);
185  CdrT::on_init_error(e);
186  }
187  static void cleanup() {
188  CdrT::cleanup(); // in reverse order
189  CarT::cleanup();
190  }
191  };
192 
193  struct NullPolicy { };
194 
195  // Partial specialization for type list algorithms
196  template <class PolicyT>
197  struct Cons<NullPolicy, PolicyT> {
198  static void init_options() { PolicyT::init_options(); }
199  static void init() { PolicyT::init(); }
200  static void on_init_error(Exception &e) { PolicyT::on_init_error(e); }
201  static void cleanup() { PolicyT::cleanup(); }
202  };
203 
204  // Conversion from policy list to combined policy
205  template <class PolicyListT>
206  struct Join {
207  typedef typename Meta::fold<PolicyListT, NullPolicy,
209  };
210 
211 
219  void parse_args(int argc, char *argv[]);
220 
229  void parse_file(const String &fname, const Desc &desc);
230 
242  void alias(const String &cmdline_opt, const String &file_opt,
243  bool overwrite = false);
244 
249  void sync_aliases();
250 
258  bool allow_unregistered_options(bool choice);
259 
265 
269  void cleanup();
270 
273 }}
274 
275 #endif // Common_Config_h
Interface and base of config policy.
Definition: Config.h:149
PropertiesPtr properties
This singleton map stores all options.
Definition: Config.cc:47
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
static void on_init_error(Exception &e)
Definition: Config.h:200
static void cleanup()
Definition: Config.h:187
void init(int argc, char *argv[], const Desc *desc=NULL)
Initialize with default policy.
Definition: Init.h:95
bool default_value(int var_code)
Returns default value for given variable.
Program options handling.
PropertiesDesc Desc
Definition: Config.h:44
static void init_options()
Definition: Config.h:150
void parse_file(const String &fname, const Desc &desc)
Parses a configuration file and stores all configuration options into the option descriptor.
Definition: Config.cc:603
Desc & cmdline_desc(const char *usage)
A macro which definds global functions like get_bool(), get_str(), get_i16() etc. ...
Definition: Config.cc:72
bool has(const String &name)
Check existence of a configuration value.
Definition: Config.h:57
#define HT_ASSERT(_e_)
Definition: Logger.h:396
Helpers to compose init policies; allow to combine two policies into one.
Definition: Config.h:174
std::shared_ptr< Properties > PropertiesPtr
Definition: Properties.h:447
Logging routines and macros.
Desc & file_desc(const char *usage)
Get the config file options description.
Definition: Config.cc:108
Importing boost metaprogramming facilities into Hypertable::Meta.
static void init_options()
Definition: Config.h:175
static void init()
Definition: Config.h:151
void cleanup()
Free all resources used.
Definition: Config.cc:666
Default init policy.
Definition: Config.h:165
Po::options_description PropertiesDesc
Definition: Properties.h:200
#define HT_END
Definition: Logger.h:220
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
#define HT_ERROR_OUT
Definition: Logger.h:301
bool allow_unregistered_options(bool choice)
Toggle allow unregistered options.
Definition: Config.cc:654
Hypertable definitions
void sync_aliases()
Sync alias values.
Definition: Config.cc:611
std::recursive_mutex rec_mutex
A global (recursive) configuration mutex.
Definition: Config.cc:46
This is a generic exception class for Hypertable.
Definition: Error.h:314
static void init()
Definition: Config.h:179
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
Desc & cmdline_hidden_desc()
Get the command line hidden options description (for positional options)
Definition: Config.cc:81
#define HT_PROPERTIES_ABBR_ACCESSORS(_const_)
Definition: Properties.h:102
PositionalDesc & cmdline_positional_desc()
Get the command line positional options description.
Definition: Config.cc:90
static void on_init_error(Exception &e)
Definition: Config.h:152
static void on_init_error(Exception &e)
Definition: Config.h:183
Meta::fold< PolicyListT, NullPolicy, Cons< Meta::_1, Meta::_2 > >::type type
Definition: Config.h:208
static void cleanup()
Definition: Config.h:156