TypelibMarshallerBase.cpp
Go to the documentation of this file.
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     , registry(&registry)
00016     , type_def(registry.get(typelib_typename))
00017     , m_typename_typelib(typelib_typename)
00018     , m_typename_orocos(orocos_typename)
00019 {
00020     if (!type_def)
00021         throw std::runtime_error(typelib_typename + " is not present in the typekit registry");
00022 
00023     layout    = Typelib::layout_of(*type_def, false, false);
00024 }
00025 
00026 Typelib::Registry const& TypelibMarshallerBase::getRegistry() const
00027 { return *registry; }
00028 
00029 bool TypelibMarshallerBase::isPlainTypelibType() const
00030 { return m_plain; }
00031 
00032 Handle* TypelibMarshallerBase::createHandle() { return new Handle(this); }
00033 void TypelibMarshallerBase::deleteHandle(Handle* handle) { delete handle; }
00034 uint8_t* TypelibMarshallerBase::getTypelibSample(Handle* handle)
00035 {
00036     return handle->typelib_sample;
00037 }
00038 uint8_t* TypelibMarshallerBase::getOrocosSample(Handle* handle)
00039 {
00040     return handle->orocos_sample;
00041 }
00042 char const* TypelibMarshallerBase::getMarshallingType() const
00043 { return m_typename_typelib.c_str(); }
00044 size_t TypelibMarshallerBase::getMarshallingSize(Handle const* handle) const
00045 { return Typelib::getDumpSize(handle->typelib_sample, layout); }
00046 void TypelibMarshallerBase::marshal(int fd, Handle* handle)
00047 { Typelib::dump(handle->typelib_sample, fd, layout); }
00048 void TypelibMarshallerBase::marshal(std::ostream& stream, Handle* handle)
00049 { Typelib::dump(handle->typelib_sample, stream, layout); }
00050 void TypelibMarshallerBase::marshal(std::vector<uint8_t>& buffer, Handle* handle)
00051 { Typelib::dump(handle->typelib_sample, buffer, layout); }
00052 int TypelibMarshallerBase::marshal(void* buffer, int buffer_size, Handle* handle)
00053 { return Typelib::dump(handle->typelib_sample, static_cast<uint8_t*>(buffer), buffer_size, layout); }
00054 void TypelibMarshallerBase::unmarshal(void const* buffer, int buffer_size, Handle* handle)
00055 {
00056     Typelib::load(handle->typelib_sample, *type_def, static_cast<uint8_t const*>(buffer), buffer_size, layout);
00057     refreshOrocosSample(handle);
00058 }
00059 void TypelibMarshallerBase::unmarshal(std::vector<uint8_t>& buffer, Handle* handle)
00060 {
00061     Typelib::load(handle->typelib_sample, *type_def, buffer, layout);
00062     refreshOrocosSample(handle);
00063 }
00064 void TypelibMarshallerBase::setTypelibSample(Handle* data, Typelib::Value typelib_data, bool refresh_orocos)
00065 { return setTypelibSample(data, reinterpret_cast<uint8_t*>(typelib_data.getData()), refresh_orocos); }
00066 
00067 orogen_transports::TypelibMarshallerBase* orogen_transports::getMarshallerFor(std::string const& type)
00068 {
00069     RTT::types::TypeInfoRepository::shared_ptr type_registry =
00070         RTT::types::TypeInfoRepository::Instance();
00071     RTT::types::TypeInfo* ti = type_registry->type(type);
00072     if (!ti)
00073     {
00074         // Try harder. Some base types don't have a
00075         // typelib-normalized name, so we should look
00076         // for the type without the leading slash
00077         ti = type_registry->type(type.substr(1));
00078         if (!ti)
00079             throw std::runtime_error("type " + type + " is not registered in the RTT type system");
00080     }
00081 
00082     if (!ti->hasProtocol(orogen_transports::TYPELIB_MARSHALLER_ID))
00083         throw std::runtime_error("type " + type + " is registered in the RTT type system, but does not have a typelib transport");
00084 
00085     orogen_transports::TypelibMarshallerBase* typelib_marshaller =
00086         dynamic_cast<orogen_transports::TypelibMarshallerBase*>(ti->getProtocol(orogen_transports::TYPELIB_MARSHALLER_ID));
00087     if (!typelib_marshaller)
00088         throw std::runtime_error("the transport object registered as typelib transport for type " + type + " is not a TypelibMarshallerBase");
00089 
00090     return typelib_marshaller;
00091 }
00092 
00093 void* orogen_transports::getOpaqueValue(std::string const& expected_type, Typelib::Value value)
00094 {
00095     orogen_transports::TypelibMarshallerBase* typelib_marshaller =
00096         getMarshallerFor(expected_type);
00097 
00098     orogen_transports::TypelibMarshallerBase::Handle* handle =
00099         typelib_marshaller->createHandle();
00100     typelib_marshaller->setTypelibSample(handle, reinterpret_cast<uint8_t*>(value.getData()), true);
00101     void* opaque_sample = typelib_marshaller->releaseOrocosSample(handle);
00102     typelib_marshaller->deleteHandle(handle);
00103     return opaque_sample;
00104 }
00105 


rtt_typelib
Author(s): Sylvain Joyeux/sylvain.joyeux@m4x.org
autogenerated on Mon Oct 6 2014 03:17:24