shape_shifter.h
Go to the documentation of this file.
00001 //=================================================================================================
00002 // Copyright (c) 2013, Johannes Meyer, TU Darmstadt
00003 // All rights reserved.
00004 
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are met:
00007 //     * Redistributions of source code must retain the above copyright
00008 //       notice, this list of conditions and the following disclaimer.
00009 //     * Redistributions in binary form must reproduce the above copyright
00010 //       notice, this list of conditions and the following disclaimer in the
00011 //       documentation and/or other materials provided with the distribution.
00012 //     * Neither the name of the Flight Systems and Automatic Control group,
00013 //       TU Darmstadt, nor the names of its contributors may be used to
00014 //       endorse or promote products derived from this software without
00015 //       specific prior written permission.
00016 
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00018 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //=================================================================================================
00028 
00029 #ifndef BLOB_SHAPE_SHIFTER_H
00030 #define BLOB_SHAPE_SHIFTER_H
00031 
00032 #include <ros/ros.h>
00033 #include <blob/Blob.h>
00034 
00035 namespace blob {
00036 
00037 struct ShapeShifter
00038 {
00039   typedef ShapeShifter Type;
00040 
00041   ShapeShifter();
00042   ShapeShifter(const Blob& blob);
00043   virtual ~ShapeShifter();
00044 
00045   typedef boost::shared_ptr< ShapeShifter > Ptr;
00046   typedef boost::shared_ptr< ShapeShifter const> ConstPtr;
00047 
00048   // Helpers for inspecting shapeshifter
00049   std::string const& getDataType()          const;
00050   std::string const& getMD5Sum()            const;
00051   std::string const& getMessageDefinition() const;
00052 
00053   ShapeShifter& morph(const std::string& md5sum, const std::string& datatype,
00054              const std::string& msg_def = std::string(), const std::string& latching = std::string());
00055 
00056   // Helper for advertising
00057   ros::Publisher advertise(ros::NodeHandle& nh, const std::string& topic, uint32_t queue_size_, bool latch=false,
00058                            const ros::SubscriberStatusCallback &connect_cb=ros::SubscriberStatusCallback()) const;
00059 
00061   template<class M>
00062   boost::shared_ptr<M> instantiate() const;
00063 
00065   template<typename Stream>
00066   void write(Stream& stream) const;
00067 
00068   template<typename Stream>
00069   void read(Stream& stream);
00070 
00072   uint32_t size() const;
00073 
00074   const Blob& blob() const
00075   {
00076     return blob_;
00077   }
00078 
00079   boost::shared_ptr<std::map<std::string, std::string> > __connection_header;
00080 
00081 private:
00082   std::string md5, datatype, msg_def, latching;
00083   Blob blob_;
00084 };
00085 
00086 typedef boost::shared_ptr<ShapeShifter > ShapeShifterPtr;
00087 typedef boost::shared_ptr<ShapeShifter const> ShapeShifterConstPtr;
00088 
00089 std::ostream& operator<<(std::ostream& s, const ShapeShifter & v)
00090 {
00091   ros::message_operations::Printer< ShapeShifter >::stream(s, "", v);
00092   return s;
00093 }
00094 
00095 template <typename ContainerAllocator>
00096 ShapeShifter Blob_<ContainerAllocator>::asMessage() const
00097 {
00098   return ShapeShifter(*this);
00099 }
00100 
00101 } // namespace blob
00102 
00103 // Message traits allow shape shifter to work with the new serialization API
00104 namespace ros {
00105 namespace message_traits {
00106 
00107 template <> struct IsMessage<blob::ShapeShifter> : TrueType { };
00108 template <> struct IsMessage<const blob::ShapeShifter> : TrueType { };
00109 
00110 template<>
00111 struct MD5Sum<blob::ShapeShifter>
00112 {
00113   static const char* value(const blob::ShapeShifter& m) { return m.getMD5Sum().c_str(); }
00114 
00115   // Used statically, a shapeshifter appears to be of any type
00116   static const char* value() { return "*"; }
00117 };
00118 
00119 template<>
00120 struct DataType<blob::ShapeShifter>
00121 {
00122   static const char* value(const blob::ShapeShifter& m) { return m.getDataType().c_str(); }
00123 
00124   // Used statically, a shapeshifter appears to be of any type
00125   static const char* value() { return "*"; }
00126 };
00127 
00128 template<>
00129 struct Definition<blob::ShapeShifter>
00130 {
00131   static const char* value(const blob::ShapeShifter& m) { return m.getMessageDefinition().c_str(); }
00132 };
00133 
00134 } // namespace message_traits
00135 
00136 
00137 namespace serialization
00138 {
00139 
00140 template<>
00141 struct Serializer<blob::ShapeShifter>
00142 {
00143   template<typename Stream>
00144   inline static void write(Stream& stream, const blob::ShapeShifter& m) {
00145     m.write(stream);
00146   }
00147 
00148   template<typename Stream>
00149   inline static void read(Stream& stream, blob::ShapeShifter& m)
00150   {
00151     m.read(stream);
00152   }
00153 
00154   inline static uint32_t serializedLength(const blob::ShapeShifter& m) {
00155     return m.size();
00156   }
00157 };
00158 
00159 
00160 template<>
00161 struct PreDeserialize<blob::ShapeShifter>
00162 {
00163   static void notify(const PreDeserializeParams<blob::ShapeShifter>& params)
00164   {
00165     std::string md5       = (*params.connection_header)["md5sum"];
00166     std::string datatype  = (*params.connection_header)["type"];
00167     std::string msg_def   = (*params.connection_header)["message_definition"];
00168     std::string latching  = (*params.connection_header)["latching"];
00169 
00170     typedef std::map<std::string, std::string> map_t;
00171     boost::shared_ptr<map_t> shmap(new map_t(*params.connection_header));
00172     params.message->__connection_header = shmap;
00173     params.message->morph(md5, datatype, msg_def, latching);
00174   }
00175 };
00176 
00177 } // namespace serialization
00178 } // namespace ros
00179 
00180 
00181 // Template implementations:
00182 
00183 namespace blob
00184 {
00185 
00186   template<class M>
00187   boost::shared_ptr<M> ShapeShifter::instantiate() const
00188   {
00189     return blob_.instantiate<M>();
00190   }
00191 
00192   template<typename Stream>
00193   void ShapeShifter::write(Stream& stream) const {
00194     if (blob_.empty()) return;
00195     std::copy(blob_.begin(), blob_.end(), stream.advance(blob_.size()));
00196   }
00197 
00198   template<typename Stream>
00199   void ShapeShifter::read(Stream& stream)
00200   {
00201     stream.getLength();
00202     stream.getData();
00203 
00204     Blob::BufferPtr buffer(new Blob::Buffer(stream.getLength()));
00205     std::copy(stream.getData(), stream.getData() + stream.getLength(), buffer->begin());
00206     blob_.set(buffer);
00207   }
00208 
00209 } // namespace blob
00210 
00211 #endif // BLOB_SHAPE_SHIFTER_H


blob
Author(s): Johannes Meyer
autogenerated on Mon Oct 6 2014 07:55:44