29 #ifndef CPP_INTROSPECTION_FIELD_TRAITS_H 30 #define CPP_INTROSPECTION_FIELD_TRAITS_H 32 #include <boost/type_traits/integral_constant.hpp> 34 #include <boost/array.hpp> 38 namespace field_traits {
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 {
45 static type&
reference(T& instance, std::size_t) {
return instance; }
46 static const type&
reference(
const T& instance, std::size_t) {
return instance; }
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; }
56 static std::size_t
capacity(
const T& x) {
return 1; }
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> > {
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]; }
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(); }
75 static std::size_t
capacity(
const std::vector<T>& x) {
return x.capacity(); }
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> > {
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]; }
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(); }
94 static std::size_t
capacity(
const boost::array<T,N>& x) {
return x.size(); }
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; } \ 122 #endif // CPP_INTROSPECTION_FIELD_TRAITS_H static type & reference(T &instance, std::size_t)
static const char * name()
static std::size_t value(const T &x)
INTROSPECTION_DECLARE_SIMPLE_TRAITS(bool,"bool")
static void resize(boost::array< T, N > &x, size_t new_size, const T &initial=T())
static std::size_t value()
static void resize(std::vector< T > &x, size_t new_size, const T &initial=T())
static bool empty(const std::vector< T > &x)
static const char * name()
static bool empty(const T &x)
static std::size_t value(const std::vector< T > &x)
static const type & reference(const boost::array< T, N > &instance, std::size_t i)
static std::size_t capacity(const std::vector< T > &x)
static std::size_t value(const boost::array< T, N > &x)
static type & reference(std::vector< T > &instance, std::size_t i)
static void resize(T &x, size_t new_size, const T &initial=T())
static const char * value()
static std::size_t capacity()
static std::size_t capacity()
static std::size_t capacity(const T &x)
static const type & reference(const std::vector< T > &instance, std::size_t i)
static bool empty(const boost::array< T, N > &x)
static const type & reference(const T &instance, std::size_t)
static std::size_t capacity(const boost::array< T, N > &x)
static type & reference(boost::array< T, N > &instance, std::size_t i)
static const char * name()
static std::size_t capacity()
static std::size_t value()
static std::size_t value()