/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
 */
22 by Gustav Hartvigsson
* Made code compile
53
void s_object_set_initialize_method (SObject * self, void (* method)(SObject *)) {
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
5.2.9 by Gustav Hartvigsson
* Added license to files
58
void s_object_set_free_method (SObject * self, void (* method)(SObject *)) {
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
64
void s_object_set_ref_method (SObject * self, int (* method)(SObject *)) {
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
70
void s_object_set_unref_method (SObject * self, int (* method)(SObject *)) {
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
76
void s_object_set_get_refcount_method (SObject * self, int (* method)(SObject *)) {
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
82
void s_object_set_to_string_method (SObject * self, char * (*method)(SObject *)) {
83
  SObjectClass * klass = s_object_get_class (self);
1 by Gustav Hartvigsson
Initial Code.
84
  klass->to_string = method;
85
}
86
87
22 by Gustav Hartvigsson
* Made code compile
88
void s_object_initialize (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
89
  self->refcount = 1;
22 by Gustav Hartvigsson
* Made code compile
90
  s_object_set_deinitialize_method (self,method_base_deinitialize);
91
  s_object_set_initialize_method (self, method_base_initialize);
92
  s_object_set_free_method (self, method_base_free);
5.2.9 by Gustav Hartvigsson
* Added license to files
93
  s_object_set_ref_method (self, method_base_ref);
94
  s_object_set_unref_method (self, method_base_unref);
95
  s_object_set_get_refcount_method (self, method_base_get_refcount);
96
  s_object_set_to_string_method (self, method_base_to_string);
1 by Gustav Hartvigsson
Initial Code.
97
}
98
5.2.9 by Gustav Hartvigsson
* Added license to files
99
SObject * s_object_new () {
22 by Gustav Hartvigsson
* Made code compile
100
  fprintf (stderr, "s_object_new () should never be used.\n");
101
  
5.2.9 by Gustav Hartvigsson
* Added license to files
102
  SObject * self = malloc (sizeof (SObject));
1 by Gustav Hartvigsson
Initial Code.
103
  //allocate the class definition of the object.
5.2.9 by Gustav Hartvigsson
* Added license to files
104
  self->base_class = malloc (sizeof(SObjectClass));
22 by Gustav Hartvigsson
* Made code compile
105
  //initialize it.
106
  s_object_initialize (self);
1 by Gustav Hartvigsson
Initial Code.
107
  return self;
108
}
109
5.2.9 by Gustav Hartvigsson
* Added license to files
110
void s_object_free (SObject * self) {
111
  SObjectClass * klass = s_object_get_class (self);
112
  klass->free (self);
1 by Gustav Hartvigsson
Initial Code.
113
}
114
5.2.9 by Gustav Hartvigsson
* Added license to files
115
void * s_object_get_class (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
116
  return self->base_class;
117
}
118
5.2.9 by Gustav Hartvigsson
* Added license to files
119
void s_object_set_class (SObject * self, SObjectClass * klass) {
1 by Gustav Hartvigsson
Initial Code.
120
  self->base_class = klass;
121
}
122
5.2.9 by Gustav Hartvigsson
* Added license to files
123
int s_object_unref (SObject * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
124
  unsigned int ret = self->base_class->unref (self);
125
  return ret;
1 by Gustav Hartvigsson
Initial Code.
126
}
127
5.2.9 by Gustav Hartvigsson
* Added license to files
128
int s_object_ref (SObject * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
129
  unsigned int ret = self->base_class->ref (self);
130
  return ret;
1 by Gustav Hartvigsson
Initial Code.
131
}
132
133
/**
134
 * This function returns the current reference count without chaning it.
135
 */
5.2.9 by Gustav Hartvigsson
* Added license to files
136
int s_object_get_refcount (SObject * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
137
  unsigned int ret = self->base_class->get_refcount (self);
138
  return ret;
1 by Gustav Hartvigsson
Initial Code.
139
}
140
5.2.9 by Gustav Hartvigsson
* Added license to files
141
char * s_object_to_string (SObject * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
142
  char * ret = self->base_class->to_string (self);
143
  return ret;
1 by Gustav Hartvigsson
Initial Code.
144
}
145
146
/* -----------------
147
 * Contrete methods.
148
 * -----------------
149
 */
22 by Gustav Hartvigsson
* Made code compile
150
151
152
void method_base_deinitialize (SObject * self) { }
153
154
void method_base_initialize (SObject * self) { }
1 by Gustav Hartvigsson
Initial Code.
155
5.2.9 by Gustav Hartvigsson
* Added license to files
156
void method_base_free (SObject * self) {
22 by Gustav Hartvigsson
* Made code compile
157
  self->base_class->free (self);
1 by Gustav Hartvigsson
Initial Code.
158
}
159
5.2.9 by Gustav Hartvigsson
* Added license to files
160
int method_base_ref (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
161
  self->refcount = self->refcount + 1;
162
  return self->refcount;
163
}
164
5.2.9 by Gustav Hartvigsson
* Added license to files
165
int method_base_unref (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
166
  self->refcount = self->refcount - 1;
4 by Gustav Hartvigsson
Fixed a few problems.
167
  if (self->refcount == 0) {
5.2.9 by Gustav Hartvigsson
* Added license to files
168
    self->base_class->free (self);
1 by Gustav Hartvigsson
Initial Code.
169
    return 0;
170
  }
171
  return self->refcount;
172
}
173
5.2.9 by Gustav Hartvigsson
* Added license to files
174
int method_base_get_refcount (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
175
  return self->refcount;
176
}
177
5.2.9 by Gustav Hartvigsson
* Added license to files
178
char * method_base_to_string (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
179
  char * ret_string = malloc (sizeof (char) * 32); // I am bad with maths..
3 by Gustav Hartvigsson
Fixed a few things...
180
  sprintf (ret_string, "References: %d ", self->base_class->get_refcount(self));
1 by Gustav Hartvigsson
Initial Code.
181
  return ret_string;
182
}
183
184