message.h
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2011, Johannes Meyer, TU Darmstadt
3 // All rights reserved.
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Flight Systems and Automatic Control group,
13 // TU Darmstadt, nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without
15 // specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //=================================================================================================
28 
29 #ifndef CPP_INTROSPECTION_MESSAGE_H
30 #define CPP_INTROSPECTION_MESSAGE_H
31 
32 #include <introspection/forwards.h>
33 #include <introspection/field.h>
34 
35 #include <std_msgs/Header.h>
36 #include <ros/time.h>
37 
38 namespace cpp_introspection {
39 
40  class Message
41  {
42  public:
43  virtual ~Message() {}
44 
45  virtual PackagePtr package() const = 0;
46  virtual const char* getPackageName() const = 0;
47 
48  virtual const char* getName() const = 0;
49  virtual const char* getDataType() const = 0;
50  virtual const char* getMD5Sum() const = 0;
51  virtual const char* getDefinition() const = 0;
52  virtual const std::type_info& getTypeId() const = 0;
53 
54  virtual bool isMessage() const { return true; }
55  virtual bool isSimple() const = 0;
56  virtual bool isFixedSize() const = 0;
57  virtual bool hasHeader() const = 0;
58 
59  template <typename T> bool hasType() const { return getTypeId() == typeid(T); }
60 
61  virtual std_msgs::Header* getHeader(const VoidPtr& instance) const = 0;
62  virtual const std_msgs::Header* getHeader(const VoidConstPtr& instance) const = 0;
63  virtual std::string* getFrameId(const VoidPtr& instance) const = 0;
64  virtual const std::string* getFrameId(const VoidConstPtr& instance) const = 0;
65  virtual ros::Time* getTimeStamp(const VoidPtr& instance) const = 0;
66  virtual const ros::Time* getTimeStamp(const VoidConstPtr& instance) const = 0;
67 
68  virtual const V_Field& fields() const = 0;
69  virtual FieldWPtr field(const std::string& name) const = 0;
70  virtual const V_FieldName& getFieldNames() const = 0;
71 
72  V_string getFields(bool expand = false, const std::string& separator = ".", const std::string& prefix = std::string()) const;
73  V_string& getFields(V_string& fields, bool expand = false, const std::string& separator = ".", const std::string& prefix = std::string()) const;
74  V_string getTypes(bool expand = false) const;
75  V_string& getTypes(V_string& types, bool expand = false) const;
76  std::vector<boost::any> getValues(bool expand = false) const;
77  std::vector<boost::any>& getValues(std::vector<boost::any>& values, bool expand = false) const;
78 
79  virtual VoidPtr createInstance() const = 0;
80  virtual void serialize(ros::serialization::OStream& stream, const VoidConstPtr& instance = VoidConstPtr()) const = 0;
81  virtual ros::SerializedMessage serialize(const VoidConstPtr& instance = VoidConstPtr()) const = 0;
82  virtual std::size_t serializationLength(const VoidConstPtr& instance = VoidConstPtr()) const = 0;
83  virtual VoidPtr deserialize(ros::serialization::IStream& stream, const VoidPtr& instance = VoidPtr()) const = 0;
84 
85  virtual bool hasInstance() const { return false; }
86  virtual VoidPtr getInstance() const { return VoidPtr(); }
87  virtual VoidConstPtr getConstInstance() const { return VoidConstPtr(); }
88  template <typename T> boost::shared_ptr<T> getInstanceAs() const { assert(getTypeId() == typeid(T)); return boost::shared_static_cast<T>(getInstance()); }
89  template <typename T> boost::shared_ptr<T const> getConstInstanceAs() const { assert(getTypeId() == typeid(T)); return boost::shared_static_cast<T const>(getConstInstance()); }
90 
91  virtual MessagePtr introspect(const VoidPtr& instance) const = 0;
92  virtual MessagePtr introspect(void *instance) const = 0;
93  virtual MessagePtr introspect(const VoidConstPtr& instance) const = 0;
94  virtual MessagePtr introspect(void const *instance) const = 0;
95  virtual MessagePtr introspect() const { return introspect(createInstance()); }
96 
97  template <typename T> typename T::Ptr narrow(const VoidPtr& instance) const { return boost::shared_static_cast<T>(instance); }
98  template <typename T> typename T::ConstPtr narrow(const VoidConstPtr& instance) const { return boost::shared_static_cast<T const>(instance); }
99 
100  typedef V_Field::iterator iterator;
101  typedef V_Field::const_iterator const_iterator;
102  const_iterator begin() const { return fields().begin(); }
103  const_iterator end() const { return fields().end(); }
104  std::size_t size() const { return fields().size(); }
105  };
106 
107  MessagePtr messageByDataType(const std::string& data_type, const std::string& package = std::string());
108  static inline MessagePtr messageByDataType(const char* data_type, const char* package) { return messageByDataType(std::string(data_type), std::string(package)); }
109  MessagePtr messageByMD5Sum(const std::string& md5sum);
110  static inline MessagePtr messageByMD5Sum(const char* md5sum) { return messageByMD5Sum(std::string(md5sum)); }
111  MessagePtr messageByTypeId(const std::type_info& type_info);
112 
113  MessagePtr expand(const MessagePtr& message, const std::string &separator = ".", const std::string &prefix = "");
114 
115 } // namespace cpp_introspection
116 
117 namespace ros {
118 namespace serialization {
119 
120  template<>
121  struct Serializer<cpp_introspection::Message>
122  {
123  template<typename Stream>
124  inline static void write(Stream& stream, const cpp_introspection::Message& message) {
125  if (!message.hasInstance()) throw std::runtime_error("Tried to serialize a message without an instance");
126  message.serialize(stream, message.getConstInstance());
127  }
128 
129  inline static uint32_t serializedLength(const cpp_introspection::Message& message) {
130  if (!message.hasInstance()) throw std::runtime_error("Tried to serialize a message without an instance");
131  return message.serializationLength(message.getConstInstance());
132  }
133  };
134 
135 }
136 }
137 
138 namespace ros {
139 namespace message_traits {
140 
141  template<> struct MD5Sum<cpp_introspection::Message>
142  {
143  static const char* value(const cpp_introspection::Message& message) { return message.getMD5Sum(); }
144  };
145  template<> struct DataType<cpp_introspection::Message>
146  {
147  static const char* value(const cpp_introspection::Message& message) { return message.getDataType(); }
148  };
149  template<> struct Definition<cpp_introspection::Message>
150  {
151  static const char* value(const cpp_introspection::Message& message) { return message.getDefinition(); }
152  };
153 
154 }
155 }
156 
157 #endif // CPP_INTROSPECTION_MESSAGE_H
std::vector< std::string > V_string
Definition: forwards.h:74
std::vector< FieldPtr > V_Field
Definition: forwards.h:57
virtual const char * getName() const =0
virtual std::string * getFrameId(const VoidPtr &instance) const =0
boost::weak_ptr< Field const > FieldWPtr
Definition: forwards.h:51
MessagePtr messageByMD5Sum(const std::string &md5sum)
T::Ptr narrow(const VoidPtr &instance) const
Definition: message.h:97
V_string getFields(bool expand=false, const std::string &separator=".", const std::string &prefix=std::string()) const
std::vector< boost::any > getValues(bool expand=false) const
virtual VoidPtr createInstance() const =0
static void write(Stream &stream, const cpp_introspection::Message &message)
Definition: message.h:124
virtual std::size_t serializationLength(const VoidConstPtr &instance=VoidConstPtr()) const =0
virtual PackagePtr package() const =0
virtual MessagePtr introspect() const
Definition: message.h:95
static const char * value(const cpp_introspection::Message &message)
Definition: message.h:151
virtual VoidPtr getInstance() const
Definition: message.h:86
MessagePtr expand(const MessagePtr &message, const std::string &separator=".", const std::string &prefix="")
V_Field::iterator iterator
Definition: message.h:100
static const char * value(const cpp_introspection::Message &message)
Definition: message.h:143
const_iterator end() const
Definition: message.h:103
virtual const char * getDefinition() const =0
virtual FieldWPtr field(const std::string &name) const =0
virtual bool hasHeader() const =0
virtual bool isMessage() const
Definition: message.h:54
boost::shared_ptr< void > VoidPtr
Definition: forwards.h:69
T::ConstPtr narrow(const VoidConstPtr &instance) const
Definition: message.h:98
MessagePtr messageByTypeId(const std::type_info &type_info)
virtual const V_Field & fields() const =0
V_Field::const_iterator const_iterator
Definition: message.h:101
boost::shared_ptr< T const > getConstInstanceAs() const
Definition: message.h:89
virtual void serialize(ros::serialization::OStream &stream, const VoidConstPtr &instance=VoidConstPtr()) const =0
virtual const char * getDataType() const =0
boost::shared_ptr< void const > VoidConstPtr
Definition: forwards.h:71
const_iterator begin() const
Definition: message.h:102
std::size_t size() const
Definition: message.h:104
virtual bool isSimple() const =0
virtual const char * getMD5Sum() const =0
virtual const V_FieldName & getFieldNames() const =0
boost::shared_ptr< T > getInstanceAs() const
Definition: message.h:88
virtual std_msgs::Header * getHeader(const VoidPtr &instance) const =0
virtual VoidConstPtr getConstInstance() const
Definition: message.h:87
virtual ros::Time * getTimeStamp(const VoidPtr &instance) const =0
bool hasType() const
Definition: message.h:59
const char * md5sum()
virtual const char * getPackageName() const =0
virtual VoidPtr deserialize(ros::serialization::IStream &stream, const VoidPtr &instance=VoidPtr()) const =0
static uint32_t serializedLength(const cpp_introspection::Message &message)
Definition: message.h:129
static const char * value(const cpp_introspection::Message &message)
Definition: message.h:147
virtual const std::type_info & getTypeId() const =0
V_string getTypes(bool expand=false) const
virtual bool hasInstance() const
Definition: message.h:85
virtual bool isFixedSize() const =0
MessagePtr messageByDataType(const std::string &data_type, const std::string &package=std::string())
std::vector< const char * > V_FieldName
Definition: forwards.h:58


cpp_introspection
Author(s): Johannes Meyer
autogenerated on Mon Jun 10 2019 12:56:17