Commit 4b02cc239d9e05d727d9f32942ca5e7534fe20f3

Authored by Georg Hopp
1 parent 8c0277e8

more makros for classvars handling and add a new class SizedData.

... ... @@ -4,6 +4,7 @@ nobase_include_HEADERS = trbase.h \
4 4 tr/interface.h \
5 5 tr/memory.h \
6 6 tr/logger.h \
  7 + tr/sized_data.h \
7 8 tr/tree_macros.h \
8 9 tr/interface/class.h \
9 10 tr/interface/indexable.h \
... ...
... ... @@ -75,6 +75,16 @@
75 75
76 76 #define TR_CLASSVARS_DECL(name) struct c_##name##_vars
77 77 #define TR_CLASSVARS(name, class) ((struct c_##name##_vars *)(class)->vars)
  78 +#define TR_CLASSVARS_BY_NAME(name) \
  79 + ((struct c_##name##_vars *)(TR_CLASS_BY_NAME(name))->vars)
  80 +#define TR_CLASSVARS_STATIC(name) \
  81 + ((struct c_##name##_vars *)(TR_CLASS_BY_NAME_STATIC(name))->vars)
  82 +
  83 +#define TR_INHERIT_CLASSVARS(dest, src) \
  84 + memcpy(&c_##dest##_vars, \
  85 + TR_CLASSVARS_BY_NAME(src), \
  86 + sizeof(TR_CLASSVAR_DECL(src)))
  87 +
78 88
79 89 /**
80 90 * Make the new class a child of an existing class.
... ...
  1 +/**
  2 + * \file
  3 + * Combines a char * with size_t to get a generic data structure.
  4 + *
  5 + * \author Georg Hopp
  6 + *
  7 + * \copyright
  8 + * Copyright © 2014 Georg Hopp
  9 + *
  10 + * This program is free software: you can redistribute it and/or modify
  11 + * it under the terms of the GNU General Public License as published by
  12 + * the Free Software Foundation, either version 3 of the License, or
  13 + * (at your option) any later version.
  14 + *
  15 + * This program is distributed in the hope that it will be useful,
  16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18 + * GNU General Public License for more details.
  19 + *
  20 + * You should have received a copy of the GNU General Public License
  21 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22 + */
  23 +
  24 +#ifndef __TR_SIZED_DATA_H__
  25 +#define __TR_SIZED_DATA_H__
  26 +
  27 +#include <sys/types.h>
  28 +
  29 +#include "trbase.h"
  30 +
  31 +TR_CLASS(TR_SizedData) {
  32 + char * data;
  33 + size_t size;
  34 +};
  35 +TR_INSTANCE_INIT(TR_SizedData);
  36 +TR_CLASSVARS_DECL(TR_SizedData) {};
  37 +
  38 +void TR_sizedDataSetData(TR_SizedData, const char *, size_t);
  39 +
  40 +#endif // __TR_SIZED_DATA_H__
  41 +
  42 +// vim: set ts=4 sw=4:
... ...
... ... @@ -5,6 +5,7 @@
5 5 #include "tr/memory.h"
6 6 #include "tr/class.h"
7 7 #include "tr/logger.h"
  8 +#include "tr/sized_data.h"
8 9 #include "tr/interface.h"
9 10 #include "tr/interface/class.h"
10 11 #include "tr/interface/subject.h"
... ...
... ... @@ -8,6 +8,8 @@ TR_CLASS = memory.c \
8 8 logger.c \
9 9 stderr.c \
10 10 syslog.c \
  11 + sized_data.c \
  12 + sized_data_set_data.c \
11 13 i_class.c \
12 14 i_subject.c \
13 15 i_observer.c \
... ...
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2014 Georg Hopp
  8 + *
  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
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include <stdarg.h>
  24 +#include <string.h>
  25 +#include <sys/types.h>
  26 +
  27 +#include "trbase.h"
  28 +
  29 +#include "tr/sized_data.h"
  30 +
  31 +static
  32 +int
  33 +sizedDataCtor(void * _this, va_list * params)
  34 +{
  35 + TR_SizedData this = _this;
  36 + char * data = va_arg(*params, char *);
  37 +
  38 + this->size = va_arg(*params, size_t);
  39 +
  40 + if (data != NULL) {
  41 + this->data = TR_malloc(this->size);
  42 + memcpy(this->data, data, this->size);
  43 + }
  44 +
  45 + return 0;
  46 +}
  47 +
  48 +static
  49 +void
  50 +sizedDataDtor(void * _this)
  51 +{
  52 + TR_MEM_FREE(((TR_SizedData)_this)->data);
  53 +}
  54 +
  55 +TR_INIT_IFACE(TR_Class, sizedDataCtor, sizedDataDtor, NULL);
  56 +TR_CREATE_CLASS(TR_SizedData, NULL, NULL, TR_IF(TR_Class));
  57 +
  58 +// vim: set ts=4 sw=4:
... ...
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2014 Georg Hopp
  8 + *
  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
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include <string.h> // for atoi() and exit()
  24 +#include <errno.h> // for errno
  25 +#include <stdio.h>
  26 +#include <unistd.h>
  27 +
  28 +#include "tr/sized_data.h"
  29 +
  30 +void
  31 +TR_sizedDataSetData(TR_SizedData this, const char * data, size_t size)
  32 +{
  33 + if (this->data && TR_getSize(this->data) < size) {
  34 + TR_MEM_FREE(this->data);
  35 + }
  36 +
  37 + if (! this->data) {
  38 + this->data = TR_malloc(size);
  39 + }
  40 +
  41 + this->size = size;
  42 + memcpy(this->data, data, size);
  43 +}
  44 +
  45 +// vim: set ts=4 sw=4:
... ...
Please register or login to post a comment