Bayeux  3.4.1
Core Foundation library for SuperNEMO
argument.h
Go to the documentation of this file.
1 //
4 // Copyright (c) 2015 by François Mauger <mauger@lpccaen.in2p3.fr>
5 //
6 // This file is part of Datatools.
7 //
8 // Datatools is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // Datatools is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with Datatools. If not, see <http://www.gnu.org/licenses/>.
20 
21 #ifndef DATATOOLS_INTROSPECTION_ARGUMENT_H
22 #define DATATOOLS_INTROSPECTION_ARGUMENT_H
23 
24 // Standard library:
25 #include <string>
26 
27 // Third party:
28 // - Boost:
29 #include <boost/optional.hpp>
30 #include <boost/any.hpp>
31 #include <boost/program_options.hpp>
32 // - Bayeux/datatools:
33 #include <datatools/properties.h>
34 
35 // This project:
38 
39 namespace datatools {
40 
41  namespace introspection {
42 
43  class method;
44 
46  class argument
48  {
49  public:
50 
52  argument();
53 
55  ~argument();
56 
58  bool has_method() const;
59 
61  void set_method(const method &);
62 
64  void reset_method();
65 
67  const method & get_method() const;
68 
70  bool has_rank() const;
71 
73  std::size_t get_rank() const;
74 
76  void set_rank(std::size_t);
77 
79  bool has_name() const;
80 
82  const std::string & get_name() const;
83 
85  void set_name(const std::string &);
86 
88  bool has_access() const;
89 
91  access_type get_access() const;
92 
94  void set_access(access_type);
95 
97  bool has_description() const;
98 
100  const std::string & get_description() const;
101 
103  void set_description(const std::string &);
104 
106  bool is_input() const;
107 
109  bool is_output() const;
110 
112  bool has_data_description() const;
113 
116 
118  const data_description & get_data_description() const;
119 
122 
124  bool has_default_value() const;
125 
127  template <class T>
128  bool is_default_value_a() const
129  {
130  return _default_value_.type() == typeid(T);
131  }
132 
134  template <class T>
135  const T & get_default_value_as() const
136  {
137  DT_THROW_IF(!has_default_value(), std::logic_error,
138  "No default value is set!");
139  DT_THROW_IF(typeid(T) != get_data_description().native_type(),
140  std::logic_error,
141  "Unmatching type info!");
142  return boost::any_cast<T>(_default_value_);
143  }
144 
146  template <class T>
147  void set_default_value_as(const T & value_)
148  {
149  DT_THROW_IF(typeid(T) != get_data_description().native_type(),
150  std::logic_error,
151  "Unmatching type info!");
152  _default_value_ = value_;
153  return;
154  }
155 
157  const boost::any & get_default_value() const;
158 
160  void set_default_value(const boost::any & any_value_);
161 
163  bool has_default_value_str() const;
164 
166  const std::string & get_default_value_str() const;
167 
169  void set_default_value_str(const std::string & v_);
170 
172  bool is_valid() const;
173 
175  void initialize();
176 
178  void initialize(const datatools::properties &);
179 
181  void reset();
182 
193  | ARG_XC_ACCESS
195  | ARG_XC_RANK
196  | ARG_XC_NAME
200  };
201 
204  uint32_t flags_ = ARG_XC_DEFAULT,
205  const std::string & prefix_ = "") const;
206 
208  virtual void tree_dump(std::ostream & out_ = std::clog,
209  const std::string & title_ = "",
210  const std::string & indent_ = "",
211  bool inherit_ = false) const;
212 
213  private:
214 
215  access_type _access_;
216  data_description _data_description_;
217  const method * _method_;
218  boost::optional<std::size_t> _rank_;
219  boost::optional<std::string> _name_;
220  boost::optional<std::string> _description_;
221  boost::any _default_value_;
222  boost::optional<std::string> _default_value_str_;
223 
224  };
225 
226  } // namespace introspection
227 
228 } // namespace datatools
229 
230 #endif // DATATOOLS_INTROSPECTION_ARGUMENT_H
231 
232 // Local Variables: --
233 // mode: c++ --
234 // c-file-style: "gnu" --
235 // tab-width: 2 --
236 // End: --
Description of a method.
Definition: method.h:45
void reset_method()
Reset the method of which the argument belong to the list of arguments.
Data description.
Description of a method argument.
Definition: argument.h:46
const method & get_method() const
Return the method of which the argument belong to the list of arguments.
Data description.
Definition: data_description.h:40
access_type
Access type associated to a method argument.
Definition: access_type.h:37
void set_description(const std::string &)
Set the argument's description.
const boost::any & get_default_value() const
Return the argument's default_value.
arg_xc_flags
Flags for export to a container of properties.
Definition: argument.h:184
bool is_valid() const
Check validity.
const std::string & get_description() const
Return a non mutable reference of the argument's description.
void set_name(const std::string &)
Set the argument's explicit name in the list of arguments it belongs to.
An interface with utilities for printable objects.
Definition: i_tree_dump.h:36
static const uint32_t bit01
Definition: bit_mask.h:28
const std::string & get_name() const
Return the argument's explicit name in the list of arguments it belongs to.
static const uint32_t bit03
Definition: bit_mask.h:30
void export_to_config(datatools::properties &config_, uint32_t flags_=ARG_XC_DEFAULT, const std::string &prefix_="") const
Export to a container of properties.
void set_data_description(const data_description &)
Set the argument's data description.
bool has_method() const
Check if the method of which the argument belong to the list of arguments is set.
static const uint32_t bit04
Definition: bit_mask.h:31
argument()
Default constructor.
static const uint32_t bit06
Definition: bit_mask.h:33
void set_default_value_as(const T &value_)
Set the argument's default_value.
Definition: argument.h:147
void set_method(const method &)
Set the method of which the argument belong to the list of arguments.
void set_access(access_type)
Set the argument's access mode.
void set_default_value(const boost::any &any_value_)
Set the argument's default_value.
bool has_access() const
Check if the argument has an access mode.
bool has_rank() const
Check if the argument has an explicit rank in the list of arguments it belongs to.
bool has_name() const
Check if the argument has an explicit name in the list of arguments it belongs to.
access_type get_access() const
Return the argument's access mode.
void set_rank(std::size_t)
Set the argument's explicit rank in the list of arguments it belongs to.
static const uint32_t bit02
Definition: bit_mask.h:29
bool has_default_value_str() const
Check if the argument has a default value string representation.
static const uint32_t bit05
Definition: bit_mask.h:32
void set_default_value_str(const std::string &v_)
Set the default value string representation.
bool has_description() const
Check if the argument has a description.
data_description & grab_data_description()
Return a mutable reference of the argument's data description.
const data_description & get_data_description() const
Return a non mutable reference of the argument's data description.
#define DT_THROW_IF(Condition, ExceptionType, Message)
Definition: exception.h:76
std::size_t get_rank() const
Return the argument's explicit rank in the list of arguments it belongs to.
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
Smart print.
bool is_default_value_a() const
Check default value type.
Definition: argument.h:128
void initialize()
Initialization.
bool is_output() const
Check if the argument is an output access argument.
Method argument access type.
static const uint32_t bit00
Definition: bit_mask.h:27
const std::string & get_default_value_str() const
Return the default value string representation.
bool is_input() const
Check if the argument is an input access argument.
bool has_data_description() const
Check if the argument has a data description.
bool has_default_value() const
Check if the argument has a default value.
A dictionary of arbitrary properties.
Definition: properties.h:125
const T & get_default_value_as() const
Return the argument's default value.
Definition: argument.h:135