/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.h

  • Committer: Gustav Hartvigsson
  • Date: 2013-09-02 21:55:23 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20130902215523-8rr3iyjan2w0apge
Initial Code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    (C) Gustav Hartvigsson, 2013.
 
3
    
 
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
 
7
    License.
 
8
 
 
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.
 
13
 
 
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/>.
 
16
*/
 
17
 
 
18
#ifndef __H_BASE_OBJECT__
 
19
#define __H_BASE_OBJECT__
 
20
 
 
21
/* ---------------------------------------------------
 
22
 * This is just a test of creating a small typesystem.
 
23
 * It will include refrence counting.
 
24
 * ---------------------------------------------------
 
25
 */
 
26
 
 
27
/**
 
28
 * The class holds all the "virtual" functions/methods that are to be used with
 
29
 * the object.
 
30
 *
 
31
 * This is an opaque data structure.
 
32
 */
 
33
typedef struct _BaseObjectClass BaseObjectClass;
 
34
 
 
35
/**
 
36
 * The instance holds the data that is associated with the object.
 
37
 * it also holds a pointer to the class of the object.
 
38
 *
 
39
 * This is an opaque data structure.
 
40
 */
 
41
typedef struct _BaseObjectInstance BaseObjectInstance;
 
42
 
 
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);
 
51
};
 
52
 
 
53
typedef struct _BaseObjectInstance {
 
54
  BaseObjectClass * base_class;
 
55
  unsigned int refcount = 0;
 
56
};
 
57
*/
 
58
 
 
59
/* -----------------
 
60
 * Helper functions...
 
61
 * -----------------
 
62
 */
 
63
/**
 
64
 * This function is used to set the method to initize a new instance of an
 
65
 * object.
 
66
 *
 
67
 * This has no use, rely...(?)
 
68
 */
 
69
// void base_object_set_init_method (BaseObjectInstance * self, BaseObjectInstance * (* method)(* BaseObjectInstance));
 
70
 
 
71
/**
 
72
 * This function is used to set the method to deinitize an object.
 
73
 * 
 
74
 * set it to a method that deinitize your object.
 
75
 */
 
76
void base_object_set_deinit_method (BaseObjectInstance * self, void (* method)(* BaseObjectInstance));
 
77
 
 
78
/**
 
79
 * This fuction is used to set the ref method.
 
80
 * 
 
81
 * DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.
 
82
 */
 
83
void base_object_set_ref_method (BaseObjectInstance * self, int (* method)(* BaseObjectInstance));
 
84
 
 
85
/**
 
86
 * This fuction is used to set the unref method.
 
87
 * 
 
88
 * DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.
 
89
 */
 
90
void base_object_set_unref_method (BaseObjectInstance * self, int (* method)(* BaseObjectInstance));
 
91
 
 
92
/**
 
93
 * This fuction is used to set the get_refcount method.
 
94
 * 
 
95
 * DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.
 
96
 */
 
97
void base_object_set_get_refcount_method (BaseObjectInstance * self, int (* method)(* BaseObjectInstance));
 
98
 
 
99
/**
 
100
 * This fuction is used to set the to_string method.
 
101
 */
 
102
void base_object_set_to_string_method (BaseObjectInstance * self, char * (* method)(* BaseObjectInstance));
 
103
 
 
104
 
 
105
/*
 
106
 * concreate functions are defined in the C file.
 
107
 */
 
108
 
 
109
/* ----------------------
 
110
 * Base object functions.
 
111
 * ----------------------
 
112
 */
 
113
 
 
114
/**
 
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.
 
117
 */
 
118
void base_object_initize (BaseObjectInstance * self);
 
119
 
 
120
/**
 
121
 * This function creates a new base object.
 
122
 */
 
123
BaseObjectInstance * base_object_new ();
 
124
 
 
125
/**
 
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.
 
128
 */
 
129
void base_object_free (BaseObjectInstance * self);
 
130
 
 
131
/**
 
132
 * This function gets the class (which hold the object methods).
 
133
 */
 
134
BaseObjectClass * base_object_get_class (BaseObjectInstance * self);
 
135
 
 
136
/**
 
137
 * This function sets the instance class of an object.
 
138
 */
 
139
void base_object_set_class (BaseObjectInstance * self, BaseObjectClass * klass);
 
140
 
 
141
/**
 
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
 
144
 * deititize method.
 
145
 * 
 
146
 * It returns the current (after change) reference count.
 
147
 */
 
148
int base_object_unref (BaseObjectInstance * self);
 
149
 
 
150
/**
 
151
 * This function is used to increse the reference count of an object.
 
152
 *
 
153
 * Returns the current (after change) reference count.
 
154
 */
 
155
int base_object_ref (BaseObjectInstance * self);
 
156
 
 
157
/**
 
158
 * This function returns the current reference count without chaning it.
 
159
 */
 
160
int base_object_get_refcount (BaseObjectInstance * self);
 
161
 
 
162
/**
 
163
 * This function returns a textual (string) that represesnts the object.
 
164
 * The method can be set using base_object_set_to_string_method.
 
165
 *
 
166
 * Note: The string that is returned must be freed.
 
167
 */
 
168
char * base_object_to_string (BaseObjectInstance * self);
 
169
 
 
170
 
 
171
#endif