tue_config
data_pointer.cpp
Go to the documentation of this file.
2 #include "tue/config/data.h"
3 #include "tue/config/map.h"
4 #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 void data_merge(Data& c1, NodeIdx me_idx, const Data& c2, NodeIdx other_idx, std::string& error)
17 {
18  NodePtr& n1 = c1.nodes[me_idx];
19  const NodePtr& n2 = c2.nodes[other_idx];
20 
21  if (n1->type() != n2->type())
22  {
23  error += "Trying to merge two nodes of different types.\n";
24  return;
25  }
26 
27  if (n1->type() == ARRAY)
28  {
29  // n1's sequence is completely replaced by n2's sequence
30  c1.nodes[me_idx] = n2->deepCopy(c2, me_idx, c1);
31  } else
32  {
33  Map* map1 = static_cast<Map*>(n1.get());
34  const Map* map2 = static_cast<const Map*>(n2.get());
35 
36  boost::shared_ptr<Map> new_map = boost::make_shared<Map>(*map1);
37 
38  // Merge children
39  const std::map<Label, NodeIdx>& children = map2->map_;
40  for(std::map<Label, NodeIdx>::const_iterator it2 = children.begin(); it2 != children.end(); ++it2)
41  {
42  const Label& label2 = it2->first;
43  std::map<Label, NodeIdx>::iterator it1 = map1->map_.find(label2);
44  if (it1 == map1->map_.end())
45  {
46  // c1 does not have that child, so add it
47  NodeIdx child_idx = c1.addNode(NodePtr(), me_idx);
48  c1.nodes[child_idx] = c2.nodes[it2->second]->deepCopy(c2, child_idx, c1);
49  new_map->map_[label2] = child_idx;
50  }
51  else
52  {
53  // n1 already has that child. Merge them.
54  data_merge(c1, it1->second, c2, it2->second, error);
55  }
56  }
57 
58  // Merge values
59  const std::map<Label, Variant>& values = map2->values;
60  for(std::map<Label, Variant>::const_iterator it2 = values.begin(); it2 != values.end(); ++it2)
61  {
62  new_map->values[it2->first] = it2->second;
63  }
64 
65  c1.nodes[me_idx] = new_map;
66  }
67 }
68 
69 // ----------------------------------------------------------------------------------------------------
70 
72 {
73  if (!data)
74  {
75  data = boost::make_shared<Data>(*ptr.data);
76  return true;
77  }
78 
79  if (!ptr.data)
80  return true;
81 
82  // std::cout << std::endl;
83  // std::cout << "----- MERGING ----" << std::endl;
84  // std::cout << *this << std::endl;
85  // std::cout << "----- WITH ----" << std::endl;
86  // std::cout << ptr << std::endl;
87 
88  std::string error;
89  data_merge(*data, idx, *ptr.data, ptr.idx, error);
90 
91  if (!error.empty())
92  {
93  std::cout << "DataPointer::add: " << error << std::endl;
94  return false;
95  }
96 
97  // std::cout << "------ RESULT -------" << std::endl;
98  // std::cout << *this << std::endl;
99  // std::cout << "------ ---- -------" << std::endl;
100 
101  return true;
102 }
103 
104 // ----------------------------------------------------------------------------------------------------
105 
106 DataPointer::DataPointer() : data(new Data), idx(0) {}
107 
108 // ----------------------------------------------------------------------------------------------------
109 
110 bool DataPointer::empty() const
111 {
112  return (data->nodes.empty() || (data->nodes.size() == 1 && data->nodes[0]->empty()));
113 }
114 
115 // ----------------------------------------------------------------------------------------------------
116 
117 DataConstPointer::DataConstPointer() : data(new Data), idx(0) {}
118 
119 // ----------------------------------------------------------------------------------------------------
120 
122 {
123  return (data->nodes.empty() || (data->nodes.size() == 1 && data->nodes[0]->empty()));
124 }
125 
126 // ----------------------------------------------------------------------------------------------------
127 
129 {
130  YAMLEmitter emitter;
131  emitter.emit(d, out);
132  return out;
133 }
134 
135 // ----------------------------------------------------------------------------------------------------
136 
138 {
139  YAMLEmitter emitter;
140  emitter.emit(d, out);
141  return out;
142 }
143 
144 } // end namespace tue
145 
146 } // end namespace config
147 
std::string
tue::config::YAMLEmitter
Definition: yaml_emitter.h:16
tue::config::NodeIdx
unsigned int NodeIdx
Definition: data_pointer.h:12
tue::config::DataPointer
Definition: data_pointer.h:17
tue::config::Map::map_
std::map< Label, NodeIdx > map_
Definition: map.h:62
sequence.h
tue::config::Map
Definition: map.h:12
tue::config::operator<<
std::ostream & operator<<(std::ostream &out, const DataConstPointer &d)
Definition: data_pointer.cpp:128
std::cout
tue::config::DataConstPointer::DataConstPointer
DataConstPointer()
Definition: data_pointer.cpp:117
data_pointer.h
tue::config::Data::nodes
std::vector< NodePtr > nodes
Definition: data.h:77
tue::config::DataPointer::idx
NodeIdx idx
Definition: data_pointer.h:24
std::ostream
tue::config::DataConstPointer::empty
bool empty() const
Definition: data_pointer.cpp:121
tue::config::DataPointer::data
boost::shared_ptr< Data > data
Definition: data_pointer.h:23
tue::config::Data::addNode
NodeIdx addNode(const NodePtr &n, NodeIdx parent)
Definition: data.h:61
tue::config::DataConstPointer::idx
NodeIdx idx
Definition: data_pointer.h:43
map.h
std::map
tue::config::DataConstPointer::data
boost::shared_ptr< const Data > data
Definition: data_pointer.h:42
tue::config::DataPointer::DataPointer
DataPointer()
Definition: data_pointer.cpp:106
tue::config::ARRAY
@ ARRAY
Definition: node_type.h:13
tue::config::Data
Definition: data.h:17
std::endl
T endl(T... args)
tue::config::YAMLEmitter::emit
void emit(const tue::config::DataConstPointer &cfg, std::ostream &out, const std::string &indent="")
Definition: yaml_emitter.cpp:64
tue::config::data_merge
void data_merge(Data &c1, NodeIdx me_idx, const Data &c2, NodeIdx other_idx, std::string &error)
Definition: data_pointer.cpp:16
std::map::begin
T begin(T... args)
data.h
tue::config::NodePtr
boost::shared_ptr< Node > NodePtr
Definition: types.h:21
tue::config::DataConstPointer
Definition: data_pointer.h:35
tue::config::DataPointer::add
bool add(const DataConstPointer &ptr)
Definition: data_pointer.cpp:71
std::string::empty
T empty(T... args)
tue::config::DataPointer::empty
bool empty() const
Definition: data_pointer.cpp:110
std::map::end
T end(T... args)
tue::config::Map::values
std::map< Label, Variant > values
Definition: map.h:64
tue
config
tue::config::ReaderWriter config
Definition: sdf_gtest.cpp:9
yaml_emitter.h