/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"
62 by Gustav Hartvigsson
* General documentation clean up.
24
#include "Callback.h"
2 by Gustav Hartvigsson
Made the code compile.
25
#include <stdlib.h>
3 by Gustav Hartvigsson
Fixed a few things...
26
#include <string.h>
27
#include <stdio.h>
28
1 by Gustav Hartvigsson
Initial Code.
29
30
/* ---------------------------
31
 * Concrete method definitions
32
 * These are implemented towards the end of this file.
33
 * ---------------------------
34
 */
35
61 by Gustav Hartvigsson
* Made the code more easy to read.
36
void
37
method_base_initialize (SObject * self);
38
39
void
40
method_base_deinitialize (SObject * self);
41
42
void
43
method_base_free (SObject * self);
44
45
int
46
method_base_ref (SObject * self);
47
48
int
49
method_base_unref (SObject * self);
50
51
int
52
method_base_get_refcount (SObject * self);
53
54
char *
55
method_base_to_string (SObject * self);
1 by Gustav Hartvigsson
Initial Code.
56
57
/* -----------------
58
 * Helper functions...
59
 * -----------------
60
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
61
void
62
s_object_set_initialize_method (SObject * self, MethodFunc method) {
5.2.9 by Gustav Hartvigsson
* Added license to files
63
  SObjectClass * klass = s_object_get_class (self);
22 by Gustav Hartvigsson
* Made code compile
64
  klass->initialize = method;
1 by Gustav Hartvigsson
Initial Code.
65
}
5.2.7 by Gustav Hartvigsson
* Switched licence to a more permisive one.
66
61 by Gustav Hartvigsson
* Made the code more easy to read.
67
void
68
s_object_set_free_method (SObject * self, MethodFunc method) {
5.2.9 by Gustav Hartvigsson
* Added license to files
69
  SObjectClass * klass = s_object_get_class (self);
70
  klass->free = method;
1 by Gustav Hartvigsson
Initial Code.
71
} 
72
5.2.9 by Gustav Hartvigsson
* Added license to files
73
61 by Gustav Hartvigsson
* Made the code more easy to read.
74
void
75
s_object_set_ref_method (SObject * self,  MethodFuncInt method) {
5.2.9 by Gustav Hartvigsson
* Added license to files
76
  SObjectClass * klass = s_object_get_class (self);
1 by Gustav Hartvigsson
Initial Code.
77
  klass->ref = method;
78
}
79
5.2.9 by Gustav Hartvigsson
* Added license to files
80
61 by Gustav Hartvigsson
* Made the code more easy to read.
81
void
82
s_object_set_unref_method (SObject * self,  MethodFuncInt method) {
5.2.9 by Gustav Hartvigsson
* Added license to files
83
  SObjectClass * klass = s_object_get_class (self);
1 by Gustav Hartvigsson
Initial Code.
84
  klass->unref = method;
85
}
86
5.2.9 by Gustav Hartvigsson
* Added license to files
87
61 by Gustav Hartvigsson
* Made the code more easy to read.
88
void
89
s_object_set_get_refcount_method (SObject * self,  MethodFuncInt method) {
5.2.9 by Gustav Hartvigsson
* Added license to files
90
  SObjectClass * klass = s_object_get_class (self);
4 by Gustav Hartvigsson
Fixed a few problems.
91
  klass->get_refcount = method;
92
}
93
5.2.9 by Gustav Hartvigsson
* Added license to files
94
61 by Gustav Hartvigsson
* Made the code more easy to read.
95
void
96
s_object_set_to_string_method (SObject * self, ToStringFunc method) {
5.2.9 by Gustav Hartvigsson
* Added license to files
97
  SObjectClass * klass = s_object_get_class (self);
1 by Gustav Hartvigsson
Initial Code.
98
  klass->to_string = method;
99
}
100
61 by Gustav Hartvigsson
* Made the code more easy to read.
101
void
102
s_object_set_deinitialize_method (SObject * self,  MethodFunc method) {
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
103
  SObjectClass * klass = s_object_get_class (self);
104
  klass->deinitialize = method;
105
}
1 by Gustav Hartvigsson
Initial Code.
106
61 by Gustav Hartvigsson
* Made the code more easy to read.
107
void
108
s_object_initialize (SObject * self, const char * name) {
48 by Gustav Hartvigsson
* Finnished SLinkedList.
109
  self->name = s_string_new (name);
110
  
1 by Gustav Hartvigsson
Initial Code.
111
  self->refcount = 1;
48 by Gustav Hartvigsson
* Finnished SLinkedList.
112
  
22 by Gustav Hartvigsson
* Made code compile
113
  s_object_set_deinitialize_method (self,method_base_deinitialize);
114
  s_object_set_initialize_method (self, method_base_initialize);
115
  s_object_set_free_method (self, method_base_free);
5.2.9 by Gustav Hartvigsson
* Added license to files
116
  s_object_set_ref_method (self, method_base_ref);
117
  s_object_set_unref_method (self, method_base_unref);
118
  s_object_set_get_refcount_method (self, method_base_get_refcount);
119
  s_object_set_to_string_method (self, method_base_to_string);
1 by Gustav Hartvigsson
Initial Code.
120
}
121
61 by Gustav Hartvigsson
* Made the code more easy to read.
122
SObject *
123
s_object_new () {
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
124
  s_warn_print ("s_object_new () should never be used in production code");
22 by Gustav Hartvigsson
* Made code compile
125
  
5.2.9 by Gustav Hartvigsson
* Added license to files
126
  SObject * self = malloc (sizeof (SObject));
1 by Gustav Hartvigsson
Initial Code.
127
  //allocate the class definition of the object.
5.2.9 by Gustav Hartvigsson
* Added license to files
128
  self->base_class = malloc (sizeof(SObjectClass));
22 by Gustav Hartvigsson
* Made code compile
129
  //initialize it.
48 by Gustav Hartvigsson
* Finnished SLinkedList.
130
  s_object_initialize (self, "SObject");
1 by Gustav Hartvigsson
Initial Code.
131
  return self;
132
}
133
61 by Gustav Hartvigsson
* Made the code more easy to read.
134
void
135
s_object_free (SObject * self) {
48 by Gustav Hartvigsson
* Finnished SLinkedList.
136
  free (self->name);
5.2.9 by Gustav Hartvigsson
* Added license to files
137
  SObjectClass * klass = s_object_get_class (self);
138
  klass->free (self);
1 by Gustav Hartvigsson
Initial Code.
139
}
140
61 by Gustav Hartvigsson
* Made the code more easy to read.
141
SObjectClass *
142
s_object_get_class (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
143
  return self->base_class;
144
}
145
61 by Gustav Hartvigsson
* Made the code more easy to read.
146
void
147
s_object_set_class (SObject * self, SObjectClass * klass) {
1 by Gustav Hartvigsson
Initial Code.
148
  self->base_class = klass;
149
}
150
61 by Gustav Hartvigsson
* Made the code more easy to read.
151
int
152
s_object_unref (SObject * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
153
  unsigned int ret = self->base_class->unref (self);
154
  return ret;
1 by Gustav Hartvigsson
Initial Code.
155
}
156
61 by Gustav Hartvigsson
* Made the code more easy to read.
157
int
158
s_object_ref (SObject * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
159
  unsigned int ret = self->base_class->ref (self);
160
  return ret;
1 by Gustav Hartvigsson
Initial Code.
161
}
162
163
/**
164
 * This function returns the current reference count without chaning it.
165
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
166
int
167
s_object_get_refcount (SObject * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
168
  unsigned int ret = self->base_class->get_refcount (self);
169
  return ret;
1 by Gustav Hartvigsson
Initial Code.
170
}
171
61 by Gustav Hartvigsson
* Made the code more easy to read.
172
char *
173
s_object_to_string (SObject * self) {
39 by Gustav Hartvigsson
* Added "check" target for testing.
174
  return self->base_class->to_string (self);
1 by Gustav Hartvigsson
Initial Code.
175
}
176
177
/* -----------------
178
 * Contrete methods.
179
 * -----------------
180
 */
