Ariles
std_map.h
Go to the documentation of this file.
1 /**
2  @file
3  @author Alexander Sherikov
4 
5  @copyright 2018 Alexander Sherikov, Licensed under the Apache License, Version 2.0.
6  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
7 
8  @brief
9 */
10 
11 #pragma once
12 
13 #include <map>
14 
15 #include "../internal/helpers.h"
16 #include "../visitors/serialization.h"
17 #include "std_pair.h"
18 
19 namespace ariles2
20 {
21  namespace read
22  {
23  template <class t_Visitor, typename t_Key, typename t_Value, class t_Compare, class t_Allocator>
25  t_Visitor &visitor,
26  std::map<t_Key, t_Value, t_Compare, t_Allocator> &entry,
27  const typename t_Visitor::Parameters &param)
28  {
29  ARILES2_TRACE_FUNCTION;
30  std::size_t size = visitor.startArray();
31  entry.clear();
32  for (std::size_t i = 0; i < size; ++i)
33  {
34  std::pair<t_Key, t_Value> map_entry;
35 
36  visitor.startArrayElement();
37  apply_read(visitor, map_entry, param);
38  entry.insert(map_entry);
39  visitor.endArrayElement();
40  }
41  visitor.endArray();
42  }
43 
44 
45  template <class t_Visitor, typename t_Value, class t_Compare, class t_Allocator>
47  t_Visitor &visitor,
48  std::map<std::string, t_Value, t_Compare, t_Allocator> &entry,
49  const typename t_Visitor::Parameters &parameters)
50  {
51  ARILES2_TRACE_FUNCTION;
52  if (true == parameters.sloppy_maps_ and true == visitor.startIteratedMap(t_Visitor::SIZE_LIMIT_MIN, 1))
53  {
54  entry.clear();
55  std::string entry_name;
56  while (true == visitor.startIteratedMapElement(entry_name))
57  {
58  apply_read(visitor, entry[entry_name], parameters);
59  visitor.endIteratedMapElement();
60  }
61  visitor.endIteratedMap();
62  }
63  else
64  {
65  apply_read<t_Visitor, std::string, t_Value, t_Compare, t_Allocator>(visitor, entry, parameters);
66  }
67  }
68  } // namespace read
69 } // namespace ariles2
70 
71 
72 namespace ariles2
73 {
74  namespace write
75  {
76  template <class t_Visitor, typename t_Key, typename t_Value, class t_Compare, class t_Allocator>
78  t_Visitor &writer,
79  const std::map<t_Key, t_Value, t_Compare, t_Allocator> &entry,
80  const typename t_Visitor::Parameters &param)
81  {
82  ARILES2_TRACE_FUNCTION;
83  writer.startArray(entry.size(), param.compact_arrays_);
84  for (typename std::map<t_Key, t_Value, t_Compare, t_Allocator>::const_iterator it = entry.begin();
85  it != entry.end();
86  ++it)
87  {
88  writer.visitArrayElement(*it, param);
89  }
90  writer.endArray();
91  }
92 
93 
94  template <class t_Visitor, typename t_Value, class t_Compare, class t_Allocator>
96  t_Visitor &writer,
97  const std::map<std::string, t_Value, t_Compare, t_Allocator> &entry,
98  const typename t_Visitor::Parameters &param)
99  {
100  ARILES2_TRACE_FUNCTION;
101  if (true == param.sloppy_maps_)
102  {
103  if (true == writer.startIteratedMap(entry.size(), param))
104  {
105  for (typename std::map<std::string, t_Value, t_Compare, t_Allocator>::const_iterator it =
106  entry.begin();
107  it != entry.end();
108  ++it)
109  {
110  writer.startIteratedMapElement(it->first);
111  apply_write(writer, it->second, param);
112  writer.endIteratedMapElement();
113  }
114  writer.endIteratedMap();
115  return;
116  }
117  }
118  apply_write<t_Visitor, std::string, t_Value, t_Compare, t_Allocator>(writer, entry, param);
119  }
120  } // namespace write
121 } // namespace ariles2
122 
123 
124 namespace ariles2
125 {
126  namespace compare
127  {
128  template <class t_Visitor, typename t_Key, typename t_Value, class t_Compare, class t_Allocator>
130  t_Visitor &visitor,
131  const std::map<t_Key, t_Value, t_Compare, t_Allocator> &left,
132  const std::map<t_Key, t_Value, t_Compare, t_Allocator> &right,
133  const typename t_Visitor::Parameters &param)
134  {
135  ARILES2_TRACE_FUNCTION;
136 
137  visitor.equal_ &= (left.size() == right.size());
138 
139  typename std::map<t_Key, t_Value, t_Compare, t_Allocator>::const_iterator left_it = left.begin();
140  typename std::map<t_Key, t_Value, t_Compare, t_Allocator>::const_iterator right_it = right.begin();
141 
142  for (; (left_it != left.end()) and (right_it != right.end()) and (true == visitor.equal_);
143  ++left_it, ++right_it)
144  {
145  apply_compare(visitor, left_it->first, right_it->first, param);
146  apply_compare(visitor, left_it->second, right_it->second, param);
147  }
148  }
149  } // namespace compare
150 } // namespace ariles2
151 
152 
153 
154 namespace ariles2
155 {
156  namespace defaults
157  {
158  template <class t_Visitor, typename t_Key, typename t_Value, class t_Compare, class t_Allocator>
160  const t_Visitor & /*visitor*/,
161  std::map<t_Key, t_Value, t_Compare, t_Allocator> &entry,
162  const typename t_Visitor::Parameters & /*param*/)
163  {
164  ARILES2_TRACE_FUNCTION;
165  entry.clear();
166  }
167  } // namespace defaults
168 } // namespace ariles2
169 
170 
171 namespace ariles2
172 {
173  namespace process
174  {
175  template <class t_Visitor, typename t_Key, typename t_Value, class t_Compare, class t_Allocator>
177  const t_Visitor &visitor,
178  std::map<t_Key, t_Value, t_Compare, t_Allocator> &entry,
179  const typename t_Visitor::Parameters &param)
180  {
181  ARILES2_TRACE_FUNCTION;
182  for (typename std::map<t_Key, t_Value, t_Compare, t_Allocator>::iterator it = entry.begin();
183  it != entry.end();
184  ++it)
185  {
186  apply_process(visitor, it->first, param);
187  apply_process(visitor, it->second, param);
188  }
189  }
190  } // namespace process
191 } // namespace ariles2
192 
193 
194 namespace ariles2
195 {
196  namespace copyfrom
197  {
198  template <
199  class t_Visitor,
200  typename t_KeyLeft,
201  typename t_ValueLeft,
202  class t_CompareLeft,
203  class t_AllocatorLeft,
204  typename t_KeyRight,
205  typename t_ValueRight,
206  class t_CompareRight,
207  class t_AllocatorRight>
209  t_Visitor &visitor,
210  std::map<t_KeyLeft, t_ValueLeft, t_CompareLeft, t_AllocatorLeft> &left,
211  const std::map<t_KeyRight, t_ValueRight, t_CompareRight, t_AllocatorRight> &right,
212  const typename t_Visitor::Parameters &param)
213  {
214  ARILES2_TRACE_FUNCTION;
215 
216  typename std::map<t_KeyRight, t_ValueRight, t_CompareRight, t_AllocatorRight>::const_iterator right_it =
217  right.begin();
218 
219  left.clear();
220 
221  for (; right_it != right.end(); ++right_it)
222  {
223  t_KeyLeft left_key;
224 
225  apply_copyfrom(visitor, left_key, right_it->first, param);
226  apply_copyfrom(visitor, left[left_key], right_it->second, param);
227  }
228  }
229  } // namespace copyfrom
230 
231 
232  namespace copyto
233  {
234  template <
235  class t_Visitor,
236  typename t_KeyLeft,
237  typename t_ValueLeft,
238  class t_CompareLeft,
239  class t_AllocatorLeft,
240  typename t_KeyRight,
241  typename t_ValueRight,
242  class t_CompareRight,
243  class t_AllocatorRight>
245  t_Visitor &visitor,
246  const std::map<t_KeyLeft, t_ValueLeft, t_CompareLeft, t_AllocatorLeft> &left,
247  std::map<t_KeyRight, t_ValueRight, t_CompareRight, t_AllocatorRight> &right,
248  const typename t_Visitor::Parameters &param)
249  {
250  ARILES2_TRACE_FUNCTION;
251 
252  typename std::map<t_KeyLeft, t_ValueLeft, t_CompareLeft, t_AllocatorLeft>::const_iterator left_it =
253  left.begin();
254 
255  right.clear();
256 
257  for (; left_it != left.end(); ++left_it)
258  {
259  t_KeyRight right_key;
260 
261  apply_copyto(visitor, left_it->first, right_key, param);
262  apply_copyto(visitor, left_it->second, right[right_key], param);
263  }
264  }
265  } // namespace copyto
266 } // namespace ariles2
ariles2::copyfrom::apply_copyfrom
void ARILES2_VISIBILITY_ATTRIBUTE apply_copyfrom(t_Visitor &visitor, t_Left &left, const t_Right &right, const typename t_Visitor::Parameters &param, ARILES2_IS_BASE_ENABLER(ariles2::Ariles, t_Left))
Definition: basic.h:323
ariles2
Definition: basic.h:16
std_pair.h
ariles2::write::apply_write
void ARILES2_VISIBILITY_ATTRIBUTE apply_write(t_Visitor &writer, const t_Entry &entry, const typename t_Visitor::Parameters &parameters, ARILES2_IS_BASE_ENABLER(ariles2::write::Base, t_Entry))
Definition: basic.h:85
ariles2::process::apply_process
void ARILES2_VISIBILITY_ATTRIBUTE apply_process(const t_Visitor &visitor, t_Entry &entry, const typename t_Visitor::Parameters &param, ARILES2_IS_BASE_ENABLER(ariles2::Ariles, t_Entry))
Definition: basic.h:293
t_Visitor
ariles2::compare::apply_compare
void ARILES2_VISIBILITY_ATTRIBUTE apply_compare(t_Visitor &visitor, const t_Left &left, const t_Right &right, const typename t_Visitor::Parameters &param, ARILES2_IS_BASE_ENABLER(ariles2::Ariles, t_Left))
Definition: basic.h:146
ARILES2_VISIBILITY_ATTRIBUTE
#define ARILES2_VISIBILITY_ATTRIBUTE
Definition: helpers.h:138
ariles2::read::apply_read
void ARILES2_VISIBILITY_ATTRIBUTE apply_read(t_Visitor &visitor, t_Entry &entry, const typename t_Visitor::Parameters &parameters, ARILES2_IS_BASE_ENABLER(ariles2::read::Base, t_Entry))
Definition: basic.h:21
ariles2::copyto::apply_copyto
void ARILES2_VISIBILITY_ATTRIBUTE apply_copyto(t_Visitor &visitor, const t_Left &left, t_Right &right, const typename t_Visitor::Parameters &param, ARILES2_IS_BASE_ENABLER(ariles2::Ariles, t_Left))
Definition: basic.h:369
ariles2::defaults::apply_defaults
void ARILES2_VISIBILITY_ATTRIBUTE apply_defaults(const t_Visitor &visitor, t_Entry &entry, const typename t_Visitor::Parameters &param, ARILES2_IS_BASE_ENABLER(ariles2::defaults::Base, t_Entry))
Definition: basic.h:237