shape_shifter.h
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2013, 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 BLOB_SHAPE_SHIFTER_H
30 #define BLOB_SHAPE_SHIFTER_H
31 
32 #include <ros/ros.h>
33 #include <blob/Blob.h>
34 
35 namespace blob {
36 
38 {
39  typedef ShapeShifter Type;
40 
41  ShapeShifter();
42  ShapeShifter(const Blob& blob);
43  virtual ~ShapeShifter();
44 
47 
48  // Helpers for inspecting shapeshifter
49  std::string const& getDataType() const;
50  std::string const& getMD5Sum() const;
51  std::string const& getMessageDefinition() const;
52 
53  ShapeShifter& morph(const std::string& md5sum, const std::string& datatype,
54  const std::string& msg_def = std::string(), const std::string& latching = std::string());
55 
56  // Helper for advertising
57  ros::Publisher advertise(ros::NodeHandle& nh, const std::string& topic, uint32_t queue_size_, bool latch=false,
59 
61  template<class M>
63 
65  template<typename Stream>
66  void write(Stream& stream) const;
67 
68  template<typename Stream>
69  void read(Stream& stream);
70 
72  uint32_t size() const;
73 
74  const Blob& blob() const
75  {
76  return blob_;
77  }
78 
80 
81 private:
82  std::string md5, datatype, msg_def, latching;
84 };
85 
88 
89 std::ostream& operator<<(std::ostream& s, const ShapeShifter & v)
90 {
92  return s;
93 }
94 
95 template <typename ContainerAllocator>
97 {
98  return ShapeShifter(*this);
99 }
100 
101 } // namespace blob
102 
103 // Message traits allow shape shifter to work with the new serialization API
104 namespace ros {
105 namespace message_traits {
106 
107 template <> struct IsMessage<blob::ShapeShifter> : TrueType { };
108 template <> struct IsMessage<const blob::ShapeShifter> : TrueType { };
109 
110 template<>
111 struct MD5Sum<blob::ShapeShifter>
112 {
113  static const char* value(const blob::ShapeShifter& m) { return m.getMD5Sum().c_str(); }
114 
115  // Used statically, a shapeshifter appears to be of any type
116  static const char* value() { return "*"; }
117 };
118 
119 template<>
120 struct DataType<blob::ShapeShifter>
121 {
122  static const char* value(const blob::ShapeShifter& m) { return m.getDataType().c_str(); }
123 
124  // Used statically, a shapeshifter appears to be of any type
125  static const char* value() { return "*"; }
126 };
127 
128 template<>
130 {
131  static const char* value(const blob::ShapeShifter& m) { return m.getMessageDefinition().c_str(); }
132 };
133 
134 } // namespace message_traits
135 
136 
137 namespace serialization
138 {
139 
140 template<>
142 {
143  template<typename Stream>
144  inline static void write(Stream& stream, const blob::ShapeShifter& m) {
145  m.write(stream);
146  }
147 
148  template<typename Stream>
149  inline static void read(Stream& stream, blob::ShapeShifter& m)
150  {
151  m.read(stream);
152  }
153 
154  inline static uint32_t serializedLength(const blob::ShapeShifter& m) {
155  return m.size();
156  }
157 };
158 
159 
160 template<>
162 {
164  {
165  std::string md5 = (*params.connection_header)["md5sum"];
166  std::string datatype = (*params.connection_header)["type"];
167  std::string msg_def = (*params.connection_header)["message_definition"];
168  std::string latching = (*params.connection_header)["latching"];
169 
170  typedef std::map<std::string, std::string> map_t;
171  boost::shared_ptr<map_t> shmap(new map_t(*params.connection_header));
172  params.message->__connection_header = shmap;
173  params.message->morph(md5, datatype, msg_def, latching);
174  }
175 };
176 
177 } // namespace serialization
178 } // namespace ros
179 
180 
181 // Template implementations:
182 
183 namespace blob
184 {
185 
186  template<class M>
188  {
189  return blob_.instantiate<M>();
190  }
191 
192  template<typename Stream>
193  void ShapeShifter::write(Stream& stream) const {
194  if (blob_.empty()) return;
195  std::copy(blob_.begin(), blob_.end(), stream.advance(blob_.size()));
196  }
197 
198  template<typename Stream>
199  void ShapeShifter::read(Stream& stream)
200  {
201  stream.getLength();
202  stream.getData();
203 
204  Blob::BufferPtr buffer(new Blob::Buffer(stream.getLength()));
205  std::copy(stream.getData(), stream.getData() + stream.getLength(), buffer->begin());
206  blob_.set(buffer);
207  }
208 
209 } // namespace blob
210 
211 #endif // BLOB_SHAPE_SHIFTER_H
static void notify(const PreDeserializeParams< blob::ShapeShifter > &params)
boost::function< void(const SingleSubscriberPublisher &)> SubscriberStatusCallback
std::string latching
Definition: shape_shifter.h:82
static const char * value(const blob::ShapeShifter &m)
boost::shared_ptr< std::map< std::string, std::string > > __connection_header
Definition: shape_shifter.h:79
std::string msg_def
Definition: shape_shifter.h:82
bool empty() const
Definition: Blob.h:108
static const char * value(const blob::ShapeShifter &m)
static const char * value(const blob::ShapeShifter &m)
std::string const & getMD5Sum() const
static void write(Stream &stream, const blob::ShapeShifter &m)
static void read(Stream &stream, blob::ShapeShifter &m)
std::string const & getDataType() const
boost::shared_ptr< ShapeShifter const > ShapeShifterConstPtr
Definition: shape_shifter.h:87
uint32_t size() const
Return the size of the serialized message.
boost::shared_ptr< ShapeShifter const > ConstPtr
Definition: shape_shifter.h:46
boost::shared_ptr< ShapeShifter > Ptr
Definition: shape_shifter.h:45
void write(Stream &stream) const
Write serialized message contents out to a stream.
std::vector< value_type > Buffer
Definition: Blob.h:54
boost::shared_ptr< M > instantiate() const
Definition: Blob.h:210
static void stream(Stream &s, const std::string &indent, const M &value)
std::ostream & operator<<(std::ostream &s, const ::blob::Blob_< ContainerAllocator > &v)
Definition: Blob.h:288
ShapeShifter & morph(const std::string &md5sum, const std::string &datatype, const std::string &msg_def=std::string(), const std::string &latching=std::string())
static uint32_t serializedLength(const blob::ShapeShifter &m)
const value_type * end() const
Definition: Blob.h:94
std::string datatype
Definition: shape_shifter.h:82
void set(const void *data, size_type size)
Definition: Blob.h:113
size_type size() const
Definition: Blob.h:91
Definition: Blob.h:43
const Blob & blob() const
Definition: shape_shifter.h:74
boost::shared_ptr< ShapeShifter > ShapeShifterPtr
Definition: shape_shifter.h:86
const value_type * begin() const
Definition: Blob.h:93
ShapeShifter asMessage() const
Definition: shape_shifter.h:96
boost::shared_ptr< M > instantiate() const
Call to try instantiating as a particular type.
std::string const & getMessageDefinition() const
ShapeShifter Type
Definition: shape_shifter.h:39
boost::shared_ptr< std::map< std::string, std::string > > connection_header
void read(Stream &stream)
ros::Publisher advertise(ros::NodeHandle &nh, const std::string &topic, uint32_t queue_size_, bool latch=false, const ros::SubscriberStatusCallback &connect_cb=ros::SubscriberStatusCallback()) const


blob
Author(s): Johannes Meyer
autogenerated on Sat Jul 27 2019 03:35:24