2
(C) Gustav Hartvigsson, 2013.
4
This program is free software: you can redistribute it and/or modify
5
it under the terms of the GNU Lesser General Public License as
6
published by the Free Software Foundation, either version 3 of the
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program. If not, see <http://www.gnu.org/licenses/>.
18
#include "baseobject.h"
20
typedef struct _BaseObjectClass {
21
// BaseObjectInstance * (* initize)(* BaseObjectInstance);
22
void (* deinitize)(* BaseObjectInstance);
23
int (* ref)(* BaseObjectInstance);
24
int (* unref)(* BaseObjectInstance);
25
int (* get_refcount)(* BaseObectInstance);
26
char * (* to_string)(* BaseObjectInstance);
29
typedef struct _BaseObjectInstance {
30
BaseObjectClass * base_class;
31
unsigned int refcount = 0;
34
/* ---------------------------
35
* Concrete method definitions
36
* These are implemented towards the end of this file.
37
* ---------------------------
40
// BaseObjectInstance * method_base_init (BaseObjectInstance * self);
42
void method_base_deinit (BaseObjectInstance * self);
44
int method_base_ref (BaseObjectInstance * self);
46
int method_base_unref (BaseObjectInstance * self);
48
int method_base_get_refcount (BaseObjectInstance * self);
50
char * method_base_to_string (BaseObjectInstance * self);
57
* This function is used to set the method to initize a new instance of an
60
* This has no use, rely...(?)
62
void base_object_set_init_method (BaseObjectInstance * self, BaseObjectInstance * (* method)(* BaseObjectInstance)) {
63
BaseObjectClass * klass = base_object_get_class (self);
64
klass->initize = method;
69
* This function is used to set the method to deinitize an object.
71
* set it to a method that deinitize your object.
73
void base_object_set_deinit_method (BaseObjectInstance * self, void (* method)(* BaseObjectInstance)) {
74
BaseObjectClass * klass = base_object_get_class (self);
75
klass->deinitize = method;
79
* This fuction is used to set the ref method.
81
* DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.
83
void base_object_set_ref_method (BaseObjectInstance * self, int (* method)(* BaseObjectInstance)) {
84
BaseObjectClass * klass = base_object_get_class (self);
89
* This fuction is used to set the unref method.
91
* DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.
93
void base_object_set_unref_method (BaseObjectInstance * self, int (* method)(* BaseObjectInstance)) {
94
BaseObjectClass * klass = base_object_get_class (self);
95
klass->unref = method;
99
* This fuction is used to set the get_refcount method.
101
* DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.
103
void base_object_set_unref_method (BaseObjectInstance * self, int (* method)(* BaseObjectInstance)) {
104
BaseObjectClass * klass = base_object_get_class (self);
105
klass->get_refcount = method;
109
* This fuction is used to set the to_string method.
111
void base_object_set_to_string_method (BaseObjectInstance * self, char * (* method)(* BaseObjectInstance)) {
112
BaseObjectClass * klass = base_object_get_class (self);
113
klass->to_string = method;
116
/* ----------------------
117
* Base object functions.
118
* ----------------------
122
* This function initizes an intance of the BaseObject, it also sets the methods
123
* to be used with the object and sets the refrence count to one.
125
void base_object_initize (BaseObjectInstance * self) {
127
// base_object_set_init_method (self, method_base_init);
128
base_object_set_deintit_method (self, method_base_deinit);
129
base_object_set_ref_method (self, method_base_ref);
130
base_object_set_unref_method (self, method_base_unref);
131
base_object_set_get_ref_count_method (self, method_base_get_refcount);
132
base_object_set_to_string_method (self, method_base_to_string);
136
* This function creates a new base object.
138
BaseObjectInstance * base_object_new () {
139
BaseObjectInstance * self = malloc (sizeof (BaseObjectInstance) + 1);
140
//allocate the class definition of the object.
141
self->base_class = malloc (class_size + 1);
143
base_object_initize (self, sizeof(BaseObjectClass) + 1);
148
* This function deinitizes/frees an object even if it is still referenced.
149
* This is usualy a bad idea, use base_object_unref instead.
151
void base_object_free (BaseObjectInstance * self) {
152
BaseObjectClass * klass = base_object_get_class (self);
153
klass->deinitize (self);
157
* This function gets the class (which hold the object methods).
159
BaseObjectClass * base_object_get_class (BaseObjectInstance * self) {
160
return self->base_class;
164
* This function sets the instance class of an object.
166
void base_object_set_class (BaseObjectInstance * self, BaseObjectClass * klass) {
167
self->base_class = klass;
171
* This function is used to decrese the reference count of an object.
172
* When an object reaches zero, it will deinitize the object using the objects
175
* It returns the current reference count.
177
int base_object_unref (BaseObjectInstance * self) {
178
return self->base_class->unref (self);
182
* This function is used to increse the reference count of an object.
184
* Returns the current reference count.
186
int base_object_ref (BaseObjectInstance * self) {
187
return self->base_class->ref (self);
191
* This function returns the current reference count without chaning it.
193
int base_object_get_refcount (BaseObjectInstance * self) {
194
return self->base_class->get_refcount (self);
198
* This function returns a textual (string) that represesnts the object.
199
* The method can be set using base_object_set_to_string_method.
201
* Note: The string that is returned must be freed.
203
char * base_object_to_string (BaseObjectInstance * self) {
204
return self->base_class->to_string (self);
213
// BaseObjectInstance * method_base_init (BaseObjectInstance * self) { }
215
void method_base_deinit (BaseObjectInstance * self) {
216
free (self->base_class);
220
int method_base_ref (BaseObjectInstance * self) {
221
self->refcount = self->refcount + 1;
222
return self->refcount;
225
int method_base_unref (BaseObjectInstance * self) {
226
self->refcount = self->refcount - 1;
227
if (self->refcount <= 0) {
228
self->base_class->deinit (self);
231
return self->refcount;
234
int method_base_get_refcount (BaseObjectInstance * self) {
235
return self->refcount;
238
char * method_base_to_string (BaseObjectInstance * self) {
239
char * ret_string = malloc (sizeof (char) * 32); // I am bad with maths..
240
snprintf (ret_string, "References: %d ", self->base_class->get_refcount(self));