cgv
reflection_traits.h
1 #pragma once
2 
3 #include <string>
4 #include <cgv/type/info/type_name.h>
5 #include <cgv/type/info/type_id.h>
6 #include <cgv/type/cond/is_enum.h>
7 #include <cgv/type/cond/is_abstract.h>
9 
10 #include "lib_begin.h"
11 
13 namespace cgv {
15  namespace reflect {
16 
17 class CGV_API reflection_handler;
18 
20 enum ReflectionTraitsKind { RTK_STD_TYPE, RTK_SELF_REFLECT, RTK_EXTERNAL_SELF_REFLECT, RTK_STRING };
21 
23 struct CGV_API abst_reflection_traits
24 {
26  virtual ~abst_reflection_traits();
28  virtual abst_reflection_traits* clone() = 0;
29 
32  virtual unsigned size() const = 0;
35  virtual void* new_instance() const = 0;
37  virtual void delete_instance(void*) const = 0;
39  virtual void* new_instances(unsigned n) const = 0;
41  virtual void delete_instances(void*) const = 0;
43  virtual cgv::type::info::TypeId get_type_id() const = 0;
45  virtual const char* get_type_name() const = 0;
47 
50  static const bool has_external = false;
53  virtual bool has_external_implementation() const;
55  virtual bool external_implementation(reflection_handler& rh, void* member_ptr);
57 
60  static const bool has_string = false;
63  virtual bool has_string_conversions() const;
65  virtual bool set_from_string(void* instance_ptr, const std::string& str_val);
67  virtual void get_to_string(const void* instance_ptr, std::string& str_val);
69 
70 
73  static const bool has_enum = false;
76  virtual bool is_enum_type() const;
78  virtual bool has_enum_interface() const;
80  virtual unsigned get_nr_enum_items() const;
82  virtual std::string get_enum_name(unsigned i) const;
84  virtual int get_enum_value(unsigned i) const;
86 };
87 
92 template <typename T, typename B, bool base_is_abst = cgv::type::cond::is_abstract<B>::value>
94 struct reflection_traits_impl : public B
95 {
97  unsigned size() const { return sizeof(T); }
99  void* new_instance() const { return new T(); }
101  void delete_instance(void* instance_ptr) const { delete static_cast<T*>(instance_ptr); }
103  void* new_instances(unsigned n) const { return new T[n]; }
105  void delete_instances(void* instance_array) const { delete [] static_cast<T*>(instance_array); }
109  const char* get_type_name() const { return cgv::type::info::type_name<T>::get_name(); }
112 };
113 
115 template <typename T, typename B>
116 struct reflection_traits_impl<T,B,true> : public B
117 {
119  unsigned size() const { return sizeof(T); }
121  void* new_instance() const { return 0; }
123  void delete_instance(void* instance_ptr) const { delete static_cast<T*>(instance_ptr); }
125  void* new_instances(unsigned n) const { return 0; }
127  void delete_instances(void* instance_array) const { delete [] static_cast<T*>(instance_array); }
131  const char* get_type_name() const { return cgv::type::info::type_name<T>::get_name(); }
134 };
135 
137 template <bool has_str, typename T, typename B>
139 {
140 };
141 
143 template <typename T, typename B>
145 {
147  static const bool has_string = true;
148 
149  bool has_string_conversions() const { return true; }
150  bool set_from_string(void* member_ptr, const std::string& str_val) {
151  return cgv::utils::from_string(*static_cast<T*>(member_ptr), str_val);
152  }
153  void get_to_string(const void* member_ptr, std::string& str_val) {
154  str_val = cgv::utils::to_string(*static_cast<const T*>(member_ptr));
155  }
156 };
157 
159 template <typename T, ReflectionTraitsKind KIND = RTK_STRING, bool has_str = true>
160 struct reflection_traits : public reflection_traits_string_impl<has_str, T, abst_reflection_traits>
161 {
162  static const ReflectionTraitsKind kind = KIND;
165 };
166 
168 
170 extern CGV_API reflection_traits<bool,RTK_STD_TYPE> get_reflection_traits(const bool&);
171 
174  }
175 }
176 
177 
178 #include <cgv/config/lib_end.h>
cgv::reflect::abst_reflection_traits::clone
virtual abst_reflection_traits * clone()=0
clone function
cgv::reflect::abst_reflection_traits::delete_instance
virtual void delete_instance(void *) const =0
delete an instance with the delete operator
cgv::reflect::abst_reflection_traits::delete_instances
virtual void delete_instances(void *) const =0
delete instances with the delete [] operator
cgv::type::info::TypeId
TypeId
ids for the different types and type constructs
Definition: type_id.h:12
cgv::reflect::reflection_traits_impl::get_type_id
cgv::type::info::TypeId get_type_id() const
return the type id
Definition: reflection_traits.h:107
cgv::reflect::reflection_traits_impl::size
unsigned size() const
return the size of the type
Definition: reflection_traits.h:97
cgv::reflect::reflection_traits_impl::delete_instances
void delete_instances(void *instance_array) const
delete instances with the delete [] operator
Definition: reflection_traits.h:105
cgv::reflect::abst_reflection_traits
abstract interface for type reflection with basic type management and optional string conversion
Definition: reflection_traits.h:24
cgv::reflect::reflection_traits_impl< T, B, true >::new_instance
void * new_instance() const
construct an instance on the heap with the new operator
Definition: reflection_traits.h:121
cgv::reflect::reflection_traits_impl::delete_instance
void delete_instance(void *instance_ptr) const
delete an instance with the delete operator
Definition: reflection_traits.h:101
cgv::reflect::reflection_traits::clone
abst_reflection_traits * clone()
clone function
Definition: reflection_traits.h:164
cgv::reflect::reflection_traits_impl
implementation of the reflection traits providing type specific interface for variable base class
Definition: reflection_traits.h:95
cgv::reflect::reflection_traits_impl< T, B, true >::is_enum_type
bool is_enum_type() const
return whether type is an enum type - this is independent of whether enum interface is implemented
Definition: reflection_traits.h:133
cgv::reflect::abst_reflection_traits::get_type_id
virtual cgv::type::info::TypeId get_type_id() const =0
return the type id
cgv::reflect::reflection_traits_impl< T, B, true >::get_type_id
cgv::type::info::TypeId get_type_id() const
return the type id
Definition: reflection_traits.h:129
cgv::reflect::reflection_traits_impl< T, B, true >::new_instances
void * new_instances(unsigned n) const
construct n instances on the heap with the new operator
Definition: reflection_traits.h:125
cgv::type::info::type_name::get_name
static const char * get_name()
return special name for standard types or type name from RTTI cleaned from keywords for all other typ...
Definition: type_name.h:56
cgv::reflect::reflection_traits_impl< T, B, true >::get_type_name
const char * get_type_name() const
return the type name
Definition: reflection_traits.h:131
cgv::utils::from_string
bool from_string(std::string &v, const std::string &s)
specialization to extract string value from string
Definition: convert_string.cxx:13
cgv::reflect::reflection_traits_impl::new_instances
void * new_instances(unsigned n) const
construct n instances on the heap with the new operator
Definition: reflection_traits.h:103
cgv::reflect::ReflectionTraitsKind
ReflectionTraitsKind
different types of reflection traits
Definition: reflection_traits.h:20
cgv::reflect::reflection_traits_impl::get_type_name
const char * get_type_name() const
return the type name
Definition: reflection_traits.h:109
cgv::reflect::reflection_traits_impl< T, B, true >::delete_instances
void delete_instances(void *instance_array) const
delete instances with the delete [] operator
Definition: reflection_traits.h:127
cgv::reflect::reflection_traits_impl::new_instance
void * new_instance() const
construct an instance on the heap with the new operator
Definition: reflection_traits.h:99
cgv::reflect::abst_reflection_traits::new_instances
virtual void * new_instances(unsigned n) const =0
construct n instances on the heap with the new operator
cgv::reflect::reflection_handler
Definition: reflection_handler.h:63
cgv::reflect::reflection_traits_impl< T, B, true >::delete_instance
void delete_instance(void *instance_ptr) const
delete an instance with the delete operator
Definition: reflection_traits.h:123
cgv::reflect::reflection_traits
Default implementation of the reflection traits providing type specific interface.
Definition: reflection_traits.h:161
cgv::type::info::type_id
template with a static member function get_id() of type TypeId returning the TypeId of the template a...
Definition: type_id.h:86
cgv::type::cond::is_enum
template condition returning, whether the given type is an enum type
Definition: is_enum.h:14
cgv::math::reflect
vec< T > reflect(const vec< T > &v, const vec< T > &n)
calculates the reflected direction of v; n is the normal of the reflecting surface
Definition: vec.h:822
convert_string.h
cgv::reflect::abst_reflection_traits::new_instance
virtual void * new_instance() const =0
construct an instance on the heap with the new operator
cgv::reflect::reflection_traits_string_impl
this template allows to distinguish between traits with and without string conversions
Definition: reflection_traits.h:139
cgv::reflect::reflection_traits_impl< T, B, true >::size
unsigned size() const
return the size of the type
Definition: reflection_traits.h:119
cgv::reflect::reflection_traits_impl::is_enum_type
bool is_enum_type() const
return whether type is an enum type - this is independent of whether enum interface is implemented
Definition: reflection_traits.h:111
cgv::utils::to_string
std::string to_string(const std::string &v, unsigned int w, unsigned int p)
specialization of conversion from string to strings
Definition: convert_string.cxx:7
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::reflect::abst_reflection_traits::get_type_name
virtual const char * get_type_name() const =0
return the type name