i_class.c
3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* \file
* The selector implementations for the class interface.
*
* \author Georg Hopp
*
* \copyright
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#include <stdlib.h>
#include <assert.h>
#include "tr/class.h"
#include "tr/interface/class.h"
#include "tr/memory.h"
/**
* Create and initialize the structures for a new interface called
* class.
* Interfaces and Classes may have the same name because internally
* everything that has to do with interfaces is prefixed with an i_
*/
TR_CREATE_INTERFACE(TR_Class, 3);
/*
* SELECTOR DEFINITION
* ===================
*/
/**
* Selector for the ctor implementation. This one accepts a va_list.
*
* \cond PRIVATE
*/
void *
TR_classNewv(TR_class_ptr class, va_list * params)
{
void * object = TR_calloc(1, class->object_size + sizeof(void*));
int ret;
* (TR_class_ptr *)object = class;
object += sizeof(void*);
TR_RETCALL(object, TR_Class, ctor, ret, params);
if (-1 == ret) {
TR_classDelete(&object);
}
return object;
}
/**
* Selector for the ctor implementation. This one accepts a variable
* amount of arguments.
*/
void *
TR_classNew(TR_class_ptr class, ...)
{
va_list params;
void * object;
va_start(params, class);
object = TR_classNewv(class, ¶ms);
va_end(params);
return object;
}
/**
* Selector for the dtor implementation. This one accepts a variable
* amount of arguments.
*/
void
TR_classDelete(void ** object)
{
if (NULL != *object) {
void * mem;
TR_CALL(*object, TR_Class, dtor);
mem = *object - sizeof(void*);
TR_MEM_FREE(mem);
*object = NULL;
}
}
/**
* Selector for the clone implementation. This one accepts a variable
* amount of arguments.
*/
void *
TR_classClone(void * _object)
{
TR_class_ptr class = TR_GET_CLASS(_object);
void * object = TR_calloc(1, class->object_size + sizeof(void*));
* (TR_class_ptr *)object = class;
object += sizeof(void*);
#undef clone
TR_CALL(object, TR_Class, clone, _object);
return object;
}
/** endcond */
/**
* Interface caller function for i_class::ctor.
* Call the constructor on an existing object identified by name.
* In fact it calls first the destructor to ensure that all
* possibly previously acquired resource are cleaned.
*/
int
TR_objectInit(void * object, ...)
{
int ret;
va_list params;
va_start(params, object);
TR_CALL(object, TR_Class, dtor);
TR_RETCALL(object, TR_Class, ctor, ret, ¶ms);
va_end(params);
return ret;
}
// vim: set ts=4 sw=4: