/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
1 by Gustav Hartvigsson
Initial Code.
1
/*
5.2.7 by Gustav Hartvigsson
* Switched licence to a more permisive one.
2
Copyright (c) 2013-2014 Gustav Hartvigsson
3
4
Permission is hereby granted, free of charge, to any person obtaining a copy
5
of this software and associated documentation files (the "Software"), to deal
6
in the Software without restriction, including without limitation the rights
7
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
copies of the Software, and to permit persons to whom the Software is
9
furnished to do so, subject to the following conditions:
10
11
The above copyright notice and this permission notice shall be included in
12
all copies or substantial portions of the Software.
13
14
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
THE SOFTWARE.
1 by Gustav Hartvigsson
Initial Code.
21
*/
22
23
#include "baseobject.h"
2 by Gustav Hartvigsson
Made the code compile.
24
#include <stdlib.h>
3 by Gustav Hartvigsson
Fixed a few things...
25
#include <string.h>
26
#include <stdio.h>
27
1 by Gustav Hartvigsson
Initial Code.
28
29
/* ---------------------------
30
 * Concrete method definitions
31
 * These are implemented towards the end of this file.
32
 * ---------------------------
33
 */
34
3 by Gustav Hartvigsson
Fixed a few things...
35
// SBaseObjectInstance * method_base_init (SBaseObjectInstance * self);
36
37
void method_base_deinit (SBaseObjectInstance * self);
38
39
int method_base_ref (SBaseObjectInstance * self);
40
41
int method_base_unref (SBaseObjectInstance * self);
42
43
int method_base_get_refcount (SBaseObjectInstance * self);
44
45
char * method_base_to_string (SBaseObjectInstance * self);
1 by Gustav Hartvigsson
Initial Code.
46
47
/* -----------------
48
 * Helper functions...
49
 * -----------------
50
 */
51
/**
52
 * This function is used to set the method to initize a new instance of an
53
 * object.
5.2.7 by Gustav Hartvigsson
* Switched licence to a more permisive one.
54
 */
3 by Gustav Hartvigsson
Fixed a few things...
55
void s_base_object_set_init_method (SBaseObjectInstance * self, SBaseObjectInstance * (* method)(* SBaseObjectInstance)) {
56
  SBaseObjectClass * klass = s_base_object_get_class (self);
1 by Gustav Hartvigsson
Initial Code.
57
  klass->initize = method;
58
}
5.2.7 by Gustav Hartvigsson
* Switched licence to a more permisive one.
59
1 by Gustav Hartvigsson
Initial Code.
60
61
/**
62
 * This function is used to set the method to deinitize an object.
63
 * 
64
 * set it to a method that deinitize your object.
65
 */
3 by Gustav Hartvigsson
Fixed a few things...
66
void s_base_object_set_deinit_method (SBaseObjectInstance * self, void (* method)(SBaseObjectInstance *)) {
67
  SBaseObjectClass * klass = s_base_object_get_class (self);
1 by Gustav Hartvigsson
Initial Code.
68
  klass->deinitize = method;
69
} 
70
71
/**
72
 * This fuction is used to set the ref method.
73
 * 
74
 * DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.
75
 */
3 by Gustav Hartvigsson
Fixed a few things...
76
void s_base_object_set_ref_method (SBaseObjectInstance * self, int (* method)(SBaseObjectInstance *)) {
77
  SBaseObjectClass * klass = s_base_object_get_class (self);
1 by Gustav Hartvigsson
Initial Code.
78
  klass->ref = method;
79
}
80
81
/**
82
 * This fuction is used to set the unref method.
83
 * 
84
 * DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.
85
 */
3 by Gustav Hartvigsson
Fixed a few things...
86
void s_base_object_set_unref_method (SBaseObjectInstance * self, int (* method)(SBaseObjectInstance *)) {
87
  SBaseObjectClass * klass = s_base_object_get_class (self);
1 by Gustav Hartvigsson
Initial Code.
88
  klass->unref = method;
89
}
90
91
/**
4 by Gustav Hartvigsson
Fixed a few problems.
92
 * This fuction is used to set the get_refcount method.
93
 * 
94
 * DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.
95
 */
