/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: 2015-07-12 19:04:18 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20150712190418-yi9rfito5ovnf6dy
* Made the code more easy to read.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
 * ---------------------------
33
33
 */
34
34
 
35
 
void method_base_initialize (SObject * self);
36
 
 
37
 
void method_base_deinitialize (SObject * self);
38
 
 
39
 
void method_base_free (SObject * self);
40
 
 
41
 
int method_base_ref (SObject * self);
42
 
 
43
 
int method_base_unref (SObject * self);
44
 
 
45
 
int method_base_get_refcount (SObject * self);
46
 
 
47
 
char * method_base_to_string (SObject * self);
 
35
void
 
36
method_base_initialize (SObject * self);
 
37
 
 
38
void
 
39
method_base_deinitialize (SObject * self);
 
40
 
 
41
void
 
42
method_base_free (SObject * self);
 
43
 
 
44
int
 
45
method_base_ref (SObject * self);
 
46
 
 
47
int
 
48
method_base_unref (SObject * self);
 
49
 
 
50
int
 
51
method_base_get_refcount (SObject * self);
 
52
 
 
53
char *
 
54
method_base_to_string (SObject * self);
48
55
 
49
56
/* -----------------
50
57
 * Helper functions...
51
58
 * -----------------
52
59
 */
53
 
void s_object_set_initialize_method (SObject * self, MethodFunc method) {
 
60
void
 
61
s_object_set_initialize_method (SObject * self, MethodFunc method) {
54
62
  SObjectClass * klass = s_object_get_class (self);
55
63
  klass->initialize = method;
56
64
}
57
65
 
58
 
void s_object_set_free_method (SObject * self, MethodFunc method) {
 
66
void
 
67
s_object_set_free_method (SObject * self, MethodFunc method) {
59
68
  SObjectClass * klass = s_object_get_class (self);
60
69
  klass->free = method;
61
70
62
71
 
63
72
 
64
 
void s_object_set_ref_method (SObject * self,  MethodFuncInt method) {
 
73
void
 
74
s_object_set_ref_method (SObject * self,  MethodFuncInt method) {
65
75
  SObjectClass * klass = s_object_get_class (self);
66
76
  klass->ref = method;
67
77
}
68
78
 
69
79
 
70
 
void s_object_set_unref_method (SObject * self,  MethodFuncInt method) {
 
80
void
 
81
s_object_set_unref_method (SObject * self,  MethodFuncInt method) {
71
82
  SObjectClass * klass = s_object_get_class (self);
72
83
  klass->unref = method;
73
84
}
74
85
 
75
86
 
76
 
void s_object_set_get_refcount_method (SObject * self,  MethodFuncInt method) {
 
87
void
 
88
s_object_set_get_refcount_method (SObject * self,  MethodFuncInt method) {
77
89
  SObjectClass * klass = s_object_get_class (self);
78
90
  klass->get_refcount = method;
79
91
}
80
92
 
81
93
 
82
 
void s_object_set_to_string_method (SObject * self, ToStringFunc method) {
 
94
void
 
95
s_object_set_to_string_method (SObject * self, ToStringFunc method) {
83
96
  SObjectClass * klass = s_object_get_class (self);
84
97
  klass->to_string = method;
85
98
}
86
99
 
87
 
void s_object_set_deinitialize_method (SObject * self,  MethodFunc method) {
 
100
void
 
101
s_object_set_deinitialize_method (SObject * self,  MethodFunc method) {
88
102
  SObjectClass * klass = s_object_get_class (self);
89
103
  klass->deinitialize = method;
90
104
}
91
105
 
92
 
void s_object_initialize (SObject * self, const char * name) {
 
106
void
 
107
s_object_initialize (SObject * self, const char * name) {
93
108
  self->name = s_string_new (name);
94
109
  
95
110
  self->refcount = 1;
103
118
  s_object_set_to_string_method (self, method_base_to_string);
104
119
}
105
120
 
106
 
SObject * s_object_new () {
 
121
SObject *
 
122
s_object_new () {
107
123
  s_warn_print ("s_object_new () should never be used in production code");
108
124
  
109
125
  SObject * self = malloc (sizeof (SObject));
114
130
  return self;
115
131
}
116
132
 
117
 
void s_object_free (SObject * self) {
 
133
void
 
134
s_object_free (SObject * self) {
118
135
  free (self->name);
119
136
  SObjectClass * klass = s_object_get_class (self);
120
137
  klass->free (self);
121
138
}
122
139
 
123
 
SObjectClass * s_object_get_class (SObject * self) {
 
140
SObjectClass *
 
141
s_object_get_class (SObject * self) {
124
142
  return self->base_class;
125
143
}
126
144
 
127
 
void s_object_set_class (SObject * self, SObjectClass * klass) {
 
145
void
 
146
s_object_set_class (SObject * self, SObjectClass * klass) {
128
147
  self->base_class = klass;
129
148
}
130
149
 
131
 
int s_object_unref (SObject * self) {
 
150
int
 
151
s_object_unref (SObject * self) {
132
152
  unsigned int ret = self->base_class->unref (self);
133
153
  return ret;
134
154
}
135
155
 
136
 
int s_object_ref (SObject * self) {
 
156
int
 
157
s_object_ref (SObject * self) {
137
158
  unsigned int ret = self->base_class->ref (self);
138
159
  return ret;
139
160
}
141
162
/**
142
163
 * This function returns the current reference count without chaning it.
143
164
 */
144
 
int s_object_get_refcount (SObject * self) {
 
165
int
 
166
s_object_get_refcount (SObject * self) {
145
167
  unsigned int ret = self->base_class->get_refcount (self);
146
168
  return ret;
147
169
}
148
170
 
149
 
char * s_object_to_string (SObject * self) {
 
171
char *
 
172
s_object_to_string (SObject * self) {
150
173
  return self->base_class->to_string (self);
151
174
}
152
175
 
156
179
 */
157
180
 
158
181
 
159
 
void method_base_deinitialize (SObject * self) { }
160
 
 
161
 
void method_base_initialize (SObject * self) { }
162
 
 
163
 
void method_base_free (SObject * self) {
 
182
void
 
183
method_base_deinitialize (SObject * self) { }
 
184
 
 
185
void
 
186
method_base_initialize (SObject * self) { }
 
187
 
 
188
void
 
189
method_base_free (SObject * self) {
164
190
  free (self->base_class);
165
191
  free (self);
166
192
}
167
193
 
168
 
int method_base_ref (SObject * self) {
 
194
int
 
195
method_base_ref (SObject * self) {
169
196
  self->refcount = self->refcount + 1;
170
197
  return self->refcount;
171
198
}
172
199
 
173
 
int method_base_unref (SObject * self) {
 
200
int
 
201
method_base_unref (SObject * self) {
174
202
  self->refcount = self->refcount - 1;
175
203
  if (self->refcount == 0) {
176
204
    s_object_free (self);
179
207
  return self->refcount;
180
208
}
181
209
 
182
 
int method_base_get_refcount (SObject * self) {
 
210
int
 
211
method_base_get_refcount (SObject * self) {
183
212
  return self->refcount;
184
213
}
185
214
 
186
 
char * method_base_to_string (SObject * self) {
 
215
char *
 
216
method_base_to_string (SObject * self) {
187
217
  char * ret_string = s_string_new_fmt ("References: %d", self->base_class->get_refcount(self));
188
218
  return ret_string;
189
219
}