messages.h 7.44 KB
Newer Older
1 2 3 4 5 6
#ifndef MESSAGES_H
#define MESSAGES_H

#include <iostream>
#include <vector>

7 8 9
#include "ros_bridge/rapidjson/include/rapidjson/rapidjson.h"
#include "ros_bridge/rapidjson/include/rapidjson/document.h"

10 11 12 13 14
namespace ros_bridge {

    //! @brief C++ representation of ros::Time
    class Time{
    public:
15 16
        Time(): _secs(0), _nsecs(0) {}
        Time(uint32_t secs, uint32_t nsecs): _secs(secs), _nsecs(nsecs) {}
17

18
        uint32_t getSecs()  const {return _secs;}
19 20 21 22
        uint32_t getNSecs() const {return _nsecs;}

        void setSecs(uint32_t secs)     {_secs = secs;}
        void setNSecs(uint32_t nsecs)   {_nsecs = nsecs;}
23

24 25 26
        bool toJson(rapidjson::Document &doc,
                    rapidjson::Document::AllocatorType &allocator) const
        {
27 28
            doc.AddMember("secs", rapidjson::Value().SetUint(this->_secs), allocator);
            doc.AddMember("nsecs", rapidjson::Value().SetUint(this->_nsecs), allocator);
29 30 31

            return true;
        }
32 33 34 35 36 37 38 39 40

    private:
        uint32_t _secs;
        uint32_t _nsecs;
    };

    //! @brief C++ representation of std_msgs/Header
    class Header{
    public:
41 42 43 44 45
        Header() : _seq(0), _frameId("") {}
        Header(uint32_t seq, const Time *stamp, const std::string &frame_id) :
            _seq(seq)
          , _stamp(stamp)
          , _frameId(frame_id) {}
46

47 48 49
        uint32_t            getSeq()       const {return _seq;};
        const Time         *getStamp()     const {return _stamp;};
        const std::string  &getFrameId()   const {return _frameId;};
50

51 52 53
        void setSeq     (uint32_t seq)                  {_seq = seq;}
        void setStamp   (const Time *stamp)             {_stamp = stamp;}
        void setFrameId (const std::string  &frameId)   {_frameId = frameId;}
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

        bool toJson(rapidjson::Document &doc,
                    rapidjson::Document::AllocatorType &allocator) const
        {
            doc.AddMember("seq", rapidjson::Value().SetUint(this->_seq), allocator);
            rapidjson::Document stamp(rapidjson::kObjectType);
            if (!this->_stamp->toJson(stamp, allocator))
                    return false;
            doc.AddMember("stamp", stamp, allocator);
            doc.AddMember("frame_id",
                          rapidjson::Value().SetString(this->_frameId.data(),
                                                       this->_frameId.length(),
                                                       allocator),
                          allocator);

            return true;
        }
71 72 73

    private:
        uint32_t    _seq;
74 75
        const Time *_stamp;
        std::string _frameId;
76 77 78
    };

    //! @brief C++ representation of geometry_msgs/Point32
79
    template <class PointType>
80
    class Point32{
81
    public:        
82
        Point32() {}
83
        Point32(const PointType* p): _point(p) {}
84

85 86
        const PointType    *getPoint()                          const   {return _point;}
        void                setPoint(const PointType *point)            {point;}
87

88
        bool toJson(rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator) const
89
        {
90 91 92 93 94 95

            doc.AddMember("x", rapidjson::Value().SetFloat(_point->x()), allocator);
            doc.AddMember("y", rapidjson::Value().SetFloat(_point->y()), allocator);
            doc.AddMember("z", rapidjson::Value().SetFloat(_point->z()), allocator);

            return true;
96 97 98
        }

    private:
99
        const PointType *_point;
100 101 102
    };

