Browse Source

added classvars. This again changes the interface for class declaration and definition.

1.0.2
Georg Hopp 12 years ago
parent
commit
7d39785a79
  1. 20
      include/tr/class.h
  2. 6
      include/tr/logger.h
  3. 4
      src/logger.c
  4. 4
      src/stderr.c
  5. 4
      src/syslog.c

20
include/tr/class.h

@ -20,7 +20,7 @@
* \author Georg Hopp * \author Georg Hopp
* *
* \copyright * \copyright
* Copyright © 2012-2013 Georg Hopp
* Copyright © 2014 Georg Hopp
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -73,6 +73,9 @@
struct c_##name data; \ 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. * Make the new class a child of an existing class.
* This is used within the class declaration and can * This is used within the class declaration and can
@ -80,8 +83,8 @@
* is undefined, but most likely the resulting code won't * is undefined, but most likely the resulting code won't
* even compile. * 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, * Some macros might translate a give NULL to _NULL,
@ -103,9 +106,13 @@
* for the ctor interface else no instances can be * for the ctor interface else no instances can be
* created. * created.
*/ */
#define TR_CREATE_CLASS(name,_parent,...) \
#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) { \ static TR_class_ptr _classInit##name##_(void) { \
c_##name.parent = _##_parent; \ c_##name.parent = _##_parent; \
if (TR_initClassVars##name) \
TR_initClassVars##name(_##name); \
c_##name.init = NULL; \ c_##name.init = NULL; \
return &c_##name; \ return &c_##name; \
}; \ }; \
@ -114,9 +121,11 @@
NULL, \ NULL, \
sizeof(struct c_##name), \ sizeof(struct c_##name), \
_classInit##name##_, \ _classInit##name##_, \
&c_vars, \
TR_INIT_IFACE_IMPL(__VA_ARGS__) \ TR_INIT_IFACE_IMPL(__VA_ARGS__) \
}; \ }; \
struct TR_class * const _##name = &c_##name
struct TR_class * const _##name = &c_##name; \
struct c_##name##_vars c_vars
/** /**
* Create a static instance of a class. * Create a static instance of a class.
@ -297,6 +306,7 @@ struct TR_class {
TR_class_ptr parent; TR_class_ptr parent;
size_t object_size; size_t object_size;
TR_fptr_classInit init; TR_fptr_classInit init;
void * vars;
struct TR_iface_impl impl; struct TR_iface_impl impl;
}; };
/** \endcond */ /** \endcond */

6
include/tr/logger.h

@ -6,7 +6,7 @@
* \author Georg Hopp * \author Georg Hopp
* *
* \copyright * \copyright
* Copyright © 2012 Georg Hopp
* Copyright © 2014 Georg Hopp
* *
* This program is free software: you can redistribute it and/or modify * 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 * 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_LoggerStderr);
TR_INSTANCE_INIT(TR_LoggerSyslog); 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; extern TR_Logger TR_logger;
#endif // __TR_LOGGER_H__ #endif // __TR_LOGGER_H__

4
src/logger.c

@ -4,7 +4,7 @@
* \author Georg Hopp * \author Georg Hopp
* *
* \copyright * \copyright
* Copyright © 2012 Georg Hopp
* Copyright © 2014 Georg Hopp
* *
* This program is free software: you can redistribute it and/or modify * 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 * 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) {} static void loggerDtor(void * _this) {}
TR_INIT_IFACE(TR_Class, loggerCtor, loggerDtor, NULL); 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: // vim: set ts=4 sw=4:

4
src/stderr.c

@ -4,7 +4,7 @@
* \author Georg Hopp * \author Georg Hopp
* *
* \copyright * \copyright
* Copyright © 2012 Georg Hopp
* Copyright © 2014 Georg Hopp
* *
* This program is free software: you can redistribute it and/or modify * 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 * 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_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_INSTANCE(TR_LoggerStderr, TR_debugConlogger, {TR_LOGGER_DEBUG});
TR_Logger TR_logger = TR_INSTANCE_CAST(TR_Logger, TR_debugConlogger); TR_Logger TR_logger = TR_INSTANCE_CAST(TR_Logger, TR_debugConlogger);

4
src/syslog.c

@ -4,7 +4,7 @@
* \author Georg Hopp * \author Georg Hopp
* *
* \copyright * \copyright
* Copyright © 2012 Georg Hopp
* Copyright © 2014 Georg Hopp
* *
* This program is free software: you can redistribute it and/or modify * 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 * 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_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: // vim: set ts=4 sw=4:
Loading…
Cancel
Save