/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
5.2.9 by Gustav Hartvigsson
* Added license to files
35
// SObject * method_base_init (SObject * self);
36
37
void method_base_free (SObject * self);
38
39
int method_base_ref (SObject * self);
40
41
int method_base_unref (SObject * self);
42
43
int method_base_get_refcount (SObject * self);
44
45
char * method_base_to_string (SObject * self);
1 by Gustav Hartvigsson
Initial Code.
46
47
/* -----------------
48
 * Helper functions...
49
 * -----------------
50
 */
5.2.9 by Gustav Hartvigsson
* Added license to files
51
void s_object_set_init_method (SObject * self, SObject * (* method)(* SObject)) {
52
  SObjectClass * klass = s_object_get_class (self);
1 by Gustav Hartvigsson
Initial Code.
53
  klass->initize = method;
54
}
5.2.7 by Gustav Hartvigsson
* Switched licence to a more permisive one.
55
5.2.9 by Gustav Hartvigsson
* Added license to files
56
void s_object_set_free_method (SObject * self, void (* method)(SObject *)) {
57
  SObjectClass * klass = s_object_get_class (self);
58
  klass->free = method;
1 by Gustav Hartvigsson
Initial Code.
59
} 
60
5.2.9 by Gustav Hartvigsson
* Added license to files
61
62
void s_object_set_ref_method (SObject * self, int (* method)(SObject *)) {
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
68
void s_object_set_unref_method (SObject * self, int (* method)(SObject *)) {
69
  SObjectClass * klass = s_object_get_class (self);
1 by Gustav Hartvigsson
Initial Code.
70
  klass->unref = method;
71
}
72
5.2.9 by Gustav Hartvigsson
* Added license to files
73
74
void s_object_set_get_refcount_method (SObject * self, int (* method)(SObject *)) {
75
  SObjectClass * klass = s_object_get_class (self);
4 by Gustav Hartvigsson
Fixed a few problems.
76
  klass->get_refcount = method;
77
}
78
5.2.9 by Gustav Hartvigsson
* Added license to files
79
80
void s_object_set_to_string_method (SObject * self, char * (*method)(SObject *)) {
81
  SObjectClass * klass = s_object_get_class (self);
1 by Gustav Hartvigsson
Initial Code.
82
  klass->to_string = method;
83
}
84
85
5.2.9 by Gustav Hartvigsson
* Added license to files
86
void s_object_initize (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
87
  self->refcount = 1;
5.2.9 by Gustav Hartvigsson
* Added license to files
88
  // s_object_set_init_method (self, method_base_init);
89
  s_object_set_free_method (self, method_base_deinit);
90
  s_object_set_ref_method (self, method_base_ref);
91
  s_object_set_unref_method (self, method_base_unref);
92
  s_object_set_get_refcount_method (self, method_base_get_refcount);
93
  s_object_set_to_string_method (self, method_base_to_string);
1 by Gustav Hartvigsson
Initial Code.
94
}
95
5.2.9 by Gustav Hartvigsson
* Added license to files
96
SObject * s_object_new () {
97
  SObject * self = malloc (sizeof (SObject));
1 by Gustav Hartvigsson
Initial Code.
98
  //allocate the class definition of the object.
5.2.9 by Gustav Hartvigsson
* Added license to files
99
  self->base_class = malloc (sizeof(SObjectClass));
1 by Gustav Hartvigsson
Initial Code.
100
  //initize it.
5.2.9 by Gustav Hartvigsson
* Added license to files
101
  s_object_initize (self);
1 by Gustav Hartvigsson
Initial Code.
102
  return self;
103
}
104
5.2.9 by Gustav Hartvigsson
* Added license to files
105
void s_object_free (SObject * self) {
106
  SObjectClass * klass = s_object_get_class (self);
107
  klass->free (self);
1 by Gustav Hartvigsson
Initial Code.
108
}
109
5.2.9 by Gustav Hartvigsson
* Added license to files
110
void * s_object_get_class (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
111
  return self->base_class;
112
}
113
5.2.9 by Gustav Hartvigsson
* Added license to files
114
void s_object_set_class (SObject * self, SObjectClass * klass) {
1 by Gustav Hartvigsson
Initial Code.
115
  self->base_class = klass;
116
}
117
5.2.9 by Gustav Hartvigsson
* Added license to files
118
int s_object_unref (SObject * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
119
  unsigned int ret = self->base_class->unref (self);
120
  return ret;
1 by Gustav Hartvigsson
Initial Code.
121
}
122
5.2.9 by Gustav Hartvigsson
* Added license to files
123
int s_object_ref (SObject * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
124
  unsigned int ret = self->base_class->ref (self);
125
  return ret;
1 by Gustav Hartvigsson
Initial Code.
126
}
127
128
/**
129
 * This function returns the current reference count without chaning it.
130
 */
5.2.9 by Gustav Hartvigsson
* Added license to files
131
int s_object_get_refcount (SObject * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
132
  unsigned int ret = self->base_class->get_refcount (self);
133
  return ret;
1 by Gustav Hartvigsson
Initial Code.
134
}
135
5.2.9 by Gustav Hartvigsson
* Added license to files
136
char * s_object_to_string (SObject * self) {
4 by Gustav Hartvigsson
Fixed a few problems.
137
  char * ret = self->base_class->to_string (self);
138
  return ret;
1 by Gustav Hartvigsson
Initial Code.
139
}
140
141
/* -----------------
142
 * Contrete methods.
143
 * -----------------
144
 */
145
 
146
 
5.2.9 by Gustav Hartvigsson
* Added license to files
147
// SObject * method_base_init (SObject * self) { }
1 by Gustav Hartvigsson
Initial Code.
148
5.2.9 by Gustav Hartvigsson
* Added license to files
149
void method_base_free (SObject * self) {
150
  self->base_class->free (self)
1 by Gustav Hartvigsson
Initial Code.
151
}
152
5.2.9 by Gustav Hartvigsson
* Added license to files
153
int method_base_ref (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
154
  self->refcount = self->refcount + 1;
155
  return self->refcount;
156
}
157
5.2.9 by Gustav Hartvigsson
* Added license to files
158
int method_base_unref (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
159
  self->refcount = self->refcount - 1;
4 by Gustav Hartvigsson
Fixed a few problems.
160
  if (self->refcount == 0) {
5.2.9 by Gustav Hartvigsson
* Added license to files
161
    self->base_class->free (self);
1 by Gustav Hartvigsson
Initial Code.
162
    return 0;
163
  }
164
  return self->refcount;
165
}
166
5.2.9 by Gustav Hartvigsson
* Added license to files
167
int method_base_get_refcount (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
168
  return self->refcount;
169
}
170
5.2.9 by Gustav Hartvigsson
* Added license to files
171
char * method_base_to_string (SObject * self) {
1 by Gustav Hartvigsson
Initial Code.
172
  char * ret_string = malloc (sizeof (char) * 32); // I am bad with maths..
3 by Gustav Hartvigsson
Fixed a few things...
173
  sprintf (ret_string, "References: %d ", self->base_class->get_refcount(self));
1 by Gustav Hartvigsson
Initial Code.
174
  return ret_string;
175
}
176
177