taskrambler  0.1.8
Web server and task management solution.
i_class.c
Go to the documentation of this file.
1 /**
2  * \file
3  *
4  * \author Georg Hopp
5  *
6  * \copyright
7  * Copyright © 2012 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 <stdlib.h>
25 #include <assert.h>
26 
27 #include "class/class.h"
28 #include "class/interface/class.h"
29 
30 #include "utils/memory.h"
31 
32 
33 const
34 struct interface i_Class = {
35  "class",
36  3
37 };
38 
39 void *
40 classNewParams(class_ptr class, va_list * params)
41 {
42  void * object = memCalloc(1, class->object_size + sizeof(void*));
43  int ret;
44 
45  * (class_ptr *)object = class;
46  object += sizeof(void*);
47 
48  RETCALL(object, Class, ctor, ret, params);
49 
50  if (-1 == ret) {
51  classDelete(&object);
52  }
53 
54  return object;
55 }
56 
57 void *
58 classNew(class_ptr class, ...)
59 {
60  va_list params;
61  void * object;
62 
63  va_start(params, class);
64  object = classNewParams(class, &params);
65  va_end(params);
66 
67  return object;
68 }
69 
70 void
71 classDelete(void ** object)
72 {
73  if (NULL != *object) {
74  void * mem;
75 
76  CALL(*object, Class, dtor);
77 
78  mem = *object - sizeof(void*);
79  MEM_FREE(mem);
80  *object = NULL;
81  }
82 }
83 
84 void *
85 classClone(void * _object)
86 {
87  class_ptr class = GET_CLASS(_object);
88  void * object = memCalloc(1, class->object_size + sizeof(void*));
89 
90  * (class_ptr *)object = class;
91  object += sizeof(void*);
92 
93 #undef clone
94  CALL(object, Class, clone, _object);
95 
96  return object;
97 }
98 
99 // vim: set ts=4 sw=4:
#define MEM_FREE(seg)
Definition: memory.h:28
void classDelete(void **object)
Definition: i_class.c:71
#define CALL(object, _iface, method,...)
Definition: class/class.h:106
void * classNew(class_ptr class,...)
Definition: i_class.c:58
void * classClone(void *_object)
Definition: i_class.c:85
#define clone(object)
#define RETCALL(object, _iface, method, ret,...)
Definition: class/class.h:113
void * memCalloc(size_t, size_t)
Definition: memory.c:818
void * classNewParams(class_ptr class, va_list *params)
Definition: i_class.c:40
size_t object_size
Definition: class/class.h:136