libtrbase  1.0.2
Web server and task management solution.
interface/class.h
Go to the documentation of this file.
1 /**
2  * \file
3  * Interface for class handling. Defines new, delete and clone selectors
4  * which in turn use the ctor, dtor and clone implementation from the
5  * class implementation.
6  *
7  * \author Georg Hopp
8  *
9  * \copyright
10  * Copyright © 2014 Georg Hopp
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #ifndef __TR_CLASS_INTERFACE_CLASS_H__
27 #define __TR_CLASS_INTERFACE_CLASS_H__
28 
29 #include <stdarg.h>
30 
31 #include "tr/class.h"
32 #include "tr/interface.h"
33 
34 typedef int (* fptr_ctor)(void *, va_list *);
35 typedef void (* fptr_dtor)(void *);
36 typedef void (* fptr_clone)(void *, void * const);
37 
38 /**
39  * The interface structure for the class interface.
40  * Each class that implements this interface (and that
41  * should be all classes) need to provide at least
42  * a funtion pointer for the fptr_ctor. Else no
43  * instances can be created.
44  */
45 TR_INTERFACE(TR_Class) {
46  TR_IFID;
47  fptr_ctor ctor;
48  fptr_dtor dtor;
49  fptr_clone clone;
50 };
51 
52 /**
53  * Interface caller function for i_class::ctor.
54  * This one will never be called direcly but only through
55  * TR_new.
56  *
57  * \see TR_classNewv
58  * \see TR_new
59  *
60  * \cond PRIVATE
61  */
62 void * TR_classNew(TR_class_ptr, ...);
63 
64 /**
65  * This is another caller function for i_class:ctor but
66  * this uses a va_list for the argument to the constructor
67  * instead of an ellipse.
68  * Again this will only be called through TR_newv.
69  *
70  * \see TR_newv
71  */
72 void * TR_classNewv(TR_class_ptr, va_list *);
73 
74 /**
75  * Interface caller function for i_class::ctor.
76  * This is called when an instance of a class is created
77  * with TR_INSTANCE (created on the stack).
78  * Never called directly.
79  *
80  * \see TR_delete
81  */
82 int TR_objectInit(void *, ...);
83 
84 /**
85  * Interface caller function for i_class::dtor.
86  * Never called directly.
87  *
88  * \see TR_delete
89  */
90 void TR_classDelete(void **);
91 
92 /**
93  * Interface caller function for i_class::clone
94  * Never called directly.
95  *
96  * \see TR_delete
97  */
98 void * TR_classClone(void *);
99 /** \endcond */
100 
101 /**
102  * Create an instance of a class by calling its constructor
103  * implementation after initialization and memory allocation
104  * of the class itself.
105  */
106 #define TR_new(class,...) TR_classNew(_##class, ##__VA_ARGS__)
107 
108 /**
109  * Create an instance of a class by calling its constructor
110  * implementation but this time with a va_list instead of a
111  * variable amount of arguments.
112  */
113 #define TR_newv(class,args) TR_classNewv(_##class, args)
114 
115 /**
116  * Destroy an instance by first calling its destructor and then
117  * free all other memory.
118  */
119 #define TR_delete(object) TR_classDelete((void **)&(object))
120 
121 /**
122  * Create a clone of an instance by calling its clone implementation.
123  * A simple memcopy is often not enough because the cloned instance
124  * might have to allocate ist own resources or reuse resources in
125  * a given way.
126  */
127 #define TR_clone(object) TR_classClone((void *)(object))
128 
129 #endif // __TR_CLASS_INTERFACE_CLASS_H__
130 
131 // vim: set ts=4 sw=4:
int(* fptr_ctor)(void *, va_list *)
void(* fptr_dtor)(void *)
#define TR_IFID
Definition: interface.h:61
TR_INTERFACE(TR_Class)
void(* fptr_clone)(void *, void *const)