cgv
Loading...
Searching...
No Matches
base.cxx
1#include "base.h"
2#include "named.h"
3#include "node.h"
4#include "group.h"
5#include <cgv/reflect/get_reflection_handler.h>
6#include <cgv/reflect/set_reflection_handler.h>
7#include <cgv/type/variant.h>
8#include <cgv/utils/tokenizer.h>
9#include <cgv/utils/scan.h>
10#include <iostream>
11#include <stdlib.h>
12
13using namespace cgv::type;
14using namespace cgv::reflect;
15using namespace cgv::type::info;
16using namespace cgv::utils;
17
18namespace cgv {
19 namespace base {
20
22{
23}
24void base::stream_stats(std::ostream&)
25{
26}
28{
29}
31{
32}
34{
35 return true;
36}
62{
63}
65{
66 return 0;
67}
68
70{
71public:
72 std::string property_declarations;
75 int reflect_group_begin(GroupKind group_kind, const std::string& group_name, void* group_ptr, abst_reflection_traits* group_rt, unsigned grp_size)
76 {
77 if (group_kind == GK_BASE_CLASS)
78 return GT_COMPLETE;
79
80 if (!property_declarations.empty())
81 property_declarations += ';';
82 std::string group_type = group_rt->get_type_name();
83 switch (group_kind) {
84 case GK_STRUCTURE : property_declarations += group_name+':'+group_type; break;
85 case GK_ARRAY : property_declarations += group_name+':'+group_type+'['+to_string(grp_size)+']'; break;
86 case GK_VECTOR : property_declarations += group_name+":vector<"+group_type+">"; break;
87 case GK_POINTER: property_declarations += group_name+":pointer("+group_type+")"; break;
88 }
89 return GT_SKIP;
90 }
92 bool reflect_member_void(const std::string& member_name,
93 void*, abst_reflection_traits* rt)
94 {
95 if (!property_declarations.empty())
96 property_declarations += ';';
97 property_declarations += member_name+':'+rt->get_type_name();
98 return true;
99 }
102 abst_reflection_traits* return_traits, const std::vector<abst_reflection_traits*>& param_value_traits)
103 {
104 if (!property_declarations.empty())
105 property_declarations += ';';
106 property_declarations += method_name+'(';
107 for (unsigned int i=0; i<param_value_traits.size(); ++i) {
108 if (i > 0)
109 property_declarations += ',';
110 property_declarations += param_value_traits[i]->get_type_name();
111 }
112 property_declarations += ')';
113 if (return_traits) {
114 property_declarations += ':';
115 property_declarations += return_traits->get_type_name();
116 }
117 return true;
118 }
119};
120
122{
123public:
124 void* instance;
125 const std::string& method;
126 const std::vector<std::string>& param_value_types;
127 const std::vector<const void*>& param_value_ptrs;
128 const std::string& result_type;
129 void* result_value_ptr;
130 bool found;
132 const std::string& _method,
133 const std::vector<std::string>& _param_value_types,
134 const std::vector<const void*>& _param_value_ptrs,
135 const std::string& _result_type,
136 void* _result_value_ptr)
137 : instance(_instance),
138 method(_method),
139 param_value_types(_param_value_types),
140 param_value_ptrs(_param_value_ptrs),
141 result_type(_result_type),
142 result_value_ptr(_result_value_ptr),
143 found(false) {}
145 bool reflect_member_void(const std::string& member_name, void* member_ptr, abst_reflection_traits* rt)
146 {
147 return true;
148 }
151 abst_reflection_traits* return_traits, const std::vector<abst_reflection_traits*>& param_value_traits)
152 {
153 if (method_name != method)
154 return true;
155 mi_ptr->call_void(instance, param_value_traits, param_value_ptrs, param_value_types, return_traits, result_value_ptr, result_type);
156 return false;
157 }
158};
159
162{
163 return true;
164}
165
166
168bool base::call_void(const std::string& method,
169 const std::vector<std::string>& param_value_types,
170 const std::vector<const void*>& param_value_ptrs,
171 const std::string& result_type,
172 void* result_value_ptr)
173{
174 call_reflection_handler csrh(this, method, param_value_types, param_value_ptrs, result_type, result_value_ptr);
176 return csrh.found;
177}
178
179
181bool base::set_void(const std::string& property, const std::string& value_type, const void* value_ptr)
182{
183 set_reflection_handler ssrh(property, value_type, value_ptr);
185 if (ssrh.found_valid_target()) {
186 on_set(ssrh.get_member_ptr());
187 return true;
188 }
189/*
190 const_type_ptr ctp = get_type();
191 if (!ctp)
192 return false;
193 const compound_interface* ci = ctp->get_compound();
194 if (!ci)
195 return false;
196 const std::vector<const compound_interface::member_interface*>& mbrs = ci->get_members();
197 for (unsigned int i=0; i<mbrs.size(); ++i) {
198 if (mbrs[i]->get_name() == property) {
199 void* member_ptr = mbrs[i]->access(this);
200 assign_variant(name_type(mbrs[i]->get_member_type()), member_ptr, value_type, value_ptr);
201 on_set(mbrs[i]->access(this));
202 return true;
203 }
204 }
205 */
206 return false;
207}
208
210void base::on_set(void* member_ptr)
211{
212}
213
215bool base::get_void(const std::string& property, const std::string& value_type, void* value_ptr)
216{
217 get_reflection_handler gsrh(property, value_type, value_ptr);
219 if (gsrh.found_valid_target())
220 return true;
221/*
222 const_type_ptr ctp = get_type();
223 if (!ctp)
224 return false;
225 const compound_interface* ci = ctp->get_compound();
226 if (!ci)
227 return false;
228 const std::vector<const compound_interface::member_interface*>& mbrs = ci->get_members();
229 for (unsigned int i=0; i<mbrs.size(); ++i) {
230 if (mbrs[i]->get_name() == property) {
231 assign_variant(value_type, value_ptr, name_type(mbrs[i]->get_member_type()), mbrs[i]->access_const(this));
232 return true;
233 }
234 }
235 */
236 return false;
237}
238
239
241std::string base::get_type_name() const
242{
243// const_type_ptr ctp = get_type();
244// if (ctp)
245// return name_type(ctp);
247}
248
250std::string base::get_default_options() const
251{
252 return std::string();
253}
256{
258 if (n)
259 return n->get_name();
260 return get_type_name();
261}
262
265{
268 std::string declarations = pdsrh.property_declarations;
269/*
270 const_type_ptr ctp = get_type();
271 if (!ctp)
272 return declarations;
273 const compound_interface* ci = ctp->get_compound();
274 if (!ci)
275 return declarations;
276 const std::vector<const compound_interface::member_interface*>& mbrs = ci->get_members();
277 for (unsigned int i=0; i<mbrs.size(); ++i) {
278 if (!declarations.empty())
279 declarations += ";";
280 declarations += mbrs[i]->get_name() + ":" + name_type(mbrs[i]->get_member_type());
281 }
282 */
283 return declarations;
284}
285
288{
289 // split into single assignments
290 std::vector<token> toks;
291 bite_all(tokenizer(property_assignments).set_skip("'\"","'\"","\\\\").set_ws(";"),toks);
292 // for each assignment
293 for (unsigned int i=0; i<toks.size(); ++i) {
294 std::vector<token> sides;
295 bite_all(tokenizer(toks[i]).set_skip("'\"","'\"","\\\\").set_ws("="),sides);
296 if (sides.size() != 2) {
297 if (report_error)
298 std::cerr << "property assignment >" << to_string(toks[i]).c_str() << "< does not match pattern lhs=rhs" << std::endl;
299 continue;
300 }
301 std::string lhs(to_string(sides[0]));
302 std::string rhs(to_string(sides[1]));
303 if (rhs[0] == '"' || rhs[0] == '\'') {
304 unsigned int n = (unsigned int) (rhs.size()-1);
305 char open = rhs[0];
306 if (rhs[n] == rhs[0])
307 --n;
308 rhs = rhs.substr(1, n);
309 for (unsigned i=1; i<rhs.size(); ++i) {
310 if (rhs[i-1] == '\\' && (rhs[i] == '\\' || rhs[i] == open))
311 rhs.erase(i-1,1);
312 }
313 set_void(lhs,"string",&rhs);
314 }
315 else if (rhs == "true" || rhs == "false") {
316 bool value = rhs == "true";
317 set_void(lhs, "bool", &value);
318 }
319 else if (is_digit(rhs[0]) || rhs[0] == '.' || rhs[0] == '+' || rhs[0] == '-') {
320 int int_value;
321 if (is_integer(rhs,int_value))
322 set_void(lhs, "int32", &int_value);
323 else {
324 double value = atof(rhs.c_str());
325 set_void(lhs, "flt64", &value);
326 }
327 }
328 else
329 set_void(lhs,"string",&rhs);
330 }
331}
332
334
336bool base::is_property(const std::string& property_name, std::string* type_name)
337{
339 std::vector<token> toks;
340 bite_all(tokenizer(prop_decls).set_ws(";"),toks);
341 // for each assignment
342 for (unsigned int i=0; i<toks.size(); ++i) {
343 std::vector<token> ts;
344 bite_all(tokenizer(toks[i]).set_ws(":"),ts);
345 if (ts.size() != 2)
346 continue;
347 if (ts[0] == property_name.c_str()) {
348 if (type_name)
349 *type_name = to_string(ts[1]);
350 return true;
351 }
352 }
353 return false;
354}
355
357
360void* base::find_member_ptr(const std::string& property_name, std::string* type_name)
361{
364 if (!fsrh.found_target())
365 return 0;
366 if (type_name)
367 *type_name = fsrh.get_reflection_traits()->get_type_name();
368 return fsrh.get_member_ptr();
369}
370
371 }
372}
virtual ~base()
make destructor virtual and not accessible from outside
Definition base.cxx:21
virtual void unregister()
overload to handle unregistration of instances
Definition base.cxx:30
virtual void on_register()
overload to handle register events that is sent after the instance has been registered
Definition base.cxx:27
virtual bool on_exit_request()
overload to handle the appication exit request, return true if exiting is allowed and false otherwise
Definition base.cxx:33
virtual void update()
this virtual update allows for example to ask a view to update the viewed value. The default implemen...
Definition base.cxx:61
virtual std::string get_property_declarations()
return a semicolon separated list of property declarations
Definition base.cxx:264
virtual data::ref_ptr< const node, true > get_node_const() const
perform downcast to const node
Definition base.cxx:53
virtual bool set_void(const std::string &property, const std::string &value_type, const void *value_ptr)
abstract interface for the setter of a dynamic property.
Definition base.cxx:181
virtual bool call_void(const std::string &method, const std::vector< std::string > &param_value_types, const std::vector< const void * > &param_value_ptrs, const std::string &result_type="", void *result_value_ptr=0)
abstract interface to call an action
Definition base.cxx:168
virtual data::ref_ptr< named, true > get_named()
perform downcast to named
Definition base.cxx:37
virtual void on_set(void *member_ptr)
this callback is called when the set_void method has changed a member and can be overloaded in derive...
Definition base.cxx:210
virtual std::string get_type_name() const
overload to return the type name of this object. By default the type interface is queried over get_ty...
Definition base.cxx:241
virtual std::string get_default_options() const
overload to provide default options for registration
Definition base.cxx:250
virtual data::ref_ptr< const group, true > get_group_const() const
perform downcast to const group
Definition base.cxx:57
virtual void stream_stats(std::ostream &)
overload to show the content of this object
Definition base.cxx:24
virtual bool self_reflect(cgv::reflect::reflection_handler &)
used for simple self reflection
Definition base.cxx:161
virtual data::ref_ptr< node, true > get_node()
perform downcast to node
Definition base.cxx:41
virtual data::ref_ptr< const named, true > get_named_const() const
perform downcast to const named
Definition base.cxx:49
virtual data::ref_ptr< group, true > get_group()
perform downcast to group
Definition base.cxx:45
void * find_member_ptr(const std::string &property_name, std::string *type_name=0)
find a member pointer by name.
Definition base.cxx:360
bool is_property(const std::string &property_name, std::string *type_name=0)
check if the given name specifies a property.
Definition base.cxx:336
virtual bool get_void(const std::string &property, const std::string &value_type, void *value_ptr)
abstract interface for the getter of a dynamic property.
Definition base.cxx:215
std::string get_name_or_type_name() const
determine name of instance by checking cgv::base::named interface and in failure fallback to get_type...
Definition base.cxx:255
virtual void * get_user_data() const
this virtual method allows to pass application specific data for internal purposes
Definition base.cxx:64
void multi_set(const std::string &property_assignments, bool report_error=true)
set several properties
Definition base.cxx:287
bool reflect_method_void(const std::string &method_name, method_interface *mi_ptr, abst_reflection_traits *return_traits, const std::vector< abst_reflection_traits * > &param_value_traits)
abstract interface to reflect a method, where return and parameter types are specified as strings.
Definition base.cxx:150
bool reflect_member_void(const std::string &member_name, void *member_ptr, abst_reflection_traits *rt)
empty implementation
Definition base.cxx:145
bool reflect_member_void(const std::string &member_name, void *, abst_reflection_traits *rt)
abstract interface to reflect a member variable, where the member type is specified as a string.
Definition base.cxx:92
bool reflect_method_void(const std::string &method_name, method_interface *mi_ptr, abst_reflection_traits *return_traits, const std::vector< abst_reflection_traits * > &param_value_traits)
abstract interface to reflect a method, where return and parameter types are specified as strings.
Definition base.cxx:101
int reflect_group_begin(GroupKind group_kind, const std::string &group_name, void *group_ptr, abst_reflection_traits *group_rt, unsigned grp_size)
abstract interface to start reflection of a group of members.
Definition base.cxx:75
complete implementation of method actions that only call one method when entering a node
Definition action.h:113
reference counted pointer, which can work together with types that are derived from ref_counted,...
Definition ref_ptr.h:160
The cgv::reflect::find_reflection_hander steers traversal to a specific member and calls the virtual ...
provides access to a member variable of an instance.
the self reflection handler is passed to the virtual self_reflect() method of cgv::base::base.
GroupKind
different support group types
the tokenizer allows to split text into tokens in a convenient way.
Definition tokenizer.h:68
in this namespace reflection of types is implemented
namespace for templates that provide type information
Definition type_access.h:9
namespace for compile time type information
namespace that holds tools that dont fit any other namespace
bool is_digit(char c)
check if char is a digit
Definition scan.cxx:20
std::string to_string(const std::string &v, unsigned int w, unsigned int p, bool)
specialization of conversion from string to strings
bool is_integer(const char *begin, const char *end, int &value)
check if the text range (begin,end( defines an integer value. If yes, store the value in the passed r...
Definition scan.cxx:367
void bite_all(tokenizer &t, std::vector< token > &result)
bite all tokens into a token vector
Definition tokenizer.h:121
the cgv namespace
Definition print.h:11
Helper functions to process strings.
abstract interface for type reflection with basic type management and optional string conversion
virtual const char * get_type_name() const =0
return the type name
abstract interface to call a method of a given instance.
traits class with a static function get_name() of type const char* that returns the type name of the ...
Definition type_name.h:54
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