libtrbase  1.0.2
Web server and task management solution.
i_class.c
Go to the documentation of this file.
1 /**
2  * \file
3  * The selector implementations for the class interface.
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 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <assert.h>
27 
28 #include "tr/class.h"
29 #include "tr/interface/class.h"
30 
31 #include "tr/memory.h"
32 
33 
34 /**
35  * Create and initialize the structures for a new interface called
36  * class.
37  * Interfaces and Classes may have the same name because internally
38  * everything that has to do with interfaces is prefixed with an i_
39  */
40 TR_CREATE_INTERFACE(TR_Class, 3);
41 
42 /*
43  * SELECTOR DEFINITION
44  * ===================
45  */
46 
47 /**
48  * Selector for the ctor implementation. This one accepts a va_list.
49  *
50  * \cond PRIVATE
51  */
52 void *
53 TR_classNewv(TR_class_ptr class, va_list * params)
54 {
55  void * object = TR_calloc(1, class->object_size + sizeof(void*));
56  int ret;
57 
58  * (TR_class_ptr *)object = class;
59  object += sizeof(void*);
60 
61  TR_RETCALL(object, TR_Class, ctor, ret, params);
62 
63  if (-1 == ret) {
64  TR_classDelete(&object);
65  }
66 
67  return object;
68 }
69 
70 /**
71  * Selector for the ctor implementation. This one accepts a variable
72  * amount of arguments.
73  */
74 void *
75 TR_classNew(TR_class_ptr class, ...)
76 {
77  va_list params;
78  void * object;
79 
80  va_start(params, class);
81  object = TR_classNewv(class, &params);
82  va_end(params);
83 
84  return object;
85 }
86 
87 /**
88  * Selector for the dtor implementation. This one accepts a variable
89  * amount of arguments.
90  */
91 void
92 TR_classDelete(void ** object)
93 {
94  if (NULL != *object) {
95  void * mem;
96 
97  TR_CALL(*object, TR_Class, dtor);
98 
99  mem = *object - sizeof(void*);
100  TR_MEM_FREE(mem);
101  *object = NULL;
102  }
103 }
104 
105 /**
106  * Selector for the clone implementation. This one accepts a variable
107  * amount of arguments.
108  */
109 void *
110 TR_classClone(void * _object)
111 {
112  TR_class_ptr class = TR_GET_CLASS(_object);
113  void * object = TR_calloc(1, class->object_size + sizeof(void*));
114 
115  * (TR_class_ptr *)object = class;
116  object += sizeof(void*);
117 
118 #undef clone
119  TR_CALL(object, TR_Class, clone, _object);
120 
121  return object;
122 }
123 /** endcond */
124 
125 /**
126  * Interface caller function for i_class::ctor.
127  * Call the constructor on an existing object identified by name.
128  * In fact it calls first the destructor to ensure that all
129  * possibly previously acquired resource are cleaned.
130  */
131 int
132 TR_objectInit(void * object, ...)
133 {
134  int ret;
135  va_list params;
136 
137  va_start(params, object);
138  TR_CALL(object, TR_Class, dtor);
139  TR_RETCALL(object, TR_Class, ctor, ret, &params);
140  va_end(params);
141 
142  return ret;
143 }
144 
145 // vim: set ts=4 sw=4:
#define TR_CALL(object, _iface, method,...)
Definition: class.h:256
#define TR_RETCALL(object, _iface, method, ret,...)
Definition: class.h:270
#define TR_MEM_FREE(seg)
Definition: memory.h:26
void * TR_calloc(size_t, size_t)
Definition: memory.c:242
TR_CREATE_INTERFACE(TR_Class, 3)