TypeFactory.h 4.97 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#pragma once

#include "ros_bridge/rapidjson/include/rapidjson/document.h"
#include "ros_bridge/include/JsonMethodes.h"
#include "ros_bridge/include/MessageBaseClass.h"
#include "utilities.h"
#include "ros_bridge/include/MessageTraits.h"

#include "ros_bridge/include/MessageGroups.h"

#include "boost/type_traits.hpp"
#include "boost/type_traits/is_base_of.hpp"

namespace ROSBridge {

//!
//! \brief The TypeFactory class is used to create C++ representatives of ROS messages.
//!
//! The TypeFactory class is used to create C++ representatives of ROS messages
//! (classes derived from \class MessageBaseClass) from a \class rapidjson::Document document.
class TypeFactory
{
23
    typedef MessageBaseClass ROSMsg;
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
public:

    TypeFactory(){}

    //!
    //! \brief Creates a \class rapidjson::Document document containing a ROS mesage from \p msg.
    //!
    //! Creates a \class rapidjson::Document document containing a ROS message from \p msg.
    //! A compile-time error will occur, if \p msg belongs to the \struct EmptyGroup or is
    //! not derived from \class MessageBaseClass.
    //! \param msg Instance of a \class MessageBaseClass subclass belonging to a ROSMessageGroup.
    //! \return rapidjson::Document document containing a ROS message.
    template <class T>
    bool create(const rapidjson::Document &doc, T &type){
        static_assert(boost::is_base_of<ROSMsg, T>::value, "Type of msg must be derived from MessageBaseClass.");
        static_assert(!::boost::is_same<typename T::Group, MessageGroups::EmptyGroup>::value,
                "msg belongs to group EmptyGroup (not allowed). Please specify Group (typedef MessageGroup Group)");
        return _create(doc, type, Type2Type<typename T::Group>());
    }

private:

    // ===========================================================================
    // EmptyGroup and not implemented Groups
    // ===========================================================================
    template<class U, class V>
    bool _create(const rapidjson::Document &doc, U &type, Type2Type<V>){
        (void)type;
        (void)doc;
        assert(false); // Implementation missing for group U::Group!
Valentin Platzgummer's avatar
Valentin Platzgummer committed
54
        return false;
55 56 57 58 59 60 61 62
    }

    // ===========================================================================
    // Point32Group
    // ===========================================================================
    template<class U>
    bool _create(const rapidjson::Document &doc, U &type, Type2Type<MessageGroups::Point32Group>){
        using namespace ROSBridge;
63
        bool ret = JsonMethodes::GeometryMsgs::Point32::fromJson(doc, type);
64 65 66 67 68 69 70 71 72 73
        assert(ret);
        return ret;
    }

    // ===========================================================================
    // GeoPointGroup
    // ===========================================================================
    template<class U>
    bool _create(const rapidjson::Document &doc, U &type, Type2Type<MessageGroups::GeoPointGroup>){
        using namespace ROSBridge;
74
        bool ret = JsonMethodes::GeographicMsgs::GeoPoint::fromJson(doc, type);
75 76 77 78 79 80 81 82 83 84
        assert(ret);
        return ret;
    }

    // ===========================================================================
    // PolygonGroup
    // ===========================================================================
    template<class U>
    bool _create(const rapidjson::Document &doc, U &type, Type2Type<MessageGroups::PolygonGroup>){
        using namespace ROSBridge;
85
        bool ret = JsonMethodes::GeometryMsgs::Polygon::fromJson(doc, type);
86 87 88 89 90 91 92 93 94 95
        assert(ret);
        return ret;
    }

    // ===========================================================================
    // PolygonStampedGroup
    // ===========================================================================
    template<class U>
    bool _create(const rapidjson::Document &doc, U &type, Type2Type<MessageGroups::PolygonStampedGroup>){
        using namespace ROSBridge;
96
        bool ret = JsonMethodes::GeometryMsgs::PolygonStamped::fromJson(doc, type);
97 98 99 100 101 102 103 104 105 106
        assert(ret);
        return ret;
    }

    // ===========================================================================
    // PolygonArrayGroup
    // ===========================================================================
    template<class U>
    bool _create(const rapidjson::Document &doc, U &type, Type2Type<MessageGroups::PolygonArrayGroup>){
        using namespace ROSBridge;
107 108 109 110 111 112 113 114 115 116 117 118
        bool ret = JsonMethodes::JSKRecognitionMsgs::PolygonArray::fromJson(doc, type);
        assert(ret);
        return ret;
    }

    // ===========================================================================
    // ProgressGroup
    // ===========================================================================
    template<class U>
    bool _create(const rapidjson::Document &doc, U &type, Type2Type<MessageGroups::ProgressGroup>){
        using namespace ROSBridge;
        bool ret = JsonMethodes::NemoMsgs::Progress::fromJson(doc, type);
119 120 121 122 123 124 125 126
        assert(ret);
        return ret;
    }


};

} // end namespace ros_bridge