2
(C) Gustav Hartvigsson, 2013.
4
This program is free software: you can redistribute it and/or modify
5
it under the terms of the GNU Lesser General Public License as
6
published by the Free Software Foundation, either version 3 of the
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program. If not, see <http://www.gnu.org/licenses/>.
18
#ifndef __H_BASE_OBJECT__
19
#define __H_BASE_OBJECT__
21
/* ---------------------------------------------------
22
* This is just a test of creating a small typesystem.
23
* It will include refrence counting.
24
* ---------------------------------------------------
28
* The class holds all the "virtual" functions/methods that are to be used with
31
* This is an opaque data structure.
33
typedef struct _BaseObjectClass BaseObjectClass;
36
* The instance holds the data that is associated with the object.
37
* it also holds a pointer to the class of the object.
39
* This is an opaque data structure.
41
typedef struct _BaseObjectInstance BaseObjectInstance;
43
/* internal structure of the structs are as follows:
44
typedef struct _BaseObjectClass {
45
// BaseObjectInstance * (* initize)(* BaseObjectInstance);
46
void (* deinitize)(* BaseObjectInstance);
47
int (* ref)(* BaseObjectInstance);
48
int (* unref)(* BaseObjectInstance);
49
int (* get_refcount)(* BaseObjectInstance);
50
char * (* to_string)(* BaseObjectInstance);
53
typedef struct _BaseObjectInstance {
54
BaseObjectClass * base_class;
55
unsigned int refcount = 0;
64
* This function is used to set the method to initize a new instance of an
67
* This has no use, rely...(?)
69
// void base_object_set_init_method (BaseObjectInstance * self, BaseObjectInstance * (* method)(* BaseObjectInstance));
72
* This function is used to set the method to deinitize an object.
74
* set it to a method that deinitize your object.
76
void base_object_set_deinit_method (BaseObjectInstance * self, void (* method)(* BaseObjectInstance));
79
* This fuction is used to set the ref method.
81
* DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.
83
void base_object_set_ref_method (BaseObjectInstance * self, int (* method)(* BaseObjectInstance));
86
* This fuction is used to set the unref method.
88
* DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.
90
void base_object_set_unref_method (BaseObjectInstance * self, int (* method)(* BaseObjectInstance));
93
* This fuction is used to set the get_refcount method.
95
* DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.
97
void base_object_set_get_refcount_method (BaseObjectInstance * self, int (* method)(* BaseObjectInstance));
100
* This fuction is used to set the to_string method.
102
void base_object_set_to_string_method (BaseObjectInstance * self, char * (* method)(* BaseObjectInstance));
106
* concreate functions are defined in the C file.
109
/* ----------------------
110
* Base object functions.
111
* ----------------------
115
* This function initizes an intance of the BaseObject, it also sets the methods
116
* to be used with the object and sets the refrence count to one.
118
void base_object_initize (BaseObjectInstance * self);
121
* This function creates a new base object.
123
BaseObjectInstance * base_object_new ();
126
* This function deinitizes/frees an object even if it is still referenced.
127
* This is usualy a bad idea, use base_object_unref instead.
129
void base_object_free (BaseObjectInstance * self);
132
* This function gets the class (which hold the object methods).
134
BaseObjectClass * base_object_get_class (BaseObjectInstance * self);
137
* This function sets the instance class of an object.
139
void base_object_set_class (BaseObjectInstance * self, BaseObjectClass * klass);
142
* This function is used to decrese the reference count of an object.
143
* When an object reaches zero, it will deinitize the object using the objects
146
* It returns the current (after change) reference count.
148
int base_object_unref (BaseObjectInstance * self);
151
* This function is used to increse the reference count of an object.
153
* Returns the current (after change) reference count.
155
int base_object_ref (BaseObjectInstance * self);
158
* This function returns the current reference count without chaning it.
160
int base_object_get_refcount (BaseObjectInstance * self);
163
* This function returns a textual (string) that represesnts the object.
164
* The method can be set using base_object_set_to_string_method.
166
* Note: The string that is returned must be freed.
168
char * base_object_to_string (BaseObjectInstance * self);