source: rrlib_rtti_conversion/defined_conversions.cpp @ 8:b9554b04aa0f

17.03
Last change on this file since 8:b9554b04aa0f was 8:b9554b04aa0f, checked in by Max Reichardt <mreichardt@…>, 2 years ago

Adds conversion operations for tuples - and unifies 'for-each' and 'get-element' conversion operations for vectors and arrays

File size: 30.0 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program; if not, write to the Free Software Foundation, Inc.,
19// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20//
21//----------------------------------------------------------------------
22/*!\file    rrlib/rtti_conversion/defined_conversions.cpp
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2016-08-13
27 *
28 */
29//----------------------------------------------------------------------
30#include "rrlib/rtti_conversion/defined_conversions.h"
31
32//----------------------------------------------------------------------
33// External includes (system with <>, local with "")
34//----------------------------------------------------------------------
35#include <iomanip>
36
37//----------------------------------------------------------------------
38// Internal includes with ""
39//----------------------------------------------------------------------
40#include "rrlib/rtti_conversion/tStaticCastOperation.h"
41#include "rrlib/rtti_conversion/definition/tVoidFunctionConversionOperation.h"
42
43//----------------------------------------------------------------------
44// Debugging
45//----------------------------------------------------------------------
46#include <cassert>
47
48//----------------------------------------------------------------------
49// Namespace usage
50//----------------------------------------------------------------------
51
52//----------------------------------------------------------------------
53// Namespace declaration
54//----------------------------------------------------------------------
55namespace rrlib
56{
57namespace rtti
58{
59namespace conversion
60{
61
62//----------------------------------------------------------------------
63// Forward declarations / typedefs / enums
64//----------------------------------------------------------------------
65
66//----------------------------------------------------------------------
67// Const values
68//----------------------------------------------------------------------
69
70// Register static casts between builtin types
71auto& cBUILTIN_TYPE_CASTS = tStaticCastOperation::
72                            Register<int8_t, int16_t, true, true>()
73                            .Register<int8_t, int32_t, true, true>()
74                            .Register<int8_t, int64_t, true, true>()
75                            .Register<int8_t, uint8_t, true, true>()
76                            .Register<int8_t, uint16_t, true, true>()
77                            .Register<int8_t, uint32_t, true, true>()
78                            .Register<int8_t, uint64_t, true, true>()
79                            .Register<int8_t, float, true, true>()
80                            .Register<int8_t, double, true, true>()
81                            .Register<int8_t, bool, true, true>()
82
83                            .Register<int16_t, int32_t, true, true>()
84                            .Register<int16_t, int64_t, true, true>()
85                            .Register<int16_t, uint8_t, true, true>()
86                            .Register<int16_t, uint16_t, true, true>()
87                            .Register<int16_t, uint32_t, true, true>()
88                            .Register<int16_t, uint64_t, true, true>()
89                            .Register<int16_t, float, true, true>()
90                            .Register<int16_t, double, true, true>()
91                            .Register<int16_t, bool, true, true>()
92
93                            .Register<int32_t, int64_t, true, true>()
94                            .Register<int32_t, uint8_t, true, true>()
95                            .Register<int32_t, uint16_t, true, true>()
96                            .Register<int32_t, uint32_t, true, true>()
97                            .Register<int32_t, uint64_t, true, true>()
98                            .Register<int32_t, float, true, true>()
99                            .Register<int32_t, double, true, true>()
100                            .Register<int32_t, bool, true, true>()
101
102                            .Register<int64_t, uint8_t, true, true>()
103                            .Register<int64_t, uint16_t, true, true>()
104                            .Register<int64_t, uint32_t, true, true>()
105                            .Register<int64_t, uint64_t, true, true>()
106                            .Register<int64_t, float, true, true>()
107                            .Register<int64_t, double, true, true>()
108                            .Register<int64_t, bool, true, true>()
109
110                            .Register<uint8_t, uint16_t, true, true>()
111                            .Register<uint8_t, uint32_t, true, true>()
112                            .Register<uint8_t, uint64_t, true, true>()
113                            .Register<uint8_t, float, true, true>()
114                            .Register<uint8_t, double, true, true>()
115                            .Register<uint8_t, bool, true, true>()
116
117                            .Register<uint16_t, uint32_t, true, true>()
118                            .Register<uint16_t, uint64_t, true, true>()
119                            .Register<uint16_t, float, true, true>()
120                            .Register<uint16_t, double, true, true>()
121                            .Register<uint16_t, bool, true, true>()
122
123                            .Register<uint32_t, uint64_t, true, true>()
124                            .Register<uint32_t, float, true, true>()
125                            .Register<uint32_t, double, true, true>()
126                            .Register<uint32_t, bool, true, true>()
127
128                            .Register<uint64_t, float, true, true>()
129                            .Register<uint64_t, double, true, true>()
130                            .Register<uint64_t, bool, true, true>()
131
132                            .Register<float, double, true, true>()
133                            .Register<float, bool, true, true>()
134
135                            .Register<double, bool, true, true>()
136
137                            .Register<rrlib::serialization::tMemoryBuffer, std::vector<uint8_t>>();
138
139//----------------------------------------------------------------------
140// Implementation
141//----------------------------------------------------------------------
142
143namespace
144{
145
146class tToStringOperation : public tRegisteredConversionOperation
147{
148public:
149  tToStringOperation() : tRegisteredConversionOperation(util::tManagedConstCharPointer("ToString", false), tSupportedTypeFilter::STRING_SERIALIZABLE, tDataType<std::string>(), nullptr, tParameterDefinition("Flags", tDataType<unsigned int>(), true))
150  {}
151
152  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override
153  {
154    if ((source_type.GetTypeTraits() & trait_flags::cIS_STRING_SERIALIZABLE) && destination_type == tDataType<std::string>())
155    {
156      return tConversionOption(source_type, destination_type, false, &FirstConversionFunction, &FinalConversionFunction);
157    }
158    return tConversionOption();
159  }
160
161  static void MainConversionFunction(const tTypedConstPointer& source_object, std::string& destination, const tCurrentConversionOperation& operation)
162  {
163    rrlib::serialization::tStringOutputStream stream;
164
165    // init stream
166    /*auto precision = operation.GetParameterValue(0);
167    if (precision)
168    {
169      stream.GetWrappedStringStream() << std::setprecision(*precision.Get<int>());
170    }*/
171    auto flags = operation.GetParameterValue();
172    if (flags)
173    {
174      unsigned int f = *flags.Get<unsigned int>();
175      if (f)
176      {
177        if (f & eTSF_BOOL_ALPHA)
178        {
179          stream.GetWrappedStringStream() << std::boolalpha;
180        }
181        if (f & eTSF_SHOW_BASE)
182        {
183          stream.GetWrappedStringStream() << std::showbase;
184        }
185        if (f & eTSF_SHOW_POINT)
186        {
187          stream.GetWrappedStringStream() << std::showpoint;
188        }
189        if (f & eTSF_SHOW_POS)
190        {
191          stream.GetWrappedStringStream() << std::showpos;
192        }
193        if (f & eTSF_UPPER_CASE)
194        {
195          stream.GetWrappedStringStream() << std::uppercase;
196        }
197        if (f & eTSF_DEC)
198        {
199          stream.GetWrappedStringStream() << std::dec;
200        }
201        if (f & eTSF_HEX)
202        {
203          stream.GetWrappedStringStream() << std::hex;
204        }
205        if (f & eTSF_OCT)
206        {
207          stream.GetWrappedStringStream() << std::oct;
208        }
209        if (f & eTSF_FIXED)
210        {
211          stream.GetWrappedStringStream() << std::fixed;
212        }
213        if (f & eTSF_SCIENTIFIC)
214        {
215          stream.GetWrappedStringStream() << std::scientific;
216        }
217      }
218    }
219
220    source_object.Serialize(stream);
221    destination = stream.ToString();
222  }
223
224  static void FirstConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation)
225  {
226    std::string intermediate;
227    MainConversionFunction(source_object, intermediate, operation);
228    operation.Continue(tTypedConstPointer(&intermediate), destination_object);
229  }
230
231  static void FinalConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation)
232  {
233    MainConversionFunction(source_object, *destination_object.Get<std::string>(), operation);
234  }
235};
236
237class tStringDeserializationOperation : public tRegisteredConversionOperation
238{
239public:
240  tStringDeserializationOperation() : tRegisteredConversionOperation(util::tManagedConstCharPointer("String Deserialization", false), tDataType<std::string>(), tSupportedTypeFilter::STRING_SERIALIZABLE)
241  {}
242
243  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override
244  {
245    if ((destination_type.GetTypeTraits() & trait_flags::cIS_STRING_SERIALIZABLE) && source_type == tDataType<std::string>())
246    {
247      return tConversionOption(source_type, destination_type, false, &FirstConversionFunction, &FinalConversionFunction);
248    }
249    return tConversionOption();
250  }
251
252  static void FirstConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation)
253  {
254    tType inter_type = operation.compiled_operation.IntermediateType();
255    char intermediate_memory[inter_type.GetSize(true)];
256    auto intermediate_object = inter_type.EmplaceGenericObject(intermediate_memory);
257    serialization::tStringInputStream stream(*source_object.Get<std::string>());
258    intermediate_object->Deserialize(stream);
259    operation.Continue(*intermediate_object, destination_object);
260  }
261
262  static void FinalConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation)
263  {
264    serialization::tStringInputStream stream(*source_object.Get<std::string>());
265    destination_object.Deserialize(stream);
266  }
267};
268
269class tBinarySerializationOperation : public tRegisteredConversionOperation
270{
271public:
272  tBinarySerializationOperation() : tRegisteredConversionOperation(util::tManagedConstCharPointer("Binary Serialization", false), tSupportedTypeFilter::BINARY_SERIALIZABLE, tDataType<serialization::tMemoryBuffer>())
273  {}
274
275  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override
276  {
277    if ((source_type.GetTypeTraits() & trait_flags::cIS_BINARY_SERIALIZABLE) && destination_type == tDataType<serialization::tMemoryBuffer>())
278    {
279      return tConversionOption(source_type, destination_type, false, &FirstConversionFunction, &FinalConversionFunction);
280    }
281    return tConversionOption();
282  }
283
284  static void FirstConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation)
285  {
286    serialization::tStackMemoryBuffer<serialization::cSTACK_BUFFERS_SIZE> buffer;
287    rrlib::serialization::tOutputStream stream(buffer);
288    source_object.Serialize(stream);
289    stream.Close();
290    operation.Continue(tTypedConstPointer(&static_cast<serialization::tMemoryBuffer&>(buffer)), destination_object);
291  }
292
293  static void FinalConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation)
294  {
295    serialization::tOutputStream stream(*destination_object.Get<serialization::tMemoryBuffer>());
296    destination_object.Serialize(stream);
297  }
298};
299
300class tBinaryDeserializationOperation : public tRegisteredConversionOperation
301{
302public:
303  tBinaryDeserializationOperation() : tRegisteredConversionOperation(util::tManagedConstCharPointer("Binary Deserialization", false), tDataType<serialization::tMemoryBuffer>(), tSupportedTypeFilter::STRING_SERIALIZABLE)
304  {}
305
306  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override
307  {
308    if ((destination_type.GetTypeTraits() & trait_flags::cIS_BINARY_SERIALIZABLE) && source_type == tDataType<serialization::tMemoryBuffer>())
309    {
310      return tConversionOption(source_type, destination_type, false, &FirstConversionFunction, &FinalConversionFunction);
311    }
312    return tConversionOption();
313  }
314
315  static void FirstConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation)
316  {
317    tType inter_type = operation.compiled_operation.IntermediateType();
318    char intermediate_memory[inter_type.GetSize(true)];
319    auto intermediate_object = inter_type.EmplaceGenericObject(intermediate_memory);
320    serialization::tInputStream stream(*source_object.Get<serialization::tMemoryBuffer>());
321    intermediate_object->Deserialize(stream);
322    operation.Continue(*intermediate_object, destination_object);
323  }
324
325  static void FinalConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation)
326  {
327    serialization::tInputStream stream(*source_object.Get<serialization::tMemoryBuffer>());
328    destination_object.Deserialize(stream);
329  }
330};
331
332class tGetListElement : public tRegisteredConversionOperation
333{
334public:
335  tGetListElement() : tRegisteredConversionOperation(util::tManagedConstCharPointer("[]", false), tSupportedTypeFilter::GET_LIST_ELEMENT, tSupportedTypeFilter::GET_LIST_ELEMENT, nullptr, tParameterDefinition("Index", tDataType<unsigned int>(), true))
336  {}
337
338  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override
339  {
340    if (source_type.IsListType() && source_type.GetElementType() == destination_type)
341    {
342      return tConversionOption(source_type, destination_type, &FirstConversionFunction, &GetDestinationReference);
343    }
344    unsigned int index = parameter ? parameter->GetData<unsigned int>() : 0;
345    if (source_type.IsArray() && source_type.GetElementType() == destination_type && index < source_type.GetArraySize())
346    {
347      return tConversionOption(source_type, destination_type, index * source_type.GetElementType().GetSize());
348    }
349    return tConversionOption();
350  }
351
352  static tTypedConstPointer GetDestinationReference(const tTypedConstPointer& source_object, const tCurrentConversionOperation& operation)
353  {
354    auto index_parameter = operation.GetParameterValue();
355    unsigned int index = index_parameter ? (*index_parameter.Get<unsigned int>()) : 0;
356    auto result = source_object.GetVectorElement(index);
357    if (!result)
358    {
359      throw std::invalid_argument("Index out of bounds");
360    }
361    return result;
362  }
363
364  static void FirstConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation)
365  {
366    auto index_parameter = operation.GetParameterValue();
367    unsigned int index = index_parameter ? (*index_parameter.Get<unsigned int>()) : 0;
368    auto intermediate = source_object.GetVectorElement(index);
369    if (!intermediate)
370    {
371      throw std::invalid_argument("Index out of bounds");
372    }
373    operation.Continue(intermediate, destination_object);
374  }
375};
376
377class tForEach : public tRegisteredConversionOperation
378{
379public:
380  tForEach() : tRegisteredConversionOperation(util::tManagedConstCharPointer("For Each", false), tSupportedTypeFilter::FOR_EACH, tSupportedTypeFilter::FOR_EACH)
381  {}
382
383  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override
384  {
385    if ((source_type.IsListType() || source_type.IsArray()) && destination_type)
386    {
387      return tConversionOption(source_type, destination_type, false, &FirstConversionFunction, &FinalConversionFunction);
388    }
389    return tConversionOption();
390  }
391
392  static void FirstConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation)
393  {
394    const tType& source_type = source_object.GetType();
395    const tType& destination_type = source_object.GetType();
396    size_t size = source_type.IsArray() ? source_type.GetArraySize() : source_object.GetVectorSize();
397    if (source_type.IsListType() && destination_type.IsListType())
398    {
399      destination_object.ResizeVector(size);
400      if (size)
401      {
402        tTypedConstPointer source_first = source_object.GetVectorElement(0);
403        tTypedPointer destination_first = destination_object.GetVectorElement(0);
404        operation.Continue(source_first, destination_object);
405        if (size > 1)
406        {
407          tTypedConstPointer source_next = source_object.GetVectorElement(1);
408          tTypedPointer destination_next = destination_object.GetVectorElement(1);
409          operation.Continue(source_next, destination_next);
410          size_t offset_source = static_cast<const char*>(source_next.GetRawDataPointer()) - static_cast<const char*>(source_first.GetRawDataPointer());
411          size_t offset_destination = static_cast<const char*>(destination_next.GetRawDataPointer()) - static_cast<const char*>(destination_first.GetRawDataPointer());
412          for (size_t i = 2; i < size; i++)
413          {
414            source_next = tTypedConstPointer(static_cast<const char*>(source_next.GetRawDataPointer()) + offset_source, source_type);
415            destination_next = tTypedPointer(static_cast<char*>(destination_next.GetRawDataPointer()) + offset_destination, destination_type);
416            operation.Continue(source_next, destination_next);
417          }
418        }
419      }
420    }
421    else if (source_type.IsArray() && destination_type.IsArray())
422    {
423      if (size != destination_type.GetArraySize())
424      {
425        throw std::runtime_error("Arrays must have the same size");
426      }
427
428      size_t source_element_offset = source_type.GetSize() / size;
429      size_t destination_element_offset = destination_type.GetSize() / size;
430      for (size_t i = 0; i < size; i++)
431      {
432        tTypedConstPointer source(static_cast<const char*>(source_object.GetRawDataPointer()) + i * source_element_offset, source_type);
433        tTypedPointer destination(static_cast<char*>(destination_object.GetRawDataPointer()) + i * destination_element_offset, destination_type);
434        operation.Continue(source, destination);
435      }
436    }
437    else if (source_type.IsArray() && destination_type.IsListType())
438    {
439      size_t size = source_type.GetArraySize();
440      destination_object.ResizeVector(size);
441      if (size)
442      {
443        size_t source_element_offset = source_type.GetSize() / size;
444        tTypedConstPointer source(static_cast<const char*>(source_object.GetRawDataPointer()), source_type);
445        tTypedPointer destination_first = destination_object.GetVectorElement(0);
446        operation.Continue(source, destination_object);
447        if (size > 1)
448        {
449          tTypedConstPointer source(static_cast<const char*>(source_object.GetRawDataPointer()) + source_element_offset, source_type);
450          tTypedPointer destination_next = destination_object.GetVectorElement(1);
451          operation.Continue(source, destination_next);
452          size_t offset_destination = static_cast<const char*>(destination_next.GetRawDataPointer()) - static_cast<const char*>(destination_first.GetRawDataPointer());
453          for (size_t i = 2; i < size; i++)
454          {
455            tTypedConstPointer source(static_cast<const char*>(source_object.GetRawDataPointer()) + i * source_element_offset, source_type);
456            destination_next = tTypedPointer(static_cast<char*>(destination_next.GetRawDataPointer()) + offset_destination, destination_type);
457            operation.Continue(source, destination_next);
458          }
459        }
460      }
461    }
462    else
463    {
464      throw std::runtime_error("Unsupported types for 'For Each' Operation");
465    }
466  }
467
468  static void FinalConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation)
469  {
470    throw std::logic_error("Not supported as single or second operation");
471  }
472};
473
474class tArrayToVector : public tRegisteredConversionOperation
475{
476public:
477  tArrayToVector() : tRegisteredConversionOperation(util::tManagedConstCharPointer("ToVector", false), tSupportedTypeFilter::ARRAY_TO_VECTOR, tSupportedTypeFilter::ARRAY_TO_VECTOR)
478  {}
479
480  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override
481  {
482    if (source_type.IsArray() && destination_type.IsListType() && source_type.GetElementType() == destination_type.GetElementType())
483    {
484      return tConversionOption(source_type, destination_type, false, &FirstConversionFunction, &FinalConversionFunction);
485    }
486    return tConversionOption();
487  }
488
489  static void FirstConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation)
490  {
491    tType inter_type = operation.compiled_operation.IntermediateType();
492    char intermediate_memory[inter_type.GetSize(true)];
493    auto intermediate_object = inter_type.EmplaceGenericObject(intermediate_memory);
494    FinalConversionFunction(source_object, *intermediate_object, operation);
495    operation.Continue(*intermediate_object, destination_object);
496  }
497
498  static void FinalConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation)
499  {
500    const tType& source_type = source_object.GetType();
501    size_t size = source_type.GetArraySize();
502    destination_object.ResizeVector(size);
503    if (size)
504    {
505      size_t source_element_offset = source_type.GetSize() / size;
506      tTypedConstPointer source(static_cast<const char*>(source_object.GetRawDataPointer()), source_type);
507      tTypedPointer destination_first = destination_object.GetVectorElement(0);
508      destination_first.DeepCopyFrom(source);
509      if (size > 1)
510      {
511        tTypedConstPointer source(static_cast<const char*>(source_object.GetRawDataPointer()) + source_element_offset, source_type);
512        tTypedPointer destination_next = destination_object.GetVectorElement(1);
513        destination_next.DeepCopyFrom(source);
514        size_t offset_destination = static_cast<const char*>(destination_next.GetRawDataPointer()) - static_cast<const char*>(destination_first.GetRawDataPointer());
515        for (size_t i = 2; i < size; i++)
516        {
517          tTypedConstPointer source(static_cast<const char*>(source_object.GetRawDataPointer()) + i * source_element_offset, source_type);
518          destination_next = tTypedPointer(static_cast<char*>(destination_next.GetRawDataPointer()) + offset_destination, destination_next.GetType());
519          destination_next.DeepCopyFrom(source);
520        }
521      }
522    }
523  }
524};
525
526class tGetTupleElement : public tRegisteredConversionOperation
527{
528public:
529  tGetTupleElement() : tRegisteredConversionOperation(util::tManagedConstCharPointer("get", false), tSupportedTypeFilter::GET_TUPLE_ELEMENT, tSupportedTypeFilter::GET_TUPLE_ELEMENT, nullptr, tParameterDefinition("Index", tDataType<unsigned int>(), true))
530  {}
531
532  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override
533  {
534    unsigned int index = parameter ? parameter->GetData<unsigned int>() : 0;
535    auto tuple_types = source_type.GetTupleTypes();
536    if (index < tuple_types.second && destination_type == tType(tuple_types.first[index].type_info))
537    {
538      return tConversionOption(source_type, destination_type, tuple_types.first[index].offset);
539    }
540    return tConversionOption();
541  }
542};
543
544class tWrapByteVectorOperation : public tRegisteredConversionOperation
545{
546public:
547  tWrapByteVectorOperation() : tRegisteredConversionOperation(util::tManagedConstCharPointer("Wrap", false), tDataType<std::vector<uint8_t>>(), tDataType<rrlib::serialization::tMemoryBuffer>(), &cCONVERSION_OPTION)
548  {}
549
550  static void FirstConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation)
551  {
552    const std::vector<uint8_t>& vector = *source_object.Get<std::vector<uint8_t>>();
553    if (vector.size())
554    {
555      const rrlib::serialization::tMemoryBuffer buffer(const_cast<uint8_t*>(&vector[0]), vector.size());
556      operation.Continue(tTypedConstPointer(&buffer), destination_object);
557    }
558    else
559    {
560      const rrlib::serialization::tMemoryBuffer buffer(0);
561      operation.Continue(tTypedConstPointer(&buffer), destination_object);
562    }
563  }
564
565  static void FinalConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation)
566  {
567    rrlib::serialization::tMemoryBuffer& buffer = *destination_object.Get<rrlib::serialization::tMemoryBuffer>();
568    const std::vector<uint8_t>& vector = *source_object.Get<std::vector<uint8_t>>();
569    const rrlib::serialization::tMemoryBuffer temp_buffer(const_cast<uint8_t*>(&vector[0]), vector.size());
570    buffer.CopyFrom(temp_buffer);
571  }
572
573  static constexpr tConversionOption cCONVERSION_OPTION = tConversionOption(tDataType<std::vector<uint8_t>>(), tDataType<rrlib::serialization::tMemoryBuffer>(), false, &FirstConversionFunction, &FinalConversionFunction);
574};
575
576constexpr tConversionOption tWrapByteVectorOperation::cCONVERSION_OPTION;
577
578class tListSize : public tRegisteredConversionOperation
579{
580public:
581  tListSize() : tRegisteredConversionOperation(util::tManagedConstCharPointer("size()", false), tSupportedTypeFilter::LISTS, tDataType<size_t>())
582  {}
583
584  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override
585  {
586    if (source_type.IsListType() && destination_type == tDataType<size_t>())
587    {
588      return tConversionOption(source_type, destination_type, false, &FirstConversionFunction, &FinalConversionFunction);
589    }
590    return tConversionOption();
591  }
592
593  static void FirstConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation)
594  {
595    size_t size = source_object.GetVectorSize();
596    operation.Continue(tTypedConstPointer(&size), destination_object);
597  }
598
599  static void FinalConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation)
600  {
601    (*destination_object.Get<size_t>()) = source_object.GetVectorSize();
602  }
603};
604
605void StringToVectorConversionFunction(const std::string& source, std::vector<char>& destination)
606{
607  destination = std::vector<char>(source.begin(), source.end());
608}
609
610void VectorToStringConversionFunction(const std::vector<char>& source, std::string& destination)
611{
612  destination = std::string(source.begin(), source.end());
613}
614
615
616const tToStringOperation cTO_STRING;
617const tStringDeserializationOperation cSTRING_DESERIALIZATION;
618const tBinarySerializationOperation cBINARY_SERIALIZATION;
619const tBinaryDeserializationOperation cBINARY_DESERIALIZATION;
620
621const tGetListElement cGET_LIST_ELEMENT;
622const tForEach cFOR_EACH;
623const tArrayToVector cARRAY_TO_VECTOR;
624const tGetTupleElement cGET_TUPLE_ELEMENT;
625
626const tWrapByteVectorOperation cWRAP_BYTE_VECTOR;
627const tListSize cLIST_SIZE;
628const tVoidFunctionConversionOperation<std::string, std::vector<char>, decltype(&StringToVectorConversionFunction), &StringToVectorConversionFunction> cSTRING_TO_VECTOR("ToVector");
629const tVoidFunctionConversionOperation<std::vector<char>, std::string, decltype(&VectorToStringConversionFunction), &VectorToStringConversionFunction> cVECTOR_TO_STRING("MakeString");
630
631}
632
633const tRegisteredConversionOperation& cTO_STRING_OPERATION = cTO_STRING;
634const tRegisteredConversionOperation& cSTRING_DESERIALIZATION_OPERATION = cSTRING_DESERIALIZATION;
635const tRegisteredConversionOperation& cBINARY_SERIALIZATION_OPERATION = cBINARY_SERIALIZATION;
636const tRegisteredConversionOperation& cBINARY_DESERIALIZATION_OPERATION = cBINARY_DESERIALIZATION;
637
638const tRegisteredConversionOperation& cGET_LIST_ELEMENT_OPERATION = cGET_LIST_ELEMENT;
639const tRegisteredConversionOperation& cFOR_EACH_OPERATION = cFOR_EACH;
640const tRegisteredConversionOperation& cARRAY_TO_VECTOR_OPERATION = cARRAY_TO_VECTOR;
641const tRegisteredConversionOperation& cGET_TUPLE_ELEMENT_OPERATION = cGET_TUPLE_ELEMENT;
642
643const tRegisteredConversionOperation& cWRAP_BYTE_VECTOR_OPERATION = cWRAP_BYTE_VECTOR;
644const tRegisteredConversionOperation& cLIST_SIZE_OPERATION = cLIST_SIZE;
645const tRegisteredConversionOperation& cSTRING_TO_VECTOR_OPERATION = cSTRING_TO_VECTOR;
646const tRegisteredConversionOperation& cMAKE_STRING_OPERATION = cVECTOR_TO_STRING;
647
648
649
650//----------------------------------------------------------------------
651// End of namespace declaration
652//----------------------------------------------------------------------
653}
654}
655}
Note: See TracBrowser for help on using the repository browser.