22 by Gustav Hartvigsson
* Made code compile
181
182
61 by Gustav Hartvigsson
* Made the code more easy to read.
183
void
184
method_base_deinitialize (SObject * self) { }
185
186
void
187
method_base_initialize (SObject * self) { }
188
189
void
190
method_base_free (SObject * self) {
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
191
  free (self->base_class);
192
  free (self);
1 by Gustav Hartvigsson
Initial Code.
193
}
194
61 by Gustav Hartvigsson
* Made the code more easy to read.
195
int
196
method_base_ref (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
197
  self->refcount = self->refcount + 1;
198
  return self->refcount;
199
}
200
61 by Gustav Hartvigsson
* Made the code more easy to read.
201
int
202
method_base_unref (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
203
  self->refcount = self->refcount - 1;
4 by Gustav Hartvigsson
Fixed a few problems.
204
  if (self->refcount == 0) {
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
205
    s_object_free (self);
1 by Gustav Hartvigsson
Initial Code.
206
    return 0;
207
  }
208
  return self->refcount;
209
}
210
61 by Gustav Hartvigsson
* Made the code more easy to read.
211
int
212
method_base_get_refcount (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
213
  return self->refcount;
214
}
215
61 by Gustav Hartvigsson
* Made the code more easy to read.
216
char *
217
method_base_to_string (SObject * self) {
39 by Gustav Hartvigsson
* Added "check" target for testing.
218
  char * ret_string = s_string_new_fmt ("References: %d", self->base_class->get_refcount(self));
1 by Gustav Hartvigsson
Initial Code.
219
  return ret_string;
220
}
221
222