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. 54
      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

54
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 */

6
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__

4
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:

4
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);

4
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:
Loading…
Cancel
Save