From 7d39785a79b8d25c6cb5ed1b6a4b057c5c212631 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Sun, 3 Aug 2014 07:39:35 +0100 Subject: [PATCH] added classvars. This again changes the interface for class declaration and definition. --- include/tr/class.h | 54 +++++++++++++++++++++++++++------------------ include/tr/logger.h | 6 ++++- src/logger.c | 4 ++-- src/stderr.c | 4 ++-- src/syslog.c | 4 ++-- 5 files changed, 43 insertions(+), 29 deletions(-) diff --git a/include/tr/class.h b/include/tr/class.h index e76126c..63f5fd8 100644 --- a/include/tr/class.h +++ b/include/tr/class.h @@ -20,7 +20,7 @@ * \author Georg Hopp * * \copyright - * Copyright © 2012-2013 Georg Hopp + * Copyright © 2014 Georg Hopp * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -73,6 +73,9 @@ struct c_##name data; \ } +#define TR_CLASSVARS_DECL(name) struct c_##name##_vars +#define TR_CLASSVARS(name, class) ((struct c_##name##_vars *)(class)->vars) + /** * Make the new class a child of an existing class. * This is used within the class declaration and can @@ -80,8 +83,8 @@ * is undefined, but most likely the resulting code won't * even compile. */ -#define TR_EXTENDS(parent) \ - const char _[sizeof(struct c_##parent)] +#define TR_EXTENDS(parent) const char _[sizeof(struct c_##parent)] +#define TR_CV_EXTENDS(parent) const char _[sizeof(struct c_##parent##_vars)] /** * Some macros might translate a give NULL to _NULL, @@ -103,20 +106,26 @@ * for the ctor interface else no instances can be * created. */ -#define TR_CREATE_CLASS(name,_parent,...) \ - static TR_class_ptr _classInit##name##_(void) { \ - c_##name.parent = _##_parent; \ - c_##name.init = NULL; \ - return &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 +#define TR_CREATE_CLASS(name,_parent,cvInit,...) \ + struct c_##name##_vars c_vars; \ + void (* TR_initClassVars##name)(TR_class_ptr) = cvInit; \ + static TR_class_ptr _classInit##name##_(void) { \ + c_##name.parent = _##_parent; \ + if (TR_initClassVars##name) \ + TR_initClassVars##name(_##name); \ + c_##name.init = NULL; \ + return &c_##name; \ + }; \ + struct TR_class c_##name = { \ + TR_CLASS_MAGIC, \ + NULL, \ + sizeof(struct c_##name), \ + _classInit##name##_, \ + &c_vars, \ + TR_INIT_IFACE_IMPL(__VA_ARGS__) \ + }; \ + struct TR_class * const _##name = &c_##name; \ + struct c_##name##_vars c_vars /** * Create a static instance of a class. @@ -293,11 +302,12 @@ struct TR_class; typedef struct TR_class * TR_class_ptr; typedef TR_class_ptr (* TR_fptr_classInit)(void); struct TR_class { - const int magic; - TR_class_ptr parent; - size_t object_size; - TR_fptr_classInit init; - struct TR_iface_impl impl; + const int magic; + TR_class_ptr parent; + size_t object_size; + TR_fptr_classInit init; + void * vars; + struct TR_iface_impl impl; }; /** \endcond */ diff --git a/include/tr/logger.h b/include/tr/logger.h index 9ef8b28..15946f7 100644 --- a/include/tr/logger.h +++ b/include/tr/logger.h @@ -6,7 +6,7 @@ * \author Georg Hopp * * \copyright - * Copyright © 2012 Georg Hopp + * Copyright © 2014 Georg Hopp * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -58,6 +58,10 @@ TR_INSTANCE_INIT(TR_Logger); TR_INSTANCE_INIT(TR_LoggerStderr); TR_INSTANCE_INIT(TR_LoggerSyslog); +TR_CLASSVARS_DECL(TR_Logger) {}; +TR_CLASSVARS_DECL(TR_LoggerStderr) {}; +TR_CLASSVARS_DECL(TR_LoggerSyslog) {}; + extern TR_Logger TR_logger; #endif // __TR_LOGGER_H__ diff --git a/src/logger.c b/src/logger.c index eb24930..5f9dfc0 100644 --- a/src/logger.c +++ b/src/logger.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright © 2012 Georg Hopp + * Copyright © 2014 Georg Hopp * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -52,6 +52,6 @@ loggerCtor(void * _this, va_list * params) static void loggerDtor(void * _this) {} TR_INIT_IFACE(TR_Class, loggerCtor, loggerDtor, NULL); -TR_CREATE_CLASS(TR_Logger, NULL, TR_IF(TR_Class)); +TR_CREATE_CLASS(TR_Logger, NULL, NULL, TR_IF(TR_Class)); // vim: set ts=4 sw=4: diff --git a/src/stderr.c b/src/stderr.c index aa3f4e6..11ac2d3 100644 --- a/src/stderr.c +++ b/src/stderr.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright © 2012 Georg Hopp + * Copyright © 2014 Georg Hopp * * This program is free software: you can redistribute it and/or modify * 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) } TR_INIT_IFACE(TR_Logger, logStderr); -TR_CREATE_CLASS(TR_LoggerStderr, TR_Logger, TR_IF(TR_Logger)); +TR_CREATE_CLASS(TR_LoggerStderr, TR_Logger, NULL, TR_IF(TR_Logger)); TR_INSTANCE(TR_LoggerStderr, TR_debugConlogger, {TR_LOGGER_DEBUG}); TR_Logger TR_logger = TR_INSTANCE_CAST(TR_Logger, TR_debugConlogger); diff --git a/src/syslog.c b/src/syslog.c index 8e226e7..3e734a9 100644 --- a/src/syslog.c +++ b/src/syslog.c @@ -4,7 +4,7 @@ * \author Georg Hopp * * \copyright - * Copyright © 2012 Georg Hopp + * Copyright © 2014 Georg Hopp * * This program is free software: you can redistribute it and/or modify * 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) } TR_INIT_IFACE(TR_Logger, logSyslog); -TR_CREATE_CLASS(TR_LoggerSyslog, TR_Logger, TR_IF(TR_Logger)); +TR_CREATE_CLASS(TR_LoggerSyslog, TR_Logger, NULL, TR_IF(TR_Logger)); // vim: set ts=4 sw=4: