field_traits.h
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2012, Johannes Meyer, TU Darmstadt
3 // All rights reserved.
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Flight Systems and Automatic Control group,
13 // TU Darmstadt, nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without
15 // specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //=================================================================================================
28 
29 #ifndef CPP_INTROSPECTION_FIELD_TRAITS_H
30 #define CPP_INTROSPECTION_FIELD_TRAITS_H
31 
32 #include <boost/type_traits/integral_constant.hpp>
33 #include <vector>
34 #include <boost/array.hpp>
35 #include <ros/message_traits.h>
36 
37 namespace cpp_introspection {
38 namespace field_traits {
39 
40  template <typename T> struct is_array : public boost::false_type {};
41  template <typename T> struct is_vector : public boost::false_type {};
42  template <typename T> struct is_container : public boost::false_type {};
43  template <typename T> struct value {
44  typedef T type;
45  static type& reference(T& instance, std::size_t) { return instance; }
46  static const type& reference(const T& instance, std::size_t) { return instance; }
47  static const char *name() { return ros::message_traits::DataType<T>::value(); }
48  };
49  template <typename T> struct size {
50  static std::size_t value() { return 1; }
51  static std::size_t value(const T& x) { return 1; }
52  static void resize(T& x, size_t new_size, const T& initial = T()) {}
53  static bool empty() { return false; }
54  static bool empty(const T& x) { return false; }
55  static std::size_t capacity() { return 1; }
56  static std::size_t capacity(const T& x) { return 1; }
57  };
58 
59  // specializations for std::vector
60  template <typename T> struct is_vector< std::vector<T> > : public boost::true_type {};
61  template <typename T> struct is_container< std::vector<T> > : public boost::true_type {};
62  template <typename T> struct value< std::vector<T> > {
63  typedef T type;
64  static type& reference(std::vector<T>& instance, std::size_t i) { return instance[i]; }
65  static const type& reference(const std::vector<T>& instance, std::size_t i) { return instance[i]; }
66  static const char *name() { return value<T>::name(); }
67  };
68  template <typename T> struct size< std::vector<T> > {
69  static std::size_t value() { return 0; }
70  static std::size_t value(const std::vector<T>& x) { return x.size(); }
71  static void resize(std::vector<T>& x, size_t new_size, const T& initial = T()) { x.resize(new_size, initial); }
72  static bool empty() { return true; }
73  static bool empty(const std::vector<T>& x) { return x.empty(); }
74  static std::size_t capacity() { return 0; }
75  static std::size_t capacity(const std::vector<T>& x) { return x.capacity(); }
76  };
77 
78  // specializations for boost::array
79  template <typename T, std::size_t N> struct is_array< boost::array<T,N> > : public boost::true_type {};
80  template <typename T, std::size_t N> struct is_container< boost::array<T,N> > : public boost::true_type {};
81  template <typename T, std::size_t N> struct value< boost::array<T,N> > {
82  typedef T type;
83  static type& reference(boost::array<T,N>& instance, std::size_t i) { return instance[i]; }
84  static const type& reference(const boost::array<T,N>& instance, std::size_t i) { return instance[i]; }
85  static const char *name() { return value<T>::name(); }
86  };
87  template <typename T, std::size_t N> struct size< boost::array<T,N> > {
88  static std::size_t value() { return boost::array<T,N>::size(); }
89  static std::size_t value(const boost::array<T,N>& x) { return x.size(); }
90  static void resize(boost::array<T,N>& x, size_t new_size, const T& initial = T()) {}
91  static bool empty() { return boost::array<T,N>::empty(); }
92  static bool empty(const boost::array<T,N>& x) { return x.empty(); }
93  static std::size_t capacity() { return N; }
94  static std::size_t capacity(const boost::array<T,N>& x) { return x.size(); }
95  };
96 
97  // simple type traits
98 #define INTROSPECTION_DECLARE_SIMPLE_TRAITS(_type, _name) \
99  template <> struct value< _type > { \
100  typedef _type type; \
101  static type& reference(_type& instance, std::size_t) { return instance; } \
102  static const type& reference(const _type& instance, std::size_t) { return instance; } \
103  static const char *name() { return _name; } \
104  }
105 
107  INTROSPECTION_DECLARE_SIMPLE_TRAITS(uint8_t, "uint8");
108  INTROSPECTION_DECLARE_SIMPLE_TRAITS(int8_t, "int8");
109  INTROSPECTION_DECLARE_SIMPLE_TRAITS(uint16_t, "uint8");
110  INTROSPECTION_DECLARE_SIMPLE_TRAITS(int16_t, "int16");
111  INTROSPECTION_DECLARE_SIMPLE_TRAITS(uint32_t, "uint32");
112  INTROSPECTION_DECLARE_SIMPLE_TRAITS(int32_t, "int32");
113  INTROSPECTION_DECLARE_SIMPLE_TRAITS(uint64_t, "uint64");
114  INTROSPECTION_DECLARE_SIMPLE_TRAITS(int64_t, "int64");
115  INTROSPECTION_DECLARE_SIMPLE_TRAITS(float, "float32");
116  INTROSPECTION_DECLARE_SIMPLE_TRAITS(double, "float64");
119 
120 }} // namespace
121 
122 #endif // CPP_INTROSPECTION_FIELD_TRAITS_H
static type & reference(T &instance, std::size_t)
Definition: field_traits.h:45
static std::size_t value(const T &x)
Definition: field_traits.h:51
INTROSPECTION_DECLARE_SIMPLE_TRAITS(bool,"bool")
static void resize(boost::array< T, N > &x, size_t new_size, const T &initial=T())
Definition: field_traits.h:90
static void resize(std::vector< T > &x, size_t new_size, const T &initial=T())
Definition: field_traits.h:71
static bool empty(const std::vector< T > &x)
Definition: field_traits.h:73
static bool empty(const T &x)
Definition: field_traits.h:54
static std::size_t value(const std::vector< T > &x)
Definition: field_traits.h:70
static const type & reference(const boost::array< T, N > &instance, std::size_t i)
Definition: field_traits.h:84
static std::size_t capacity(const std::vector< T > &x)
Definition: field_traits.h:75
static std::size_t value(const boost::array< T, N > &x)
Definition: field_traits.h:89
static type & reference(std::vector< T > &instance, std::size_t i)
Definition: field_traits.h:64
static void resize(T &x, size_t new_size, const T &initial=T())
Definition: field_traits.h:52
static const char * value()
static std::size_t capacity(const T &x)
Definition: field_traits.h:56
static const type & reference(const std::vector< T > &instance, std::size_t i)
Definition: field_traits.h:65
static bool empty(const boost::array< T, N > &x)
Definition: field_traits.h:92
static const type & reference(const T &instance, std::size_t)
Definition: field_traits.h:46
static std::size_t capacity(const boost::array< T, N > &x)
Definition: field_traits.h:94
static type & reference(boost::array< T, N > &instance, std::size_t i)
Definition: field_traits.h:83


cpp_introspection
Author(s): Johannes Meyer
autogenerated on Mon Jun 10 2019 12:56:17