cgv
Loading...
Searching...
No Matches
register.h
1#pragma once
2
3#include <cgv/base/base.h>
4#include <cgv/base/named.h>
5#include <cgv/utils/token.h>
6#include <cgv/type/info/type_name.h>
7#include <string>
8#include <iostream>
9#include <map>
10
11#include "lib_begin.h"
12
13#ifndef _WIN32
14#include <unistd.h>
15#include <dlfcn.h>
16#endif
17
19namespace cgv {
21 namespace base {
22
23
27
29extern void CGV_API enable_registration();
31extern void CGV_API disable_registration();
33extern bool CGV_API is_registration_enabled();
35
44extern void CGV_API define_registration_order(const std::string& partial_order, bool before_contructor_execution = false, const std::string& when = "always");
45
48{
49 registration_order_definition(const std::string& partial_order, bool before_contructor_execution = false, const std::string& when = "always");
50};
51
53extern void CGV_API enable_registration_debugging();
55extern void CGV_API disable_registration_debugging();
57extern bool CGV_API is_registration_debugging_enabled();
58
60extern void CGV_API enable_permanent_registration();
62extern void CGV_API disable_permanent_registration();
64extern bool CGV_API is_permanent_registration_enabled();
66extern void CGV_API unregister_all_objects();
68extern bool CGV_API request_exit_from_all_objects();
70extern unsigned CGV_API get_nr_permanently_registered_objects();
72extern base_ptr CGV_API get_permanently_registered_object(unsigned i);
73
74
76
79extern void CGV_API enable_registration_event_cleanup();
81extern void CGV_API disable_registration_event_cleanup();
83extern bool CGV_API is_registration_event_cleanup_enabled();
84
86
90
92extern void CGV_API register_object(base_ptr object, const std::string& options = "");
94extern void CGV_API unregister_object(base_ptr object, const std::string& options = "");
95
97struct CGV_API object_constructor : public cgv::base::base
98{
99public:
103 virtual std::string get_constructed_type_name() const = 0;
105 virtual base_ptr construct_object() const = 0;
106};
107
108// type specific specialization of helper class to perform delayed registration and creation of objects in case that the registration is disabled
109template <class T>
111{
112public:
114 std::string get_type_name() const { return cgv::type::info::type_name<object_constructor_impl<T> >::get_name(); }
117 // creation function
118 base_ptr construct_object() const { return base_ptr(new T()); }
119};
120
121// type specific specialization of helper class to perform delayed registration and creation of objects in case that the registration is disabled
122template <class T, typename CA>
124{
125 // buffer constructor argument
126 CA ca;
127public:
128 // construct from option
129 object_constructor_impl_1(const CA& _ca) : ca(_ca) {}
134 // creation function
135 base_ptr construct_object() const { return base_ptr(new T(ca)); }
136};
137
138// type specific specialization of helper class to perform delayed registration and creation of objects in case that the registration is disabled
139template <class T, typename CA1, typename CA2>
141{
142 // buffer constructor arguments
143 CA1 ca1;
144 CA2 ca2;
145public:
146 // construct from option
147 object_constructor_impl_2(const CA1& _ca1, const CA2& _ca2) : ca1(_ca1), ca2(_ca2) {}
152 // creation function
153 base_ptr construct_object() const { return base_ptr(new T(ca1,ca2)); }
154};
155
157template <class T>
159{
161 explicit object_registration(const std::string& options) {
163 register_object(base_ptr(new T()),options);
164 else
166 }
167};
168
170template <class T, typename CA>
172{
174 object_registration_1(const CA& arg, const std::string& options = "") {
176 register_object(base_ptr(new T(arg)),options);
177 else
179 }
180};
181
183template <class T, typename CA1, typename CA2>
185{
187 object_registration_2(const CA1& a1, const CA2& a2, const std::string& options = "")
188 {
190 register_object(base_ptr(new T(a1,a2)),options);
191 else
193 }
194};
196
199
201
202struct CGV_API server
203{
204};
205
207
209struct CGV_API driver
210{
211};
212
214
218{
220 virtual void register_object(base_ptr object, const std::string& options = "") = 0;
222 virtual void unregister_object(base_ptr object, const std::string& options = "") = 0;
223};
224
226struct CGV_API factory : public base
227{
228protected:
230 std::string created_type_name;
236 std::string object_options;
237public:
239 factory(const std::string& _created_type_name, bool _singleton = false, const std::string& _object_options = "");
241 virtual std::string get_object_options() const;
243 const std::string& get_created_type_name() const;
245 std::string get_property_declarations();
247 bool set_void(const std::string& property, const std::string& value_type, const void* value_ptr);
249 bool get_void(const std::string& property, const std::string& value_type, void* value_ptr);
251 bool is_singleton_factory() const;
253 base_ptr get_singleton() const;
255 void release_singleton();
257 base_ptr create_object();
260};
261
263template <class T>
264struct factory_impl : public factory
265{
266 inline factory_impl(const std::string& _created_type_name, bool _is_singleton = false, const std::string& _object_options = "") :
268 inline base_ptr create_object_impl() { return base_ptr(new T()); }
269 std::string get_type_name() const { return std::string("factory_impl<")+get_created_type_name()+">"; }
270};
271
273template <class T, typename CA>
274struct factory_impl_1 : public factory
275{
276 CA ca;
277 inline factory_impl_1(CA _ca, const std::string& _created_type_name, bool is_singleton = false, const std::string& _object_options = "") :
279 inline base_ptr create_object_impl() { return base_ptr(new T(ca)); }
280 std::string get_type_name() const { return std::string("factory_impl_1<")+get_created_type_name()+">"; }
281};
282
284template <class T, typename CA1, typename CA2>
285struct factory_impl_2 : public factory
286{
287 CA1 ca1;
288 CA2 ca2;
289 inline factory_impl_2(CA1 _ca1, CA2 _ca2, const std::string& _created_type_name, bool is_singleton = false, const std::string& _object_options = "") :
291 inline base_ptr create_object_impl() { return base_ptr(new T(ca2)); }
292 std::string get_type_name() const { return std::string("factory_impl_2<")+get_created_type_name()+">"; }
293};
294
295extern std::string CGV_API guess_created_type_name(const char* item_text);
296extern void CGV_API register_factory_object(base_ptr fo, const char* item_text, char shortcut);
297
299template <class T>
301{
303
310 factory_registration(const std::string& _created_type_name, const std::string& _options = "", bool _is_singleton = false, const std::string& _object_options = "") {
312 }
314
316 factory_registration(const char* item_text, char shortcut, bool is_singleton = false) {
317 register_factory_object(new factory_impl<T>(guess_created_type_name(item_text), is_singleton), item_text, shortcut);
318 }
319};
320
322template <class T, typename CA>
324{
326
333 factory_registration_1(const std::string& _created_type_name, const CA& _ca, const std::string& _options = "", bool _is_singleton = false, const std::string& _object_options = "") {
335 }
337
339 factory_registration_1(const char* item_text, char shortcut, const CA& _ca, bool is_singleton = false) {
340 register_factory_object(new factory_impl_1<T,CA>(_ca, guess_created_type_name(item_text), is_singleton), item_text, shortcut);
341 }
342};
343
345template <class T, typename CA1, typename CA2>
347{
349
356 factory_registration_2(const std::string& _created_type_name, const CA1& _ca1, const CA2& _ca2, const std::string& _options = "", bool _is_singleton = false, const std::string& _object_options = "") {
358 }
359};
360
361
363
364
368class CGV_API test : public base
369{
370public:
372 static int nr_failed;
373protected:
375 std::string test_name;
377 bool (*test_func)();
378public:
380 test(const std::string& _test_name, bool (*_test_func)());
382 std::string get_type_name() const;
384 std::string get_test_name() const;
386 bool exec_test() const;
387};
388
390#define TEST_ASSERT(V) \
391 if (!(V)) { \
392 std::cerr << "\n" << __FILE__ << "(" << __LINE__ << ") : error: test failure" << std::endl; \
393 ++cgv::base::test::nr_failed; \
394 }
395
397#define TEST_ASSERT_EQ(V,Q) \
398 if ((V) != (Q)) { \
399 std::cerr << "\n" << __FILE__ << "(" << __LINE__ << ") : error: test failure " << (V) << "!=" << (Q) << std::endl; \
400 ++cgv::base::test::nr_failed; \
401 }
402
404struct CGV_API test_registration
405{
407 test_registration(const std::string& _test_name, bool (*_test_func)());
408};
410
411
412
413
417struct CGV_API resource_file_info
418{
420 unsigned int file_offset;
422 unsigned int file_length;
424 const char* file_data;
426 std::string source_file;
429 unsigned int _file_offset = 0,
430 unsigned int _file_length = 0,
431 const char* _file_data = 0,
432 const std::string& _source_file = "");
433};
434
436extern CGV_API std::map<std::string, resource_file_info>& ref_resource_file_map();
437
439extern CGV_API void register_resource_file(const std::string& file_path, unsigned int file_offset, unsigned int file_length, const char* file_data, const std::string& source_file = "");
440
443{
445 resource_file_registration(const char* file_data);
446};
447
449extern CGV_API void register_resource_string(const std::string& string_name, const char* string_data);
450
453{
455 resource_string_registration(const std::string& string_name, const char* string_data);
456};
457
459
460
463{
465 virtual void handle_args(std::vector<std::string>& args) = 0;
466};
467
470{
471 CT_UNKNOWN, // command is not known to framework
472 CT_EMPTY, // command specification was empty
473 CT_COMMENT, // a comment was given starting with '/'
474 CT_SHOW, // a show command was specified, currently only show all is supported
475 CT_PERSISTENT, // the persistent command means that all successive value set commands in a config file should be updated during execution when the user changes one of them
476 CT_INITIAL, // reverts a persistent command
477 CT_PLUGIN, // loads a plugin
478 CT_CONFIG, // executes a config file
479 CT_GUI, // loads a gui description file
480 CT_NAME, // sets a value of a registered object of the given name
481 CT_TYPE // sets a value of a registered object of the given type
482};
483
486{
490 std::vector<cgv::utils::token> parameters;
491};
492
494extern CommandType CGV_API analyze_command(const cgv::utils::token& cmd, bool eliminate_quotes = true, command_info* info_ptr = 0);
495
497extern bool CGV_API process_command(const command_info& info);
498
501
503
521extern bool CGV_API process_command(const std::string& cmd, bool eliminate_quotes = true);
522
524extern void CGV_API show_all();
526
530
532extern CGV_API void register_prog_name(const char* prog_name);
534extern CGV_API std::string& ref_prog_name();
536extern CGV_API std::string& ref_prog_path_prefix();
538extern CGV_API void process_command_line_args(int argc, char** argv);
540
543
545
547{
549 virtual void multi_observe(base_ptr bp, const std::string& property_assignments, size_t off) = 0;
550};
551
553
555{
556public:
558 virtual config_file_observer* find_config_file_observer(const std::string& file_name, const std::string& content) = 0;
560 virtual bool process_gui_file(const std::string& file_name) = 0;
561};
562
565
567extern named_ptr CGV_API find_object_by_name(const std::string& name);
569extern base_ptr CGV_API find_object_by_type(const std::string& type_name);
571extern bool CGV_API process_config_file(const std::string& file_name);
573extern bool CGV_API process_gui_file(const std::string& file_name);
575
579
581extern CGV_API void* load_plugin(const std::string& file_name);
583extern CGV_API std::string& ref_plugin_name();
585extern CGV_API bool unload_plugin(void* handle);
587
588
589 }
590}
591
592#include <cgv/config/lib_end.h>
base class for all classes that can be registered with support for dynamic properties (see also secti...
Definition base.h:75
base_ptr construct_object() const
creation function
Definition register.h:135
std::string get_type_name() const
return the type name of the object constructor class
Definition register.h:131
std::string get_constructed_type_name() const
return the type name of the to be constructed object
Definition register.h:133
base_ptr construct_object() const
creation function
Definition register.h:153
std::string get_constructed_type_name() const
return the type name of the to be constructed object
Definition register.h:151
std::string get_type_name() const
return the type name of the object constructor class
Definition register.h:149
base_ptr construct_object() const
creation function
Definition register.h:118
std::string get_type_name() const
return the type name of the object constructor class
Definition register.h:114
std::string get_constructed_type_name() const
return the type name of the to be constructed object
Definition register.h:116
complete implementation of method actions that only call one method when entering a node
Definition action.h:113
structure used to register a test function
Definition register.h:369
std::string test_name
name of test function
Definition register.h:375
static int nr_failed
static counter for all tests
Definition register.h:372
unsigned get_nr_permanently_registered_objects()
access to number of permanently registered objects
Definition register.cxx:519
void unregister_all_objects()
unregister all existing objects to clean up
Definition register.cxx:508
bool process_command(const command_info &info)
process a command given by a command info structure, return whether command was processed correctly
std::string & ref_prog_path_prefix()
return a refence to the path prefix of the started executable, this can be prepended for example to d...
Definition register.cxx:131
void enable_permanent_registration()
register a registration listener that stores pointers to all registered objects
Definition register.cxx:498
data::ref_ptr< base, true > base_ptr
ref counted pointer to base
Definition base.h:37
void enable_registration_event_cleanup()
Enable cleanup of registration events (default).
Definition register.cxx:548
std::string & ref_plugin_name()
return a reference to the currently loaded plugin
Definition register.cxx:137
void disable_registration()
if registration is disable, all registration events are stored and sent at the momement when registra...
Definition register.cxx:480
base_ptr get_permanently_registered_object(unsigned i)
access to i-th permanently registered object
Definition register.cxx:525
void enable_registration_debugging()
enable registration debugging
Definition register.cxx:365
named_ptr find_object_by_name(const std::string &name)
in case permanent registration is active, look for a registered object by name
Definition register.cxx:654
bool is_registration_event_cleanup_enabled()
return whether registration cleanup is enabled
Definition register.cxx:575
CommandType analyze_command(const cgv::utils::token &cmd, bool eliminate_quotes, command_info *info_ptr)
parse a command and optionally store result in the command info, returns the command type
Definition register.cxx:992
CommandType
enumerate type for all command types supported in configuration files
Definition register.h:470
bool is_registration_enabled()
check whether registration is enabled
Definition register.cxx:493
void register_config_file_driver(config_file_driver *cfd)
method to register a config_file_driver
Definition register.cxx:687
void register_resource_file(const std::string &file_path, unsigned int file_offset, unsigned int file_length, const char *file_data, const std::string &source_file)
register a resource file
Definition register.cxx:930
void disable_permanent_registration()
deregister registration listener and dereference pointers to registered objects
Definition register.cxx:532
bool request_exit_from_all_objects()
calls the on_exit_request method for all registered objects and return true if exiting is allowed
Definition register.cxx:513
bool is_registration_debugging_enabled()
check whether registration debugging is enabled
Definition register.cxx:376
void register_prog_name(const char *_prog_name)
set the file name of the current program.
Definition register.cxx:915
void show_all()
show information about all registered members
Definition register.cxx:954
void unregister_object(base_ptr object, const std::string &options)
unregister an object and send event to all current registration ref_listeners()
Definition register.cxx:617
void enable_registration()
enable registration and send all registration events that where emitted during disabled registration
Definition register.cxx:381
void disable_registration_debugging()
disable registration debugging
Definition register.cxx:370
void process_command_line_args(int argc, char **argv)
process the command line arguments: extract program name and load all plugins
std::map< std::string, resource_file_info > & ref_resource_file_map()
return a reference to a mapping of resource file names to resource file infos
Definition register.cxx:143
bool process_gui_file(const std::string &file_name)
interpret a gui file
Definition register.cxx:789
void * load_plugin(const std::string &file_name)
load a plugin or dll and return a handle to the plugin, or 0 if loading was not successful.
void register_resource_string(const std::string &string_name, const char *string_data)
register a resource string
Definition register.cxx:936
void register_object(base_ptr object, const std::string &options)
register an object and send event to all current registration ref_listeners()
Definition register.cxx:581
std::string & ref_prog_name()
return a refence to the name of the started executable
Definition register.cxx:125
void define_registration_order(const std::string &partial_order, bool before_contructor_execution, const std::string &when)
specify a partial order of objects for registration
Definition register.cxx:247
base_ptr find_object_by_type(const std::string &type_name)
in case permanent registration is active, look for a registered object by type name
Definition register.cxx:660
bool is_permanent_registration_enabled()
check whether permanent registration is enabled
Definition register.cxx:543
void disable_registration_event_cleanup()
disable cleanup of registration events (see enable_registration_event_cleanup).
Definition register.cxx:565
bool process_config_file(const std::string &_file_name)
interpret a config file
Definition register.cxx:783
bool unload_plugin(void *handle)
unload the plugin with the given handle
the cgv namespace
Definition print.h:11
interface for objects that process unknown command line arguments
Definition register.h:463
virtual void handle_args(std::vector< std::string > &args)=0
this function is called on registered objects with the list of unknown command line parameters
a structure to store an analized command
Definition register.h:486
CommandType command_type
the command type
Definition register.h:488
std::vector< cgv::utils::token > parameters
the parameters, one file name parameter for PLUGIN, CONFIG, GUI and two parameters (name/type,...
Definition register.h:490
abstract interface for a config file driver that handles permanent registration and gui config files.
Definition register.h:555
virtual config_file_observer * find_config_file_observer(const std::string &file_name, const std::string &content)=0
create or find a config_file_observer from the given file name and the read content of the config fil...
virtual bool process_gui_file(const std::string &file_name)=0
process a gui file
abstract interface for observers of config files.
Definition register.h:547
virtual void multi_observe(base_ptr bp, const std::string &property_assignments, size_t off)=0
to be implemented method that adds permanent registration for a list of property assignments
interfaces that add several listeners and objects.
Definition register.h:210
implementation of factory for objects of type T using a constructor with one argument of type CA
Definition register.h:275
base_ptr create_object_impl()
overload to create an object
Definition register.h:279
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 register.h:280
implementation of factory for objects of type T using a constructor with two arguments of types CA1 a...
Definition register.h:286
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 register.h:292
base_ptr create_object_impl()
overload to create an object
Definition register.h:291
implementation of factory for objects of type T using the standard constructor
Definition register.h:265
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 register.h:269
base_ptr create_object_impl()
overload to create an object
Definition register.h:268
convenience class to register a factory of the given class type that uses a constructor with one argu...
Definition register.h:324
factory_registration_1(const std::string &_created_type_name, const CA &_ca, const std::string &_options="", bool _is_singleton=false, const std::string &_object_options="")
this registers an instance of a standard factory implementation
Definition register.h:333
factory_registration_1(const char *item_text, char shortcut, const CA &_ca, bool is_singleton=false)
this constructor is only provided for downward compatibility and should not be used anymore.
Definition register.h:339
convenience class to register a factory of the given class type that uses a constructor with one argu...
Definition register.h:347
factory_registration_2(const std::string &_created_type_name, const CA1 &_ca1, const CA2 &_ca2, const std::string &_options="", bool _is_singleton=false, const std::string &_object_options="")
this registers an instance of a standard factory implementation
Definition register.h:356
convenience class to register a factory of the given class type
Definition register.h:301
factory_registration(const char *item_text, char shortcut, bool is_singleton=false)
this constructor is only provided for downward compatibility and should not be used anymore.
Definition register.h:316
factory_registration(const std::string &_created_type_name, const std::string &_options="", bool _is_singleton=false, const std::string &_object_options="")
this registers an instance of the default factory implementation
Definition register.h:310
interface for a factory that allows to create objects derived from cgv::base::base
Definition register.h:227
base_ptr singleton
pointer to the single object created by the factory in case is_singleton is true
Definition register.h:234
bool is_singleton
store whether the factory can only create one object
Definition register.h:232
std::string object_options
store the options used for registering newly created objects
Definition register.h:236
std::string created_type_name
store the type name of the to be created objects
Definition register.h:230
const std::string & get_created_type_name() const
overload to return the type name of the objects that the factory can create
Definition register.cxx:866
virtual base_ptr create_object_impl()=0
overload to create an object
abstract base class of helpers to perform delayed registration and creation of objects in case that t...
Definition register.h:98
std::string get_type_name() const
return the type name of the object constructor class
Definition register.h:101
virtual std::string get_constructed_type_name() const =0
return the type name of the to be constructed object
virtual base_ptr construct_object() const =0
creation function
convenience class to register an object of the given class type with one constructor argument
Definition register.h:172
object_registration_1(const CA &arg, const std::string &options="")
pass information about the target registration listener in the options argument
Definition register.h:174
convenience class to register an object of the given class type with two constructor arguments
Definition register.h:185
object_registration_2(const CA1 &a1, const CA2 &a2, const std::string &options="")
pass information about the target registration listener in the options argument
Definition register.h:187
convenience class to register an object of the given class type
Definition register.h:159
object_registration(const std::string &options)
pass information about the target registration listener in the options argument
Definition register.h:161
interfaces that allows to listen to registration events.
Definition register.h:218
virtual void register_object(base_ptr object, const std::string &options="")=0
overload to handle registration events
virtual void unregister_object(base_ptr object, const std::string &options="")=0
overload to handle unregistration events
helper class whose constructor calls the define_registration_order() function
Definition register.h:48
information registered with each resource file
Definition register.h:418
std::string source_file
name of
Definition register.h:426
const char * file_data
pointer to
Definition register.h:424
unsigned int file_length
length of the resource file in bytes
Definition register.h:422
unsigned int file_offset
at which location the resource file starts within the executable or dll
Definition register.h:420
convenience class to register a resource file
Definition register.h:443
convenience class to register a resource string
Definition register.h:453
interfaces that add provides very basic functionality.
Definition register.h:203
declare an instance of test_registration as static variable in order to register a test function in a...
Definition register.h:405
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
representation of a token in a text by two pointers begin and end, that point to the first character ...
Definition token.h:18