Changeset 7:4cc15587a7e6 in rrlib_rtti_conversion


Ignore:
Timestamp:
08.09.2017 04:36:16 (22 months ago)
Author:
Max Reichardt <mreichardt@…>
Branch:
17.03
Phase:
public
Message:

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

Files:
7 edited

Legend:

Unmodified
Added
Removed
  • defined_conversions.cpp

    r0 r7  
    3939//---------------------------------------------------------------------- 
    4040#include "rrlib/rtti_conversion/tStaticCastOperation.h" 
     41#include "rrlib/rtti_conversion/definition/tVoidFunctionConversionOperation.h" 
    4142 
    4243//---------------------------------------------------------------------- 
     
    132133                            .Register<float, bool, true, true>() 
    133134 
    134                             .Register<double, bool, true, true>(); 
     135                            .Register<double, bool, true, true>() 
     136 
     137                            .Register<rrlib::serialization::tMemoryBuffer, std::vector<uint8_t>>(); 
    135138 
    136139//---------------------------------------------------------------------- 
     
    138141//---------------------------------------------------------------------- 
    139142 
     143namespace 
     144{ 
     145 
    140146class tToStringOperation : public tRegisteredConversionOperation 
    141147{ 
     
    144150  {} 
    145151 
    146   virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type) const override 
     152  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override 
    147153  { 
    148154    if ((source_type.GetTypeTraits() & trait_flags::cIS_STRING_SERIALIZABLE) && destination_type == tDataType<std::string>()) 
     
    235241  {} 
    236242 
    237   virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type) const override 
     243  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override 
    238244  { 
    239245    if ((destination_type.GetTypeTraits() & trait_flags::cIS_STRING_SERIALIZABLE) && source_type == tDataType<std::string>()) 
     
    267273  {} 
    268274 
    269   virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type) const override 
     275  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override 
    270276  { 
    271277    if ((source_type.GetTypeTraits() & trait_flags::cIS_BINARY_SERIALIZABLE) && destination_type == tDataType<serialization::tMemoryBuffer>()) 
     
    298304  {} 
    299305 
    300   virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type) const override 
     306  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override 
    301307  { 
    302308    if ((destination_type.GetTypeTraits() & trait_flags::cIS_BINARY_SERIALIZABLE) && source_type == tDataType<serialization::tMemoryBuffer>()) 
     
    330336  {} 
    331337 
    332   virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type) const override 
     338  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override 
    333339  { 
    334340    if (source_type.IsListType() && source_type.GetElementType() == destination_type) 
     
    370376  {} 
    371377 
    372   virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type) const override 
     378  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override 
    373379  { 
    374380    if ((source_type.GetTypeTraits() & trait_flags::cIS_LIST_TYPE) && destination_type) 
     
    411417}; 
    412418 
     419class tGetArrayElement : public tRegisteredConversionOperation 
     420{ 
     421public: 
     422  tGetArrayElement() : tRegisteredConversionOperation(util::tManagedConstCharPointer("[]", false), tSupportedTypeFilter::GET_ARRAY_ELEMENT, tSupportedTypeFilter::GET_ARRAY_ELEMENT, nullptr, tParameterDefinition("Index", tDataType<unsigned int>(), true)) 
     423  {} 
     424 
     425  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override 
     426  { 
     427    unsigned int index = parameter ? parameter->GetData<unsigned int>() : 0; 
     428    if (source_type.IsArray() && source_type.GetElementType() == destination_type && index < source_type.GetArraySize()) 
     429    { 
     430      return tConversionOption(source_type, destination_type, index * source_type.GetElementType().GetSize()); 
     431    } 
     432    return tConversionOption(); 
     433  } 
     434}; 
     435 
     436class tForEachArray : public tRegisteredConversionOperation 
     437{ 
     438public: 
     439  tForEachArray() : tRegisteredConversionOperation(util::tManagedConstCharPointer("For Each", false), tSupportedTypeFilter::GENERIC_ARRAY_CAST, tSupportedTypeFilter::GENERIC_ARRAY_CAST) 
     440  {} 
     441 
     442  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override 
     443  { 
     444    if ((source_type.GetTypeTraits() & trait_flags::cIS_ARRAY) && destination_type) 
     445    { 
     446      return tConversionOption(source_type, destination_type, false, &FirstConversionFunction, &FinalConversionFunction); 
     447    } 
     448    return tConversionOption(); 
     449  } 
     450 
     451  static void FirstConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation) 
     452  { 
     453    tType source_type = source_object.GetType(); 
     454    tType destination_type = destination_object.GetType(); 
     455    size_t size = source_type.GetArraySize(); 
     456    if (size != destination_type.GetArraySize()) 
     457    { 
     458      throw std::runtime_error("Arrays must have the same size"); 
     459    } 
     460 
     461    size_t source_element_offset = source_type.GetSize() / size; 
     462    size_t destination_element_offset = destination_type.GetSize() / size; 
     463    for (size_t i = 0; i < size; i++) 
     464    { 
     465      tTypedConstPointer source(static_cast<const char*>(source_object.GetRawDataPointer()) + i * source_element_offset, source_type); 
     466      tTypedPointer destination(static_cast<char*>(destination_object.GetRawDataPointer()) + i * destination_element_offset, destination_type); 
     467      operation.Continue(source, destination); 
     468    } 
     469  } 
     470 
     471  static void FinalConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation) 
     472  { 
     473    throw std::logic_error("Not supported as single or second operation"); 
     474  } 
     475}; 
     476 
     477class tWrapByteVectorOperation : public tRegisteredConversionOperation 
     478{ 
     479public: 
     480  tWrapByteVectorOperation() : tRegisteredConversionOperation(util::tManagedConstCharPointer("Wrap", false), tDataType<std::vector<uint8_t>>(), tDataType<rrlib::serialization::tMemoryBuffer>(), &cCONVERSION_OPTION) 
     481  {} 
     482 
     483  static void FirstConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation) 
     484  { 
     485    const std::vector<uint8_t>& vector = *source_object.Get<std::vector<uint8_t>>(); 
     486    if (vector.size()) 
     487    { 
     488      const rrlib::serialization::tMemoryBuffer buffer(const_cast<uint8_t*>(&vector[0]), vector.size()); 
     489      operation.Continue(tTypedConstPointer(&buffer), destination_object); 
     490    } 
     491    else 
     492    { 
     493      const rrlib::serialization::tMemoryBuffer buffer(0); 
     494      operation.Continue(tTypedConstPointer(&buffer), destination_object); 
     495    } 
     496  } 
     497 
     498  static void FinalConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation) 
     499  { 
     500    rrlib::serialization::tMemoryBuffer& buffer = *destination_object.Get<rrlib::serialization::tMemoryBuffer>(); 
     501    const std::vector<uint8_t>& vector = *source_object.Get<std::vector<uint8_t>>(); 
     502    const rrlib::serialization::tMemoryBuffer temp_buffer(const_cast<uint8_t*>(&vector[0]), vector.size()); 
     503    buffer.CopyFrom(temp_buffer); 
     504  } 
     505 
     506  static constexpr tConversionOption cCONVERSION_OPTION = tConversionOption(tDataType<std::vector<uint8_t>>(), tDataType<rrlib::serialization::tMemoryBuffer>(), false, &FirstConversionFunction, &FinalConversionFunction); 
     507}; 
     508 
     509constexpr tConversionOption tWrapByteVectorOperation::cCONVERSION_OPTION; 
     510 
     511class tListSize : public tRegisteredConversionOperation 
     512{ 
     513public: 
     514  tListSize() : tRegisteredConversionOperation(util::tManagedConstCharPointer("size()", false), tSupportedTypeFilter::LISTS, tDataType<size_t>()) 
     515  {} 
     516 
     517  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override 
     518  { 
     519    if (source_type.IsListType() && destination_type == tDataType<size_t>()) 
     520    { 
     521      return tConversionOption(source_type, destination_type, false, &FirstConversionFunction, &FinalConversionFunction); 
     522    } 
     523    return tConversionOption(); 
     524  } 
     525 
     526  static void FirstConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation) 
     527  { 
     528    size_t size = source_object.GetVectorSize(); 
     529    operation.Continue(tTypedConstPointer(&size), destination_object); 
     530  } 
     531 
     532  static void FinalConversionFunction(const tTypedConstPointer& source_object, const tTypedPointer& destination_object, const tCurrentConversionOperation& operation) 
     533  { 
     534    (*destination_object.Get<size_t>()) = source_object.GetVectorSize(); 
     535  } 
     536}; 
     537 
     538void StringToVectorConversionFunction(const std::string& source, std::vector<char>& destination) 
     539{ 
     540  destination = std::vector<char>(source.begin(), source.end()); 
     541} 
     542 
     543void VectorToStringConversionFunction(const std::vector<char>& source, std::string& destination) 
     544{ 
     545  destination = std::string(source.begin(), source.end()); 
     546} 
     547 
    413548 
    414549const tToStringOperation cTO_STRING; 
     550const tStringDeserializationOperation cSTRING_DESERIALIZATION; 
     551const tBinarySerializationOperation cBINARY_SERIALIZATION; 
     552const tBinaryDeserializationOperation cBINARY_DESERIALIZATION; 
     553const tGetListElement cGET_LIST_ELEMENT; 
     554const tForEach cFOR_EACH; 
     555const tGetArrayElement cGET_ARRAY_ELEMENT; 
     556const tForEachArray cFOR_EACH_ARRAY; 
     557const tWrapByteVectorOperation cWRAP_BYTE_VECTOR; 
     558const tListSize cLIST_SIZE; 
     559const tVoidFunctionConversionOperation<std::string, std::vector<char>, decltype(&StringToVectorConversionFunction), &StringToVectorConversionFunction> cSTRING_TO_VECTOR("ToVector"); 
     560const tVoidFunctionConversionOperation<std::vector<char>, std::string, decltype(&VectorToStringConversionFunction), &VectorToStringConversionFunction> cVECTOR_TO_STRING("MakeString"); 
     561 
     562} 
     563 
    415564const tRegisteredConversionOperation& cTO_STRING_OPERATION = cTO_STRING; 
    416 const tStringDeserializationOperation cSTRING_DESERIALIZATION; 
    417565const tRegisteredConversionOperation& cSTRING_DESERIALIZATION_OPERATION = cSTRING_DESERIALIZATION; 
    418 const tBinarySerializationOperation cBINARY_SERIALIZATION; 
    419566const tRegisteredConversionOperation& cBINARY_SERIALIZATION_OPERATION = cBINARY_SERIALIZATION; 
    420 const tBinaryDeserializationOperation cBINARY_DESERIALIZATION; 
    421567const tRegisteredConversionOperation& cBINARY_DESERIALIZATION_OPERATION = cBINARY_DESERIALIZATION; 
    422 const tGetListElement cGET_LIST_ELEMENT; 
    423568const tRegisteredConversionOperation& cGET_LIST_ELEMENT_OPERATION = cGET_LIST_ELEMENT; 
    424 const tForEach cFOR_EACH; 
    425569const tRegisteredConversionOperation& cFOR_EACH_OPERATION = cFOR_EACH; 
     570const tRegisteredConversionOperation& cGET_ARRAY_ELEMENT_OPERATION = cGET_ARRAY_ELEMENT; 
     571const tRegisteredConversionOperation& cFOR_EACH_OPERATION_ARRAY = cFOR_EACH_ARRAY; 
     572const tRegisteredConversionOperation& cWRAP_BYTE_VECTOR_OPERATION = cWRAP_BYTE_VECTOR; 
     573const tRegisteredConversionOperation& cLIST_SIZE_OPERATION = cLIST_SIZE; 
     574const tRegisteredConversionOperation& cSTRING_TO_VECTOR_OPERATION = cSTRING_TO_VECTOR; 
     575const tRegisteredConversionOperation& cMAKE_STRING_OPERATION = cVECTOR_TO_STRING; 
     576 
     577 
    426578 
    427579//---------------------------------------------------------------------- 
  • defined_conversions.h

    r0 r7  
    8383extern const tRegisteredConversionOperation& cFOR_EACH_OPERATION;               //!< Special conversion operation for std::vectors that applies second conversion operation on all elements 
    8484 
     85extern const tRegisteredConversionOperation& cGET_ARRAY_ELEMENT_OPERATION;      //!< Get Element with specified index (parameter) from array type (std::array) 
     86extern const tRegisteredConversionOperation& cFOR_EACH_OPERATION_ARRAY;         //!< Special conversion operation for std::arrays that applies second conversion operation on all elements 
     87extern const tRegisteredConversionOperation& cARRAY_TO_VECTOR_OPERATION;        //!< Converts std::array<T> to std::vector<T> 
     88 
     89extern const tRegisteredConversionOperation& cWRAP_BYTE_VECTOR_OPERATION;       //!< Converts std::vector<uint> to a MemoryBuffer. This is particulary efficient (zero-copy) when first operation in a sequence. 
     90extern const tRegisteredConversionOperation& cLIST_SIZE_OPERATION;              //!< Returns std::vector<T> size for any type T 
     91extern const tRegisteredConversionOperation& cSTRING_TO_VECTOR_OPERATION;       //!< Converts std::string to char vector 
     92extern const tRegisteredConversionOperation& cMAKE_STRING_OPERATION;            //!< Creates std::string from std::vector<char> 
     93 
    8594//---------------------------------------------------------------------- 
    8695// End of namespace declaration 
  • tConversionOperationSequence.cpp

    r6 r7  
    198198 
    199199  // For each operation 
    200   else if (first_operation == &cFOR_EACH_OPERATION) 
    201   { 
    202     if (!(type_source.IsListType() && type_destination.IsListType())) 
    203     { 
    204       throw std::runtime_error("ForEach operation only applicable on list types"); 
     200  else if (first_operation == &cFOR_EACH_OPERATION || first_operation == &cFOR_EACH_OPERATION_ARRAY) 
     201  { 
     202    if ((first_operation == &cFOR_EACH_OPERATION && (!(type_source.IsListType() && type_destination.IsListType()))) || (first_operation == &cFOR_EACH_OPERATION_ARRAY && (!(type_source.IsArray() && type_destination.IsArray() && type_source.GetArraySize() == destination_type.GetArraySize())))) 
     203    { 
     204      throw std::runtime_error("ForEach operation only applicable on list types and array types (of same size)"); 
    205205    } 
    206206    if (!second_operation) 
     
    214214    else 
    215215    { 
    216       temp_conversion_option_2 = second_operation->GetConversionOption(type_source.GetElementType(), type_destination.GetElementType()); 
     216      temp_conversion_option_2 = second_operation->GetConversionOption(type_source.GetElementType(), type_destination.GetElementType(), operations[1].parameter.get()); 
    217217      if (temp_conversion_option_2.type == tConversionOptionType::NONE) 
    218218      { 
     
    221221    } 
    222222    conversion2 = &temp_conversion_option_2; 
    223     temp_conversion_option_1 = cFOR_EACH_OPERATION.GetConversionOption(type_source, type_destination); 
     223    temp_conversion_option_1 = first_operation->GetConversionOption(type_source, type_destination, operations[0].parameter.get()); 
    224224    conversion1 = &temp_conversion_option_1; 
    225225  } 
     
    228228  else if (second_operation) 
    229229  { 
    230     temp_conversion_option_1 = first_operation->GetConversionOption(type_source, type_intermediate); 
    231     temp_conversion_option_2 = second_operation->GetConversionOption(type_intermediate, type_destination); 
     230    temp_conversion_option_1 = first_operation->GetConversionOption(type_source, type_intermediate, operations[0].parameter.get()); 
     231    temp_conversion_option_2 = second_operation->GetConversionOption(type_intermediate, type_destination, operations[1].parameter.get()); 
    232232    if (temp_conversion_option_1.type != tConversionOptionType::NONE && temp_conversion_option_2.type != tConversionOptionType::NONE) 
    233233    { 
     
    240240  else 
    241241  { 
    242     temp_conversion_option_1 = first_operation->GetConversionOption(type_source, type_destination); 
     242    temp_conversion_option_1 = first_operation->GetConversionOption(type_source, type_destination, operations[0].parameter.get()); 
    243243    if (temp_conversion_option_1.type != tConversionOptionType::NONE) 
    244244    { 
     
    251251      { 
    252252        type_intermediate = type_intermediate ? type_intermediate : first_operation->SupportedDestinationTypes().single_type; 
    253         temp_conversion_option_1 = first_operation->GetConversionOption(type_source, type_intermediate); 
     253        temp_conversion_option_1 = first_operation->GetConversionOption(type_source, type_intermediate, operations[0].parameter.get()); 
    254254        temp_conversion_option_2 = tStaticCastOperation::GetImplicitConversionOption(type_intermediate, type_destination); 
    255255      } 
     
    258258        type_intermediate = type_intermediate ? type_intermediate : first_operation->SupportedSourceTypes().single_type; 
    259259        temp_conversion_option_1 = tStaticCastOperation::GetImplicitConversionOption(type_source, type_intermediate); 
    260         temp_conversion_option_2 = first_operation->GetConversionOption(type_intermediate, type_destination); 
     260        temp_conversion_option_2 = first_operation->GetConversionOption(type_intermediate, type_destination, operations[1].parameter.get()); 
    261261      } 
    262262      if (temp_conversion_option_1.type != tConversionOptionType::NONE && temp_conversion_option_2.type != tConversionOptionType::NONE) 
  • tRegisteredConversionOperation.cpp

    r2 r7  
    191191    if (name == operation->Name()) 
    192192    { 
    193       auto option = operation->GetConversionOption(source_type, destination_type); 
     193      auto option = operation->GetConversionOption(source_type, destination_type, nullptr); 
    194194      if (option.type != tConversionOptionType::NONE) 
    195195      { 
     
    210210} 
    211211 
    212 tConversionOption tRegisteredConversionOperation::GetConversionOption(const tType& source_type, const tType& destination_type) const 
     212tConversionOption tRegisteredConversionOperation::GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const 
    213213{ 
    214214  if ((!source_type) || (!destination_type)) 
  • tRegisteredConversionOperation.h

    r2 r7  
    7979  BINARY_SERIALIZABLE, //!< All binary-serializable types are supported 
    8080  STRING_SERIALIZABLE, //!< All string-serializable types are supported 
     81  LISTS,               //!< All list types are supported 
    8182  ALL,                 //!< All types are supported 
    8283 
     
    8485  STATIC_CAST,         //!< Types supported by static casts (only used for tStaticCastOperation) 
    8586  GENERIC_VECTOR_CAST, //!< Types supported by generic vector cast 
    86   GET_LIST_ELEMENT     //!< Types supported by get list element 
     87  GENERIC_ARRAY_CAST,  //!< Types supported by generic array cast 
     88  GET_LIST_ELEMENT,    //!< Types supported by get list element 
     89  GET_ARRAY_ELEMENT,   //!< Types supported by get array element 
    8790}; 
    8891 
     
    205208   * \param source_type Source Type 
    206209   * \param destination_type Destination Type 
     210   * \param parameter Conversion parameter (nullptr means default) 
    207211   * \return Conversion option for the specified types (result's type is tConversionOptionType::NONE if no option for specified types can be provided) 
    208212   */ 
    209   virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type) const; 
     213  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const; 
    210214 
    211215  /*! 
  • tStaticCastOperation.cpp

    r0 r7  
    7878{} 
    7979 
    80 tConversionOption tStaticCastOperation::GetConversionOption(const tType& source_type, const tType& destination_type) const 
     80tConversionOption tStaticCastOperation::GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const 
    8181{ 
    8282  if (source_type == destination_type) 
  • tStaticCastOperation.h

    r0 r7  
    309309  static tConversionOption GetImplicitConversionOption(const rrlib::rtti::tType& source_type, const rrlib::rtti::tType& destination_type, const tRegisteredConversionOperation::tRegisteredOperations& registered_operations); 
    310310 
    311   virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type) const override; 
     311  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override; 
    312312}; 
    313313 
Note: See TracChangeset for help on using the changeset viewer.