CasePacker.cpp 1.95 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1
#include "ros_bridge/include/CasePacker.h"
2 3 4 5 6 7 8 9 10

const char* ROSBridge::CasePacker::topicKey         = "topic";
const char* ROSBridge::CasePacker::messageTypeKey   = "messageType";

ROSBridge::CasePacker::CasePacker()
{

}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
11 12 13 14 15 16 17 18 19 20
bool ROSBridge::CasePacker::showTag(const ROSBridge::CasePacker::JsonDoc &doc, Tag &tag)
{
    if( !_getTopic(doc, tag.topic()) )
        return false;
    if( !_getMessageType(doc, tag.messageType()) )
        return false;
    return true;
}

void ROSBridge::CasePacker::_addTag(JsonDoc &doc, const std::string &topic, const std::string &messageType)
21 22 23 24 25 26 27
{
    using namespace ROSBridge;
    using namespace rapidjson;

    {
    // add topic
    rapidjson::Value key(CasePacker::topicKey, doc.GetAllocator());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
28
    rapidjson::Value value(topic.c_str(), doc.GetAllocator());
29 30 31 32 33
    doc.AddMember(key, value, doc.GetAllocator());
    }

    // add messageType
    rapidjson::Value key(CasePacker::messageTypeKey, doc.GetAllocator());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
34
    rapidjson::Value value(messageType.c_str(), doc.GetAllocator());
35 36 37 38 39 40 41 42 43 44 45 46 47
    doc.AddMember(key, value, doc.GetAllocator());
}

void ROSBridge::CasePacker::_removeTag(JsonDoc &doc)
{
    using namespace ROSBridge;
    using namespace rapidjson;
    if ( doc.HasMember(CasePacker::topicKey) )
        doc.RemoveMember(CasePacker::topicKey);
    if ( doc.HasMember(CasePacker::messageTypeKey) )
        doc.RemoveMember(CasePacker::messageTypeKey);
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
48 49 50 51 52 53 54
bool ROSBridge::CasePacker::_getTopic(const ROSBridge::CasePacker::JsonDoc &doc, std::string &topic)
{
    if (!doc.HasMember(CasePacker::topicKey) || !doc[CasePacker::topicKey].IsString())
        return false;
    topic = doc[CasePacker::topicKey].GetString();
    return true;
}
55

Valentin Platzgummer's avatar
Valentin Platzgummer committed
56 57 58 59 60 61 62
bool ROSBridge::CasePacker::_getMessageType(const ROSBridge::CasePacker::JsonDoc &doc, std::string &messageType)
{
    if (!doc.HasMember(CasePacker::messageTypeKey) || !doc[CasePacker::messageTypeKey].IsString())
        return false;
    messageType = doc[CasePacker::messageTypeKey].GetString();
    return true;
}