Bayeux  3.4.1
Core Foundation library for SuperNEMO
target_command_interface.h
Go to the documentation of this file.
1 //
4 // Copyright (c) 2016 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_UI_TARGET_COMMAND_INTERFACE_H
22 #define DATATOOLS_UI_TARGET_COMMAND_INTERFACE_H
23 
24 // This project:
26 
27 namespace datatools {
28 
29  namespace ui {
30 
36  template <typename Type>
38  : public base_command_interface
39  {
40  public:
41 
45  _target_(nullptr)
46  {
47  return;
48  }
49 
51  target_command_interface(const std::string & name_,
52  const std::string & description_ = "",
53  const version_id & vid_ = version_id::invalid())
54  : base_command_interface(name_, description_, vid_),
55  _target_(nullptr)
56  {
57  return;
58  }
59 
61  target_command_interface(Type & target_,
62  const std::string & name_,
63  const std::string & description_ = "",
64  const version_id & vid_ = version_id::invalid())
65  : base_command_interface(name_, description_, vid_),
66  _target_(nullptr)
67  {
68  _set_target(target_);
69  return;
70  }
71 
74  {
75  return;
76  }
77 
79  bool has_target() const
80  {
81  return _target_ != nullptr;
82  }
83 
85  void set_target(Type & target_)
86  {
87  DT_THROW_IF(is_initialized(), std::logic_error,
88  "Target command interface is already initialized!");
89  _set_target(target_);
90  return;
91  }
92 
94  const Type & get_target() const
95  {
96  return *_target_;
97  }
98 
100  Type & grab_target()
101  {
102  return _grab_target();
103  }
104 
106  virtual void tree_dump(std::ostream & out_ = std::clog,
107  const std::string & title_ = "",
108  const std::string & indent_ = "",
109  bool inherit_ = false) const
110  {
111  this->base_command_interface::tree_dump(out_, title_, indent_, true);
112 
113  out_ << indent_ << i_tree_dumpable::inherit_tag(inherit_)
114  << "Target : [@" << _target_ << "]" << std::endl;
115 
116  return;
117  }
118 
119  protected:
120 
122  Type & _grab_target()
123  {
124  return *_target_;
125  }
126 
128  void _set_target(Type & target_)
129  {
130  _target_ = &target_;
131  return;
132  }
133 
134  private:
135 
136  Type * _target_ = nullptr;
137 
138  };
139 
145  template <typename Type>
147  : public base_command_interface
148  {
149  public:
150 
154  _target_(nullptr)
155  {
156  return;
157  }
158 
160  const_target_command_interface(const std::string & name_,
161  const std::string & description_ = "",
162  const version_id & vid_ = version_id::invalid())
163  : base_command_interface(name_, description_, vid_),
164  _target_(nullptr)
165  {
166  return;
167  }
168 
170  const_target_command_interface(const Type & target_,
171  const std::string & name_,
172  const std::string & description_ = "",
173  const version_id & vid_ = version_id::invalid())
174  : base_command_interface(name_, description_, vid_),
175  _target_(nullptr)
176  {
177  _set_target(target_);
178  return;
179  }
180 
183  {
184  return;
185  }
186 
188  bool has_target() const
189  {
190  return _target_ != nullptr;
191  }
192 
194  void set_target(const Type & target_)
195  {
196  DT_THROW_IF(is_initialized(), std::logic_error,
197  "Target command interface is already initialized!");
198  _set_target(target_);
199  return;
200  }
201 
203  const Type & get_target() const
204  {
205  return *_target_;
206  }
207 
209  virtual void tree_dump(std::ostream & out_ = std::clog,
210  const std::string & title_ = "",
211  const std::string & indent_ = "",
212  bool inherit_ = false) const
213  {
214  this->base_command_interface::tree_dump(out_, title_, indent_, true);
215 
216  out_ << indent_ << i_tree_dumpable::inherit_tag(inherit_)
217  << "Target : [@" << _target_ << "]" << std::endl;
218 
219  return;
220  }
221 
222  protected:
223 
225  const Type & _get_target()
226  {
227  return *_target_;
228  }
229 
231  void _set_target(const Type & target_)
232  {
233  _target_ = &target_;
234  return;
235  }
236 
237  private:
238 
239  const Type * _target_ = nullptr;
240 
241  };
242 
243  } // namespace ui
244 
245 } // namespace datatools
246 
247 #endif // DATATOOLS_UI_TARGET_COMMAND_INTERFACE_H
248 
249 // Local Variables: --
250 // mode: c++ --
251 // c-file-style: "gnu" --
252 // tab-width: 2 --
253 // End: --
Output stream manipulator.
Definition: i_tree_dump.h:124
void _set_target(const Type &target_)
Set the target.
Definition: target_command_interface.h:231
target_command_interface(const std::string &name_, const std::string &description_="", const version_id &vid_=version_id::invalid())
Constructor.
Definition: target_command_interface.h:51
virtual ~const_target_command_interface()
Destructor.
Definition: target_command_interface.h:182
void _set_target(Type &target_)
Set the target.
Definition: target_command_interface.h:128
Type & grab_target()
Return the target.
Definition: target_command_interface.h:100
void set_target(const Type &target_)
Set the target object.
Definition: target_command_interface.h:194
void set_target(Type &target_)
Set the target object.
Definition: target_command_interface.h:85
Base command interface.
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
Smart print.
Base command interface for a const target object.
Definition: target_command_interface.h:146
static const version_id & invalid()
Return an invalid version identifier.
Base command interface for a target object.
Definition: target_command_interface.h:37
const_target_command_interface()
Default constructor.
Definition: target_command_interface.h:152
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
Smart print.
Definition: target_command_interface.h:209
target_command_interface(Type &target_, const std::string &name_, const std::string &description_="", const version_id &vid_=version_id::invalid())
Constructor.
Definition: target_command_interface.h:61
bool has_target() const
Check if the target is set.
Definition: target_command_interface.h:79
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
Smart print.
Definition: target_command_interface.h:106
const Type & get_target() const
Return the target.
Definition: target_command_interface.h:203
const_target_command_interface(const Type &target_, const std::string &name_, const std::string &description_="", const version_id &vid_=version_id::invalid())
Constructor.
Definition: target_command_interface.h:170
#define DT_THROW_IF(Condition, ExceptionType, Message)
Definition: exception.h:76
Command interface for arbitrary objects.
Definition: base_command_interface.h:54
target_command_interface()
Default constructor.
Definition: target_command_interface.h:43
const Type & _get_target()
Return the target.
Definition: target_command_interface.h:225
const_target_command_interface(const std::string &name_, const std::string &description_="", const version_id &vid_=version_id::invalid())
Constructor.
Definition: target_command_interface.h:160
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
const Type & get_target() const
Return the target.
Definition: target_command_interface.h:94
Type & _grab_target()
Return the target.
Definition: target_command_interface.h:122
virtual bool is_initialized() const =0
Check if the parser is initialized.
A class representing a version ID :
Definition: version_id.h:67
virtual ~target_command_interface()
Destructor.
Definition: target_command_interface.h:73
bool has_target() const
Check if the target is set.
Definition: target_command_interface.h:188