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