tue_config
reader.cpp
Go to the documentation of this file.
1 #include "tue/config/reader.h"
2 #include "tue/config/node.h"
3 #include "tue/config/data.h"
4 
5 namespace tue
6 {
7 namespace config
8 {
9 
10 // ----------------------------------------------------------------------------------------------------
11 
12 Reader::Reader(const DataConstPointer& ptr) : idx_(ptr.idx), cfg_(ptr.data)
13 {
14 }
15 
16 // ----------------------------------------------------------------------------------------------------
17 
19 {
20 }
21 
22 // ----------------------------------------------------------------------------------------------------
23 
24 bool Reader::read(const std::string& name, const NodeType type, const RequiredOrOptional /*opt*/)
25 {
26  Label label;
27  NodeIdx child_idx; // Needed for checking if the child node is indeed the type(map/array) we are looking for.
28  if (cfg_->getLabel(name, label) && cfg_->nodes[idx_]->readGroup(label, child_idx))
29  {
30  // check if child matches the type you want to read.
31  if (cfg_->nodes[child_idx]->type() == type)
32  {
33  idx_ = child_idx;
34  return true;
35  }
36  }
37 
38  return false;
39 }
40 
41 // ----------------------------------------------------------------------------------------------------
42 
44 {
45  NodeIdx parent = cfg_->getParent(idx_);
46  if (parent == static_cast<NodeIdx>(-1))
47  return false;
48 
49  idx_ = parent;
50 
51  // If the parent is an array, go up one more
52  if (cfg_->nodes[idx_]->type() == ARRAY)
53  idx_ = cfg_->getParent(idx_);
54 
55  return true;
56 }
57 
58 // ----------------------------------------------------------------------------------------------------
59 
61 {
62  if (cfg_->nodes[idx_]->type() == ARRAY)
63  return cfg_->nodes[idx_]->firstChild(idx_);
64 
65  NodeIdx right_sibling = cfg_->getRightSibling(idx_);
66  if (right_sibling == static_cast<NodeIdx>(-1))
67  return false;
68 
69  idx_ = right_sibling;
70  return true;
71 }
72 
73 
74 // ----------------------------------------------------------------------------------------------------
75 
76 bool Reader::hasChild(const std::string& name, NodeType type) const
77 {
78  Label label;
79  NodeIdx child_idx; // Needed for checking if the child node is indeed the type(map/array) we are looking for.
80  if (cfg_->getLabel(name, label) && cfg_->nodes[idx_]->readGroup(label, child_idx))
81  {
82  // check if child matches the type you want to read.
83  return cfg_->nodes[child_idx]->type() == type;
84  }
85  return false;
86 }
87 
88 } // end namespace config
89 
90 } // end namespace tue
91 
tue::config::Reader::next
bool next()
next go to next item in an array
Definition: reader.cpp:60
std::string
tue::config::NodeIdx
unsigned int NodeIdx
Definition: data_pointer.h:12
tue::config::Reader::cfg_
boost::shared_ptr< const Data > cfg_
Definition: reader.h:136
tue::config::Reader::idx_
NodeIdx idx_
Definition: reader.h:134
tue::config::Reader::Reader
Reader(const DataConstPointer &ptr)
Definition: reader.cpp:12
node.h
tue::config::Reader::hasChild
bool hasChild(const std::string &name, NodeType type) const
hasChild check if node has a child with key 'name' and type ARRAY or MAP
Definition: reader.cpp:76
tue::config::RequiredOrOptional
RequiredOrOptional
Definition: types.h:15
reader.h
tue::config::Reader::read
bool read(const std::string &name, const NodeType type, const RequiredOrOptional opt=OPTIONAL)
read read child with key 'name' of type ARRAY or MAP
Definition: reader.cpp:24
tue::config::ARRAY
@ ARRAY
Definition: node_type.h:13
data.h
tue::config::DataConstPointer
Definition: data_pointer.h:35
tue::config::NodeType
NodeType
Definition: node_type.h:10
tue::config::Reader::~Reader
virtual ~Reader()
Definition: reader.cpp:18
tue::config::Reader::end
bool end()
end go to parent node.
Definition: reader.cpp:43
tue
config
tue::config::ReaderWriter config
Definition: sdf_gtest.cpp:9