tue_config
reader.h
Go to the documentation of this file.
1 #ifndef TUE_CONFIG_READER_H_
2 #define TUE_CONFIG_READER_H_
3 
4 #include "tue/config/types.h"
5 #include "tue/config/data.h"
6 
7 
8 namespace tue
9 {
10 
11 namespace config
12 {
13 
14 class Data;
15 
16 class Reader
17 {
18 
19 public:
20 
21  Reader(const DataConstPointer& ptr);
22 
23  virtual ~Reader();
24 
25  template<typename T>
33  bool value(const std::string& name, T& value, const RequiredOrOptional /*opt*/ = REQUIRED) const
34  {
35  Label label;
36  if (!cfg_->getLabel(name, label))
37  return false;
38 
39  Variant v;
40  if (!cfg_->nodes[idx_]->value(label, v))
41  return false;
42 
43  if (!v.getValue(value))
44  return false;
45 
46  return true;
47  }
48 
55  bool readArray(const std::string& name, const RequiredOrOptional opt = OPTIONAL) { return read(name, ARRAY, opt); }
56 
63  bool readGroup(const std::string& name, const RequiredOrOptional opt = OPTIONAL) { return read(name, MAP, opt); }
64 
69  bool endArray() { return end(); }
70 
75  bool endGroup() { return end(); }
76 
81  bool nextArrayItem() { return next(); }
82 
88  bool hasArray(const std::string& name) { return hasChild(name, ARRAY); }
89 
95  bool hasGroup(const std::string& name) { return hasChild(name, MAP); }
96 
101  inline DataConstPointer data() const { return DataConstPointer(cfg_, idx_); }
102 
103 private:
104 
112  bool read(const std::string& name, const NodeType type, const RequiredOrOptional opt = OPTIONAL);
113 
118  bool end();
119 
124  bool next();
125 
132  bool hasChild(const std::string& name, NodeType type) const;
133 
134  NodeIdx idx_; // Current node index
135 
136  boost::shared_ptr<const Data> cfg_; // Data pointer
137 
138 };
139 
140 } // end namespace config
141 
142 } // end namespace tue
143 
144 #endif
tue::config::Reader::next
bool next()
next go to next item in an array
Definition: reader.cpp:60
std::string
tue::config::Reader::nextArrayItem
bool nextArrayItem()
nextArrayItem go to the next item in the array, wrapping next() for readbility
Definition: reader.h:81
types.h
tue::config::Reader::value
bool value(const std::string &name, T &value, const RequiredOrOptional=REQUIRED) const
value read value of child with key 'name'
Definition: reader.h:33
tue::config::NodeIdx
unsigned int NodeIdx
Definition: data_pointer.h:12
tue::config::Reader::hasGroup
bool hasGroup(const std::string &name)
hasGroup check if current node has a child, which is a group, with the key 'name'
Definition: reader.h:95
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::readGroup
bool readGroup(const std::string &name, const RequiredOrOptional opt=OPTIONAL)
readGroup read the child with key 'name', which should be a group
Definition: reader.h:63
tue::config::Reader::Reader
Reader(const DataConstPointer &ptr)
Definition: reader.cpp:12
tue::config::Reader::data
DataConstPointer data() const
data get the data from the current reading/writing point
Definition: reader.h:101
tue::config::Reader
Definition: reader.h:16
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
tue::config::Reader::endGroup
bool endGroup()
endGroup go to the parrent of current group, wrapping end() for readbility
Definition: reader.h:75
tue::config::Reader::hasArray
bool hasArray(const std::string &name)
hasArray check if current node has a child, which is an array, with the key 'name'
Definition: reader.h:88
tue::config::OPTIONAL
@ OPTIONAL
Definition: types.h:18
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
tue::config::MAP
@ MAP
Definition: node_type.h:12
data.h
tue::config::DataConstPointer
Definition: data_pointer.h:35
tue::config::NodeType
NodeType
Definition: node_type.h:10
tue::config::Reader::readArray
bool readArray(const std::string &name, const RequiredOrOptional opt=OPTIONAL)
readArray read the child with key 'name', which should be an array
Definition: reader.h:55
tue::config::Reader::~Reader
virtual ~Reader()
Definition: reader.cpp:18
tue::config::REQUIRED
@ REQUIRED
Definition: types.h:17
tue::config::Variant::getValue
bool getValue(int &v)
Definition: variant.h:124
tue::config::Reader::end
bool end()
end go to parent node.
Definition: reader.cpp:43
tue::config::Reader::endArray
bool endArray()
endArray go to parrent of current array, wrapping end() for readibility
Definition: reader.h:69
tue
tue::config::Variant
Definition: variant.h:111
config
tue::config::ReaderWriter config
Definition: sdf_gtest.cpp:9