/simpletypesystem/trunk

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