Changeset 9:f909044a9f7b in rrlib_rtti_conversion for defined_conversions.cpp


Ignore:
Timestamp:
22.09.2017 01:26:15 (2 years ago)
Author:
Max Reichardt <mreichardt@…>
Branch:
17.03
Phase:
public
Message:

Adds option to specify type conversions operations that are not usually combined (you could call combinations deprecated) and fixes various bugs found with unit test in finroc_plugins_runtime_construction

File:
1 edited

Legend:

Unmodified
Added
Removed
  • defined_conversions.cpp

    r8 r9  
    144144{ 
    145145 
     146const unsigned int cSTRING_OPERATION_DEFAULT_FLAGS = 0; 
     147 
    146148class tToStringOperation : public tRegisteredConversionOperation 
    147149{ 
    148150public: 
    149   tToStringOperation() : tRegisteredConversionOperation(util::tManagedConstCharPointer("ToString", false), tSupportedTypeFilter::STRING_SERIALIZABLE, tDataType<std::string>(), nullptr, tParameterDefinition("Flags", tDataType<unsigned int>(), true)) 
    150   {} 
     151  tToStringOperation() : tRegisteredConversionOperation(util::tManagedConstCharPointer("ToString", false), tSupportedTypeFilter::STRING_SERIALIZABLE, tDataType<std::string>(), nullptr, tParameterDefinition("Flags", &cSTRING_OPERATION_DEFAULT_FLAGS, true)) 
     152  { 
     153  } 
    151154 
    152155  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override 
     
    233236    MainConversionFunction(source_object, *destination_object.Get<std::string>(), operation); 
    234237  } 
    235 }; 
     238} cTO_STRING; 
    236239 
    237240class tStringDeserializationOperation : public tRegisteredConversionOperation 
    238241{ 
    239242public: 
    240   tStringDeserializationOperation() : tRegisteredConversionOperation(util::tManagedConstCharPointer("String Deserialization", false), tDataType<std::string>(), tSupportedTypeFilter::STRING_SERIALIZABLE) 
     243  tStringDeserializationOperation() : tRegisteredConversionOperation(util::tManagedConstCharPointer("String Deserialization", false), tDataType<std::string>(), tSupportedTypeFilter::STRING_SERIALIZABLE, nullptr, tParameterDefinition(), &cTO_STRING) 
    241244  {} 
    242245 
     
    265268    destination_object.Deserialize(stream); 
    266269  } 
    267 }; 
     270} cSTRING_DESERIALIZATION; 
    268271 
    269272class tBinarySerializationOperation : public tRegisteredConversionOperation 
     
    296299    destination_object.Serialize(stream); 
    297300  } 
    298 }; 
     301} cBINARY_SERIALIZATION; 
    299302 
    300303class tBinaryDeserializationOperation : public tRegisteredConversionOperation 
    301304{ 
    302305public: 
    303   tBinaryDeserializationOperation() : tRegisteredConversionOperation(util::tManagedConstCharPointer("Binary Deserialization", false), tDataType<serialization::tMemoryBuffer>(), tSupportedTypeFilter::STRING_SERIALIZABLE) 
     306  tBinaryDeserializationOperation() : tRegisteredConversionOperation(util::tManagedConstCharPointer("Binary Deserialization", false), tDataType<serialization::tMemoryBuffer>(), tSupportedTypeFilter::BINARY_SERIALIZABLE, nullptr, tParameterDefinition(), &cBINARY_SERIALIZATION) 
    304307  {} 
    305308 
     
    328331    destination_object.Deserialize(stream); 
    329332  } 
    330 }; 
     333} cBINARY_DESERIALIZATION; 
    331334 
    332335class tGetListElement : public tRegisteredConversionOperation 
     
    342345      return tConversionOption(source_type, destination_type, &FirstConversionFunction, &GetDestinationReference); 
    343346    } 
    344     unsigned int index = parameter ? parameter->GetData<unsigned int>() : 0; 
     347    unsigned int index = 0; 
     348    if (parameter && parameter->GetType() == tDataType<std::string>()) 
     349    { 
     350      index = serialization::Deserialize<unsigned int>(parameter->GetData<std::string>()); 
     351    } 
     352    else if (parameter) 
     353    { 
     354      assert(parameter->GetType() == tDataType<unsigned int>()); 
     355      index = parameter->GetData<unsigned int>(); 
     356    } 
    345357    if (source_type.IsArray() && source_type.GetElementType() == destination_type && index < source_type.GetArraySize()) 
    346358    { 
     
    373385    operation.Continue(intermediate, destination_object); 
    374386  } 
    375 }; 
     387} cGET_LIST_ELEMENT; 
    376388 
    377389class tForEach : public tRegisteredConversionOperation 
     
    393405  { 
    394406    const tType& source_type = source_object.GetType(); 
    395     const tType& destination_type = source_object.GetType(); 
     407    const tType& destination_type = destination_object.GetType(); 
     408    const tType source_element_type = source_type.GetElementType(); 
     409    const tType destination_element_type = destination_type.GetElementType(); 
    396410    size_t size = source_type.IsArray() ? source_type.GetArraySize() : source_object.GetVectorSize(); 
    397411    if (source_type.IsListType() && destination_type.IsListType()) 
     
    402416        tTypedConstPointer source_first = source_object.GetVectorElement(0); 
    403417        tTypedPointer destination_first = destination_object.GetVectorElement(0); 
    404         operation.Continue(source_first, destination_object); 
     418        operation.Continue(source_first, destination_first); 
    405419        if (size > 1) 
    406420        { 
     
    412426          for (size_t i = 2; i < size; i++) 
    413427          { 
    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); 
     428            source_next = tTypedConstPointer(static_cast<const char*>(source_next.GetRawDataPointer()) + offset_source, source_element_type); 
     429            destination_next = tTypedPointer(static_cast<char*>(destination_next.GetRawDataPointer()) + offset_destination, destination_element_type); 
    416430            operation.Continue(source_next, destination_next); 
    417431          } 
     
    430444      for (size_t i = 0; i < size; i++) 
    431445      { 
    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); 
     446        tTypedConstPointer source(static_cast<const char*>(source_object.GetRawDataPointer()) + i * source_element_offset, source_element_type); 
     447        tTypedPointer destination(static_cast<char*>(destination_object.GetRawDataPointer()) + i * destination_element_offset, destination_element_type); 
    434448        operation.Continue(source, destination); 
    435449      } 
     
    442456      { 
    443457        size_t source_element_offset = source_type.GetSize() / size; 
    444         tTypedConstPointer source(static_cast<const char*>(source_object.GetRawDataPointer()), source_type); 
     458        tTypedConstPointer source(static_cast<const char*>(source_object.GetRawDataPointer()), source_element_type); 
    445459        tTypedPointer destination_first = destination_object.GetVectorElement(0); 
    446         operation.Continue(source, destination_object); 
     460        operation.Continue(source, destination_first); 
    447461        if (size > 1) 
    448462        { 
    449           tTypedConstPointer source(static_cast<const char*>(source_object.GetRawDataPointer()) + source_element_offset, source_type); 
     463          tTypedConstPointer source(static_cast<const char*>(source_object.GetRawDataPointer()) + source_element_offset, source_element_type); 
    450464          tTypedPointer destination_next = destination_object.GetVectorElement(1); 
    451465          operation.Continue(source, destination_next); 
     
    453467          for (size_t i = 2; i < size; i++) 
    454468          { 
    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); 
     469            tTypedConstPointer source(static_cast<const char*>(source_object.GetRawDataPointer()) + i * source_element_offset, source_element_type); 
     470            destination_next = tTypedPointer(static_cast<char*>(destination_next.GetRawDataPointer()) + offset_destination, destination_element_type); 
    457471            operation.Continue(source, destination_next); 
    458472          } 
     
    470484    throw std::logic_error("Not supported as single or second operation"); 
    471485  } 
    472 }; 
     486} cFOR_EACH; 
    473487 
    474488class tArrayToVector : public tRegisteredConversionOperation 
     
    499513  { 
    500514    const tType& source_type = source_object.GetType(); 
     515    const tType source_element_type = source_type.GetElementType(); 
    501516    size_t size = source_type.GetArraySize(); 
    502517    destination_object.ResizeVector(size); 
     
    504519    { 
    505520      size_t source_element_offset = source_type.GetSize() / size; 
    506       tTypedConstPointer source(static_cast<const char*>(source_object.GetRawDataPointer()), source_type); 
     521      tTypedConstPointer source(static_cast<const char*>(source_object.GetRawDataPointer()), source_element_type); 
    507522      tTypedPointer destination_first = destination_object.GetVectorElement(0); 
    508523      destination_first.DeepCopyFrom(source); 
    509524      if (size > 1) 
    510525      { 
    511         tTypedConstPointer source(static_cast<const char*>(source_object.GetRawDataPointer()) + source_element_offset, source_type); 
     526        tTypedConstPointer source(static_cast<const char*>(source_object.GetRawDataPointer()) + source_element_offset, source_element_type); 
    512527        tTypedPointer destination_next = destination_object.GetVectorElement(1); 
    513528        destination_next.DeepCopyFrom(source); 
     
    515530        for (size_t i = 2; i < size; i++) 
    516531        { 
    517           tTypedConstPointer source(static_cast<const char*>(source_object.GetRawDataPointer()) + i * source_element_offset, source_type); 
     532          tTypedConstPointer source(static_cast<const char*>(source_object.GetRawDataPointer()) + i * source_element_offset, source_element_type); 
    518533          destination_next = tTypedPointer(static_cast<char*>(destination_next.GetRawDataPointer()) + offset_destination, destination_next.GetType()); 
    519534          destination_next.DeepCopyFrom(source); 
     
    522537    } 
    523538  } 
    524 }; 
     539} cARRAY_TO_VECTOR; 
    525540 
    526541class tGetTupleElement : public tRegisteredConversionOperation 
     
    532547  virtual tConversionOption GetConversionOption(const tType& source_type, const tType& destination_type, const tGenericObject* parameter) const override 
    533548  { 
    534     unsigned int index = parameter ? parameter->GetData<unsigned int>() : 0; 
     549    unsigned int index = 0; 
     550    if (parameter && parameter->GetType() == tDataType<std::string>()) 
     551    { 
     552      index = serialization::Deserialize<unsigned int>(parameter->GetData<std::string>()); 
     553    } 
     554    else if (parameter) 
     555    { 
     556      assert(parameter->GetType() == tDataType<unsigned int>()); 
     557      index = parameter->GetData<unsigned int>(); 
     558    } 
    535559    auto tuple_types = source_type.GetTupleTypes(); 
    536560    if (index < tuple_types.second && destination_type == tType(tuple_types.first[index].type_info)) 
     
    540564    return tConversionOption(); 
    541565  } 
    542 }; 
     566} cGET_TUPLE_ELEMENT; 
    543567 
    544568class tWrapByteVectorOperation : public tRegisteredConversionOperation 
     
    567591    rrlib::serialization::tMemoryBuffer& buffer = *destination_object.Get<rrlib::serialization::tMemoryBuffer>(); 
    568592    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 }; 
     593    buffer = rrlib::serialization::tMemoryBuffer(const_cast<uint8_t*>(&vector[0]), vector.size()); 
     594    //const rrlib::serialization::tMemoryBuffer temp_buffer(const_cast<uint8_t*>(&vector[0]), vector.size()); 
     595    //buffer.CopyFrom(temp_buffer); 
     596  } 
     597 
     598  static constexpr tConversionOption cCONVERSION_OPTION = tConversionOption(tDataType<std::vector<uint8_t>>(), tDataType<rrlib::serialization::tMemoryBuffer>(), true, &FirstConversionFunction, &FinalConversionFunction); 
     599} cWRAP_BYTE_VECTOR; 
    575600 
    576601constexpr tConversionOption tWrapByteVectorOperation::cCONVERSION_OPTION; 
     
    601626    (*destination_object.Get<size_t>()) = source_object.GetVectorSize(); 
    602627  } 
    603 }; 
     628} cLIST_SIZE; 
    604629 
    605630void StringToVectorConversionFunction(const std::string& source, std::vector<char>& destination) 
     
    613638} 
    614639 
    615  
    616 const tToStringOperation cTO_STRING; 
    617 const tStringDeserializationOperation cSTRING_DESERIALIZATION; 
    618 const tBinarySerializationOperation cBINARY_SERIALIZATION; 
    619 const tBinaryDeserializationOperation cBINARY_DESERIALIZATION; 
    620  
    621 const tGetListElement cGET_LIST_ELEMENT; 
    622 const tForEach cFOR_EACH; 
    623 const tArrayToVector cARRAY_TO_VECTOR; 
    624 const tGetTupleElement cGET_TUPLE_ELEMENT; 
    625  
    626 const tWrapByteVectorOperation cWRAP_BYTE_VECTOR; 
    627 const tListSize cLIST_SIZE; 
    628640const tVoidFunctionConversionOperation<std::string, std::vector<char>, decltype(&StringToVectorConversionFunction), &StringToVectorConversionFunction> cSTRING_TO_VECTOR("ToVector"); 
    629641const tVoidFunctionConversionOperation<std::vector<char>, std::string, decltype(&VectorToStringConversionFunction), &VectorToStringConversionFunction> cVECTOR_TO_STRING("MakeString"); 
Note: See TracChangeset for help on using the changeset viewer.