96
void s_base_object_set_get_refcount_method (SBaseObjectInstance * self, int (* method)(SBaseObjectInstance *)) {
97
  SBaseObjectClass * klass = s_base_object_get_class (self);
98
  klass->get_refcount = method;
99
}
100
101
/**
1 by Gustav Hartvigsson
Initial Code.
102
 * This fuction is used to set the to_string method.
103
 */
3 by Gustav Hartvigsson
Fixed a few things...
104
void s_base_object_set_to_string_method (SBaseObjectInstance * self, char * (*method)(SBaseObjectInstance *)) {
105
  SBaseObjectClass * klass = s_base_object_get_class (self);
1 by Gustav Hartvigsson
Initial Code.
106
  klass->to_string = method;
107
}
108
109
/* ----------------------
110
 * Base object functions.
111
 * ----------------------
112
 */
113
114
/**
115
 * This function initizes an intance of the BaseObject, it also sets the methods
116
 * to be used with the object and sets the refrence count to one.
117
 */
3 by Gustav Hartvigsson
Fixed a few things...
118
void s_base_object_initize (SBaseObjectInstance * self) {
1 by Gustav Hartvigsson
Initial Code.
119
  self->refcount = 1;
3 by Gustav Hartvigsson
Fixed a few things...
120
  // s_base_object_set_init_method (self, method_base_init);
121
  s_base_object_set_deinit_method (self, method_base_deinit);
122
  s_base_object_set_ref_method (self, method_base_ref);
123
  s_base_object_set_unref_method (self, method_base_unref);
124
  s_base_object_set_get_refcount_method (self, method_base_get_refcount);
125
  s_base_object_set_to_string_method (self, method_base_to_string);
1 by Gustav Hartvigsson
Initial Code.
126
}
127
128
/**
129
 * This function creates a new base object.
130
 */
3 by Gustav Hartvigsson
Fixed a few things...
131
SBaseObjectInstance * s_base_object_new () {
5.2.1 by Gustav Hartvigsson
Started work on the Map (SMap) data structure.
132
  SBaseObjectInstance * self = malloc (sizeof (SBaseObjectInstance));
1 by Gustav Hartvigsson
Initial Code.
133
  //allocate the class definition of the object.
3 by Gustav Hartvigsson
Fixed a few things...
134
  self->base_class = malloc (sizeof(SBaseObjectClass));
1 by Gustav Hartvigsson
Initial Code.
135
  //initize it.
3 by Gustav Hartvigsson
Fixed a few things...
136
  s_base_object_initize (self);
1 by Gustav Hartvigsson
Initial Code.
137
  return self;
138
}
139
140
/**
141
 * This function deinitizes/frees an object even if it is still referenced.
3 by Gustav Hartvigsson
Fixed a few things...
142
 * This is usualy a bad idea, use s_base_object_unref instead.
1 by Gustav Hartvigsson
Initial Code.
143
 */
3 by Gustav Hartvigsson
Fixed a few things...
144
void s_base_object_free (SBaseObjectInstance * self) {
145
  SBaseObjectClass * klass = s_base_object_get_class (self);
1 by Gustav Hartvigsson
Initial Code.
146
  klass->deinitize (self);
147
}
148
149
/**
150
 * This function gets the class (which hold the object methods).
151
 */
3 by Gustav Hartvigsson
Fixed a few things...
152
void * s_base_object_get_class (SBaseObjectInstance * self) {
1 by Gustav Hartvigsson
Initial Code.
153
  return self->base_class;
154
}
155
156
/**
157
 * This function sets the instance class of an object.
158
 */
3 by Gustav Hartvigsson
Fixed a few things...
159
void s_base_object_set_class (SBaseObjectInstance * self, SBaseObjectClass * klass) {
1 by Gustav Hartvigsson
Initial Code.
160
  self->base_class = klass;
161
}
162
163
/**
164
 * This function is used to decrese the reference count of an object.
165
 * When an object reaches zero, it will deinitize the object using the objects
166
 * deititize method.
167
 * 
168
 * It returns the current reference count.
169
 */
