uLib-0.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Options.h
Go to the documentation of this file.
1 /*//////////////////////////////////////////////////////////////////////////////
2 // CMT Cosmic Muon Tomography project //////////////////////////////////////////
4 
5  Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
6  All rights reserved
7 
8  Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
9 
10  ------------------------------------------------------------------
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU Lesser General Public
13  License as published by the Free Software Foundation; either
14  version 3.0 of the License, or (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  Lesser General Public License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public
22  License along with this library.
23 
25 
26 
27 
28 #ifndef U_CORE_OPTIONS_H
29 #define U_CORE_OPTIONS_H
30 
31 #include <boost/program_options.hpp>
32 
33 namespace uLib {
34 
35 namespace detail {
36 
37 struct Options {
38 
42  class options_description_easy_init {
43  typedef boost::program_options::options_description_easy_init InitClass;
44  InitClass m_init;
45  public:
46  options_description_easy_init(InitClass init) :
47  m_init(init) {}
48 
49 
50  options_description_easy_init&
51  operator()(const char* name,
52  const char* description)
53  {
54  m_init(name,description);
55  return *this;
56  }
57 
58  options_description_easy_init&
59  operator()(const char* name,
60  const boost::program_options::value_semantic* s)
61  {
62  m_init(name,s);
63  return *this;
64  }
65 
66 
67  options_description_easy_init&
68  operator()(const char* name,
69  const boost::program_options::value_semantic* s,
70  const char* description)
71  {
72  m_init(name,s,description);
73  return *this;
74  }
75 
76 
77  template <typename T>
78  options_description_easy_init&
79  operator()(const char* name,
80  T * value,
81  const T default_value,
82  const char* description)
83  {
84  m_init(name,
85  boost::program_options::value<T>(value)->default_value(default_value),
86  description);
87  return *this;
88  }
89 
90  template <typename T>
91  options_description_easy_init&
92  operator()(const char* name,
93  T * value,
94  const char* description)
95  {
96  assert(value);
97  m_init(name,
98  boost::program_options::value<T>(value)->default_value(*value),
99  description);
100  return *this;
101  }
102 
103  template <typename T>
104  options_description_easy_init&
105  operator()(const char* name,
106  T * value,
107  const T default_value)
108  {
109  m_init(name,
110  boost::program_options::value<T>(value)->default_value(default_value));
111  return *this;
112  }
113 
114  template <typename T>
115  options_description_easy_init&
116  operator()(const char* name,
117  T * value)
118  {
119  assert(value);
120  m_init(name,
121  boost::program_options::value<T>(value)->default_value(*value));
122  return *this;
123  }
124 
125  };
126 
127 }; // Options
128 
129 } // detail
130 
131 
132 
133 class Options {
134  boost::program_options::options_description m_global;
135  boost::program_options::options_description m_configuration;
136  boost::program_options::positional_options_description m_posdesc;
137  boost::program_options::variables_map m_vm;
138 
139 public:
140  typedef detail::Options::options_description_easy_init initType;
141 
142  Options(const char *str = "Program options");
143 
144  initType add_options() {
145  return initType(m_global.add_options());
146  }
147 
148  initType add_config_options() {
149  return initType(m_configuration.add_options());
150  }
151 
152  void add_positional_option(const char *name, int max_count) {
153  // TODO:
154  // m_posdesc(name,max_count);
155  }
156 
157  void parse_command_line(int argc, char *argv[]);
158 
159  void parse_config_file(std::string &str);
160 
161  void parse_config_file(const char *fname);
162 
163  template <typename T>
164  static inline boost::program_options::typed_value<T>* value(T *v, T dvalue) {
165  boost::program_options::typed_value<T> *r = boost::program_options::value<T>(v);
166  r->default_value(dvalue);
167  return r;
168  }
169 
170  bool count(const char *str) const;
171 
172 };
173 
174 
175 } // uLib
176 
177 
178 
179 #endif // U_CORE_OPTIONS_H