1//
2// Pair.h
3//
4// Library: Foundation
5// Package: Dynamic
6// Module: Pair
7//
8// Definition of the Pair class.
9//
10// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// SPDX-License-Identifier: BSL-1.0
14//
15
16
17#ifndef Foundation_Pair_INCLUDED
18#define Foundation_Pair_INCLUDED
19
20
21#include "Poco/Foundation.h"
22#include "Poco/Dynamic/Var.h"
23#include "Poco/Dynamic/VarHolder.h"
24#include <utility>
25
26
27namespace Poco {
28namespace Dynamic {
29
30
31template <typename K>
32class Pair
33 /// Pair allows to define a pair of values.
34{
35public:
36 typedef typename std::pair<K, Var> Data;
37
38 Pair(): _data()
39 /// Creates an empty Pair
40 {
41 }
42
43 Pair(const Pair& other): _data(other._data)
44 /// Creates the Pair from another pair.
45 {
46 }
47
48 Pair(const Data& val): _data(val)
49 /// Creates the Pair from the given value.
50 {
51 }
52
53 template <typename T>
54 Pair(const std::pair<K, T>& val): _data(std::make_pair(val.first, val.second))
55 /// Creates Pair form standard pair.
56 {
57 }
58
59 template <typename T>
60 Pair(const K& first, const T& second): _data(std::make_pair(first, second))
61 /// Creates pair from two values.
62 {
63 }
64
65 virtual ~Pair()
66 /// Destroys the Pair.
67 {
68 }
69
70 Pair& swap(Pair& other)
71 /// Swaps the content of the two Pairs.
72 {
73 std::swap(_data, other._data);
74 return *this;
75 }
76
77 Pair& operator = (const Pair& other)
78 /// Copy constructs Pair from another pair.
79 {
80 Pair(other).swap(*this);
81 return *this;
82 }
83
84 inline const K& first() const
85 /// Returns the first member of the pair.
86 {
87 return _data.first;
88 }
89
90 inline const Var& second() const
91 /// Returns the second member of the pair.
92 {
93 return _data.second;
94 }
95
96 std::string toString()
97 {
98 std::string str;
99 Var(*this).template convert<std::string>(str);
100 return str;
101 }
102
103private:
104 Data _data;
105};
106
107
108template <>
109class VarHolderImpl<Pair<std::string> >: public VarHolder
110{
111public:
112 VarHolderImpl(const Pair<std::string>& val): _val(val)
113 {
114 }
115
116 ~VarHolderImpl()
117 {
118 }
119
120 const std::type_info& type() const
121 {
122 return typeid(Pair<std::string>);
123 }
124
125 void convert(Int8& val) const
126 {
127 throw BadCastException("Cannot cast Pair type to Int8");
128 }
129
130 void convert(Int16& val) const
131 {
132 throw BadCastException("Cannot cast Pair type to Int16");
133 }
134
135 void convert(Int32& val) const
136 {
137 throw BadCastException("Cannot cast Pair type to Int32");
138 }
139
140 void convert(Int64& val) const
141 {
142 throw BadCastException("Cannot cast Pair type to Int64");
143 }
144
145 void convert(UInt8& val) const
146 {
147 throw BadCastException("Cannot cast Pair type to UInt8");
148 }
149
150 void convert(UInt16& val) const
151 {
152 throw BadCastException("Cannot cast Pair type to UInt16");
153 }
154
155 void convert(UInt32& val) const
156 {
157 throw BadCastException("Cannot cast Pair type to UInt32");
158 }
159
160 void convert(UInt64& val) const
161 {
162 throw BadCastException("Cannot cast Pair type to UInt64");
163 }
164
165 void convert(bool& val) const
166 {
167 throw BadCastException("Cannot cast Pair type to bool");
168 }
169
170 void convert(float& val) const
171 {
172 throw BadCastException("Cannot cast Pair type to float");
173 }
174
175 void convert(double& val) const
176 {
177 throw BadCastException("Cannot cast Pair type to double");
178 }
179
180 void convert(char& val) const
181 {
182 throw BadCastException("Cannot cast Pair type to char");
183 }
184
185 void convert(std::string& val) const
186 {
187 // Serialize in JSON format: equals an object
188 // JSON format definition: { string ':' value } string:value pair n-times, sep. by ','
189 val.append("{ ");
190 Var key(_val.first());
191 Impl::appendJSONKey(val, key);
192 val.append(" : ");
193 Impl::appendJSONValue(val, _val.second());
194 val.append(" }");
195 }
196
197 void convert(Poco::DateTime&) const
198 {
199 throw BadCastException("Pair -> Poco::DateTime");
200 }
201
202 void convert(Poco::LocalDateTime&) const
203 {
204 throw BadCastException("Pair -> Poco::LocalDateTime");
205 }
206
207 void convert(Poco::Timestamp&) const
208 {
209 throw BadCastException("Pair -> Poco::Timestamp");
210 }
211
212 VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
213 {
214 return cloneHolder(pVarHolder, _val);
215 }
216
217 const Pair<std::string>& value() const
218 {
219 return _val;
220 }
221
222 bool isArray() const
223 {
224 return false;
225 }
226
227 bool isStruct() const
228 {
229 return false;
230 }
231
232 bool isInteger() const
233 {
234 return false;
235 }
236
237 bool isSigned() const
238 {
239 return false;
240 }
241
242 bool isNumeric() const
243 {
244 return false;
245 }
246
247 bool isString() const
248 {
249 return false;
250 }
251
252private:
253 Pair<std::string> _val;
254};
255
256
257template <>
258class VarHolderImpl<Pair<int> >: public VarHolder
259{
260public:
261 VarHolderImpl(const Pair<int>& val): _val(val)
262 {
263 }
264
265 ~VarHolderImpl()
266 {
267 }
268
269 const std::type_info& type() const
270 {
271 return typeid(Pair<int>);
272 }
273
274 void convert(Int8& val) const
275 {
276 throw BadCastException("Cannot cast Pair type to Int8");
277 }
278
279 void convert(Int16& val) const
280 {
281 throw BadCastException("Cannot cast Pair type to Int16");
282 }
283
284 void convert(Int32& val) const
285 {
286 throw BadCastException("Cannot cast Pair type to Int32");
287 }
288
289 void convert(Int64& val) const
290 {
291 throw BadCastException("Cannot cast Pair type to Int64");
292 }
293
294 void convert(UInt8& val) const
295 {
296 throw BadCastException("Cannot cast Pair type to UInt8");
297 }
298
299 void convert(UInt16& val) const
300 {
301 throw BadCastException("Cannot cast Pair type to UInt16");
302 }
303
304 void convert(UInt32& val) const
305 {
306 throw BadCastException("Cannot cast Pair type to UInt32");
307 }
308
309 void convert(UInt64& val) const
310 {
311 throw BadCastException("Cannot cast Pair type to UInt64");
312 }
313
314 void convert(bool& val) const
315 {
316 throw BadCastException("Cannot cast Pair type to bool");
317 }
318
319 void convert(float& val) const
320 {
321 throw BadCastException("Cannot cast Pair type to float");
322 }
323
324 void convert(double& val) const
325 {
326 throw BadCastException("Cannot cast Pair type to double");
327 }
328
329 void convert(char& val) const
330 {
331 throw BadCastException("Cannot cast Pair type to char");
332 }
333
334 void convert(std::string& val) const
335 {
336 // Serialize in JSON format: equals an object
337 // JSON format definition: { string ':' value } string:value pair n-times, sep. by ','
338 val.append("{ ");
339 Var key(_val.first());
340 Impl::appendJSONKey(val, key);
341 val.append(" : ");
342 Impl::appendJSONValue(val, _val.second());
343 val.append(" }");
344 }
345
346 void convert(Poco::DateTime&) const
347 {
348 throw BadCastException("Pair -> Poco::DateTime");
349 }
350
351 void convert(Poco::LocalDateTime&) const
352 {
353 throw BadCastException("Pair -> Poco::LocalDateTime");
354 }
355
356 void convert(Poco::Timestamp&) const
357 {
358 throw BadCastException("Pair -> Poco::Timestamp");
359 }
360
361 VarHolder* clone(Placeholder<VarHolder>* pVarHolder = 0) const
362 {
363 return cloneHolder(pVarHolder, _val);
364 }
365
366 const Pair<int>& value() const
367 {
368 return _val;
369 }
370
371 bool isArray() const
372 {
373 return false;
374 }
375
376 bool isStruct() const
377 {
378 return false;
379 }
380
381 bool isInteger() const
382 {
383 return false;
384 }
385
386 bool isSigned() const
387 {
388 return false;
389 }
390
391 bool isNumeric() const
392 {
393 return false;
394 }
395
396 bool isString() const
397 {
398 return false;
399 }
400
401private:
402 Pair<int> _val;
403};
404
405
406} // namespace Dynamic
407
408
409} // namespace Poco
410
411
412#endif // Foundation_Pair_INCLUDED
413