/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
1 by Gustav Hartvigsson
Initial Code.
1
/*
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
2
Copyright (c) 2013-2016 Gustav Hartvigsson
5.2.7 by Gustav Hartvigsson
* Switched licence to a more permisive one.
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
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
35
void
36
method_base_free (SObject * self);
37
38
int
39
method_base_ref (SObject * self);
40
41
int
42
method_base_unref (SObject * self);
43
44
int
45
method_base_get_refcount (SObject * self);
46
47
char *
48
method_base_to_string (SObject * self);
1 by Gustav Hartvigsson
Initial Code.
49
50
/* -----------------
51
 * Helper functions...
52
 * -----------------
53
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
54
void
55
s_object_set_free_method (SObject * self, MethodFunc method) {
5.2.9 by Gustav Hartvigsson
* Added license to files
56
  SObjectClass * klass = s_object_get_class (self);
57
  klass->free = method;
107 by Gustav Hartvigsson
* Removed depricaded functions from SObject code.
58
}
1 by Gustav Hartvigsson
Initial Code.
59
5.2.9 by Gustav Hartvigsson
* Added license to files
60
61 by Gustav Hartvigsson
* Made the code more easy to read.
61
void
62
s_object_set_ref_method (SObject * self,  MethodFuncInt method) {
5.2.9 by Gustav Hartvigsson
* Added license to files
63
  SObjectClass * klass = s_object_get_class (self);
1 by Gustav Hartvigsson
Initial Code.
64
  klass->ref = method;
65
}
66
5.2.9 by Gustav Hartvigsson
* Added license to files
67
61 by Gustav Hartvigsson
* Made the code more easy to read.
68
void
69
s_object_set_unref_method (SObject * self,  MethodFuncInt method) {
5.2.9 by Gustav Hartvigsson
* Added license to files
70
  SObjectClass * klass = s_object_get_class (self);
1 by Gustav Hartvigsson
Initial Code.
71
  klass->unref = method;
72
}
73
5.2.9 by Gustav Hartvigsson
* Added license to files
74
61 by Gustav Hartvigsson
* Made the code more easy to read.
75
void
76
s_object_set_get_refcount_method (SObject * self,  MethodFuncInt method) {
5.2.9 by Gustav Hartvigsson
* Added license to files
77
  SObjectClass * klass = s_object_get_class (self);
4 by Gustav Hartvigsson
Fixed a few problems.
78
  klass->get_refcount = method;
79
}
80
5.2.9 by Gustav Hartvigsson
* Added license to files
81
61 by Gustav Hartvigsson
* Made the code more easy to read.
82
void
83
s_object_set_to_string_method (SObject * self, ToStringFunc method) {
5.2.9 by Gustav Hartvigsson
* Added license to files
84
  SObjectClass * klass = s_object_get_class (self);
1 by Gustav Hartvigsson
Initial Code.
85
  klass->to_string = method;
86
}
87
61 by Gustav Hartvigsson
* Made the code more easy to read.
88
void
89
s_object_set_deinitialize_method (SObject * self,  MethodFunc method) {
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
90
  SObjectClass * klass = s_object_get_class (self);
91
  klass->deinitialize = method;
92
}
1 by Gustav Hartvigsson
Initial Code.
93
61 by Gustav Hartvigsson
* Made the code more easy to read.
94
void
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
95
s_object_initialize (SObject * self, const schar * name) {
48 by Gustav Hartvigsson
* Finnished SLinkedList.
96
  self->name = s_string_new (name);
107 by Gustav Hartvigsson
* Removed depricaded functions from SObject code.
97
1 by Gustav Hartvigsson
Initial Code.
98
  self->refcount = 1;
107 by Gustav Hartvigsson
* Removed depricaded functions from SObject code.
99
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
100
  self->callbacks = s_map_new (COMPFUNC (s_string_is_equal),
101
                               HASH_FUNC (sdbm_hash),
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
102
                               FREEFUNC (s_free),
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
103
                               FREEFUNC (s_callback_entry_free));
104
22 by Gustav Hartvigsson
* Made code compile
105
  s_object_set_free_method (self, method_base_free);
5.2.9 by Gustav Hartvigsson
* Added license to files
106
  s_object_set_ref_method (self, method_base_ref);
107
  s_object_set_unref_method (self, method_base_unref);
108
  s_object_set_get_refcount_method (self, method_base_get_refcount);
109
  s_object_set_to_string_method (self, method_base_to_string);
1 by Gustav Hartvigsson
Initial Code.
110
}
111
61 by Gustav Hartvigsson
* Made the code more easy to read.
112
SObject *
113
s_object_new () {
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
114
  s_warn_print ("s_object_new () should never be used in production code");
107 by Gustav Hartvigsson
* Removed depricaded functions from SObject code.
115
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
116
  SObject * self = s_malloc (sizeof (SObject));
1 by Gustav Hartvigsson
Initial Code.
117
  //allocate the class definition of the object.
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
118
  self->base_class = s_malloc (sizeof(SObjectClass));
22 by Gustav Hartvigsson
* Made code compile
119
  //initialize it.
48 by Gustav Hartvigsson
* Finnished SLinkedList.
120
  s_object_initialize (self, "SObject");
1 by Gustav Hartvigsson
Initial Code.
121
  return self;
122
}
123
61 by Gustav Hartvigsson
* Made the code more easy to read.
124
void
125
s_object_free (SObject * self) {
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
126
  s_free (self->name);
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
127
  s_map_free (self->callbacks, TRUE);
5.2.9 by Gustav Hartvigsson
* Added license to files
128
  SObjectClass * klass = s_object_get_class (self);
129
  klass->free (self);
1 by Gustav Hartvigsson
Initial Code.
130
}
131
61 by Gustav Hartvigsson
* Made the code more easy to read.
132
SObjectClass *
133
s_object_get_class (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
134
  return self->base_class;
135
}
136
61 by Gustav Hartvigsson
* Made the code more easy to read.
137
void
138
s_object_set_class (SObject * self, SObjectClass * klass) {
1 by Gustav Hartvigsson
Initial Code.
139
  self->base_class = klass;
140
}
141
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
142
sint
61 by Gustav Hartvigsson
* Made the code more easy to read.
143
s_object_unref (SObject * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
144
  unsigned int ret = self->base_class->unref (self);
145
  return ret;
1 by Gustav Hartvigsson
Initial Code.
146
}
147
61 by Gustav Hartvigsson
* Made the code more easy to read.
148
int
149
s_object_ref (SObject * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
150
  unsigned int ret = self->base_class->ref (self);
151
  return ret;
1 by Gustav Hartvigsson
Initial Code.
152
}
153
154
/**
155
 * This function returns the current reference count without chaning it.
156
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
157
int
158
s_object_get_refcount (SObject * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
159
  unsigned int ret = self->base_class->get_refcount (self);
160
  return ret;
1 by Gustav Hartvigsson
Initial Code.
161
}
162
61 by Gustav Hartvigsson
* Made the code more easy to read.
163
char *
164
s_object_to_string (SObject * self) {
39 by Gustav Hartvigsson
* Added "check" target for testing.
165
  return self->base_class->to_string (self);
1 by Gustav Hartvigsson
Initial Code.
166
}
167
168
/* -----------------
169
 * Contrete methods.
170
 * -----------------
171
 */
