00001 00026 #include "odva_ethernetip/serialization/copy_serializable.h" 00027 #include "odva_ethernetip/serialization/serializable_buffer.h" 00028 #include "odva_ethernetip/serialization/buffer_reader.h" 00029 #include "odva_ethernetip/serialization/buffer_writer.h" 00030 00031 namespace eip { 00032 namespace serialization { 00033 00034 void copy_serializable(Serializable& dst, const Serializable& src) 00035 { 00036 // check if this is SerializableBuffer to avoid unnecessary copy 00037 const SerializableBuffer* src_sb = dynamic_cast<const SerializableBuffer*>(&src); 00038 SerializableBuffer* dst_sb = dynamic_cast<SerializableBuffer*>(&dst); 00039 if (src_sb) 00040 { 00041 if (dst_sb) 00042 { 00043 (*dst_sb) = (*src_sb); 00044 } 00045 else 00046 { 00047 BufferReader reader(src_sb->getData()); 00048 dst.deserialize(reader, src_sb->getLength()); 00049 } 00050 return; 00051 } 00052 else if (dst_sb) 00053 { 00054 // cannot serialize to a SerializableBuffer, since it will attempt not to 00055 // copy the buffer but just return to it. 00056 // This should never be required in practice. Not implementing yet 00057 // TODO: fix if actually ever needed 00058 throw std::logic_error("Cannot get payload from Serializable to SerializableBuffer"); 00059 } 00060 else 00061 { 00062 // must serialize and then deserialize again 00063 size_t length = src.getLength(); 00064 std::vector<char> buf(length); 00065 BufferWriter writer(buffer(buf)); 00066 src.serialize(writer); 00067 BufferReader reader(buffer(buf)); 00068 dst.deserialize(reader, length); 00069 } 00070 } 00071 00072 } // namespace serialization 00073 } // namespace eip