00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00040 #include <warehouse/exception.h>
00041 #include <std_msgs/String.h>
00042 #include <yaml-cpp/yaml.h>
00043
00044
00045 #ifndef WAREHOUSE_MESSAGE_WITH_METADATA_H
00046 #define WAREHOUSE_MESSAGE_WITH_METADATA_H
00047
00048 namespace warehouse
00049 {
00050
00054 template <class M>
00055 struct MessageWithMetadata : public M
00056 {
00057 MessageWithMetadata (const M& msg, const std::string& metadata) :
00058 metadata(metadata)
00059 {
00060 M::operator=(msg);
00061 }
00062
00063 MessageWithMetadata (const MessageWithMetadata& m) :
00064 M(m), metadata(m.metadata)
00065 {}
00066
00067 MessageWithMetadata ()
00068 {}
00069
00070 std::string metadata;
00071
00072 typedef boost::shared_ptr<MessageWithMetadata<M> > Ptr;
00073 typedef boost::shared_ptr<MessageWithMetadata<M> const> ConstPtr;
00074 };
00075
00076
00077
00083 class MetadataString
00084 {
00085 public:
00086
00087 MetadataString ()
00088 {
00089 done = false;
00090 out << YAML::BeginMap;
00091 }
00092
00093 template <typename T>
00094 MetadataString& add (const std::string& key,
00095 const T& value)
00096 {
00097 ROS_ASSERT_MSG (!done, "Can't use this metadata object any more");
00098 out << YAML::Key << key;
00099 out << YAML::Value << value;
00100 return *this;
00101 }
00102
00103 operator std::string ()
00104 {
00105 out << YAML::EndMap;
00106 done = true;
00107 return std::string(out.c_str());
00108 }
00109
00110
00111 private:
00112
00113 YAML::Emitter out;
00114 bool done;
00115 };
00116
00117
00118 template <typename T>
00119 T getMetadataVal (const std::string& metadata, const std::string& key)
00120 {
00121 std::stringstream s(metadata, std::ios_base::in);
00122 YAML::Parser parser(s);
00123 YAML::Node doc;
00124 const bool success = parser.GetNextDocument(doc);
00125 ROS_ASSERT_MSG(success, "Error parsing metadata %s", metadata.c_str());
00126 if (const YAML::Node* p_name = doc.FindValue(key))
00127 {
00128 T val;
00129 *p_name >> val;
00130 return val;
00131 }
00132 else
00133 {
00134 throw MissingKeyException(metadata, key);
00135 }
00136 }
00137
00138
00139 }
00140
00141 #endif // include guard