/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
11 by Gustav Hartvigsson
* Finnished up a the inheritance documentation.
1
<!DOCTYPE html>
2
<html>
3
  <head>
4
    <title> The (Super) Simple Type System </title>
5
  </head>
6
  <body>
7
    <h1>inheritance</h1>
8
    <nav><a href="./index.html"> Back to Index </a></nav>
9
    <p>
10
      Inheritance in SSTS is interesting, it follows the same paradigm as
11
      <a href="https://developer.gnome.org/gobject/stable/chapter-gobject.html">
12
      GObject </a>.
13
    </p>
14
    <h2>Header file</h2>
15
    <p>
16
      In the header file the structs that will define the object are difened
17
      along with the functions that operate on the object.
18
    </p>
19
    <p>
20
      <code>Foo.h</code>
21
    </p>
22
    <hr/>
23
    <pre>
24
#ifndef __H_FOO__
25
#define __H_FOO__
26
27
#include "baseobject.h"
28
#include &#60;stdbool.h&#62;
29
typedef struct _FooClass FooClass;
30
typedef struct _Foo Foo;
31
typedef struct _FooPrivate FooPrivate; //the private data is defined in the .c file
32
33
struct _FooClass {
34
  SBaseObjectClass parent_class; // The parent class holds the methods of the object
35
  CompFunc some_method; // Some method associated with the object.
36
};
37
38
struct _Foo {
39
  SBaseObjectInstance parent;
40
  FooPrivate * priv;
41
};
42
    </pre>
43
    <hr/>
44
    <p>
45
      This was the declaration of the the data structures.
46
    </p><p>
47
      Then we need some functions to operate on the objects:
48
    </p>
49
    <hr/>
50
    <pre>
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);
55
56
#endif //__H_FOO__
57
    </pre>
58
    <hr/>
59
    <h2> C-Code file </h2>
60
    <p>
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.
64
    </p>
65
    <p>
66
      Remeber, never trust anyone elses code.
67
    </p>
68
    <p>
69
      <code>Foo.c</code>
70
    </p>
71
    <pre>
72
#include "Foo.h"
73
74
struct _FooPrivate {
75
  char * my_str; //The private data.
76
};
77
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
81
82
//The constructor
83
Foo * foo_new (char * my_str) {
84
  Foo * self = malloc ( sizeof (Foo));
85
  FooClass * klass = malloc( sizeof (FooClass));
86
  
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;
92
  
93
  return self;
94
}
95
96
void foo_free (Foo * self) {
97
  s_base_object_free (self); // Calls foo_deinit_method
98
}
99
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);
103
}
104
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);
108
}
109
110
    </pre>
111
    <p>
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.
114
    </p>
115
    <p>
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.
121
    </p>
122
    <p>
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.
125
    </p>
126
    <p>
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>.
130
    </p>
131
    <p>
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.
134
    </p>
135
    <pre>
136
void foo_deinit_method (Foo * self) {
137
  FooClass klass = (FooClass *) s_base_object_get_class (self);
138
  free(self->priv->my_str);
139
  free(klass);
140
  free(self);
141
}
142
143
void foo_to_string_method (Foo * self) {
144
  return self->priv->my_str;
145
}
146
147
bool foo_comp_method (char * str1, char * str2) {
148
  if ( strcmp ( str1, str2 ) == 0 ) {
149
    return true;
150
  } else {
151
    return false;
152
  }
153
}
154
    </pre>
155
    <p>
156
      And that is it.
157
    </p>
158
    <p>
159
      Please note that not any of the objects in the system are thread-safe,
160
      and probably never will be.
161
    </p>
162
    <footer style="padding-top: 10em">
163
      <!-- <A name="anchor"><p>  * Note </p></A> -->
164
    </footer>
165
  </body>
166
</html>