Commit c5c48bb32051fa8e3fc6755475fb72276cd43482

Authored by Georg Hopp
1 parent bbf4463f

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).
@@ -61,11 +61,18 @@ @@ -61,11 +61,18 @@
61 * that are needed to create a new class. 61 * that are needed to create a new class.
62 */ 62 */
63 #define TR_CLASS(name) \ 63 #define TR_CLASS(name) \
  64 + struct TR_class c_##name; \
64 struct c_##name; \ 65 struct c_##name; \
65 typedef struct c_##name * name; \ 66 typedef struct c_##name * name; \
66 extern struct TR_class * const _##name; \ 67 extern struct TR_class * const _##name; \
67 struct c_##name 68 struct c_##name
68 69
  70 +#define TR_INSTANCE_INIT(name) \
  71 + struct c_##name##_object { \
  72 + void * TR_class; \
  73 + struct c_##name data; \
  74 + }
  75 +
69 /** 76 /**
70 * Make the new class a child of an existing class. 77 * Make the new class a child of an existing class.
71 * This is used within the class declaration and can 78 * This is used within the class declaration and can
@@ -97,25 +104,19 @@ @@ -97,25 +104,19 @@
97 * created. 104 * created.
98 */ 105 */
99 #define TR_CREATE_CLASS(name,_parent,...) \ 106 #define TR_CREATE_CLASS(name,_parent,...) \
100 - static struct TR_class c_##name; \  
101 static TR_class_ptr _classInit##name##_(void) { \ 107 static TR_class_ptr _classInit##name##_(void) { \
102 c_##name.parent = _##_parent; \ 108 c_##name.parent = _##_parent; \
103 c_##name.init = NULL; \ 109 c_##name.init = NULL; \
104 return &c_##name; \ 110 return &c_##name; \
105 }; \ 111 }; \
106 - static struct TR_class c_##name = { \ 112 + struct TR_class c_##name = { \
107 TR_CLASS_MAGIC, \ 113 TR_CLASS_MAGIC, \
108 NULL, \ 114 NULL, \
109 sizeof(struct c_##name), \ 115 sizeof(struct c_##name), \
110 _classInit##name##_, \ 116 _classInit##name##_, \
111 TR_INIT_IFACE_IMPL(__VA_ARGS__) \ 117 TR_INIT_IFACE_IMPL(__VA_ARGS__) \
112 }; \ 118 }; \
113 - struct TR_class * const _##name = &c_##name; \  
114 - struct c_##name##_object { \  
115 - void * TR_class; \  
116 - struct c_##name data; \  
117 - }  
118 - 119 + struct TR_class * const _##name = &c_##name
119 120
120 /** 121 /**
121 * Create a static instance of a class. 122 * Create a static instance of a class.
@@ -54,6 +54,10 @@ TR_CLASS(TR_LoggerSyslog) { @@ -54,6 +54,10 @@ TR_CLASS(TR_LoggerSyslog) {
54 TR_EXTENDS(TR_Logger); 54 TR_EXTENDS(TR_Logger);
55 }; 55 };
56 56
  57 +TR_INSTANCE_INIT(TR_Logger);
  58 +TR_INSTANCE_INIT(TR_LoggerStderr);
  59 +TR_INSTANCE_INIT(TR_LoggerSyslog);
  60 +
57 extern TR_Logger TR_logger; 61 extern TR_Logger TR_logger;
58 62
59 #endif // __TR_LOGGER_H__ 63 #endif // __TR_LOGGER_H__
Please register or login to post a comment