cgv
Loading...
Searching...
No Matches
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
13namespace cgv {
15 namespace reflect {
16
17class CGV_API reflection_handler;
18
20enum ReflectionTraitsKind { RTK_STD_TYPE, RTK_SELF_REFLECT, RTK_EXTERNAL_SELF_REFLECT, RTK_STRING };
21
24{
29
33 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;
45 virtual const char* get_type_name() const = 0;
47
51 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
61 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
74 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
93template <typename T, typename B, bool base_is_abst = cgv::type::cond::is_abstract<B>::value>
94struct 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); }
112};
113
115template <typename T, typename B>
116struct 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); }
134};
135
137template <bool has_str, typename T, typename B>
141
143template <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
159template <typename T, ReflectionTraitsKind KIND = RTK_STRING, bool has_str = true>
160struct reflection_traits : public reflection_traits_string_impl<has_str, T, abst_reflection_traits>
161{
162 static const ReflectionTraitsKind kind = KIND;
165};
166
168
170extern 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>
the self reflection handler is passed to the virtual self_reflect() method of cgv::base::base.
Helper functions to convert numeric types into strings using std streams.
ReflectionTraitsKind
different types of reflection traits
TypeId
ids for the different types and type constructs
Definition type_id.h:12
std::string to_string(const std::string &v, unsigned int w, unsigned int p, bool)
specialization of conversion from string to strings
bool from_string(std::string &v, const std::string &s)
specialization to extract string value from string
the cgv namespace
Definition print.h:11
abstract interface for type reflection with basic type management and optional string conversion
virtual void delete_instances(void *) const =0
delete instances with the delete [] operator
virtual unsigned size() const =0
return the size of the type
virtual abst_reflection_traits * clone()=0
clone function
virtual void delete_instance(void *) const =0
delete an instance with the delete operator
virtual cgv::type::info::TypeId get_type_id() const =0
return the type id
virtual const char * get_type_name() const =0
return the type name
virtual void * new_instance() const =0
construct an instance on the heap with the new operator
virtual void * new_instances(unsigned n) const =0
construct n instances on the heap with the new operator
cgv::type::info::TypeId get_type_id() const
return the type id
void * new_instance() const
construct an instance on the heap with the new operator
bool is_enum_type() const
return whether type is an enum type - this is independent of whether enum interface is implemented
void delete_instance(void *instance_ptr) const
delete an instance with the delete operator
unsigned size() const
return the size of the type
void delete_instances(void *instance_array) const
delete instances with the delete [] operator
const char * get_type_name() const
return the type name
void * new_instances(unsigned n) const
construct n instances on the heap with the new operator
implementation of the reflection traits providing type specific interface for variable base class
void * new_instances(unsigned n) const
construct n instances on the heap with the new operator
void delete_instances(void *instance_array) const
delete instances with the delete [] operator
bool is_enum_type() const
return whether type is an enum type - this is independent of whether enum interface is implemented
unsigned size() const
return the size of the type
cgv::type::info::TypeId get_type_id() const
return the type id
const char * get_type_name() const
return the type name
void * new_instance() const
construct an instance on the heap with the new operator
void delete_instance(void *instance_ptr) const
delete an instance with the delete operator
this template allows to distinguish between traits with and without string conversions
Default implementation of the reflection traits providing type specific interface.
abst_reflection_traits * clone()
clone function
template condition returning, whether the given type is an enum type
Definition is_enum.h:12
template with a static member function get_id() of type TypeId returning the TypeId of the template a...
Definition type_id.h:86
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