/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk

« back to all changes in this revision

Viewing changes to src/baseobject.c

  • Committer: Gustav Hartvigsson
  • Date: 2013-09-03 22:18:54 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20130903221854-0xxnlloz690hs22o
Made the code compile.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
*/
17
17
 
18
18
#include "baseobject.h"
 
19
#include <stdlib.h>
19
20
 
20
21
typedef struct _BaseObjectClass {
21
 
  // BaseObjectInstance * (* initize)(* BaseObjectInstance);
22
 
  void (* deinitize)(* BaseObjectInstance);
23
 
  int (* ref)(* BaseObjectInstance);
24
 
  int (* unref)(* BaseObjectInstance);
25
 
  int (* get_refcount)(* BaseObectInstance);
26
 
  char * (* to_string)(* BaseObjectInstance);
 
22
  // BaseObjectInstance * (* initize)(BaseObjectInstance *);
 
23
  void (*deinitize)(BaseObjectInstance *);
 
24
  int (*ref)(BaseObjectInstance *);
 
25
  int (*unref)(BaseObjectInstance *);
 
26
  int (*get_refcount)(BaseObjectInstance *);
 
27
  char * (*to_string)(BaseObjectInstance *);
27
28
};
28
29
 
29
30
typedef struct _BaseObjectInstance {
30
31
  BaseObjectClass * base_class;
31
 
  unsigned int refcount = 0;
 
32
  unsigned int refcount;
32
33
};
33
34
 
34
35
/* ---------------------------
70
71
 * 
71
72
 * set it to a method that deinitize your object.
72
73
 */
73
 
void base_object_set_deinit_method (BaseObjectInstance * self, void (* method)(* BaseObjectInstance)) {
 
74
void base_object_set_deinit_method (BaseObjectInstance * self, void (* method)(BaseObjectInstance *)) {
74
75
  BaseObjectClass * klass = base_object_get_class (self);
75
76
  klass->deinitize = method;
76
77
80
81
 * 
81
82
 * DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.
82
83
 */
83
 
void base_object_set_ref_method (BaseObjectInstance * self, int (* method)(* BaseObjectInstance)) {
 
84
void base_object_set_ref_method (BaseObjectInstance * self, int (* method)(BaseObjectInstance *)) {
84
85
  BaseObjectClass * klass = base_object_get_class (self);
85
86
  klass->ref = method;
86
87
}
90
91
 * 
91
92
 * DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.
92
93
 */
93
 
void base_object_set_unref_method (BaseObjectInstance * self, int (* method)(* BaseObjectInstance)) {
 
94
void base_object_set_unref_method (BaseObjectInstance * self, int (* method)(BaseObjectInstance *)) {
94
95
  BaseObjectClass * klass = base_object_get_class (self);
95
96
  klass->unref = method;
96
97
}
97
98
 
98
99
/**
99
 
 * This fuction is used to set the get_refcount method.
100
 
 * 
101
 
 * DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.
102
 
 */
103
 
void base_object_set_unref_method (BaseObjectInstance * self, int (* method)(* BaseObjectInstance)) {
104
 
  BaseObjectClass * klass = base_object_get_class (self);
105
 
  klass->get_refcount = method;
106
 
}
107
 
 
108
 
/**
109
100
 * This fuction is used to set the to_string method.
110
101
 */
111
 
void base_object_set_to_string_method (BaseObjectInstance * self, char * (* method)(* BaseObjectInstance)) {
 
102
void base_object_set_to_string_method (BaseObjectInstance * self, char * (*method)(BaseObjectInstance *)) {
112
103
  BaseObjectClass * klass = base_object_get_class (self);
113
104
  klass->to_string = method;
114
105
}
125
116
void base_object_initize (BaseObjectInstance * self) {
126
117
  self->refcount = 1;
127
118
  // base_object_set_init_method (self, method_base_init);
128
 
  base_object_set_deintit_method (self, method_base_deinit);
 
119
  base_object_set_deinit_method (self, method_base_deinit);
129
120
  base_object_set_ref_method (self, method_base_ref);
130
121
  base_object_set_unref_method (self, method_base_unref);
131
 
  base_object_set_get_ref_count_method (self, method_base_get_refcount);
 
122
  base_object_set_get_refcount_method (self, method_base_get_refcount);
132
123
  base_object_set_to_string_method (self, method_base_to_string);
133
124
}
134
125
 
138
129
BaseObjectInstance * base_object_new () {
139
130
  BaseObjectInstance * self = malloc (sizeof (BaseObjectInstance) + 1);
140
131
  //allocate the class definition of the object.
141
 
  self->base_class = malloc (class_size + 1);
 
132
  self->base_class = malloc (sizeof(BaseObjectClass));
142
133
  //initize it.
143
 
  base_object_initize (self, sizeof(BaseObjectClass) + 1);
 
134
  base_object_initize (self);
144
135
  return self;
145
136
}
146
137
 
225
216
int method_base_unref (BaseObjectInstance * self) {
226
217
  self->refcount = self->refcount - 1;
227
218
  if (self->refcount <= 0) {
228
 
    self->base_class->deinit (self);
 
219
    self->base_class->deinitize (self);
229
220
    return 0;
230
221
  }
231
222
  return self->refcount;