00001 #include "utilities/message_field.h" 00002 #include "utilities/message.h" 00003 #include <variant_topic_tools/MessageVariant.h> 00004 00005 namespace utilities 00006 { 00007 00008 MessageField::MessageField(QObject* parent, const QString& type, 00009 const QString& field) 00010 : QObject(parent), type_(type), field_(field) 00011 { 00012 variant_topic_tools::MessageDefinition definition; 00013 definition.load(type_.toStdString()); 00014 if (!definition.hasField(field_.toStdString())) 00015 { 00016 throw ros::Exception("The " + type.toStdString() + 00017 " message does not have the " + field_.toStdString() + 00018 " field."); 00019 } 00020 } 00021 00022 MessageField::~MessageField() {} 00023 00024 QString MessageField::getType() const { return type_; } 00025 00026 QString MessageField::getField() const { return field_; } 00027 00028 void MessageField::setType(const QString& type) 00029 { 00030 variant_topic_tools::MessageDefinition definition; 00031 definition.load(type.toStdString()); 00032 type_ = type; 00033 } 00034 00035 void MessageField::setField(const QString& field) 00036 { 00037 variant_topic_tools::MessageDefinition definition; 00038 definition.load(type_.toStdString()); 00039 if (!definition.hasField(field.toStdString())) 00040 { 00041 throw ros::Exception("The " + type_.toStdString() + 00042 " message does not have the " + field.toStdString() + 00043 " field."); 00044 } 00045 field_ = field; 00046 } 00047 00048 variant_topic_tools::BuiltinVariant 00049 MessageField::getFieldValue(const Message& message) 00050 { 00051 std::string type(message.getVariant().getType().getIdentifier()); 00052 if (type != type_.toStdString()) 00053 { 00054 throw ros::Exception(type + " does not match to " + type_.toStdString() + 00055 "."); 00056 } 00057 return message.getVariant().getMember(field_.toStdString()); 00058 } 00059 }