cgv
Loading...
Searching...
No Matches
base_generator.h
1#pragma once
2
3#include "base.h"
4#include <cgv/type/variant.h>
5#include <map>
6
7#include "lib_begin.h"
8
10namespace cgv {
12 namespace base {
13
15{
16 bool has_changed{false};
17 abst_property_access() = default;
18 virtual ~abst_property_access() = default;
19 virtual const char* get_type_name() const = 0;
20 virtual bool set(const std::string& value_type, const void* value_ptr) = 0;
21 virtual bool get(const std::string& value_type, void* value_ptr) = 0;
22};
23
24template <typename T>
26{
27 T* ptr;
29 const char* get_type_name() const override { return cgv::type::info::type_name<T>::get_name(); }
30 bool set(const std::string& value_type, const void* value_ptr) override { cgv::type::get_variant(*ptr,value_type,value_ptr); has_changed = true; return true; }
31 bool get(const std::string& value_type, void* value_ptr) override { cgv::type::set_variant(*ptr,value_type,value_ptr); return true; }
32};
33
34template <typename T>
36{
37 T* ptr;
39 const char* get_type_name() const override { return cgv::type::info::type_name<T>::get_name(); }
40 bool set(const std::string& value_type, const void* value_ptr) override {
41 if (value_type == get_type_name()) {
42 *ptr = *((const T*) value_ptr);
43 has_changed = true;
44 return true;
45 }
46 else if (value_type == "string") {
47 return has_changed = cgv::utils::from_string(*ptr, *((std::string*)value_ptr));
48 }
49 return false;
50 }
51 bool get(const std::string& value_type, void* value_ptr) override {
52 if (value_type == get_type_name()) {
53 *((T*) value_ptr) = *ptr;
54 return true;
55 }
56 else if (value_type == "string") {
57 *((std::string*)value_ptr) = cgv::utils::to_string(*ptr);
58 }
59 return false;
60 }
61};
62
63template <typename T>
68
80template <> struct property_access<std::string> : public standard_type_property_access<std::string> { property_access<std::string>(std::string* _ptr) : standard_type_property_access<std::string>(_ptr) {}; };
81
85class CGV_API base_generator : public base
86{
87protected:
88 typedef std::map<std::string, abst_property_access*> map_type;
89 typedef map_type::iterator iter_type;
90 typedef map_type::const_iterator const_iter_type;
92 map_type property_map;
93public:
97 std::string get_type_name() const;
99 void add_void(const std::string& name, abst_property_access* apa);
101 template <typename T>
102 void add(const std::string& property, T& value) { add_void(property, new property_access<T>(&value)); }
104 void del(const std::string& property);
106 bool changed(const std::string& property) const;
108 std::string get_property_declarations();
110 bool set_void(const std::string& property, const std::string& value_type, const void* value_ptr);
112 bool get_void(const std::string& property, const std::string& value_type, void* value_ptr);
113};
114
116
129template <typename T>
130bool has_property(const std::string& options, const std::string& property, T& value, bool report_error = true) {
132 bg.add(property, value);
133 bg.multi_set(options, report_error);
134 return bg.changed(property);
135}
136
137 }
138}
139
140
141#include <cgv/config/lib_end.h>
implements a dynamic object, that can be composed of independent variables, which are handled as prop...
map_type property_map
store the properties as map from property name to type and pointer to instance
void add(const std::string &property, T &value)
add a property by deriving property access from type of reference value
base class for all classes that can be registered with support for dynamic properties (see also secti...
Definition base.h:75
complete implementation of method actions that only call one method when entering a node
Definition action.h:113
bool has_property(const std::string &options, const std::string &property, T &value, bool report_error=true)
simple parsing support to access values of properties in a string of property assignment
signed char int8_type
this type provides an 8 bit signed integer type
int int32_type
this type provides an 32 bit signed integer type
short int16_type
this type provides an 16 bit signed integer type
unsigned short uint16_type
this type provides an 16 bit unsigned integer type
unsigned long long uint64_type
this type provides an 64 bit unsigned integer type
unsigned int uint32_type
this type provides an 32 bit unsigned integer type
long long int64_type
this type provides an 64 bit signed integer type
unsigned char uint8_type
this type provides an 8 bit unsigned integer type
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
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