/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
48 by Gustav Hartvigsson
* Finnished SLinkedList.
92
void s_object_initialize (SObject * self, const char * name) {
93
  self->name = s_string_new (name);
94
  
1 by Gustav Hartvigsson
Initial Code.
95
  self->refcount = 1;
48 by Gustav Hartvigsson
* Finnished SLinkedList.
96
  
22 by Gustav Hartvigsson
* Made code compile
97
  s_object_set_deinitialize_method (self,method_base_deinitialize);
98
  s_object_set_initialize_method (self, method_base_initialize);
99
  s_object_set_free_method (self, method_base_free);
5.2.9 by Gustav Hartvigsson
* Added license to files
100
  s_object_set_ref_method (self, method_base_ref);
101
  s_object_set_unref_method (self, method_base_unref);
102
  s_object_set_get_refcount_method (self, method_base_get_refcount);
103
  s_object_set_to_string_method (self, method_base_to_string);
1 by Gustav Hartvigsson
Initial Code.
104
}
105
5.2.9 by Gustav Hartvigsson
* Added license to files
106
SObject * s_object_new () {
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
107
  s_warn_print ("s_object_new () should never be used in production code");
22 by Gustav Hartvigsson
* Made code compile
108
  
5.2.9 by Gustav Hartvigsson
* Added license to files
109
  SObject * self = malloc (sizeof (SObject));
1 by Gustav Hartvigsson
Initial Code.
110
  //allocate the class definition of the object.
5.2.9 by Gustav Hartvigsson
* Added license to files
111
  self->base_class = malloc (sizeof(SObjectClass));
22 by Gustav Hartvigsson
* Made code compile
112
  //initialize it.
48 by Gustav Hartvigsson
* Finnished SLinkedList.
113
  s_object_initialize (self, "SObject");
1 by Gustav Hartvigsson
Initial Code.
114
  return self;
115
}
116
5.2.9 by Gustav Hartvigsson
* Added license to files
117
void s_object_free (SObject * self) {
48 by Gustav Hartvigsson
* Finnished SLinkedList.
118
  free (self->name);
5.2.9 by Gustav Hartvigsson
* Added license to files
119
  SObjectClass * klass = s_object_get_class (self);
120
  klass->free (self);
1 by Gustav Hartvigsson
Initial Code.
121
}
122
49 by Gustav Hartvigsson
* started work SBox (Untested).
123
SObjectClass * s_object_get_class (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
124
  return self->base_class;
125
}
126
5.2.9 by Gustav Hartvigsson
* Added license to files
127
void s_object_set_class (SObject * self, SObjectClass * klass) {
1 by Gustav Hartvigsson
Initial Code.
128
  self->base_class = klass;
129
}
130
5.2.9 by Gustav Hartvigsson
* Added license to files
131
int s_object_unref (SObject * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
132
  unsigned int ret = self->base_class->unref (self);
133
  return ret;
1 by Gustav Hartvigsson
Initial Code.
134
}
135
5.2.9 by Gustav Hartvigsson
* Added license to files
136
int s_object_ref (SObject * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
137
  unsigned int ret = self->base_class->ref (self);
138
  return ret;
1 by Gustav Hartvigsson
Initial Code.
139
}
140
141
/**
142
 * This function returns the current reference count without chaning it.
143
 */
5.2.9 by Gustav Hartvigsson
* Added license to files
144
int s_object_get_refcount (SObject * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
145
  unsigned int ret = self->base_class->get_refcount (self);
146
  return ret;
1 by Gustav Hartvigsson
Initial Code.
147
}
148
5.2.9 by Gustav Hartvigsson
* Added license to files
149
char * s_object_to_string (SObject * self) {
39 by Gustav Hartvigsson
* Added "check" target for testing.
150
  return self->base_class->to_string (self);
1 by Gustav Hartvigsson
Initial Code.
151
}
152
153
/* -----------------
154
 * Contrete methods.
155
 * -----------------
156
 */
22 by Gustav Hartvigsson
* Made code compile
157
158
159
void method_base_deinitialize (SObject * self) { }
160
161
void method_base_initialize (SObject * self) { }
1 by Gustav Hartvigsson
Initial Code.
162
5.2.9 by Gustav Hartvigsson
* Added license to files
163
void method_base_free (SObject * self) {
32 by Gustav Hartvigsson
* Added some compile options to the root CMakeLists.txt
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) {
39 by Gustav Hartvigsson
* Added "check" target for testing.
187
  char * ret_string = s_string_new_fmt ("References: %d", self->base_class->get_refcount(self));
1 by Gustav Hartvigsson
Initial Code.
188
  return ret_string;
189
}
190
191