Commit 7d39785a79b8d25c6cb5ed1b6a4b057c5c212631
1 parent
6a5d1668
added classvars. This again changes the interface for class declaration and definition.
Showing
5 changed files
with
43 additions
and
29 deletions
| @@ -20,7 +20,7 @@ | @@ -20,7 +20,7 @@ | ||
| 20 | * \author Georg Hopp | 20 | * \author Georg Hopp |
| 21 | * | 21 | * |
| 22 | * \copyright | 22 | * \copyright |
| 23 | - * Copyright © 2012-2013 Georg Hopp | 23 | + * Copyright © 2014 Georg Hopp |
| 24 | * | 24 | * |
| 25 | * This program is free software: you can redistribute it and/or modify | 25 | * This program is free software: you can redistribute it and/or modify |
| 26 | * it under the terms of the GNU General Public License as published by | 26 | * it under the terms of the GNU General Public License as published by |
| @@ -73,6 +73,9 @@ | @@ -73,6 +73,9 @@ | ||
| 73 | struct c_##name data; \ | 73 | struct c_##name data; \ |
| 74 | } | 74 | } |
| 75 | 75 | ||
| 76 | +#define TR_CLASSVARS_DECL(name) struct c_##name##_vars | ||
| 77 | +#define TR_CLASSVARS(name, class) ((struct c_##name##_vars *)(class)->vars) | ||
| 78 | + | ||
| 76 | /** | 79 | /** |
| 77 | * Make the new class a child of an existing class. | 80 | * Make the new class a child of an existing class. |
| 78 | * This is used within the class declaration and can | 81 | * This is used within the class declaration and can |
| @@ -80,8 +83,8 @@ | @@ -80,8 +83,8 @@ | ||
| 80 | * is undefined, but most likely the resulting code won't | 83 | * is undefined, but most likely the resulting code won't |
| 81 | * even compile. | 84 | * even compile. |
| 82 | */ | 85 | */ |
| 83 | -#define TR_EXTENDS(parent) \ | ||
| 84 | - const char _[sizeof(struct c_##parent)] | 86 | +#define TR_EXTENDS(parent) const char _[sizeof(struct c_##parent)] |
| 87 | +#define TR_CV_EXTENDS(parent) const char _[sizeof(struct c_##parent##_vars)] | ||
| 85 | 88 | ||
| 86 | /** | 89 | /** |
| 87 | * Some macros might translate a give NULL to _NULL, | 90 | * Some macros might translate a give NULL to _NULL, |
| @@ -103,20 +106,26 @@ | @@ -103,20 +106,26 @@ | ||
| 103 | * for the ctor interface else no instances can be | 106 | * for the ctor interface else no instances can be |
| 104 | * created. | 107 | * created. |
| 105 | */ | 108 | */ |
| 106 | -#define TR_CREATE_CLASS(name,_parent,...) \ | ||
| 107 | - static TR_class_ptr _classInit##name##_(void) { \ | ||
| 108 | - c_##name.parent = _##_parent; \ | ||
| 109 | - c_##name.init = NULL; \ | ||
| 110 | - return &c_##name; \ | ||
| 111 | - }; \ | ||
| 112 | - struct TR_class c_##name = { \ | ||
| 113 | - TR_CLASS_MAGIC, \ | ||
| 114 | - NULL, \ | ||
| 115 | - sizeof(struct c_##name), \ | ||
| 116 | - _classInit##name##_, \ | ||
| 117 | - TR_INIT_IFACE_IMPL(__VA_ARGS__) \ | ||
| 118 | - }; \ | ||
| 119 | - struct TR_class * const _##name = &c_##name | 109 | +#define TR_CREATE_CLASS(name,_parent,cvInit,...) \ |
| 110 | + struct c_##name##_vars c_vars; \ | ||
| 111 | + void (* TR_initClassVars##name)(TR_class_ptr) = cvInit; \ | ||
| 112 | + static TR_class_ptr _classInit##name##_(void) { \ | ||
| 113 | + c_##name.parent = _##_parent; \ | ||
| 114 | + if (TR_initClassVars##name) \ | ||
| 115 | + TR_initClassVars##name(_##name); \ | ||
| 116 | + c_##name.init = NULL; \ | ||
| 117 | + return &c_##name; \ | ||
| 118 | + }; \ | ||
| 119 | + struct TR_class c_##name = { \ | ||
| 120 | + TR_CLASS_MAGIC, \ | ||
| 121 | + NULL, \ | ||
| 122 | + sizeof(struct c_##name), \ | ||
| 123 | + _classInit##name##_, \ | ||
| 124 | + &c_vars, \ | ||
| 125 | + TR_INIT_IFACE_IMPL(__VA_ARGS__) \ | ||
| 126 | + }; \ | ||
| 127 | + struct TR_class * const _##name = &c_##name; \ | ||
| 128 | + struct c_##name##_vars c_vars | ||
| 120 | 129 | ||
| 121 | /** | 130 | /** |
| 122 | * Create a static instance of a class. | 131 | * Create a static instance of a class. |
| @@ -293,11 +302,12 @@ struct TR_class; | @@ -293,11 +302,12 @@ struct TR_class; | ||
| 293 | typedef struct TR_class * TR_class_ptr; | 302 | typedef struct TR_class * TR_class_ptr; |
| 294 | typedef TR_class_ptr (* TR_fptr_classInit)(void); | 303 | typedef TR_class_ptr (* TR_fptr_classInit)(void); |
| 295 | struct TR_class { | 304 | struct TR_class { |
| 296 | - const int magic; | ||
| 297 | - TR_class_ptr parent; | ||
| 298 | - size_t object_size; | ||
| 299 | - TR_fptr_classInit init; | ||
| 300 | - struct TR_iface_impl impl; | 305 | + const int magic; |
| 306 | + TR_class_ptr parent; | ||
| 307 | + size_t object_size; | ||
| 308 | + TR_fptr_classInit init; | ||
| 309 | + void * vars; | ||
| 310 | + struct TR_iface_impl impl; | ||
| 301 | }; | 311 | }; |
| 302 | /** \endcond */ | 312 | /** \endcond */ |
| 303 | 313 |
| @@ -6,7 +6,7 @@ | @@ -6,7 +6,7 @@ | ||
| 6 | * \author Georg Hopp | 6 | * \author Georg Hopp |
| 7 | * | 7 | * |
| 8 | * \copyright | 8 | * \copyright |
| 9 | - * Copyright © 2012 Georg Hopp | 9 | + * Copyright © 2014 Georg Hopp |
| 10 | * | 10 | * |
| 11 | * This program is free software: you can redistribute it and/or modify | 11 | * This program is free software: you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by | 12 | * it under the terms of the GNU General Public License as published by |
| @@ -58,6 +58,10 @@ TR_INSTANCE_INIT(TR_Logger); | @@ -58,6 +58,10 @@ TR_INSTANCE_INIT(TR_Logger); | ||
| 58 | TR_INSTANCE_INIT(TR_LoggerStderr); | 58 | TR_INSTANCE_INIT(TR_LoggerStderr); |
| 59 | TR_INSTANCE_INIT(TR_LoggerSyslog); | 59 | TR_INSTANCE_INIT(TR_LoggerSyslog); |
| 60 | 60 | ||
| 61 | +TR_CLASSVARS_DECL(TR_Logger) {}; | ||
| 62 | +TR_CLASSVARS_DECL(TR_LoggerStderr) {}; | ||
| 63 | +TR_CLASSVARS_DECL(TR_LoggerSyslog) {}; | ||
| 64 | + | ||
| 61 | extern TR_Logger TR_logger; | 65 | extern TR_Logger TR_logger; |
| 62 | 66 | ||
| 63 | #endif // __TR_LOGGER_H__ | 67 | #endif // __TR_LOGGER_H__ |
| @@ -4,7 +4,7 @@ | @@ -4,7 +4,7 @@ | ||
| 4 | * \author Georg Hopp | 4 | * \author Georg Hopp |
| 5 | * | 5 | * |
| 6 | * \copyright | 6 | * \copyright |
| 7 | - * Copyright © 2012 Georg Hopp | 7 | + * Copyright © 2014 Georg Hopp |
| 8 | * | 8 | * |
| 9 | * This program is free software: you can redistribute it and/or modify | 9 | * This program is free software: you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by | 10 | * it under the terms of the GNU General Public License as published by |
| @@ -52,6 +52,6 @@ loggerCtor(void * _this, va_list * params) | @@ -52,6 +52,6 @@ loggerCtor(void * _this, va_list * params) | ||
| 52 | static void loggerDtor(void * _this) {} | 52 | static void loggerDtor(void * _this) {} |
| 53 | 53 | ||
| 54 | TR_INIT_IFACE(TR_Class, loggerCtor, loggerDtor, NULL); | 54 | TR_INIT_IFACE(TR_Class, loggerCtor, loggerDtor, NULL); |
| 55 | -TR_CREATE_CLASS(TR_Logger, NULL, TR_IF(TR_Class)); | 55 | +TR_CREATE_CLASS(TR_Logger, NULL, NULL, TR_IF(TR_Class)); |
| 56 | 56 | ||
| 57 | // vim: set ts=4 sw=4: | 57 | // vim: set ts=4 sw=4: |
| @@ -4,7 +4,7 @@ | @@ -4,7 +4,7 @@ | ||
| 4 | * \author Georg Hopp | 4 | * \author Georg Hopp |
| 5 | * | 5 | * |
| 6 | * \copyright | 6 | * \copyright |
| 7 | - * Copyright © 2012 Georg Hopp | 7 | + * Copyright © 2014 Georg Hopp |
| 8 | * | 8 | * |
| 9 | * This program is free software: you can redistribute it and/or modify | 9 | * This program is free software: you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by | 10 | * it under the terms of the GNU General Public License as published by |
| @@ -34,7 +34,7 @@ logStderr(void * this, TR_logger_level level, const char * const msg) | @@ -34,7 +34,7 @@ logStderr(void * this, TR_logger_level level, const char * const msg) | ||
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | TR_INIT_IFACE(TR_Logger, logStderr); | 36 | TR_INIT_IFACE(TR_Logger, logStderr); |
| 37 | -TR_CREATE_CLASS(TR_LoggerStderr, TR_Logger, TR_IF(TR_Logger)); | 37 | +TR_CREATE_CLASS(TR_LoggerStderr, TR_Logger, NULL, TR_IF(TR_Logger)); |
| 38 | 38 | ||
| 39 | TR_INSTANCE(TR_LoggerStderr, TR_debugConlogger, {TR_LOGGER_DEBUG}); | 39 | TR_INSTANCE(TR_LoggerStderr, TR_debugConlogger, {TR_LOGGER_DEBUG}); |
| 40 | TR_Logger TR_logger = TR_INSTANCE_CAST(TR_Logger, TR_debugConlogger); | 40 | TR_Logger TR_logger = TR_INSTANCE_CAST(TR_Logger, TR_debugConlogger); |
| @@ -4,7 +4,7 @@ | @@ -4,7 +4,7 @@ | ||
| 4 | * \author Georg Hopp | 4 | * \author Georg Hopp |
| 5 | * | 5 | * |
| 6 | * \copyright | 6 | * \copyright |
| 7 | - * Copyright © 2012 Georg Hopp | 7 | + * Copyright © 2014 Georg Hopp |
| 8 | * | 8 | * |
| 9 | * This program is free software: you can redistribute it and/or modify | 9 | * This program is free software: you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by | 10 | * it under the terms of the GNU General Public License as published by |
| @@ -47,6 +47,6 @@ logSyslog(void * this, TR_logger_level level, const char * const msg) | @@ -47,6 +47,6 @@ logSyslog(void * this, TR_logger_level level, const char * const msg) | ||
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | TR_INIT_IFACE(TR_Logger, logSyslog); | 49 | TR_INIT_IFACE(TR_Logger, logSyslog); |
| 50 | -TR_CREATE_CLASS(TR_LoggerSyslog, TR_Logger, TR_IF(TR_Logger)); | 50 | +TR_CREATE_CLASS(TR_LoggerSyslog, TR_Logger, NULL, TR_IF(TR_Logger)); |
| 51 | 51 | ||
| 52 | // vim: set ts=4 sw=4: | 52 | // vim: set ts=4 sw=4: |
Please
register
or
login
to post a comment