shape_shifter.h
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2009, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of Willow Garage, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 ********************************************************************/
34 
35 #ifndef TOPIC_TOOLS_SHAPE_SHIFTER_H
36 #define TOPIC_TOOLS_SHAPE_SHIFTER_H
37 
38 #include "ros/ros.h"
39 #include "ros/console.h"
40 #include "ros/assert.h"
41 #include <vector>
42 #include <string>
43 #include <string.h>
44 
45 #include <ros/message_traits.h>
46 #include "macros.h"
47 
48 namespace topic_tools
49 {
50 
51 class ShapeShifterException : public ros::Exception
52 {
53 public:
54  ShapeShifterException(const std::string& msg)
55  : ros::Exception(msg) {}
56 };
57 
58 
59 class TOPIC_TOOLS_DECL ShapeShifter
60 {
61 public:
64 
65  static bool uses_old_API_;
66 
67  // Constructor and destructor
68  ShapeShifter();
69  virtual ~ShapeShifter();
70 
71  // Helpers for inspecting shapeshifter
72  std::string const& getDataType() const;
73  std::string const& getMD5Sum() const;
74  std::string const& getMessageDefinition() const;
75 
76  void morph(const std::string& md5sum, const std::string& datatype, const std::string& msg_def,
77  const std::string& latching);
78 
79  // Helper for advertising
80  ros::Publisher advertise(ros::NodeHandle& nh, const std::string& topic, uint32_t queue_size_, bool latch=false,
82 
84  template<class M>
85  boost::shared_ptr<M> instantiate() const;
86 
88  template<typename Stream>
89  void write(Stream& stream) const;
90 
91  template<typename Stream>
92  void read(Stream& stream);
93 
95  uint32_t size() const;
96 
97 private:
98 
99  std::string md5, datatype, msg_def, latching;
100  bool typed;
101 
102  std::vector<uint8_t> msgBuf;
103 };
104 
105 }
106 
107 
108 // Message traits allow shape shifter to work with the new serialization API
109 namespace ros {
110 namespace message_traits {
111 
112 template <> struct IsMessage<topic_tools::ShapeShifter> : TrueType { };
113 template <> struct IsMessage<const topic_tools::ShapeShifter> : TrueType { };
114 
115 template<>
116 struct MD5Sum<topic_tools::ShapeShifter>
117 {
118  static const char* value(const topic_tools::ShapeShifter& m) { return m.getMD5Sum().c_str(); }
119 
120  // Used statically, a shapeshifter appears to be of any type
121  static const char* value() { return "*"; }
122 };
123 
124 template<>
125 struct DataType<topic_tools::ShapeShifter>
126 {
127  static const char* value(const topic_tools::ShapeShifter& m) { return m.getDataType().c_str(); }
128 
129  // Used statically, a shapeshifter appears to be of any type
130  static const char* value() { return "*"; }
131 };
132 
133 template<>
134 struct Definition<topic_tools::ShapeShifter>
135 {
136  static const char* value(const topic_tools::ShapeShifter& m) { return m.getMessageDefinition().c_str(); }
137 };
138 
139 } // namespace message_traits
140 
141 
142 namespace serialization
143 {
144 
145 template<>
146 struct Serializer<topic_tools::ShapeShifter>
147 {
148  template<typename Stream>
149  inline static void write(Stream& stream, const topic_tools::ShapeShifter& m) {
150  m.write(stream);
151  }
152 
153  template<typename Stream>
154  inline static void read(Stream& stream, topic_tools::ShapeShifter& m)
155  {
156  m.read(stream);
157  }
158 
159  inline static uint32_t serializedLength(const topic_tools::ShapeShifter& m) {
160  return m.size();
161  }
162 };
163 
164 
165 template<>
166 struct PreDeserialize<topic_tools::ShapeShifter>
167 {
169  {
170  std::string md5 = (*params.connection_header)["md5sum"];
171  std::string datatype = (*params.connection_header)["type"];
172  std::string msg_def = (*params.connection_header)["message_definition"];
173  std::string latching = (*params.connection_header)["latching"];
174 
175  params.message->morph(md5, datatype, msg_def, latching);
176  }
177 };
178 
179 } // namespace serialization
180 
181 } //namespace ros
182 
183 
184 
185 // Template implementations:
186 
187 namespace topic_tools
188 {
189 
190  //
191  // only used in testing, seemingly
192  //
193 template<class M>
195 {
196  if (!typed)
197  throw ShapeShifterException("Tried to instantiate message from an untyped shapeshifter.");
198 
199  if (ros::message_traits::datatype<M>() != getDataType())
200  throw ShapeShifterException("Tried to instantiate message without matching datatype.");
201 
202  if (ros::message_traits::md5sum<M>() != getMD5Sum())
203  throw ShapeShifterException("Tried to instantiate message without matching md5sum.");
204 
205  boost::shared_ptr<M> p(boost::make_shared<M>());
206 
207  // The IStream never modifies its data, and nothing else has access to this
208  // object, so the const_cast here is ok
209  ros::serialization::IStream s(const_cast<unsigned char*>(msgBuf.data()),
210  msgBuf.size());
212 
213  return p;
214 }
215 
216 template<typename Stream>
217 void ShapeShifter::write(Stream& stream) const {
218  if (msgBuf.size() > 0)
219  memcpy(stream.advance(msgBuf.size()), msgBuf.data(), msgBuf.size());
220 }
221 
222 template<typename Stream>
223 void ShapeShifter::read(Stream& stream)
224 {
225  stream.getLength();
226  stream.getData();
227 
228  // stash this message in our buffer
229  msgBuf.resize(stream.getLength());
230  memcpy(msgBuf.data(), stream.getData(), stream.getLength());
231 }
232 
233 } // namespace topic_tools
234 
235 
236 #endif
ros::message_traits::Definition< topic_tools::ShapeShifter >::value
static const char * value(const topic_tools::ShapeShifter &m)
Definition: shape_shifter.h:136
ros::message_traits::TrueType
topic_tools::ShapeShifter::getDataType
std::string const & getDataType() const
Definition: shape_shifter.cpp:45
ros::serialization::PreDeserializeParams
ros::Publisher
ros::serialization::deserialize
void deserialize(Stream &stream, boost::array< T, N > &t)
boost::shared_ptr
s
XmlRpcServer s
ros
ros.h
ros::serialization::Stream
ros::message_traits::DataType
macros.h
ros::message_traits::MD5Sum< topic_tools::ShapeShifter >::value
static const char * value()
Definition: shape_shifter.h:121
ros::Exception
ros::serialization::IStream
ros::SubscriberStatusCallback
boost::function< void(const SingleSubscriberPublisher &)> SubscriberStatusCallback
ros::message_traits::DataType< topic_tools::ShapeShifter >::value
static const char * value(const topic_tools::ShapeShifter &m)
Definition: shape_shifter.h:127
ros::serialization::PreDeserialize
topic_tools::ShapeShifter::read
void read(Stream &stream)
Definition: shape_shifter.h:223
console.h
topic_tools::ShapeShifter::instantiate
boost::shared_ptr< M > instantiate() const
Call to try instantiating as a particular type.
Definition: shape_shifter.h:194
ros::message_traits::DataType< topic_tools::ShapeShifter >::value
static const char * value()
Definition: shape_shifter.h:130
message_traits.h
topic_tools::ShapeShifter::write
void write(Stream &stream) const
Write serialized message contents out to a stream.
Definition: shape_shifter.h:217
topic_tools::ShapeShifterException
Definition: shape_shifter.h:83
topic_tools::ShapeShifter::typed
bool typed
Definition: shape_shifter.h:132
ros::serialization::Serializer< topic_tools::ShapeShifter >::write
static void write(Stream &stream, const topic_tools::ShapeShifter &m)
Definition: shape_shifter.h:149
ros::serialization::PreDeserializeParams::message
boost::shared_ptr< M > message
ros::serialization::Serializer< topic_tools::ShapeShifter >::read
static void read(Stream &stream, topic_tools::ShapeShifter &m)
Definition: shape_shifter.h:154
ros::message_traits::MD5Sum
topic_tools::ShapeShifter::getMD5Sum
std::string const & getMD5Sum() const
Definition: shape_shifter.cpp:48
topic_tools::ShapeShifterException::ShapeShifterException
ShapeShifterException(const std::string &msg)
Definition: shape_shifter.h:118
ros::serialization::Serializer< topic_tools::ShapeShifter >::serializedLength
static uint32_t serializedLength(const topic_tools::ShapeShifter &m)
Definition: shape_shifter.h:159
ros::serialization::PreDeserializeParams::connection_header
boost::shared_ptr< std::map< std::string, std::string > > connection_header
ros::message_traits::IsMessage
topic_tools::ShapeShifter::size
uint32_t size() const
Return the size of the serialized message.
Definition: shape_shifter.cpp:74
datatype
const char * datatype()
topic_tools::ShapeShifter::msgBuf
std::vector< uint8_t > msgBuf
Definition: shape_shifter.h:134
TOPIC_TOOLS_DECL
#define TOPIC_TOOLS_DECL
Definition: macros.h:42
topic_tools::ShapeShifter
Definition: shape_shifter.h:91
assert.h
topic_tools
Definition: parse.h:33
ros::message_traits::MD5Sum< topic_tools::ShapeShifter >::value
static const char * value(const topic_tools::ShapeShifter &m)
Definition: shape_shifter.h:118
ros::Exception::Exception
Exception(const std::string &what)
ros::serialization::Serializer
ros::NodeHandle
ros::serialization::PreDeserialize< topic_tools::ShapeShifter >::notify
static void notify(const PreDeserializeParams< topic_tools::ShapeShifter > &params)
Definition: shape_shifter.h:168
topic_tools::ShapeShifter::getMessageDefinition
std::string const & getMessageDefinition() const
Definition: shape_shifter.cpp:51


topic_tools
Author(s): Morgan Quigley, Brian Gerkey, Dirk Thomas , Jacob Perron
autogenerated on Thu Nov 23 2023 04:02:10