22 by Gustav Hartvigsson
* Made code compile
172
61 by Gustav Hartvigsson
* Made the code more easy to read.
173
void
174
method_base_free (SObject * self) {
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
175
  s_free (self->base_class);
176
  s_free (self);
1 by Gustav Hartvigsson
Initial Code.
177
}
178
61 by Gustav Hartvigsson
* Made the code more easy to read.
179
int
180
method_base_ref (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
181
  self->refcount = self->refcount + 1;
182
  return self->refcount;
183
}
184
61 by Gustav Hartvigsson
* Made the code more easy to read.
185
int
186
method_base_unref (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
187
  self->refcount = self->refcount - 1;
4 by Gustav Hartvigsson
Fixed a few problems.
188
  if (self->refcount == 0) {
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
189
    s_object_free (self);
1 by Gustav Hartvigsson
Initial Code.
190
    return 0;
191
  }
192
  return self->refcount;
193
}
194
61 by Gustav Hartvigsson
* Made the code more easy to read.
195
int
196
method_base_get_refcount (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
197
  return self->refcount;
198
}
199
61 by Gustav Hartvigsson
* Made the code more easy to read.
200
char *
201
method_base_to_string (SObject * self) {
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
202
  char * ret_string = s_string_new_fmt ("(SObject, References: %d)",
203
                                        self->base_class->get_refcount(self));
1 by Gustav Hartvigsson
Initial Code.
204
  return ret_string;
205
}
206
207