Bayeux  3.4.1
Core Foundation library for SuperNEMO
ihs.h
Go to the documentation of this file.
1 //
4 // Copyright (c) 2015-2017 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_IHS_H
22 #define DATATOOLS_UI_IHS_H
23 
24 // Standard libraries:
25 #include <cstdlib>
26 #include <string>
27 #include <iostream>
28 #include <sstream>
29 #include <exception>
30 #include <map>
31 #include <vector>
32 #include <set>
33 
34 // Third Party:
35 // - Boost:
36 #include <boost/filesystem.hpp>
37 
38 // This project:
40 #include <datatools/properties.h>
43 
44 namespace datatools {
45 
46  namespace ui {
47 
49  class ihs
51  {
52  public:
53 
55  enum node_type {
56  NODE_INVALID = -1,
59  };
60 
62  static std::string node_type_to_label(node_type);
63 
65  static node_type label_to_node_type(const std::string & label_);
66 
72  static void path_remove_scheme(const std::string & path_, std::string & out_);
73 
81  static bool path_has_scheme(const std::string & path_, const std::string & scheme_ = "");
82 
83  // Future: Replace with URL validation utility
84  // //! Check if a path is valid
85  // static bool path_validation(const std::string & path_);
86 
88  class node
90  {
91  public:
92 
94  node();
95 
97  node(node_type type_, const std::string & full_path_);
98 
100  virtual ~node();
101 
103  void reset();
104 
106  void set_type(node_type type_);
107 
109  node_type get_type() const;
110 
112  bool is_root() const;
113 
115  bool is_valid() const;
116 
118  bool is_interface() const;
119 
121  bool is_command() const;
122 
125 
127  const datatools::properties & get_metadata() const;
128 
130  const std::string & get_full_path() const;
131 
133  void set_full_path(const std::string &);
134 
136  bool has_description() const;
137 
139  void set_description(const std::string &);
140 
142  const std::string & get_description() const;
143 
145  bool has_interface() const;
146 
148  bool has_command() const;
149 
152 
155 
157  void reset_interface();
158 
160  void set_command(base_command *);
161 
163  void reset_command();
164 
167 
170 
172  const base_command_interface & get_interface() const;
173 
176 
178  const base_command & get_command() const;
179 
181  bool has_parent_node() const;
182 
184  void set_parent_node(node & node_);
185 
187  void unset_parent_node();
188 
191 
193  bool is_child_of(node & node_) const;
194 
196  const node & get_parent_node() const;
197 
199  void add_child_node(node & node_);
200 
202  void remove_child_node(node & node_);
203 
205  std::size_t get_number_of_children() const;
206 
208  bool has_children() const;
209 
211  void set_ihs(ihs &);
212 
214  virtual void tree_dump(std::ostream & out_ = std::clog,
215  const std::string & title_ = "",
216  const std::string & indent_ = "",
217  bool inherit = false) const;
218 
220  void set_trait(const std::string & trait_label_, const bool set_ = true);
221 
223  void reset_trait(const std::string & trait_label_);
224 
226  bool is_trait(const std::string & trait_label_) const;
227 
228  private:
229 
230  ihs * _ihs_ = nullptr;
231  node_type _type_;
232  datatools::properties _metadata_;
233  std::string _full_path_;
234  std::string _description_;
235  bool _owned_interface_ = false;
236  base_command_interface * _interface_ = nullptr;
237  bool _owned_command_ = false;
238  base_command * _command_ = nullptr;
239  node * _parent_node_;
240  std::set<node *> _children_;
241 
242  friend class ihs;
243 
244  };
245 
246  typedef std::map<std::string, node> node_dict_type;
247 
249  ihs(const std::string & scheme_ = "");
250 
252  virtual ~ihs();
253 
254  // //! Return the root path
255  // const std::string & get_root_path() const;
256 
258  bool has_scheme() const;
259 
261  void set_scheme(const std::string &);
262 
264  const std::string & get_scheme() const;
265 
267  void add_interface(const std::string & parent_path_,
268  const std::string & interface_name_,
269  const std::string & description_ = "");
270 
272  void add_interface(const std::string & parent_path_,
273  base_command_interface * interface_);
274 
276  void add_interface(const std::string & parent_path_,
277  base_command_interface & interface_);
278 
280  void remove_interface(const std::string & interface_path_, bool recursive_ = false);
281 
283  void add_command(const std::string & parent_path_,
284  base_command & cmd_);
285 
287  void add_command(const std::string & parent_path_,
288  base_command * cmd_);
289 
291  void remove_command(const std::string & command_path_);
292 
294  void remove(const std::string & path_, bool recursive_ = false);
295 
297  bool exists(const std::string & path_) const;
298 
300  bool is_interface(const std::string & path_) const;
301 
303  bool has_interface(const std::string & path_) const;
304 
306  bool is_command(const std::string & path_) const;
307 
309  void set_trait(const std::string & path_,
310  const std::string & trait_label_,
311  const bool set_ = true);
312 
314  void reset_trait(const std::string & path_,
315  const std::string & trait_label_);
316 
318  bool is_trait(const std::string & path_,
319  const std::string & trait_label_) const;
320 
322  bool has_command(const std::string & path_) const;
323 
325  base_command & grab_command(const std::string & path_);
326 
328  const base_command & get_command(const std::string & path_) const;
329 
331  base_command_interface & grab_interface(const std::string & path_);
332 
334  const base_command_interface & get_interface(const std::string & path_) const;
335 
337  bool has_children(const std::string & path_) const;
338 
340  void build_children_paths(const std::string & path_,
341  std::vector<std::string> & children_paths_) const;
342 
344  std::string get_parent_path(const std::string & path_) const;
345 
347  bool has_child(const std::string & path_, const std::string & name_) const;
348 
350  void reset();
351 
354  std::string canonical_path(const std::string & path_) const;
355 
357  virtual void tree_dump(std::ostream & out_ = std::clog,
358  const std::string & title_ = "",
359  const std::string & indent_ = "",
360  bool inherit = false) const;
361 
363  void build_path(std::set<std::string> & paths_, const uint32_t flags_ = 0) const;
364 
365  protected:
366 
368  node & _grab_node(const std::string & path_);
369 
371  const node & _get_node(const std::string & path_) const;
372 
373  private:
374 
376  void _set_scheme_(const std::string &);
377 
378  private:
379 
380  std::string _scheme_;
381  node_dict_type _nodes_;
382 
383  };
384 
385  } // namespace ui
386 
387 } // namespace datatools
388 
389 #endif // DATATOOLS_UI_IHS_H
390 
391 // Local Variables: --
392 // mode: c++ --
393 // c-file-style: "gnu" --
394 // tab-width: 2 --
395 // End: --
Interface Hierarchy System.
Definition: ihs.h:49
bool has_command(const std::string &path_) const
Check if a path has an associated command.
const node & _get_node(const std::string &path_) const
Return the node at given full path.
bool has_interface(const std::string &path_) const
Check if a path has an associated command interface.
base_command_interface & grab_interface(const std::string &path_)
Return the mutable command interface at given full path.
void reset_interface()
Reset interface.
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit=false) const
Smart print.
node()
Default constructor.
const std::string & get_description() const
Return the description.
bool exists(const std::string &path_) const
Check if node exists given its full path.
Interface node (directory populated)
Definition: ihs.h:57
std::size_t get_number_of_children() const
Return the number of child nodes.
const std::string & get_full_path() const
Return the full path.
A node in the IHS.
Definition: ihs.h:88
bool is_interface() const
Check if node is an interface node (directory)
bool is_trait(const std::string &trait_label_) const
Check if a trait id set.
node_type get_type() const
Return the node type.
void reset_trait(const std::string &path_, const std::string &trait_label_)
Reset trait flag.
node & grab_parent_node()
Return the parent node (if set)
void build_path(std::set< std::string > &paths_, const uint32_t flags_=0) const
Build a set of paths.
void add_child_node(node &node_)
Add a child node.
An interface with utilities for printable objects.
Definition: i_tree_dump.h:36
bool has_parent_node() const
Check if the parent node is set.
const base_command & get_command() const
Return the command.
void remove(const std::string &path_, bool recursive_=false)
Remove path.
Base class for command line interface command objects.
Base command interface.
const std::string & get_scheme() const
Return the scheme.
std::string get_parent_path(const std::string &path_) const
Return the parent path of a path.
base_command_interface & grab_interface()
Return the command interface.
virtual ~ihs()
Destructor.
void set_parent_node(node &node_)
Set parent node.
node_type
Node type.
Definition: ihs.h:55
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit=false) const
Smart print.
const base_command & get_command(const std::string &path_) const
Return the command at given full path.
bool has_description() const
Check if node has a description.
void set_trait(const std::string &trait_label_, const bool set_=true)
Set trait flag.
ihs(const std::string &scheme_="")
Default constructor.
void set_external_interface(base_command_interface &)
Set the external command interface.
bool has_children() const
Check if some children are set.
void set_full_path(const std::string &)
Set the full path.
bool is_valid() const
Check if node is valid.
void add_command(const std::string &parent_path_, base_command &cmd_)
Add a command given its parent's full path.
base_command & grab_command(const std::string &path_)
Return the mutable command at given full path.
bool is_command(const std::string &path_) const
Check if a path is associated to a command interface.
bool is_interface(const std::string &path_) const
Check if a path is a command interface.
bool has_scheme() const
Check is scheme is set.
void remove_interface(const std::string &interface_path_, bool recursive_=false)
Remove a command interface given its full path.
base_command & grab_command()
Return the command.
void set_description(const std::string &)
Set the description.
void reset_command()
Reset command.
std::string canonical_path(const std::string &path_) const
Command node (file/leaf)
Definition: ihs.h:58
void set_ihs(ihs &)
Set the host IHS.
Invalid node type.
Definition: ihs.h:56
bool has_children(const std::string &path_) const
Check if path has children.
const base_command_interface & get_interface() const
Return the command interface.
static bool path_has_scheme(const std::string &path_, const std::string &scheme_="")
bool has_command() const
Check if node has a command set.
static std::string node_type_to_label(node_type)
Convert node type to label.
void set_command(base_command *)
Set the command.
static node_type label_to_node_type(const std::string &label_)
Convert label to node type.
Base command.
Definition: base_command.h:48
void set_scheme(const std::string &)
Set the scheme.
Command interface for arbitrary objects.
Definition: base_command_interface.h:54
bool is_root() const
Check if node is the root.
void remove_child_node(node &node_)
Remove a child node.
node & _grab_node(const std::string &path_)
Return the mutable node at given full path.
static void path_remove_scheme(const std::string &path_, std::string &out_)
const datatools::properties & get_metadata() const
Return the metadata.
bool has_child(const std::string &path_, const std::string &name_) const
Check if a given path has a child with given name.
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
void reset()
Reset the node.
std::map< std::string, node > node_dict_type
Definition: ihs.h:246
void set_trait(const std::string &path_, const std::string &trait_label_, const bool set_=true)
Set trait flag.
void build_children_paths(const std::string &path_, std::vector< std::string > &children_paths_) const
Build the list of children paths of a path.
void add_interface(const std::string &parent_path_, const std::string &interface_name_, const std::string &description_="")
Add an interface given its parent's full path and its basename.
A base class with useful attributes usable in many contexts.
Definition: enriched_base.h:52
const base_command_interface & get_interface(const std::string &path_) const
Return the command interface at given full path.
void set_interface(base_command_interface *)
Set the command interface.
void set_type(node_type type_)
Set the node type.
bool is_child_of(node &node_) const
Check if the node is child of another node.
void reset_trait(const std::string &trait_label_)
Reset trait flag.
bool is_command() const
Check if node is a command node (file)
const node & get_parent_node() const
Return the parent node (if set)
void remove_command(const std::string &command_path_)
Remove a command given its full path.
void reset()
Reset.
datatools::properties & grab_metadata()
Return the mutable metadata.
void set_external_command(base_command &)
Set the external command.
bool has_interface() const
Check if node has a command interface set.
bool is_trait(const std::string &path_, const std::string &trait_label_) const
Check if a trait id set.
virtual ~node()
Destructor.
A dictionary of arbitrary properties.
Definition: properties.h:125
void unset_parent_node()
Remove parent node.