#ifndef JSONGENERATOR_H #define JSONGENERATOR_H #include "rapidjson/include/rapidjson/document.h" #include "include/messages.h" #include "assert.h" namespace ros_bridge { enum MessageType{TimeMessage, HeaderMessage, Point32Message, PolygonMessage, PolygonStampedMessage, PolygonArrayMessage}; template , class PolygonStampedType = class PolygonStamped<> , class PolygonArrayType = class PolygonArray<> > class JsonGenerator { public: JsonGenerator() : _time(nullptr) {} // Add set() function if new MessageType required! void add(const TimeType &time) {_time = &time;} void add(const HeaderType &header) {_header = &header;} void add(const PointType &point) {_point = &point;} void add(const PolygonType &polygon) {_polygon = &polygon;} // Modify run() function if new MessageType required! bool run(MessageType type, JsonDocumentType &doc, AllocatorType &allocator) { switch (type) { case TimeMessage: if (_time != nullptr) return _timeToJson(doc, allocator); assert(false); // _time not set! break; case HeaderMessage: if (_header != nullptr) return _headerToJson(doc, allocator); assert(false); // _header not set! break; case Point32Message: if (_point != nullptr) return _point32ToJson(doc, allocator); assert(false); // _point not set! break; case PolygonMessage: if (_polygon != nullptr) return _polygonToJson(doc, allocator); assert(false); // _polygon not set! break; case PolygonStampedMessage: if (_polygonStamped != nullptr) return _polygonStampedToJson(doc, allocator); assert(false); // _polygonStamped not set! break; case PolygonArrayMessage: if (_polygonArray != nullptr) return _polygonArrayToJson(doc, allocator); assert(false); // _polygonArray not set! break; default: assert(false); // Unknown MessageType! } return false; } // Modify clear() function if new MessageType required! void clear() { _time = nullptr; _header = nullptr; _point = nullptr; _polygon = nullptr; } private: bool _timeToJson(JsonDocumentType &doc, AllocatorType &allocator) { doc.AddMember("secs", rapidjson::Value().SetUint(_time->getSecs()), allocator); doc.AddMember("nsecs", rapidjson::Value().SetUint(_time->getNSecs()), allocator); return true; } // TODO bool _HeaderToJson(JsonDocumentType &doc, AllocatorType &allocator) { return false; } bool _Point32ToJson(JsonDocumentType &doc, AllocatorType &allocator) { return false; } bool _PolygonToJson(JsonDocumentType &doc, AllocatorType &allocator) { return false; } bool _PolygonStampedToJson(JsonDocumentType &doc, AllocatorType &allocator) { return false; } bool _PolygonArrayToJson(JsonDocumentType &doc, AllocatorType &allocator) { return false; } // Add new MessageType here. const TimeType *_time; const HeaderType *_header; const PointType *_point; const PolygonType *_polygon; const PolygonStampedType *_polygonStamped; const PolygonArrayType *_polygonArray; }; namespace detail { } } #endif // JSONGENERATOR_H