00001 #include "TypelibMarshallerBase.hpp" 00002 #include "TypelibMarshallerHandle.hpp" 00003 00004 #include <rtt/types/TypeInfoRepository.hpp> 00005 00006 using namespace orogen_transports; 00007 00008 typedef TypelibMarshallerBase::Handle Handle; 00009 00010 TypelibMarshallerBase::TypelibMarshallerBase(bool plain, 00011 std::string const& typelib_typename, 00012 std::string const& orocos_typename, 00013 Typelib::Registry const& registry) 00014 : m_plain(plain) 00015 , type_def(registry.get(typelib_typename)) 00016 , m_typename_typelib(typelib_typename) 00017 , m_typename_orocos(orocos_typename) 00018 { 00019 if (!type_def) 00020 throw std::runtime_error(typelib_typename + " is not present in the typekit registry"); 00021 00022 layout = Typelib::layout_of(*type_def, false, false); 00023 } 00024 00025 bool TypelibMarshallerBase::isPlainTypelibType() const 00026 { return m_plain; } 00027 00028 Handle* TypelibMarshallerBase::createHandle() { return new Handle(this); } 00029 void TypelibMarshallerBase::deleteHandle(Handle* handle) { delete handle; } 00030 uint8_t* TypelibMarshallerBase::getTypelibSample(Handle* handle) 00031 { 00032 return handle->typelib_sample; 00033 } 00034 char const* TypelibMarshallerBase::getMarshallingType() const 00035 { return m_typename_typelib.c_str(); } 00036 size_t TypelibMarshallerBase::getMarshallingSize(Handle const* handle) const 00037 { return Typelib::getDumpSize(handle->typelib_sample, layout); } 00038 void TypelibMarshallerBase::marshal(int fd, Handle* handle) 00039 { Typelib::dump(handle->typelib_sample, fd, layout); } 00040 void TypelibMarshallerBase::marshal(std::ostream& stream, Handle* handle) 00041 { Typelib::dump(handle->typelib_sample, stream, layout); } 00042 void TypelibMarshallerBase::marshal(std::vector<uint8_t>& buffer, Handle* handle) 00043 { Typelib::dump(handle->typelib_sample, buffer, layout); } 00044 int TypelibMarshallerBase::marshal(void* buffer, int buffer_size, Handle* handle) 00045 { return Typelib::dump(handle->typelib_sample, static_cast<uint8_t*>(buffer), buffer_size, layout); } 00046 void TypelibMarshallerBase::unmarshal(void const* buffer, int buffer_size, Handle* handle) 00047 { 00048 Typelib::load(handle->typelib_sample, *type_def, static_cast<uint8_t const*>(buffer), buffer_size, layout); 00049 refreshOrocosSample(handle); 00050 } 00051 void TypelibMarshallerBase::unmarshal(std::vector<uint8_t>& buffer, Handle* handle) 00052 { 00053 Typelib::load(handle->typelib_sample, *type_def, buffer, layout); 00054 refreshOrocosSample(handle); 00055 } 00056 void TypelibMarshallerBase::setTypelibSample(Handle* data, Typelib::Value typelib_data, bool refresh_orocos) 00057 { return setTypelibSample(data, reinterpret_cast<uint8_t*>(typelib_data.getData()), refresh_orocos); } 00058 00059 void* orogen_transports::getOpaqueValue(std::string const& expected_type, Typelib::Value value) 00060 { 00061 RTT::types::TypeInfoRepository::shared_ptr type_registry = 00062 RTT::types::TypeInfoRepository::Instance(); 00063 RTT::types::TypeInfo* ti = type_registry->type(expected_type); 00064 if (!ti) 00065 throw std::runtime_error("type " + expected_type + " is not registered in the RTT type system"); 00066 00067 if (!ti->hasProtocol(orogen_transports::TYPELIB_MARSHALLER_ID)) 00068 throw std::runtime_error("type " + expected_type + " is registered in the RTT type system, but does not have a typelib transport"); 00069 00070 orogen_transports::TypelibMarshallerBase* typelib_marshaller = 00071 dynamic_cast<orogen_transports::TypelibMarshallerBase*>(ti->getProtocol(orogen_transports::TYPELIB_MARSHALLER_ID)); 00072 if (!typelib_marshaller) 00073 throw std::runtime_error("the transport object registered as typelib transport for type " + expected_type + " is not a TypelibMarshallerBase"); 00074 00075 orogen_transports::TypelibMarshallerBase::Handle* handle = 00076 typelib_marshaller->createHandle(); 00077 typelib_marshaller->setTypelibSample(handle, reinterpret_cast<uint8_t*>(value.getData()), true); 00078 void* opaque_sample = typelib_marshaller->releaseOrocosSample(handle); 00079 typelib_marshaller->deleteHandle(handle); 00080 return opaque_sample; 00081 } 00082