tue_config
writer.cpp
Go to the documentation of this file.
1 #include "tue/config/writer.h"
2 #include "tue/config/node.h"
3 #include "tue/config/data.h"
4 #include "tue/config/map.h"
5 #include "tue/config/sequence.h"
6 
7 #include <boost/make_shared.hpp>
8 
9 namespace tue
10 {
11 namespace config
12 {
13 
14 // ----------------------------------------------------------------------------------------------------
15 
16 Writer::Writer() : idx_(0)
17 {
18  cfg_.reset(new Data);
19 }
20 
21 // ----------------------------------------------------------------------------------------------------
22 
23 Writer::Writer(DataPointer& cfg) : idx_(cfg.idx), cfg_(cfg.data)
24 {
25  if (!cfg_)
26  {
27  cfg_.reset(new Data);
28  cfg.data = cfg_;
29  }
30 }
31 
32 // ----------------------------------------------------------------------------------------------------
33 
35 {
36 }
37 
38 // ----------------------------------------------------------------------------------------------------
39 
41 {
42  if (cfg_->nodes[idx_]->type() != MAP)
43  return false;
44 
45  Label label = cfg_->getOrAddLabel(name);
46 
47  if (cfg_->nodes[idx_]->readGroup(label, idx_))
48  return true;
49 
50  // If no child node with name is known, create a new one.
51  NodeIdx n = cfg_->addNode(boost::make_shared<Map>(label), idx_);
52 
53  if (!cfg_->nodes[idx_]->addGroup(label, n, idx_))
54  return false;
55 
56  return true;
57 }
58 
59 // ----------------------------------------------------------------------------------------------------
60 
62 {
63  if (cfg_->nodes[idx_]->type() != MAP)
64  return false;
65 
66  Label label = cfg_->getOrAddLabel(name);
67 
68  if (cfg_->nodes[idx_]->readGroup(label, idx_))
69  return true;
70 
71  // If no child node with name is known, create a new one.
72  NodeIdx n = cfg_->addNode(boost::make_shared<Sequence>(label), idx_);
73 
74  if (!cfg_->nodes[idx_]->addGroup(label, n, idx_))
75  return false;
76 
77  return true;
78 }
79 
80 // ----------------------------------------------------------------------------------------------------
81 
83 {
84  if (cfg_->nodes[idx_]->type() != ARRAY)
85  return false;
86 
87  NodePtr node = boost::make_shared<Map>("");
88  NodeIdx n = cfg_->addNode(node, idx_);
89 
90  NodeIdx previous;
91  if (!cfg_->nodes[idx_]->add(n, previous))
92  return false;
93 
94  if (previous != static_cast<NodeIdx>(-1))
95  cfg_->setRightSibling(previous, n);
96 
97  idx_ = n;
98 
99  return true;
100 }
101 
102 // ----------------------------------------------------------------------------------------------------
103 
105 {
106  NodeIdx parent = cfg_->getParent(idx_);
107  if (parent == static_cast<NodeIdx>(-1))
108  return false;
109 
110  idx_ = parent;
111 
112  // If the parent is an array, go up one more
113  if (cfg_->nodes[idx_]->type() == ARRAY)
114  idx_ = cfg_->getParent(idx_);
115 
116  return true;
117 }
118 
119 } // end namespace tue
120 
121 } // end namespace config
122 
std::string
tue::config::NodeIdx
unsigned int NodeIdx
Definition: data_pointer.h:12
tue::config::DataPointer
Definition: data_pointer.h:17
sequence.h
tue::config::Writer::cfg_
boost::shared_ptr< Data > cfg_
Definition: writer.h:105
node.h
tue::config::DataPointer::data
boost::shared_ptr< Data > data
Definition: data_pointer.h:23
map.h
tue::config::Writer::idx_
NodeIdx idx_
Definition: writer.h:103
tue::config::Writer::writeGroup
bool writeGroup(const std::string &name)
writeGroup starts writing a group. Or start extending it, if it already exists.
Definition: writer.cpp:40
tue::config::ARRAY
@ ARRAY
Definition: node_type.h:13
tue::config::Data
Definition: data.h:17
tue::config::MAP
@ MAP
Definition: node_type.h:12
tue::config::Writer::writeArray
bool writeArray(const std::string &name)
writeArray starts writing an array. Or start extending it, if it already exists.
Definition: writer.cpp:61
tue::config::Writer::~Writer
virtual ~Writer()
Definition: writer.cpp:34
data.h
tue::config::NodePtr
boost::shared_ptr< Node > NodePtr
Definition: types.h:21
tue::config::Writer::addArrayItem
bool addArrayItem()
addArrayItem create a new item in the array
Definition: writer.cpp:82
tue::config::Writer::Writer
Writer()
Definition: writer.cpp:16
tue
tue::config::Writer::end
bool end()
end go to parent node.
Definition: writer.cpp:104
writer.h
config
tue::config::ReaderWriter config
Definition: sdf_gtest.cpp:9