3 by Gustav Hartvigsson
Fixed a few things...
170
int s_base_object_unref (SBaseObjectInstance * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
171
  unsigned int ret = self->base_class->unref (self);
172
  return ret;
1 by Gustav Hartvigsson
Initial Code.
173
}
174
175
/**
176
 * This function is used to increse the reference count of an object.
177
 *
178
 * Returns the current reference count.
179
 */
3 by Gustav Hartvigsson
Fixed a few things...
180
int s_base_object_ref (SBaseObjectInstance * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
181
  unsigned int ret = self->base_class->ref (self);
182
  return ret;
1 by Gustav Hartvigsson
Initial Code.
183
}
184
185
/**
186
 * This function returns the current reference count without chaning it.
187
 */
3 by Gustav Hartvigsson
Fixed a few things...
188
int s_base_object_get_refcount (SBaseObjectInstance * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
189
  unsigned int ret = self->base_class->get_refcount (self);
190
  return ret;
1 by Gustav Hartvigsson
Initial Code.
191
}
192
193
/**
194
 * This function returns a textual (string) that represesnts the object.
3 by Gustav Hartvigsson
Fixed a few things...
195
 * The method can be set using s_base_object_set_to_string_method.
1 by Gustav Hartvigsson
Initial Code.
196
 *
197
 * Note: The string that is returned must be freed.
198
 */
3 by Gustav Hartvigsson
Fixed a few things...
199
char * s_base_object_to_string (SBaseObjectInstance * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
200
  char * ret = self->base_class->to_string (self);
201
  return ret;
1 by Gustav Hartvigsson
Initial Code.
202
}
203
204
/* -----------------
205
 * Contrete methods.
206
 * -----------------
207
 */
208
 
209
 
3 by Gustav Hartvigsson
Fixed a few things...
210
// SBaseObjectInstance * method_base_init (SBaseObjectInstance * self) { }
1 by Gustav Hartvigsson
Initial Code.
211
3 by Gustav Hartvigsson
Fixed a few things...
212
void method_base_deinit (SBaseObjectInstance * self) {
1 by Gustav Hartvigsson
Initial Code.
213
  free (self->base_class);
214
  free (self);
215
}
216
3 by Gustav Hartvigsson
Fixed a few things...
217
int method_base_ref (SBaseObjectInstance * self) {
1 by Gustav Hartvigsson
Initial Code.
218
  self->refcount = self->refcount + 1;
219
  return self->refcount;
220
}
221
3 by Gustav Hartvigsson
Fixed a few things...
222
int method_base_unref (SBaseObjectInstance * self) {
1 by Gustav Hartvigsson
Initial Code.
223
  self->refcount = self->refcount - 1;
4 by Gustav Hartvigsson
Fixed a few problems.
224
  if (self->refcount == 0) {
2 by Gustav Hartvigsson
Made the code compile.
225
    self->base_class->deinitize (self);
1 by Gustav Hartvigsson
Initial Code.
226
    return 0;
227
  }
228
  return self->refcount;
229
}
230
3 by Gustav Hartvigsson
Fixed a few things...
231
int method_base_get_refcount (SBaseObjectInstance * self) {
1 by Gustav Hartvigsson
Initial Code.
232
  return self->refcount;
233
}
234
3 by Gustav Hartvigsson
Fixed a few things...
235
char * method_base_to_string (SBaseObjectInstance * self) {
1 by Gustav Hartvigsson
Initial Code.
236
  char * ret_string = malloc (sizeof (char) * 32); // I am bad with maths..
3 by Gustav Hartvigsson
Fixed a few things...
237
  sprintf (ret_string, "References: %d ", self->base_class->get_refcount(self));
1 by Gustav Hartvigsson
Initial Code.
238
  return ret_string;
239
}
240
241