/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
 */
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
22 by Gustav Hartvigsson
* Made code compile
100
  s_object_set_free_method (self, method_base_free);
5.2.9 by Gustav Hartvigsson
* Added license to files
101
  s_object_set_ref_method (self, method_base_ref);
102
  s_object_set_unref_method (self, method_base_unref);
103
  s_object_set_get_refcount_method (self, method_base_get_refcount);
104
  s_object_set_to_string_method (self, method_base_to_string);
1 by Gustav Hartvigsson
Initial Code.
105
}
106
61 by Gustav Hartvigsson
* Made the code more easy to read.
107
SObject *
108
s_object_new () {
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
109
  s_warn_print ("s_object_new () should never be used in production code");
107 by Gustav Hartvigsson
* Removed depricaded functions from SObject code.
110
5.2.9 by Gustav Hartvigsson
* Added license to files
111
  SObject * self = malloc (sizeof (SObject));
1 by Gustav Hartvigsson
Initial Code.
112
  //allocate the class definition of the object.
5.2.9 by Gustav Hartvigsson
* Added license to files
113
  self->base_class = malloc (sizeof(SObjectClass));
22 by Gustav Hartvigsson
* Made code compile
114
  //initialize it.
48 by Gustav Hartvigsson
* Finnished SLinkedList.
115
  s_object_initialize (self, "SObject");
1 by Gustav Hartvigsson
Initial Code.
116
  return self;
117
}
118
61 by Gustav Hartvigsson
* Made the code more easy to read.
119
void
120
s_object_free (SObject * self) {
48 by Gustav Hartvigsson
* Finnished SLinkedList.
121
  free (self->name);
5.2.9 by Gustav Hartvigsson
* Added license to files
122
  SObjectClass * klass = s_object_get_class (self);
123
  klass->free (self);
1 by Gustav Hartvigsson
Initial Code.
124
}
125
61 by Gustav Hartvigsson
* Made the code more easy to read.
126
SObjectClass *
127
s_object_get_class (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
128
  return self->base_class;
129
}
130
61 by Gustav Hartvigsson
* Made the code more easy to read.
131
void
132
s_object_set_class (SObject * self, SObjectClass * klass) {
1 by Gustav Hartvigsson
Initial Code.
133
  self->base_class = klass;
134
}
135
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
136
sint
61 by Gustav Hartvigsson
* Made the code more easy to read.
137
s_object_unref (SObject * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
138
  unsigned int ret = self->base_class->unref (self);
139
  return ret;
1 by Gustav Hartvigsson
Initial Code.
140
}
141
61 by Gustav Hartvigsson
* Made the code more easy to read.
142
int
143
s_object_ref (SObject * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
144
  unsigned int ret = self->base_class->ref (self);
145
  return ret;
1 by Gustav Hartvigsson
Initial Code.
146
}
147
148
/**
149
 * This function returns the current reference count without chaning it.
150
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
151
int
152
s_object_get_refcount (SObject * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
153
  unsigned int ret = self->base_class->get_refcount (self);
154
  return ret;
1 by Gustav Hartvigsson
Initial Code.
155
}
156
61 by Gustav Hartvigsson
* Made the code more easy to read.
157
char *
158
s_object_to_string (SObject * self) {
39 by Gustav Hartvigsson
* Added "check" target for testing.
159
  return self->base_class->to_string (self);
1 by Gustav Hartvigsson
Initial Code.
160
}
161
162
/* -----------------
163
 * Contrete methods.
164
 * -----------------
165
 */
22 by Gustav Hartvigsson
* Made code compile
166
61 by Gustav Hartvigsson
* Made the code more easy to read.
167
void
168
method_base_free (SObject * self) {
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
169
  free (self->base_class);
170
  free (self);
1 by Gustav Hartvigsson
Initial Code.
171
}
172
61 by Gustav Hartvigsson
* Made the code more easy to read.
173
int
174
method_base_ref (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
175
  self->refcount = self->refcount + 1;
176
  return self->refcount;
177
}
178
61 by Gustav Hartvigsson
* Made the code more easy to read.
179
int
180
method_base_unref (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
181
  self->refcount = self->refcount - 1;
4 by Gustav Hartvigsson
Fixed a few problems.
182
  if (self->refcount == 0) {
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
183
    s_object_free (self);
1 by Gustav Hartvigsson
Initial Code.
184
    return 0;
185
  }
186
  return self->refcount;
187
}
188
61 by Gustav Hartvigsson
* Made the code more easy to read.
189
int
190
method_base_get_refcount (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
191
  return self->refcount;
192
}
193
61 by Gustav Hartvigsson
* Made the code more easy to read.
194
char *
195
method_base_to_string (SObject * self) {
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
196
  char * ret_string = s_string_new_fmt ("(SObject, References: %d)", self->base_class->get_refcount(self));
1 by Gustav Hartvigsson
Initial Code.
197
  return ret_string;
198
}
199
200