Pipe Inpection Robot / Mbed OS capstone_finish

Dependencies:   BufferedSerial motor_sn7544

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers message_traits.h Source File

message_traits.h

00001 /*
00002  * Copyright (C) 2009, Willow Garage, Inc.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *   * Redistributions of source code must retain the above copyright notice,
00007  *     this list of conditions and the following disclaimer.
00008  *   * Redistributions in binary form must reproduce the above copyright
00009  *     notice, this list of conditions and the following disclaimer in the
00010  *     documentation and/or other materials provided with the distribution.
00011  *   * Neither the names of Willow Garage, Inc. nor the names of its
00012  *     contributors may be used to endorse or promote products derived from
00013  *     this software without specific prior written permission.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00025  * POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00028 #ifndef ROSLIB_MESSAGE_TRAITS_H
00029 #define ROSLIB_MESSAGE_TRAITS_H
00030 
00031 #include "message_forward.h"
00032 
00033 #include <ros/time.h>
00034 
00035 #include <string>
00036 #include <boost/utility/enable_if.hpp>
00037 #include <boost/type_traits/remove_const.hpp>
00038 #include <boost/type_traits/remove_reference.hpp>
00039 
00040 namespace std_msgs
00041 {
00042   ROS_DECLARE_MESSAGE(Header)
00043 }
00044 
00045 #define ROS_IMPLEMENT_SIMPLE_TOPIC_TRAITS(msg, md5sum, datatype, definition) \
00046   namespace ros \
00047   { \
00048   namespace message_traits \
00049   { \
00050   template<> struct MD5Sum<msg> \
00051   { \
00052     static const char* value() { return md5sum; } \
00053     static const char* value(const msg&) { return value(); } \
00054   }; \
00055   template<> struct DataType<msg> \
00056   { \
00057     static const char* value() { return datatype; } \
00058     static const char* value(const msg&) { return value(); } \
00059   }; \
00060   template<> struct Definition<msg> \
00061   { \
00062     static const char* value() { return definition; } \
00063     static const char* value(const msg&) { return value(); } \
00064   }; \
00065   } \
00066   }
00067 
00068 
00069 namespace ros
00070 {
00071 namespace message_traits
00072 {
00073 
00074 /**
00075  * \brief Base type for compile-type true/false tests.  Compatible with Boost.MPL.  classes inheriting from this type
00076  * are \b true values.
00077  */
00078 struct TrueType
00079 {
00080   static const bool value = true;
00081   typedef TrueType type;
00082 };
00083 
00084 /**
00085  * \brief Base type for compile-type true/false tests.  Compatible with Boost.MPL.  classes inheriting from this type
00086  * are \b false values.
00087  */
00088 struct FalseType
00089 {
00090   static const bool value = false;
00091   typedef FalseType type;
00092 };
00093 
00094 /**
00095  * \brief A simple datatype is one that can be memcpy'd directly in array form, i.e. it's a POD, fixed-size type and
00096  * sizeof(M) == sum(serializationLength(M:a...))
00097  */
00098 template<typename M> struct IsSimple : public FalseType {};
00099 /**
00100  * \brief A fixed-size datatype is one whose size is constant, i.e. it has no variable-length arrays or strings
00101  */
00102 template<typename M> struct IsFixedSize : public FalseType {};
00103 /**
00104  * \brief HasHeader informs whether or not there is a header that gets serialized as the first thing in the message
00105  */
00106 template<typename M> struct HasHeader : public FalseType {};
00107 
00108 /**
00109  * \brief Am I message or not
00110  */
00111 template<typename M> struct IsMessage : public FalseType {};
00112 
00113 /**
00114  * \brief Specialize to provide the md5sum for a message
00115  */
00116 template<typename M>
00117 struct MD5Sum
00118 {
00119   static const char* value()
00120   {
00121     return M::__s_getMD5Sum().c_str();
00122   }
00123 
00124   static const char* value(const M& m)
00125   {
00126     return m.__getMD5Sum().c_str();
00127   }
00128 };
00129 
00130 /**
00131  * \brief Specialize to provide the datatype for a message
00132  */
00133 template<typename M>
00134 struct DataType
00135 {
00136   static const char* value()
00137   {
00138     return M::__s_getDataType().c_str();
00139   }
00140 
00141   static const char* value(const M& m)
00142   {
00143     return m.__getDataType().c_str();
00144   }
00145 };
00146 
00147 /**
00148  * \brief Specialize to provide the definition for a message
00149  */
00150 template<typename M>
00151 struct Definition
00152 {
00153   static const char* value()
00154   {
00155     return M::__s_getMessageDefinition().c_str();
00156   }
00157 
00158   static const char* value(const M& m)
00159   {
00160     return m.__getMessageDefinition().c_str();
00161   }
00162 };
00163 
00164 /**
00165  * \brief Header trait.  In the default implementation pointer()
00166  * returns &m.header if HasHeader<M>::value is true, otherwise returns NULL
00167  */
00168 template<typename M, typename Enable = void>
00169 struct Header
00170 {
00171   static std_msgs::Header* pointer(M& m) { (void)m; return 0; }
00172   static std_msgs::Header const* pointer(const M& m) { (void)m; return 0; }
00173 };
00174 
00175 template<typename M>
00176 struct Header<M, typename boost::enable_if<HasHeader<M> >::type >
00177 {
00178   static std_msgs::Header* pointer(M& m) { return &m.header; }
00179   static std_msgs::Header const* pointer(const M& m) { return &m.header; }
00180 };
00181 
00182 /**
00183  * \brief FrameId trait.  In the default implementation pointer()
00184  * returns &m.header.frame_id if HasHeader<M>::value is true, otherwise returns NULL.  value()
00185  * does not exist, and causes a compile error
00186  */
00187 template<typename M, typename Enable = void>
00188 struct FrameId
00189 {
00190   static std::string* pointer(M& m) { (void)m; return 0; }
00191   static std::string const* pointer(const M& m) { (void)m; return 0; }
00192 };
00193 
00194 template<typename M>
00195 struct FrameId<M, typename boost::enable_if<HasHeader<M> >::type >
00196 {
00197   static std::string* pointer(M& m) { return &m.header.frame_id; }
00198   static std::string const* pointer(const M& m) { return &m.header.frame_id; }
00199   static std::string value(const M& m) { return m.header.frame_id; }
00200 };
00201 
00202 /**
00203  * \brief TimeStamp trait.  In the default implementation pointer()
00204  * returns &m.header.stamp if HasHeader<M>::value is true, otherwise returns NULL.  value()
00205  * does not exist, and causes a compile error
00206  */
00207 template<typename M, typename Enable = void>
00208 struct TimeStamp
00209 {
00210   static ros::Time* pointer(M& m) { (void)m; return 0; }
00211   static ros::Time const* pointer(const M& m) { (void)m; return 0; }
00212 };
00213 
00214 template<typename M>
00215 struct TimeStamp<M, typename boost::enable_if<HasHeader<M> >::type >
00216 {
00217   static ros::Time* pointer(typename boost::remove_const<M>::type &m) { return &m.header.stamp; }
00218   static ros::Time const* pointer(const M& m) { return &m.header.stamp; }
00219   static ros::Time value(const M& m) { return m.header.stamp; }
00220 };
00221 
00222 /**
00223  * \brief returns MD5Sum<M>::value();
00224  */
00225 template<typename M>
00226 inline const char* md5sum()
00227 {
00228   return MD5Sum<typename boost::remove_reference<typename boost::remove_const<M>::type>::type>::value();
00229 }
00230 
00231 /**
00232  * \brief returns DataType<M>::value();
00233  */
00234 template<typename M>
00235 inline const char* datatype()
00236 {
00237   return DataType<typename boost::remove_reference<typename boost::remove_const<M>::type>::type>::value();
00238 }
00239 
00240 /**
00241  * \brief returns Definition<M>::value();
00242  */
00243 template<typename M>
00244 inline const char* definition()
00245 {
00246   return Definition<typename boost::remove_reference<typename boost::remove_const<M>::type>::type>::value();
00247 }
00248 
00249 /**
00250  * \brief returns MD5Sum<M>::value(m);
00251  */
00252 template<typename M>
00253 inline const char* md5sum(const M& m)
00254 {
00255   return MD5Sum<typename boost::remove_reference<typename boost::remove_const<M>::type>::type>::value(m);
00256 }
00257 
00258 /**
00259  * \brief returns DataType<M>::value(m);
00260  */
00261 template<typename M>
00262 inline const char* datatype(const M& m)
00263 {
00264   return DataType<typename boost::remove_reference<typename boost::remove_const<M>::type>::type>::value(m);
00265 }
00266 
00267 /**
00268  * \brief returns Definition<M>::value(m);
00269  */
00270 template<typename M>
00271 inline const char* definition(const M& m)
00272 {
00273   return Definition<typename boost::remove_reference<typename boost::remove_const<M>::type>::type>::value(m);
00274 }
00275 
00276 /**
00277  * \brief returns Header<M>::pointer(m);
00278  */
00279 template<typename M>
00280 inline std_msgs::Header* header(M& m)
00281 {
00282   return Header<typename boost::remove_reference<typename boost::remove_const<M>::type>::type>::pointer(m);
00283 }
00284 
00285 /**
00286  * \brief returns Header<M>::pointer(m);
00287  */
00288 template<typename M>
00289 inline std_msgs::Header const* header(const M& m)
00290 {
00291   return Header<typename boost::remove_reference<typename boost::remove_const<M>::type>::type>::pointer(m);
00292 }
00293 
00294 /**
00295  * \brief returns FrameId<M>::pointer(m);
00296  */
00297 template<typename M>
00298 inline std::string* frameId(M& m)
00299 {
00300   return FrameId<typename boost::remove_reference<typename boost::remove_const<M>::type>::type>::pointer(m);
00301 }
00302 
00303 /**
00304  * \brief returns FrameId<M>::pointer(m);
00305  */
00306 template<typename M>
00307 inline std::string const* frameId(const M& m)
00308 {
00309   return FrameId<typename boost::remove_reference<typename boost::remove_const<M>::type>::type>::pointer(m);
00310 }
00311 
00312 /**
00313  * \brief returns TimeStamp<M>::pointer(m);
00314  */
00315 template<typename M>
00316 inline ros::Time* timeStamp(M& m)
00317 {
00318   return TimeStamp<typename boost::remove_reference<typename boost::remove_const<M>::type>::type>::pointer(m);
00319 }
00320 
00321 /**
00322  * \brief returns TimeStamp<M>::pointer(m);
00323  */
00324 template<typename M>
00325 inline ros::Time const* timeStamp(const M& m)
00326 {
00327   return TimeStamp<typename boost::remove_reference<typename boost::remove_const<M>::type>::type>::pointer(m);
00328 }
00329 
00330 /**
00331  * \brief returns IsSimple<M>::value;
00332  */
00333 template<typename M>
00334 inline bool isSimple()
00335 {
00336   return IsSimple<typename boost::remove_reference<typename boost::remove_const<M>::type>::type>::value;
00337 }
00338 
00339 /**
00340  * \brief returns IsFixedSize<M>::value;
00341  */
00342 template<typename M>
00343 inline bool isFixedSize()
00344 {
00345   return IsFixedSize<typename boost::remove_reference<typename boost::remove_const<M>::type>::type>::value;
00346 }
00347 
00348 /**
00349  * \brief returns HasHeader<M>::value;
00350  */
00351 template<typename M>
00352 inline bool hasHeader()
00353 {
00354   return HasHeader<typename boost::remove_reference<typename boost::remove_const<M>::type>::type>::value;
00355 }
00356 
00357 } // namespace message_traits
00358 } // namespace ros
00359 
00360 #endif // ROSLIB_MESSAGE_TRAITS_H