    //! @brief C++ representation of geometry_msgs/Polygon
103
    template <class PointType,
104 105
              template <class, class...> class ContainerType =  std::vector,
              class PointWrapper  = class Point32<PointType> >
106 107 108
    class Polygon{
    public:
        Polygon(){}
109
        Polygon(const ContainerType<PointType> *points) : _points(points) {}
110 111


112
        const ContainerType<PointType> *getPoints() const {return _points;}
113

114
        void setPoints(const ContainerType<PointType> *points) {_points = points;}
115

116
        bool toJson(rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator)
117
        {
118
            rapidjson::Value points(rapidjson::kArrayType);
119

120 121
            for(long i=0; i < _points->size(); ++i) {
                rapidjson::Document point(rapidjson::kObjectType);
122
                if ( !( (*_points)[i].toJson(point, allocator) ) )
123 124 125
                    return false;
                points.PushBack(point, allocator);
            }
126

127 128 129
            doc.AddMember("points", points, allocator);

            return true;
130 131 132
        }

    private:
133
        const ContainerType<PointType> *_points;
134 135 136
    };

    //! @brief C++ representation of geometry_msgs/PolygonStamped
137
    template <class PointType, class PolygonType = Polygon<PointType>, class HeaderType = Header>
138 139 140
    class PolygonStamped : public PolygonType{
    public:
        PolygonStamped();
141
        PolygonStamped(const Header *header, const PolygonType *polygon) :
142
            _header(header)
143 144 145 146 147 148
          , _polygon(polygon) {}


        const HeaderType  *getHeader()  const   {return _header;}
        const PolygonType *getPolygon() const   {return _polygon;}

149
        void setHeader(const HeaderType *header)          {_header = header;}
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
        void setPolygon(const PolygonType *polygon)   {_polygon = polygon;}

        bool toJson(rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator)
        {
            rapidjson::Document header(rapidjson::kObjectType);
            if (!this->_header->toJson(header, allocator))
                return false;
            rapidjson::Document polygon(rapidjson::kObjectType);
            if (!this->_polygon->toJson(polygon, allocator))
                return false;
            doc.AddMember("header", header, allocator);
            doc.AddMember("polygon", polygon, allocator);

            return true;
        }
165 166

    private:
167 168
        HeaderType *_header;
        PolygonType *_polygon;
169 170 171
    };

    //! @brief C++ representation of jsk_recognition_msgs/PolygonArray
172 173
    template <class PointType,
              class PolygonType = Polygon<PointType>,
174 175
              template <class, class...> class ContainerType = std::vector,
              class HeaderType = Header >
176 177
    class PolygonArray{
    public:
178
        PolygonArray() {}
179
        PolygonArray(const HeaderType                   *header,
180 181 182
                     const ContainerType<PolygonType>   *polygons,
                     const ContainerType<uint32_t>      *labels,
                     const ContainerType<_Float32>      *likelihood)
183 184 185 186 187 188
          : _header(header)
          , _polygons(polygons)
          , _labels(labels)
          , _likelihood(likelihood)
        {}

189 190 191 192 193 194 195 196 197
        const HeaderType                 *getHeader()     const {return _header;}
        const ContainerType<PolygonType> *getPolygons()   const {return _polygons;}
        const ContainerType<uint32_t>    *getLabels()     const {return _labels;}
        const ContainerType<_Float32>    *getLikelihood() const {return _likelihood;}

        void *setHeader     (const HeaderType                 *header)      {_header     = header;}
        void *setPolygons   (const ContainerType<PolygonType> *polygon)     {_polygons   = polygon;}
        void *setLabels     (const ContainerType<uint32_t>    *labels)      {_labels     = labels;}
        void *setLikelihood (const ContainerType<_Float32>    *likelihood)  {_likelihood = likelihood;}
198 199 200



201
    private:
202 203 204 205
        HeaderType                 *_header;
        ContainerType<PolygonType> *_polygons;
        ContainerType<uint32_t>    *_labels;
        ContainerType<_Float32>    *_likelihood;
206 207 208 209
    };
}

#endif // MESSAGES_H