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.
76
char * my_str; //The private data.
79
void foo_deinit_method (Foo * self); // Prototype the deinit method.
80
void foo_to_string_method (Foo * self); // Prototype the to_string method.
81
bool foo_comp_method (char * str1, char * str2); // Prototype the comp method
84
Foo * foo_new (char * my_str) {
85
Foo * self = malloc ( sizeof (Foo));
86
FooClass * klass = malloc( sizeof (FooClass));
88
s_base_object_set_class (self, klass);
89
s_base_object_initize (self);
90
s_base_object_set_deinit_method (self, foo_deinit_method);
91
s_base_object_set_to_string_method (self, foo_to_string_method);
92
klass->some_method = foo_comp_method;
97
void foo_free (Foo * self) {
98
s_base_object_free (self); // Calls foo_deinit_method
101
bool foo_comp (Foo * self, char * other_str) {
102
FooClass * klass = (FooClass *) s_base_object_get_class (self);
103
return klass->some_method (self->priv->my_str, other_str);
106
void foo_set_str (Foo * self, char * new_str) {
107
free (self->priv->my_str);
108
self->priv->my_str = s_string_new (new_str);
114
Now we are almost done, the only things that remain is to write the
115
methods. but first we have to go through what the code does.
118
In the constructor <code> foo_new </code> we first create a pointer to
119
an instance of the object and an instance of the object class. The class
120
is then set to the object instance using the
121
<code> s_base_object_set_class </code> function. It is important to set
122
the objects class <b> before </b> you initize the object.
125
After the class is set the object is initized, this adds the standard
126
<code> deinit, ref, unref </code> and <code> to_string </code> methods.
129
We then have to set our own <code> to_string </code> and <code> deinit
130
</code> methods using the <code> s_base_object_set_to_string_method,
131
</code> and <code> s_base_object_set_deinit_method </code>.
134
In the start of the <code>C</code>-file we wrote some prototypes or
135
declarations of these methods, now it is time to implement them.
139
void foo_deinit_method (Foo * self) {
140
FooClass klass = (FooClass *) s_base_object_get_class (self);
141
free(self->priv->my_str);
146
void foo_to_string_method (Foo * self) {
147
return self->priv->my_str;
150
bool foo_comp_method (char * str1, char * str2) {
151
if ( strcmp ( str1, str2 ) == 0 ) {
163
Please note that not any of the objects in the system are thread-safe,
164
and probably never will be.
166
<footer style="padding-top: 10em">
167
<!-- <A name="anchor"><p> * Note </p></A> -->