cgv
Loading...
Searching...
No Matches
set_reflection_handler.cxx
1#include "set_reflection_handler.h"
2#include <cgv/type/variant.h>
3#include <cgv/type/info/type_id.h>
4#include <string.h>
5
6using namespace cgv::type;
7
8namespace cgv {
9 namespace reflect {
10
13{
14 return true;
15}
16
18set_reflection_handler::set_reflection_handler(const std::string& _target, const std::string& _value_type,
19 const void* _value_ptr, abst_reflection_traits* _value_rt)
20 : find_reflection_handler(_target), value_type(_value_type), value_ptr(_value_ptr), value_rt(_value_rt) {}
21
22set_reflection_handler::set_reflection_handler(const std::string& _target, const void* _value_ptr, abst_reflection_traits* _value_rt)
23 : find_reflection_handler(_target), value_ptr(_value_ptr), value_rt(_value_rt) {}
24
26void set_reflection_handler::process_member_void(const std::string& member_name, void* member_ptr,
27 abst_reflection_traits* rt, GroupKind group_kind, unsigned grp_size)
28{
29 find_reflection_handler::process_member_void(member_name, member_ptr, rt, group_kind, grp_size);
30 valid = false;
31 if (value_rt) {
32 if (info::is_fundamental(rt->get_type_id())) {
33 if (info::is_fundamental(value_rt->get_type_id())) {
34 cgv::type::assign_variant(rt->get_type_name(), member_ptr, value_rt->get_type_name(), value_ptr);
35 valid = true;
36 }
37 else if (value_rt->has_string_conversions()) {
38 std::string tmp_str;
39 value_rt->get_to_string(value_ptr, tmp_str);
40 set_variant(tmp_str, rt->get_type_name(), member_ptr);
41 valid = true;
42 }
43
44 }
45 if (!valid && rt->has_string_conversions() && value_rt->has_string_conversions()) {
46 std::string tmp;
47 value_rt->get_to_string(value_ptr, tmp);
48 valid = rt->set_from_string(member_ptr, tmp);
49 }
50 if (!valid && rt->has_string_conversions() && value_rt->get_type_id() == info::TI_STRING) {
51 valid = rt->set_from_string(member_ptr, *static_cast<const std::string*>(value_ptr));
52 }
53 }
54 else {
55 if (info::is_fundamental(rt->get_type_id())) {
56 cgv::type::assign_variant(rt->get_type_name(), member_ptr, value_type, value_ptr);
57 valid = true;
58 }
59 else if (value_type == rt->get_type_name()) {
60 memcpy(member_ptr, value_ptr, rt->size());
61 valid = true;
62 }
63 else if (value_type == "string" && rt->has_string_conversions()) {
64 valid = rt->set_from_string(member_ptr, *static_cast<const std::string*>(value_ptr));
65 }
66 }
67}
68
69 }
70}
The cgv::reflect::find_reflection_hander steers traversal to a specific member and calls the virtual ...
virtual void process_member_void(const std::string &member_name, void *member_ptr, abst_reflection_traits *rt, GroupKind group_kind=GK_NO_GROUP, unsigned grp_size=-1)
virtual method that is overloaded by derived classes to handle the target member
GroupKind
different support group types
bool is_creative() const
this should return true
void process_member_void(const std::string &member_name, void *member_ptr, abst_reflection_traits *rt, GroupKind group_kind, unsigned grp_size)
virtual method that is overloaded by derived classes to handle the target member
set_reflection_handler(const std::string &_target, const std::string &_value_type, const void *_value_ptr, abst_reflection_traits *_value_rt=0)
construct from target, value type, pointer to value and optionally reflection_traits
@ TI_STRING
wide character type
Definition type_id.h:31
namespace for compile time type information
the cgv namespace
Definition print.h:11
abstract interface for type reflection with basic type management and optional string conversion
virtual unsigned size() const =0
return the size of the type
virtual bool has_string_conversions() const
whether type can be converted to string, defaults to false
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 get_to_string(const void *instance_ptr, std::string &str_val)
convert given instance into a string value
virtual bool set_from_string(void *instance_ptr, const std::string &str_val)
convert a given string value to the reflected type and store in the instance pointer