00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
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
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
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 }}
00121
00122 #endif // CPP_INTROSPECTION_FIELD_TRAITS_H