4
<title> The (Super) Simple Type System </title>
8
<nav><a href="./index.html"> Back to Index </a></nav>
10
Inheritance in SSTS is interesting, it follows the same paradigm as
11
<a href="https://developer.gnome.org/gobject/stable/chapter-gobject.html">
16
In the header file the structs that will define the object are difened
17
along with the functions that operate on the object.
27
#include "baseobject.h"
28
#include <stdbool.h>
29
typedef struct _FooClass FooClass;
30
typedef struct _Foo Foo;
31
typedef struct _FooPrivate FooPrivate; //the private data is defined in the .c file
34
SBaseObjectClass parent_class; // The parent class holds the methods of the object
35
CompFunc some_method; // Some method associated with the object.
39
SBaseObjectInstance parent;
45
This was the declaration of the the data structures.
47
Then we need some functions to operate on the objects:
51
Foo * foo_new (char * my_str);
52
void foo_free (Foo * self);
53
bool foo_comp (Foo * self, char * other_str);
54
void foo_set_str (Foo * self, char * new_str);
59
<h2> C-Code file </h2>
61
In the .c file we define the real functions and methods to be used with
62
the object. It is a littl fidely at first but when the objet is in place
63
it should be ready to use.
66
Remeber, never trust anyone elses code.
75
char * my_str; //The private data.
78
void foo_deinit_method (Foo * self); // Prototype the deinit method.
79
void foo_to_string_method (Foo * self); // Prototype the to_string method.
80
bool foo_comp_method (char * str1, char * str2); // Prototype the comp method
83
Foo * foo_new (char * my_str) {
84
Foo * self = malloc ( sizeof (Foo));
85
FooClass * klass = malloc( sizeof (FooClass));
87
s_base_object_set_class (self, klass);
88
s_base_object_initize (self);
89
s_base_object_set_deinit_method (self, foo_deinit_method);
90
s_base_object_set_to_string_method (self, foo_to_string_method);
91
klass->some_method = foo_comp_method;
96
void foo_free (Foo * self) {
97
s_base_object_free (self); // Calls foo_deinit_method
100
bool foo_comp (Foo * self, char * other_str) {
101
FooClass * klass = (FooClass *) s_base_object_get_class (self);
102
return klass->some_method (self->priv->my_str, other_str);
105
void foo_set_str (Foo * self, char * new_str) {
106
free (self->priv->my_str);
107
self->priv->my_str = s_string_new (new_str);
112
Now we are almost done, the only things that remain is to write the
113
methods. but first we have to go through what the code does.
116
In the constructor <code> foo_new </code> we first create a pointer to
117
an instance of the object and an instance of the object class. The class
118
is then set to the object instance using the
119
<code> s_base_object_set_class </code> function. It is important to set
120
the objects class <b> before </b> you initize the object.
123
After the class is set the object is initized, this adds the standard
124
<code> deinit, ref, unref </code> and <code> to_string </code> methods.
127
We then have to set our own <code> to_string </code> and <code> deinit
128
</code> methods using the <code> s_base_object_set_to_string_method,
129
</code> and <code> s_base_object_set_deinit_method </code>.
132
In the start of the <code>C</code>-file we wrote some prototypes or
133
declarations of these methods, now it is time to implement them.
136
void foo_deinit_method (Foo * self) {
137
FooClass klass = (FooClass *) s_base_object_get_class (self);
138
free(self->priv->my_str);
143
void foo_to_string_method (Foo * self) {
144
return self->priv->my_str;
147
bool foo_comp_method (char * str1, char * str2) {
148
if ( strcmp ( str1, str2 ) == 0 ) {
159
Please note that not any of the objects in the system are thread-safe,
160
and probably never will be.
162
<footer style="padding-top: 10em">
163
<!-- <A name="anchor"><p> * Note </p></A> -->