TypeFactory.h 4.48 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 23 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
#pragma once
#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.
template <class StringType = std::string>
class TypeFactory
{
    typedef MessageBaseClass<StringType> ROSMsg;
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!
    }

    // ===========================================================================
    // Point32Group
    // ===========================================================================
    template<class U>
    bool _create(const rapidjson::Document &doc, U &type, Type2Type<MessageGroups::Point32Group>){
        using namespace ROSBridge;
        bool ret = JsonMethodes::Point32::fromJson(doc, type);
        assert(ret);
        return ret;
    }

    // ===========================================================================
    // GeoPointGroup
    // ===========================================================================
    template<class U>
    bool _create(const rapidjson::Document &doc, U &type, Type2Type<MessageGroups::GeoPointGroup>){
        using namespace ROSBridge;
        bool ret = JsonMethodes::GeoPoint::fromJson(doc, type);
        assert(ret);
        return ret;
    }

    // ===========================================================================
    // PolygonGroup
    // ===========================================================================
    template<class U>
    bool _create(const rapidjson::Document &doc, U &type, Type2Type<MessageGroups::PolygonGroup>){
        using namespace ROSBridge;
        bool ret = JsonMethodes::Polygon::fromJson(doc, type);
        assert(ret);
        return ret;
    }

    // ===========================================================================
    // PolygonStampedGroup
    // ===========================================================================
    template<class U>
    bool _create(const rapidjson::Document &doc, U &type, Type2Type<MessageGroups::PolygonStampedGroup>){
        using namespace ROSBridge;
        bool ret = JsonMethodes::PolygonStamped::fromJson(doc, type);
        assert(ret);
        return ret;
    }

    // ===========================================================================
    // PolygonArrayGroup
    // ===========================================================================
    template<class U>
    bool _create(const rapidjson::Document &doc, U &type, Type2Type<MessageGroups::PolygonArrayGroup>){
        using namespace ROSBridge;
        bool ret = JsonMethodes::PolygonArray::fromJson(doc, type);
        assert(ret);
        return ret;
    }


};

} // end namespace ros_bridge