Bayeux  3.4.1
Core Foundation library for SuperNEMO
target_command.h
Go to the documentation of this file.
1 //
4 // Copyright (c) 2015-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_H
22 #define DATATOOLS_UI_TARGET_COMMAND_H
23 
24 // Standard library:
25 #include <string>
26 #include <vector>
27 #include <memory>
28 
29 // This project:
31 
32 namespace datatools {
33 
34  namespace ui {
35 
37  template <typename Type>
39  : public base_command
40  {
41  public:
42 
45  : base_command(),
46  _target_(nullptr)
47  {
48  return;
49  }
50 
52  target_command(Type & target_,
53  const std::string & name_,
54  const std::string & description_ = "",
55  const version_id & vid_ = version_id::invalid())
56  : base_command(name_, description_, vid_),
57  _target_(nullptr)
58  {
59  _set_target(target_);
60  return;
61  }
62 
64  virtual ~target_command()
65  {
66  return;
67  }
68 
70  virtual bool is_valid() const
71  {
72  return base_command::is_valid() && has_target();
73  }
74 
75  bool has_target() const
76  {
77  return _target_ != nullptr;
78  }
79 
81  const Type & get_target() const
82  {
83  return *_target_;
84  }
85 
87  Type & grab_target()
88  {
89  return _grab_target();
90  }
91 
93  virtual void tree_dump(std::ostream & out_ = std::clog,
94  const std::string & title_ = "",
95  const std::string & indent_ = "",
96  bool inherit_ = false) const
97  {
98  this->base_command::tree_dump(out_, title_, indent_, true);
99 
100  out_ << indent_ << i_tree_dumpable::inherit_tag(inherit_)
101  << "Target : [@" << _target_ << "]" << std::endl;
102 
103  return;
104  }
105 
106  protected:
107 
109  Type & _grab_target()
110  {
111  return *_target_;
112  }
113 
115  void _set_target(Type & target_)
116  {
117  _target_ = &target_;
118  return;
119  }
120 
121  private:
122 
123  Type * _target_ = nullptr;
124 
125  };
126 
128  template <typename Type>
130  : public base_command
131  {
132  public:
133 
136  : base_command(),
137  _target_(nullptr)
138  {
139  return;
140  }
141 
143  const_target_command(const Type & target_,
144  const std::string & name_,
145  const std::string & description_ = "",
146  const version_id & vid_ = version_id::invalid())
147  : base_command(name_, description_, vid_),
148  _target_(nullptr)
149  {
150  _set_target(target_);
151  return;
152  }
153 
156  {
157  return;
158  }
159 
161  virtual bool is_valid() const
162  {
163  return base_command::is_valid() && has_target();
164  }
165 
166  bool has_target() const
167  {
168  return _target_ != nullptr;
169  }
170 
172  const Type & get_target() const
173  {
174  return *_target_;
175  }
176 
178  virtual void tree_dump(std::ostream & out_ = std::clog,
179  const std::string & title_ = "",
180  const std::string & indent_ = "",
181  bool inherit_ = false) const
182  {
183  this->base_command::tree_dump(out_, title_, indent_, true);
184 
185  out_ << indent_ << i_tree_dumpable::inherit_tag(inherit_)
186  << "Target : [@" << _target_ << "]" << std::endl;
187 
188  return;
189  }
190 
191  protected:
192 
194  const Type & _get_target() const
195  {
196  return *_target_;
197  }
198 
200  void _set_target(const Type & target_)
201  {
202  _target_ = &target_;
203  return;
204  }
205 
206  private:
207 
208  const Type * _target_ = nullptr;
209 
210  };
211 
212  } // namespace ui
213 
214 } // namespace datatools
215 
216 #endif // DATATOOLS_UI_TARGET_COMMAND_H
217 
218 // Local Variables: --
219 // mode: c++ --
220 // c-file-style: "gnu" --
221 // tab-width: 2 --
222 // End: --
Output stream manipulator.
Definition: i_tree_dump.h:124
virtual ~target_command()
Destructor.
Definition: target_command.h:64
const_target_command(const Type &target_, const std::string &name_, const std::string &description_="", const version_id &vid_=version_id::invalid())
Constructor.
Definition: target_command.h:143
virtual bool is_valid() const
Check validity.
Definition: target_command.h:70
bool has_target() const
Definition: target_command.h:166
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.h:93
const Type & get_target() const
Return the target.
Definition: target_command.h:172
virtual bool is_valid() const
Check validity.
Base class for command line interface command objects.
static const version_id & invalid()
Return an invalid version identifier.
Base command for a const target object.
Definition: target_command.h:129
const Type & get_target() const
Return the target.
Definition: target_command.h:81
bool has_target() const
Definition: target_command.h:75
virtual ~const_target_command()
Destructor.
Definition: target_command.h:155
Base command.
Definition: base_command.h:48
Type & grab_target()
Return the target.
Definition: target_command.h:87
virtual bool is_valid() const
Check validity.
Definition: target_command.h:161
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
Base command for a target object.
Definition: target_command.h:38
void _set_target(Type &target_)
Set the target.
Definition: target_command.h:115
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.h:178
target_command(Type &target_, const std::string &name_, const std::string &description_="", const version_id &vid_=version_id::invalid())
Constructor.
Definition: target_command.h:52
target_command()
Default constructor.
Definition: target_command.h:44
Type & _grab_target()
Return the target.
Definition: target_command.h:109
const Type & _get_target() const
Return the target.
Definition: target_command.h:194
void _set_target(const Type &target_)
Set the target.
Definition: target_command.h:200
const_target_command()
Default constructor.
Definition: target_command.h:135
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
Smart print.
A class representing a version ID :
Definition: version_id.h:67