From c5c48bb32051fa8e3fc6755475fb72276cd43482 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Thu, 31 Jul 2014 14:48:29 +0100 Subject: [PATCH] modify static instance generation so that static instances of a class can now be created anywhere. But the class needs to be prepared for this after its definition (TR_INSTANCE_INIT). --- include/tr/class.h | 17 +++++++++-------- include/tr/logger.h | 4 ++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/include/tr/class.h b/include/tr/class.h index add6419..98487eb 100644 --- a/include/tr/class.h +++ b/include/tr/class.h @@ -61,11 +61,18 @@ * that are needed to create a new class. */ #define TR_CLASS(name) \ + struct TR_class c_##name; \ struct c_##name; \ typedef struct c_##name * name; \ extern struct TR_class * const _##name; \ struct c_##name +#define TR_INSTANCE_INIT(name) \ + struct c_##name##_object { \ + void * TR_class; \ + struct c_##name data; \ + } + /** * Make the new class a child of an existing class. * This is used within the class declaration and can @@ -97,25 +104,19 @@ * created. */ #define TR_CREATE_CLASS(name,_parent,...) \ - static struct TR_class c_##name; \ static TR_class_ptr _classInit##name##_(void) { \ c_##name.parent = _##_parent; \ c_##name.init = NULL; \ return &c_##name; \ }; \ - static struct TR_class c_##name = { \ + struct TR_class c_##name = { \ TR_CLASS_MAGIC, \ NULL, \ sizeof(struct c_##name), \ _classInit##name##_, \ TR_INIT_IFACE_IMPL(__VA_ARGS__) \ }; \ - struct TR_class * const _##name = &c_##name; \ - struct c_##name##_object { \ - void * TR_class; \ - struct c_##name data; \ - } - + struct TR_class * const _##name = &c_##name /** * Create a static instance of a class. diff --git a/include/tr/logger.h b/include/tr/logger.h index 3ebd06d..9ef8b28 100644 --- a/include/tr/logger.h +++ b/include/tr/logger.h @@ -54,6 +54,10 @@ TR_CLASS(TR_LoggerSyslog) { TR_EXTENDS(TR_Logger); }; +TR_INSTANCE_INIT(TR_Logger); +TR_INSTANCE_INIT(TR_LoggerStderr); +TR_INSTANCE_INIT(TR_LoggerSyslog); + extern TR_Logger TR_logger; #endif // __TR_LOGGER_H__