field_traits.h
Go to the documentation of this file.
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_FIELD_TRAITS_H
00030 #define CPP_INTROSPECTION_FIELD_TRAITS_H
00031 
00032 #include <boost/type_traits/integral_constant.hpp>
00033 #include <vector>
00034 #include <boost/array.hpp>
00035 #include <ros/message_traits.h>
00036 
00037 namespace cpp_introspection {
00038 namespace field_traits {
00039 
00040   template <typename T> struct is_array : public boost::false_type {};
00041   template <typename T> struct is_vector : public boost::false_type {};
00042   template <typename T> struct is_container : public boost::false_type {};
00043   template <typename T> struct value {
00044     typedef T type;
00045     static type& reference(T& instance, std::size_t) { return instance; }
00046     static const type& reference(const T& instance, std::size_t) { return instance; }
00047     static const char *name() { return ros::message_traits::DataType<T>::value(); }
00048   };
00049   template <typename T> struct size {
00050     static std::size_t value() { return 1; }
00051     static std::size_t value(const T& x) { return 1; }
00052     static void resize(T& x, size_t new_size, const T& initial = T()) {}
00053     static bool empty() { return false; }
00054     static bool empty(const T& x) { return false; }
00055     static std::size_t capacity() { return 1; }
00056     static std::size_t capacity(const T& x) { return 1; }
00057   };
00058 
00059   // specializations for std::vector
00060   template <typename T> struct is_vector< std::vector<T> > : public boost::true_type {};
00061   template <typename T> struct is_container< std::vector<T> > : public boost::true_type {};
00062   template <typename T> struct value< std::vector<T> > {
00063     typedef T type;
00064     static type& reference(std::vector<T>& instance, std::size_t i) { return instance[i]; }
00065     static const type& reference(const std::vector<T>& instance, std::size_t i) { return instance[i]; }
00066     static const char *name() { return value<T>::name(); }
00067   };
00068   template <typename T> struct size< std::vector<T> > {
00069     static std::size_t value() { return 0; }
00070     static std::size_t value(const std::vector<T>& x) { return x.size(); }
00071     static void resize(std::vector<T>& x, size_t new_size, const T& initial = T()) { x.resize(new_size, initial); }
00072     static bool empty() { return true; }
00073     static bool empty(const std::vector<T>& x) { return x.empty(); }
00074     static std::size_t capacity() { return 0; }
00075     static std::size_t capacity(const std::vector<T>& x) { return x.capacity(); }
00076   };
00077 
00078   // specializations for boost::array
00079   template <typename T, std::size_t N> struct is_array< boost::array<T,N> > : public boost::true_type {};
00080   template <typename T, std::size_t N> struct is_container< boost::array<T,N> > : public boost::true_type {};
00081   template <typename T, std::size_t N> struct value< boost::array<T,N> > {
00082     typedef T type;
00083     static type& reference(boost::array<T,N>& instance, std::size_t i) { return instance[i]; }
00084     static const type& reference(const boost::array<T,N>& instance, std::size_t i) { return instance[i]; }
00085     static const char *name() { return value<T>::name(); }
00086   };
00087   template <typename T, std::size_t N> struct size< boost::array<T,N> > {
00088     static std::size_t value() { return boost::array<T,N>::size(); }
00089     static std::size_t value(const boost::array<T,N>& x) { return x.size(); }
00090     static void resize(boost::array<T,N>& x, size_t new_size, const T& initial = T()) {}
00091     static bool empty() { return boost::array<T,N>::empty(); }
00092     static bool empty(const boost::array<T,N>& x) { return x.empty(); }
00093     static std::size_t capacity() { return N; }
00094     static std::size_t capacity(const boost::array<T,N>& x) { return x.size(); }
00095   };
00096 
00097   // simple type traits
00098 #define INTROSPECTION_DECLARE_SIMPLE_TRAITS(_type, _name) \
00099   template <> struct value< _type > { \
00100     typedef _type type; \
00101     static type& reference(_type& instance, std::size_t) { return instance; } \
00102     static const type& reference(const _type& instance, std::size_t) { return instance; } \
00103     static const char *name() { return _name; } \
00104   }
00105 
00106   INTROSPECTION_DECLARE_SIMPLE_TRAITS(bool, "bool");
00107   INTROSPECTION_DECLARE_SIMPLE_TRAITS(uint8_t, "uint8");
00108   INTROSPECTION_DECLARE_SIMPLE_TRAITS(int8_t, "int8");
00109   INTROSPECTION_DECLARE_SIMPLE_TRAITS(uint16_t, "uint8");
00110   INTROSPECTION_DECLARE_SIMPLE_TRAITS(int16_t, "int16");
00111   INTROSPECTION_DECLARE_SIMPLE_TRAITS(uint32_t, "uint32");
00112   INTROSPECTION_DECLARE_SIMPLE_TRAITS(int32_t, "int32");
00113   INTROSPECTION_DECLARE_SIMPLE_TRAITS(uint64_t, "uint64");
00114   INTROSPECTION_DECLARE_SIMPLE_TRAITS(int64_t, "int64");
00115   INTROSPECTION_DECLARE_SIMPLE_TRAITS(float, "float32");
00116   INTROSPECTION_DECLARE_SIMPLE_TRAITS(double, "float64");
00117   INTROSPECTION_DECLARE_SIMPLE_TRAITS(ros::Time, "time");
00118   INTROSPECTION_DECLARE_SIMPLE_TRAITS(ros::Duration, "duration");
00119 
00120 }} // namespace
00121 
00122 #endif // CPP_INTROSPECTION_FIELD_TRAITS_H


cpp_introspection
Author(s): Johannes Meyer
autogenerated on Sun Oct 5 2014 23:17:53