source: rrlib_rtti_conversion/tRegisteredConversionOperation.cpp @ 7:4cc15587a7e6

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

Adds support for std::array types - tohether with additional type conversion operations

File size: 9.6 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/tRegisteredConversionOperation.cpp
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2016-07-15
27 *
28 */
29//----------------------------------------------------------------------
30#include "rrlib/rtti_conversion/tRegisteredConversionOperation.h"
31
32//----------------------------------------------------------------------
33// External includes (system with <>, local with "")
34//----------------------------------------------------------------------
35#include "rrlib/thread/tLock.h"
36
37//----------------------------------------------------------------------
38// Internal includes with ""
39//----------------------------------------------------------------------
40#include "rrlib/rtti_conversion/tConversionOption.h"
41#include "rrlib/rtti_conversion/tStaticCastOperation.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//----------------------------------------------------------------------
69const char* tRegisteredConversionOperation::cSTATIC_CAST_NAME = "static_cast";
70
71//----------------------------------------------------------------------
72// Implementation
73//----------------------------------------------------------------------
74
75tRegisteredConversionOperation::tRegisteredConversionOperation(util::tManagedConstCharPointer name, const tSupportedTypes& supported_source_types, const tSupportedTypes& supported_destination_types,
76    const tConversionOption* single_conversion_option, const tParameterDefinition& parameter) :
77  name(std::move(name)),
78  supported_source_types(supported_source_types),
79  supported_destination_types(supported_destination_types),
80  parameter(parameter),
81  single_conversion_option(single_conversion_option)
82{
83  if (parameter && (!(parameter.GetType().GetTypeTraits() & trait_flags::cIS_STRING_SERIALIZABLE)))
84  {
85    throw std::runtime_error(std::string("Conversion operation: '") + this->name.Get() + "'. Parameters have to be string serializable.");
86  }
87  handle = static_cast<decltype(handle)>(tRegisteredConversionOperation::RegisteredOperations().operations.Add(this));
88}
89
90tRegisteredConversionOperation::tRegisteredConversionOperation() :
91  name(cSTATIC_CAST_NAME, false),
92  supported_source_types(tSupportedTypeFilter::STATIC_CAST),
93  supported_destination_types(tSupportedTypeFilter::STATIC_CAST),
94  parameter(),
95  single_conversion_option(nullptr),
96  handle(-1)
97{
98  handle = static_cast<decltype(handle)>(tRegisteredConversionOperation::RegisteredOperations().operations.Add(this));
99}
100
101tRegisteredConversionOperation::~tRegisteredConversionOperation()
102{
103}
104
105void tRegisteredConversionOperation::AutoDelete()
106{
107  tRegisteredConversionOperation::tRegisteredOperations& registered_operations = tRegisteredConversionOperation::RegisteredOperations();
108  registered_operations.auto_delete.Emplace(this);
109}
110
111const tRegisteredConversionOperation* tRegisteredConversionOperation::Deserialize(rrlib::serialization::tInputStream& stream, bool throw_exception_if_not_found)
112{
113  const tRegisteredConversionOperation* result = nullptr;
114  if (tRegisteredConversionOperation::GetRegisteredOperations().operations.ReadEntry(stream, result))
115  {
116    std::string name = stream.ReadString();
117    tRegisteredConversionOperation::tSupportedTypes source_types = tType();
118    tRegisteredConversionOperation::tSupportedTypes destination_types = tType();
119    stream >> source_types.filter;
120    if (source_types.filter == tSupportedTypeFilter::SINGLE)
121    {
122      source_types.single_type = tType::GetType(stream.ReadShort());
123    }
124    stream >> destination_types.filter;
125    if (destination_types.filter == tSupportedTypeFilter::SINGLE)
126    {
127      destination_types.single_type = tType::GetType(stream.ReadShort());
128    }
129
130    // Find matching conversion operation
131    if (name == cSTATIC_CAST_NAME)
132    {
133      return &tStaticCastOperation::GetInstance();
134    }
135
136    tRegisteredConversionOperation::tRegisteredOperations& registered_operations = tRegisteredConversionOperation::RegisteredOperations();
137    for (auto & operation : registered_operations.operations)
138    {
139      if (operation->supported_source_types.filter == source_types.filter && operation->supported_source_types.single_type == source_types.single_type && operation->supported_destination_types.filter == destination_types.filter && operation->supported_destination_types.single_type == destination_types.single_type && name == operation->Name())
140      {
141        return operation;
142      }
143    }
144    if (throw_exception_if_not_found)
145    {
146      throw std::runtime_error("Encoded registered conversion operation not found");
147    }
148  }
149  return result;
150}
151
152std::pair<const tRegisteredConversionOperation*, bool> tRegisteredConversionOperation::Find(const std::string& name)
153{
154  std::pair<const tRegisteredConversionOperation*, bool> result(nullptr, false);
155  if (name == cSTATIC_CAST_NAME)
156  {
157    result.first = &tStaticCastOperation::GetInstance();
158    return result;
159  }
160
161  tRegisteredConversionOperation::tRegisteredOperations& registered_operations = tRegisteredConversionOperation::RegisteredOperations();
162  for (auto & operation : registered_operations.operations)
163  {
164    if (name == operation->Name())
165    {
166      if (!result.first)
167      {
168        result.first = operation;
169      }
170      else
171      {
172        result.second = true;
173        return result;
174      }
175    }
176  }
177  return result;
178}
179
180const tRegisteredConversionOperation& tRegisteredConversionOperation::Find(const std::string& name, const tType& source_type, const tType& destination_type)
181{
182  const tRegisteredConversionOperation* result = nullptr;
183  if (name == cSTATIC_CAST_NAME)
184  {
185    return tStaticCastOperation::GetInstance();
186  }
187
188  tRegisteredConversionOperation::tRegisteredOperations& registered_operations = tRegisteredConversionOperation::RegisteredOperations();
189  for (auto & operation : registered_operations.operations)
190  {
191    if (name == operation->Name())
192    {
193      auto option = operation->GetConversionOption(source_type, destination_type, nullptr);
194      if (option.type != tConversionOptionType::NONE)
195      {
196        if (result)
197        {
198          throw std::runtime_error("Lookup of registered conversion operation " + name + " is ambiguous");
199        }
200        result = operation;
201      }
202    }
203  }
204  if (!result)
205  {
206    throw std::runtime_error("Lookup of registered conversion operation " + name + " with specified types failed");
207  }
208
209  return *result;
210}
211
212tConversionOption tRegisteredConversionOperation::GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const
213{
214  if ((!source_type) || (!destination_type))
215  {
216    throw std::invalid_argument("Source type and destination type must be specified");
217  }
218  if (!single_conversion_option)
219  {
220    throw std::logic_error("Method must be overridden if no single conversion option is specified");
221  }
222  if (single_conversion_option->source_type == source_type && single_conversion_option->destination_type == destination_type)
223  {
224    return *single_conversion_option;
225  }
226  return tConversionOption();
227}
228
229tRegisteredConversionOperation::tRegisteredOperations& tRegisteredConversionOperation::RegisteredOperations()
230{
231  static tRegisteredOperations operations;
232  return operations;
233}
234
235serialization::tOutputStream& operator << (serialization::tOutputStream& stream, const tRegisteredConversionOperation& operation)
236{
237  if (tRegisteredConversionOperation::GetRegisteredOperations().operations.WriteEntry(stream, operation.GetHandle()))
238  {
239    stream << operation.Name() << operation.SupportedSourceTypes().filter;
240    if (operation.SupportedSourceTypes().filter == tSupportedTypeFilter::SINGLE)
241    {
242      stream << operation.SupportedSourceTypes().single_type;
243    }
244    stream << operation.SupportedDestinationTypes().filter;
245    if (operation.SupportedDestinationTypes().filter == tSupportedTypeFilter::SINGLE)
246    {
247      stream << operation.SupportedDestinationTypes().single_type;
248    }
249  }
250
251  return stream;
252}
253
254//----------------------------------------------------------------------
255// End of namespace declaration
256//----------------------------------------------------------------------
257}
258}
259}
Note: See TracBrowser for help on using the repository browser.