/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-04 15:05:39 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20130904150539-7xfgt38hv5q3ycl2
Fixed a few things...
And added an error type...

Show diffs side-by-side

added added

removed removed

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