bzr branch
http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
|
1
by Gustav Hartvigsson
Initial Code. |
1 |
/*
|
2 |
(C) Gustav Hartvigsson, 2013.
|
|
3 |
|
|
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
|
|
7 |
License.
|
|
8 |
||
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.
|
|
13 |
||
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/>.
|
|
16 |
*/
|
|
17 |
||
18 |
#include "baseobject.h" |
|
|
2
by Gustav Hartvigsson
Made the code compile. |
19 |
#include <stdlib.h> |
|
1
by Gustav Hartvigsson
Initial Code. |
20 |
|
21 |
typedef struct _BaseObjectClass { |
|
|
2
by Gustav Hartvigsson
Made the code compile. |
22 |
// BaseObjectInstance * (* initize)(BaseObjectInstance *); |
23 |
void (*deinitize)(BaseObjectInstance *); |
|
24 |
int (*ref)(BaseObjectInstance *); |
|
25 |
int (*unref)(BaseObjectInstance *); |
|
26 |
int (*get_refcount)(BaseObjectInstance *); |
|
27 |
char * (*to_string)(BaseObjectInstance *); |
|
|
1
by Gustav Hartvigsson
Initial Code. |
28 |
};
|
29 |
||
30 |
typedef struct _BaseObjectInstance { |
|
31 |
BaseObjectClass * base_class; |
|
|
2
by Gustav Hartvigsson
Made the code compile. |
32 |
unsigned int refcount; |
|
1
by Gustav Hartvigsson
Initial Code. |
33 |
};
|
34 |
||
35 |
/* ---------------------------
|
|
36 |
* Concrete method definitions
|
|
37 |
* These are implemented towards the end of this file.
|
|
38 |
* ---------------------------
|
|
39 |
*/
|
|
40 |
||
41 |
// BaseObjectInstance * method_base_init (BaseObjectInstance * self);
|
|
42 |
||
43 |
void method_base_deinit (BaseObjectInstance * self); |
|
44 |
||
45 |
int method_base_ref (BaseObjectInstance * self); |
|
46 |
||
47 |
int method_base_unref (BaseObjectInstance * self); |
|
48 |
||
49 |
int method_base_get_refcount (BaseObjectInstance * self); |
|
50 |
||
51 |
char * method_base_to_string (BaseObjectInstance * self); |
|
52 |
||
53 |
/* -----------------
|
|
54 |
* Helper functions...
|
|
55 |
* -----------------
|
|
56 |
*/
|
|
57 |
/**
|
|
58 |
* This function is used to set the method to initize a new instance of an
|
|
59 |
* object.
|
|
60 |
*
|
|
61 |
* This has no use, rely...(?)
|
|
62 |
*/ /* |
|
63 |
void base_object_set_init_method (BaseObjectInstance * self, BaseObjectInstance * (* method)(* BaseObjectInstance)) {
|
|
64 |
BaseObjectClass * klass = base_object_get_class (self);
|
|
65 |
klass->initize = method;
|
|
66 |
}
|
|
67 |
*/
|
|
68 |
||
69 |
/**
|
|
70 |
* This function is used to set the method to deinitize an object.
|
|
71 |
*
|
|
72 |
* set it to a method that deinitize your object.
|
|
73 |
*/
|
|
|
2
by Gustav Hartvigsson
Made the code compile. |
74 |
void base_object_set_deinit_method (BaseObjectInstance * self, void (* method)(BaseObjectInstance *)) { |
|
1
by Gustav Hartvigsson
Initial Code. |
75 |
BaseObjectClass * klass = base_object_get_class (self); |
76 |
klass->deinitize = method; |
|
77 |
} |
|
78 |
||
79 |
/**
|
|
80 |
* This fuction is used to set the ref method.
|
|
81 |
*
|
|
82 |
* DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.
|
|
83 |
*/
|
|
|
2
by Gustav Hartvigsson
Made the code compile. |
84 |
void base_object_set_ref_method (BaseObjectInstance * self, int (* method)(BaseObjectInstance *)) { |
|
1
by Gustav Hartvigsson
Initial Code. |
85 |
BaseObjectClass * klass = base_object_get_class (self); |
86 |
klass->ref = method; |
|
87 |
}
|
|
88 |
||
89 |
/**
|
|
90 |
* This fuction is used to set the unref method.
|
|
91 |
*
|
|
92 |
* DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.
|
|
93 |
*/
|
|
|
2
by Gustav Hartvigsson
Made the code compile. |
94 |
void base_object_set_unref_method (BaseObjectInstance * self, int (* method)(BaseObjectInstance *)) { |
|
1
by Gustav Hartvigsson
Initial Code. |
95 |
BaseObjectClass * klass = base_object_get_class (self); |
96 |
klass->unref = method; |
|
97 |
}
|
|
98 |
||
99 |
/**
|
|
100 |
* This fuction is used to set the to_string method.
|
|
101 |
*/
|
|
|
2
by Gustav Hartvigsson
Made the code compile. |
102 |
void base_object_set_to_string_method (BaseObjectInstance * self, char * (*method)(BaseObjectInstance *)) { |
|
1
by Gustav Hartvigsson
Initial Code. |
103 |
BaseObjectClass * klass = base_object_get_class (self); |
104 |
klass->to_string = method; |
|
105 |
}
|
|
106 |
||
107 |
/* ----------------------
|
|
108 |
* Base object functions.
|
|
109 |
* ----------------------
|
|
110 |
*/
|
|
111 |
||
112 |
/**
|
|
113 |
* This function initizes an intance of the BaseObject, it also sets the methods
|
|
114 |
* to be used with the object and sets the refrence count to one.
|
|
115 |
*/
|
|
116 |
void base_object_initize (BaseObjectInstance * self) { |
|
117 |
self->refcount = 1; |
|
118 |
// base_object_set_init_method (self, method_base_init); |
|
|
2
by Gustav Hartvigsson
Made the code compile. |
119 |
base_object_set_deinit_method (self, method_base_deinit); |
|
1
by Gustav Hartvigsson
Initial Code. |
120 |
base_object_set_ref_method (self, method_base_ref); |
121 |
base_object_set_unref_method (self, method_base_unref); |
|
|
2
by Gustav Hartvigsson
Made the code compile. |
122 |
base_object_set_get_refcount_method (self, method_base_get_refcount); |
|
1
by Gustav Hartvigsson
Initial Code. |
123 |
base_object_set_to_string_method (self, method_base_to_string); |
124 |
}
|
|
125 |
||
126 |
/**
|
|
127 |
* This function creates a new base object.
|
|
128 |
*/
|
|
129 |
BaseObjectInstance * base_object_new () { |
|
130 |
BaseObjectInstance * self = malloc (sizeof (BaseObjectInstance) + 1); |
|
131 |
//allocate the class definition of the object. |
|
|
2
by Gustav Hartvigsson
Made the code compile. |
132 |
self->base_class = malloc (sizeof(BaseObjectClass)); |
|
1
by Gustav Hartvigsson
Initial Code. |
133 |
//initize it. |
|
2
by Gustav Hartvigsson
Made the code compile. |
134 |
base_object_initize (self); |
|
1
by Gustav Hartvigsson
Initial Code. |
135 |
return self; |
136 |
}
|
|
137 |
||
138 |
/**
|
|
139 |
* This function deinitizes/frees an object even if it is still referenced.
|
|
140 |
* This is usualy a bad idea, use base_object_unref instead.
|
|
141 |
*/
|
|
142 |
void base_object_free (BaseObjectInstance * self) { |
|
143 |
BaseObjectClass * klass = base_object_get_class (self); |
|
144 |
klass->deinitize (self); |
|
145 |
}
|
|
146 |
||
147 |
/**
|
|
148 |
* This function gets the class (which hold the object methods).
|
|
149 |
*/
|
|
150 |
BaseObjectClass * base_object_get_class (BaseObjectInstance * self) { |
|
151 |
return self->base_class; |
|
152 |
}
|
|
153 |
||
154 |
/**
|
|
155 |
* This function sets the instance class of an object.
|
|
156 |
*/
|
|
157 |
void base_object_set_class (BaseObjectInstance * self, BaseObjectClass * klass) { |
|
158 |
self->base_class = klass; |
|
159 |
}
|
|
160 |
||
161 |
/**
|
|
162 |
* This function is used to decrese the reference count of an object.
|
|
163 |
* When an object reaches zero, it will deinitize the object using the objects
|
|
164 |
* deititize method.
|
|
165 |
*
|
|
166 |
* It returns the current reference count.
|
|
167 |
*/
|
|
168 |
int base_object_unref (BaseObjectInstance * self) { |
|
169 |
return self->base_class->unref (self); |
|
170 |
}
|
|
171 |
||
172 |
/**
|
|
173 |
* This function is used to increse the reference count of an object.
|
|
174 |
*
|
|
175 |
* Returns the current reference count.
|
|
176 |
*/
|
|
177 |
int base_object_ref (BaseObjectInstance * self) { |
|
178 |
return self->base_class->ref (self); |
|
179 |
}
|
|
180 |
||
181 |
/**
|
|
182 |
* This function returns the current reference count without chaning it.
|
|
183 |
*/
|
|
184 |
int base_object_get_refcount (BaseObjectInstance * self) { |
|
185 |
return self->base_class->get_refcount (self); |
|
186 |
}
|
|
187 |
||
188 |
/**
|
|
189 |
* This function returns a textual (string) that represesnts the object.
|
|
190 |
* The method can be set using base_object_set_to_string_method.
|
|
191 |
*
|
|
192 |
* Note: The string that is returned must be freed.
|
|
193 |
*/
|
|
194 |
char * base_object_to_string (BaseObjectInstance * self) { |
|
195 |
return self->base_class->to_string (self); |
|
196 |
}
|
|
197 |
||
198 |
/* -----------------
|
|
199 |
* Contrete methods.
|
|
200 |
* -----------------
|
|
201 |
*/
|
|
202 |
|
|
203 |
|
|
204 |
// BaseObjectInstance * method_base_init (BaseObjectInstance * self) { }
|
|
205 |
||
206 |
void method_base_deinit (BaseObjectInstance * self) { |
|
207 |
free (self->base_class); |
|
208 |
free (self); |
|
209 |
}
|
|
210 |
||
211 |
int method_base_ref (BaseObjectInstance * self) { |
|
212 |
self->refcount = self->refcount + 1; |
|
213 |
return self->refcount; |
|
214 |
}
|
|
215 |
||
216 |
int method_base_unref (BaseObjectInstance * self) { |
|
217 |
self->refcount = self->refcount - 1; |
|
218 |
if (self->refcount <= 0) { |
|
|
2
by Gustav Hartvigsson
Made the code compile. |
219 |
self->base_class->deinitize (self); |
|
1
by Gustav Hartvigsson
Initial Code. |
220 |
return 0; |
221 |
} |
|
222 |
return self->refcount; |
|
223 |
}
|
|
224 |
||
225 |
int method_base_get_refcount (BaseObjectInstance * self) { |
|
226 |
return self->refcount; |
|
227 |
}
|
|
228 |
||
229 |
char * method_base_to_string (BaseObjectInstance * self) { |
|
230 |
char * ret_string = malloc (sizeof (char) * 32); // I am bad with maths.. |
|
231 |
snprintf (ret_string, "References: %d ", self->base_class->get_refcount(self)); |
|
232 |
return ret_string; |
|
233 |
}
|
|
234 |
||
235 |