00001 //================================================================================================= 00002 // Copyright (c) 2012, 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 CPP_INTROSPECTION_MESSAGE_TEMPLATE_H 00030 #define CPP_INTROSPECTION_MESSAGE_TEMPLATE_H 00031 00032 #include <introspection/message.h> 00033 #include <ros/message_traits.h> 00034 00035 #include <cstring> 00036 00037 namespace cpp_introspection { 00038 00039 template <typename T> 00040 class MessageTemplate : public Message { 00041 public: 00042 typedef T MessageType; 00043 00044 virtual ~MessageTemplate() {} 00045 00046 const char* getDataType() const { return ros::message_traits::DataType<MessageType>::value(); } 00047 const char* getMD5Sum() const { return ros::message_traits::MD5Sum<MessageType>::value(); } 00048 const char* getDefinition() const { return ros::message_traits::Definition<MessageType>::value(); } 00049 const std::type_info& getTypeId() const { return typeid(MessageType); } 00050 00051 bool isSimple() const { return ros::message_traits::isSimple<MessageType>(); } 00052 bool isFixedSize() const { return ros::message_traits::isFixedSize<MessageType>(); } 00053 bool hasHeader() const { return ros::message_traits::hasHeader<MessageType>(); } 00054 00055 ::std_msgs::Header* getHeader(const VoidPtr& instance) const; 00056 const ::std_msgs::Header* getHeader(const VoidConstPtr& instance) const; 00057 std::string* getFrameId(const VoidPtr& instance) const; 00058 const std::string* getFrameId(const VoidConstPtr& instance) const; 00059 ros::Time* getTimeStamp(const VoidPtr& instance) const; 00060 const ros::Time* getTimeStamp(const VoidConstPtr& instance) const; 00061 00062 VoidPtr createInstance() const; 00063 void serialize(ros::serialization::OStream& stream, const VoidConstPtr& instance = VoidConstPtr()) const; 00064 ros::SerializedMessage serialize(const VoidConstPtr& instance = VoidConstPtr()) const; 00065 std::size_t serializationLength(const VoidConstPtr& instance = VoidConstPtr()) const; 00066 VoidPtr deserialize(ros::serialization::IStream& stream, const VoidPtr& instance = VoidPtr()) const; 00067 00068 MessagePtr introspect(const VoidPtr& instance) const; 00069 MessagePtr introspect(void *instance) const; 00070 MessagePtr introspect(const VoidConstPtr& instance) const; 00071 MessagePtr introspect(void const *instance) const; 00072 }; 00073 00074 template <typename T> 00075 ::std_msgs::Header* MessageTemplate<T>::getHeader(const VoidPtr& instance) const { 00076 MessageType* x = static_cast<MessageType*>(instance.get()); 00077 if (!x) return 0; 00078 return ros::message_traits::Header<MessageType>::pointer(*x); 00079 } 00080 00081 template <typename T> 00082 const ::std_msgs::Header* MessageTemplate<T>::getHeader(const VoidConstPtr& instance) const { 00083 const MessageType* x = static_cast<const MessageType*>(instance.get()); 00084 if (!x) return 0; 00085 return ros::message_traits::Header<MessageType>::pointer(*x); 00086 } 00087 00088 template <typename T> 00089 std::string* MessageTemplate<T>::getFrameId(const VoidPtr& instance) const { 00090 MessageType* x = static_cast<MessageType*>(instance.get()); 00091 if (!x) return 0; 00092 return ros::message_traits::FrameId<MessageType>::pointer(*x); 00093 } 00094 00095 template <typename T> 00096 const std::string* MessageTemplate<T>::getFrameId(const VoidConstPtr& instance) const { 00097 const MessageType* x = static_cast<const MessageType*>(instance.get()); 00098 if (!x) return 0; 00099 return ros::message_traits::FrameId<MessageType>::pointer(*x); 00100 } 00101 00102 template <typename T> 00103 ros::Time* MessageTemplate<T>::getTimeStamp(const VoidPtr& instance) const { 00104 MessageType* x = static_cast<MessageType*>(instance.get()); 00105 if (!x) return 0; 00106 return ros::message_traits::TimeStamp<MessageType>::pointer(*x); 00107 } 00108 00109 template <typename T> 00110 const ros::Time* MessageTemplate<T>::getTimeStamp(const VoidConstPtr& instance) const { 00111 const MessageType* x = static_cast<const MessageType*>(instance.get()); 00112 if (!x) return 0; 00113 return ros::message_traits::TimeStamp<MessageType>::pointer(*x); 00114 } 00115 00116 template <typename T> 00117 VoidPtr MessageTemplate<T>::createInstance() const 00118 { 00119 return VoidPtr(new MessageType()); 00120 } 00121 00122 template <typename T> 00123 void MessageTemplate<T>::serialize(ros::serialization::OStream& stream, const VoidConstPtr& instance) const 00124 { 00125 const MessageType* x = static_cast<const MessageType*>(instance.get()); 00126 if (!x) return; 00127 ros::serialization::serialize(stream, *x); 00128 } 00129 00130 template <typename T> 00131 ros::SerializedMessage MessageTemplate<T>::serialize(const VoidConstPtr& instance) const 00132 { 00133 const MessageType* x = static_cast<const MessageType*>(instance.get()); 00134 if (!x) return ros::SerializedMessage(); 00135 return ros::serialization::serializeMessage<T>(*x); 00136 } 00137 00138 template <typename T> 00139 std::size_t MessageTemplate<T>::serializationLength(const VoidConstPtr& instance) const 00140 { 00141 const MessageType* x = static_cast<const MessageType*>(instance.get()); 00142 if (!x) return 0; 00143 return ros::serialization::serializationLength(*x); 00144 } 00145 00146 template <typename T> 00147 VoidPtr MessageTemplate<T>::deserialize(ros::serialization::IStream& stream, const VoidPtr& instance) const 00148 { 00149 typename MessageType::Ptr x = boost::shared_static_cast<MessageType>(instance); 00150 if (!x) x = boost::static_pointer_cast<MessageType>(createInstance()); 00151 ros::serialization::deserialize(stream, *x); 00152 return x; 00153 } 00154 00155 } // namespace 00156 00157 #endif // CPP_INTROSPECTION_MESSAGE_TEMPLATE_H