cgv
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 
9 using namespace cgv::type;
10 
12 namespace cgv {
14  namespace base {
15 
16 struct CGV_API abst_property_access
17 {
18  bool has_changed;
19  abst_property_access();
20  virtual const char* get_type_name() const = 0;
21  virtual bool set(const std::string& value_type, const void* value_ptr) = 0;
22  virtual bool get(const std::string& value_type, void* value_ptr) = 0;
23 };
24 
25 template <typename T>
26 struct standard_type_property_access : public abst_property_access
27 {
28  T* ptr;
29  standard_type_property_access(T* _ptr) : ptr(_ptr) {}
30  const char* get_type_name() const { return cgv::type::info::type_name<T>::get_name(); }
31  bool set(const std::string& value_type, const void* value_ptr) { get_variant(*ptr,value_type,value_ptr); has_changed = true; return true; }
32  bool get(const std::string& value_type, void* value_ptr) { set_variant(*ptr,value_type,value_ptr); return true; }
33 };
34 
35 template <typename T>
36 struct emulated_property_access : public abst_property_access
37 {
38  T* ptr;
39  emulated_property_access(T* _ptr) : ptr(_ptr) {}
40  const char* get_type_name() const { return cgv::type::info::type_name<T>::get_name(); }
41  bool set(const std::string& value_type, const void* value_ptr) {
42  if (value_type == get_type_name()) {
43  *ptr = *((const T*) value_ptr);
44  has_changed = true;
45  return true;
46  }
47  else if (value_type == "string") {
48  return has_changed = cgv::utils::from_string(*ptr, *((std::string*)value_ptr));
49  }
50  return false;
51  }
52  bool get(const std::string& value_type, void* value_ptr) {
53  if (value_type == get_type_name()) {
54  *((T*) value_ptr) = *ptr;
55  return true;
56  }
57  else if (value_type == "string") {
58  *((std::string*)value_ptr) = cgv::utils::to_string(*ptr);
59  }
60  return false;
61  }
62 };
63 
64 template <typename T>
65 struct property_access : public emulated_property_access<T>
66 {
67  property_access(T* _ptr) : emulated_property_access<T>(_ptr) {}
68 };
69 
70 template <> struct property_access<int8_type> : public standard_type_property_access<int8_type> { property_access<int8_type>( int8_type* _ptr) : standard_type_property_access<int8_type>(_ptr) {}; };
71 template <> struct property_access<int16_type> : public standard_type_property_access<int16_type> { property_access<int16_type>(int16_type* _ptr) : standard_type_property_access<int16_type>(_ptr) {}; };
72 template <> struct property_access<int32_type> : public standard_type_property_access<int32_type> { property_access<int32_type>(int32_type* _ptr) : standard_type_property_access<int32_type>(_ptr) {}; };
73 template <> struct property_access<int64_type> : public standard_type_property_access<int64_type> { property_access<int64_type>(int64_type* _ptr) : standard_type_property_access<int64_type>(_ptr) {}; };
74 template <> struct property_access<uint8_type> : public standard_type_property_access<uint8_type> { property_access<uint8_type>( uint8_type* _ptr) : standard_type_property_access<uint8_type>(_ptr) {}; };
75 template <> struct property_access<uint16_type> : public standard_type_property_access<uint16_type> { property_access<uint16_type>(uint16_type* _ptr) : standard_type_property_access<uint16_type>(_ptr) {}; };
76 template <> struct property_access<uint32_type> : public standard_type_property_access<uint32_type> { property_access<uint32_type>(uint32_type* _ptr) : standard_type_property_access<uint32_type>(_ptr) {}; };
77 template <> struct property_access<uint64_type> : public standard_type_property_access<uint64_type> { property_access<uint64_type>(uint64_type* _ptr) : standard_type_property_access<uint64_type>(_ptr) {}; };
78 template <> struct property_access<float> : public standard_type_property_access<float> { property_access<float>(float* _ptr) : standard_type_property_access<float>(_ptr) {}; };
79 template <> struct property_access<double> : public standard_type_property_access<double> { property_access<double>(double* _ptr) : standard_type_property_access<double>(_ptr) {}; };
80 template <> struct property_access<bool> : public standard_type_property_access<bool> { property_access<bool>(bool* _ptr) : standard_type_property_access<bool>(_ptr) {}; };
81 template <> 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) {}; };
82 
86 class CGV_API base_generator : public cgv::base::base
87 {
88 protected:
89  typedef std::map<std::string,abst_property_access*> map_type;
90  typedef map_type::iterator iter_type;
91  typedef map_type::const_iterator const_iter_type;
93  map_type property_map;
94 public:
96  std::string get_type_name() const;
98  void add_void(const std::string& name, abst_property_access* apa);
100  template <typename T>
101  void add(const std::string& property, T& value) { add_void(property, new property_access<T>(&value)); }
103  void del(const std::string& property);
105  bool changed(const std::string& property) const;
107  std::string get_property_declarations();
109  bool set_void(const std::string& property, const std::string& value_type, const void* value_ptr);
111  bool get_void(const std::string& property, const std::string& value_type, void* value_ptr);
112 };
113 
115 
128 template <typename T>
129 bool has_property(const std::string& options, const std::string& property, T& value, bool report_error = true) {
130  base_generator bg;
131  bg.add(property, value);
132  bg.multi_set(options, report_error);
133  return bg.changed(property);
134 }
135 
136  }
137 }
138 
139 
140 #include <cgv/config/lib_end.h>
cgv::type::int32_type
int int32_type
this type provides an 32 bit signed integer type
Definition: standard_types.h:12
cgv::base::base::multi_set
void multi_set(const std::string &property_assignments, bool report_error=true)
set several properties
Definition: base.cxx:279
cgv::type::uint8_type
unsigned char uint8_type
this type provides an 8 bit unsigned integer type
Definition: standard_types.h:16
cgv::base::has_property
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
Definition: base_generator.h:129
cgv::type::uint16_type
unsigned short uint16_type
this type provides an 16 bit unsigned integer type
Definition: standard_types.h:18
cgv::type::int16_type
short int16_type
this type provides an 16 bit signed integer type
Definition: standard_types.h:10
cgv::type
namespace for compile time type information
Definition: has_virtual_destructor.h:8
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::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::type::int64_type
long long int64_type
this type provides an 64 bit signed integer type
Definition: standard_types.h:14
cgv::base::base_generator::changed
bool changed(const std::string &property) const
return whether property has changed
Definition: base_generator.cxx:42
cgv::type::uint32_type
unsigned int uint32_type
this type provides an 32 bit unsigned integer type
Definition: standard_types.h:20
cgv::base::base_generator::add
void add(const std::string &property, T &value)
add a property by deriving property access from type of reference value
Definition: base_generator.h:101
cgv::base::base_generator::property_map
map_type property_map
store the properties as map from property name to type and pointer to instance
Definition: base_generator.h:93
cgv::base::base
Definition: base.h:57
cgv::type::info::get_type_name
const char * get_type_name(TypeId tid)
function that returns the name of a type specified through TypeId
Definition: type_id.cxx:117
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::base::base_generator
Definition: base_generator.h:87
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::type::uint64_type
unsigned long long uint64_type
this type provides an 64 bit unsigned integer type
Definition: standard_types.h:22
cgv::type::int8_type
char int8_type
this type provides an 8 bit signed integer type
Definition: standard_types.h:8