/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk

« back to all changes in this revision

Viewing changes to src/baseobject.c

  • Committer: Gustav Hartvigsson
  • Date: 2013-09-02 21:55:23 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20130902215523-8rr3iyjan2w0apge
Initial Code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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"
 
19
 
 
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);
 
27
};
 
28
 
 
29
typedef struct _BaseObjectInstance {
 
30
  BaseObjectClass * base_class;
 
31
  unsigned int refcount = 0;
 
32
};
 
33
 
 
34
/* ---------------------------
 
35
 * Concrete method definitions
 
36
 * These are implemented towards the end of this file.
 
37
 * ---------------------------
 
38
 */
 
39
 
 
40
// BaseObjectInstance * method_base_init (BaseObjectInstance * self);
 
41
 
 
42
void method_base_deinit (BaseObjectInstance * self);
 
43
 
 
44
int method_base_ref (BaseObjectInstance * self);
 
45
 
 
46
int method_base_unref (BaseObjectInstance * self);
 
47
 
 
48
int method_base_get_refcount (BaseObjectInstance * self);
 
49
 
 
50
char * method_base_to_string (BaseObjectInstance * self);
 
51
 
 
52
/* -----------------
 
53
 * Helper functions...
 
54
 * -----------------
 
55
 */
 
56
/**
 
57
 * This function is used to set the method to initize a new instance of an
 
58
 * object.
 
59
 *
 
60
 * This has no use, rely...(?)
 
61
 */ /*
 
62
void base_object_set_init_method (BaseObjectInstance * self, BaseObjectInstance * (* method)(* BaseObjectInstance)) {
 
63
  BaseObjectClass * klass = base_object_get_class (self);
 
64
  klass->initize = method;
 
65
}
 
66
*/
 
67
 
 
68
/**
 
69
 * This function is used to set the method to deinitize an object.
 
70
 * 
 
71
 * set it to a method that deinitize your object.
 
72
 */
 
73
void base_object_set_deinit_method (BaseObjectInstance * self, void (* method)(* BaseObjectInstance)) {
 
74
  BaseObjectClass * klass = base_object_get_class (self);
 
75
  klass->deinitize = method;
 
76
 
77
 
 
78
/**
 
79
 * This fuction is used to set the ref method.
 
80
 * 
 
81
 * DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.
 
82
 */
 
83
void base_object_set_ref_method (BaseObjectInstance * self, int (* method)(* BaseObjectInstance)) {
 
84
  BaseObjectClass * klass = base_object_get_class (self);
 
85
  klass->ref = method;
 
86
}
 
87
 
 
88
/**
 
89
 * This fuction is used to set the unref method.
 
90
 * 
 
91
 * DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.
 
92
 */
 
93
void base_object_set_unref_method (BaseObjectInstance * self, int (* method)(* BaseObjectInstance)) {
 
94
  BaseObjectClass * klass = base_object_get_class (self);
 
95
  klass->unref = method;
 
96
}
 
97
 
 
98
/**
 
99
 * This fuction is used to set the get_refcount method.
 
100
 * 
 
101
 * DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.
 
102
 */
 
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;
 
106
}
 
107
 
 
108
/**
 
109
 * This fuction is used to set the to_string method.
 
110
 */
 
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;
 
114
}
 
115
 
 
116
/* ----------------------
 
117
 * Base object functions.
 
118
 * ----------------------
 
119
 */
 
120
 
 
121
/**
 
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.
 
124
 */
 
125
void base_object_initize (BaseObjectInstance * self) {
 
126
  self->refcount = 1;
 
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);
 
133
}
 
134
 
 
135
/**
 
136
 * This function creates a new base object.
 
137
 */
 
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);
 
142
  //initize it.
 
143
  base_object_initize (self, sizeof(BaseObjectClass) + 1);
 
144
  return self;
 
145
}
 
146
 
 
147
/**
 
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.
 
150
 */
 
151
void base_object_free (BaseObjectInstance * self) {
 
152
  BaseObjectClass * klass = base_object_get_class (self);
 
153
  klass->deinitize (self);
 
154
}
 
155
 
 
156
/**
 
157
 * This function gets the class (which hold the object methods).
 
158
 */
 
159
BaseObjectClass * base_object_get_class (BaseObjectInstance * self) {
 
160
  return self->base_class;
 
161
}
 
162
 
 
163
/**
 
164
 * This function sets the instance class of an object.
 
165
 */
 
166
void base_object_set_class (BaseObjectInstance * self, BaseObjectClass * klass) {
 
167
  self->base_class = klass;
 
168
}
 
169
 
 
170
/**
 
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
 
173
 * deititize method.
 
174
 * 
 
175
 * It returns the current reference count.
 
176
 */
 
177
int base_object_unref (BaseObjectInstance * self) {
 
178
  return self->base_class->unref (self);
 
179
}
 
180
 
 
181
/**
 
182
 * This function is used to increse the reference count of an object.
 
183
 *
 
184
 * Returns the current reference count.
 
185
 */
 
186
int base_object_ref (BaseObjectInstance * self) {
 
187
  return self->base_class->ref (self);
 
188
}
 
189
 
 
190
/**
 
191
 * This function returns the current reference count without chaning it.
 
192
 */
 
193
int base_object_get_refcount (BaseObjectInstance * self) {
 
194
  return self->base_class->get_refcount (self);
 
195
}
 
196
 
 
197
/**
 
198
 * This function returns a textual (string) that represesnts the object.
 
199
 * The method can be set using base_object_set_to_string_method.
 
200
 *
 
201
 * Note: The string that is returned must be freed.
 
202
 */
 
203
char * base_object_to_string (BaseObjectInstance * self) {
 
204
  return self->base_class->to_string (self);
 
205
}
 
206
 
 
207
/* -----------------
 
208
 * Contrete methods.
 
209
 * -----------------
 
210
 */
 
211
 
 
212
 
 
213
// BaseObjectInstance * method_base_init (BaseObjectInstance * self) { }
 
214
 
 
215
void method_base_deinit (BaseObjectInstance * self) {
 
216
  free (self->base_class);
 
217
  free (self);
 
218
}
 
219
 
 
220
int method_base_ref (BaseObjectInstance * self) {
 
221
  self->refcount = self->refcount + 1;
 
222
  return self->refcount;
 
223
}
 
224
 
 
225
int method_base_unref (BaseObjectInstance * self) {
 
226
  self->refcount = self->refcount - 1;
 
227
  if (self->refcount <= 0) {
 
228
    self->base_class->deinit (self);
 
229
    return 0;
 
230
  }
 
231
  return self->refcount;
 
232
}
 
233
 
 
234
int method_base_get_refcount (BaseObjectInstance * self) {
 
235
  return self->refcount;
 
236
}
 
237
 
 
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));
 
241
  return ret_string;
 
242
}
 
243
 
 
244