/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<!DOCTYPE html>
<html>
  <head>
    <title> The (Super) Simple Type System </title>
  </head>
  <body>
    <h1>inheritance</h1>
    <nav><a href="./index.html"> Back to Index </a></nav>
    <p>
      Inheritance in SSTS is interesting, it follows the same paradigm as
      <a href="https://developer.gnome.org/gobject/stable/chapter-gobject.html">
      GObject </a>.
    </p>
    <h2>Header file</h2>
    <p>
      In the header file the structs that will define the object are difened
      along with the functions that operate on the object.
    </p>
    <p>
      <code>Foo.h</code>
    </p>
    <hr/>
    <pre>
#ifndef __H_FOO__
#define __H_FOO__

#include "baseobject.h"
#include &#60;stdbool.h&#62;
typedef struct _FooClass FooClass;
typedef struct _Foo Foo;
typedef struct _FooPrivate FooPrivate; //the private data is defined in the .c file

struct _FooClass {
  SBaseObjectClass parent_class; // The parent class holds the methods of the object
  CompFunc some_method; // Some method associated with the object.
};

struct _Foo {
  SBaseObjectInstance parent;
  FooPrivate * priv;
};
    </pre>
    <hr/>
    <p>
      This was the declaration of the the data structures.
    </p><p>
      Then we need some functions to operate on the objects:
    </p>
    <hr/>
    <pre>
Foo * foo_new (char * my_str);
void foo_free (Foo * self);
bool foo_comp (Foo * self, char * other_str);
void foo_set_str (Foo * self, char * new_str);

#endif //__H_FOO__
    </pre>
    <hr/>
    <h2> C-Code file </h2>
    <p>
      In the .c file we define the real functions and methods to be used with
      the object. It is a littl fidely at first but when the objet is in place
      it should be ready to use.
    </p>
    <p>
      Remeber, never trust anyone elses code.
    </p>
    <p>
      <code>Foo.c</code>
    </p>
    <hr/>
    <pre>
#include "Foo.h"

struct _FooPrivate {
  char * my_str; //The private data.
};

void foo_deinit_method (Foo * self); // Prototype the deinit method.
void foo_to_string_method (Foo * self); // Prototype the to_string method.
bool foo_comp_method (char * str1, char * str2); // Prototype the comp method

//The constructor
Foo * foo_new (char * my_str) {
  Foo * self = malloc ( sizeof (Foo));
  FooClass * klass = malloc( sizeof (FooClass));
  
  s_base_object_set_class (self, klass);
  s_base_object_initize (self);
  s_base_object_set_deinit_method (self, foo_deinit_method);
  s_base_object_set_to_string_method (self, foo_to_string_method);
  klass->some_method = foo_comp_method;
  
  return self;
}

void foo_free (Foo * self) {
  s_base_object_free (self); // Calls foo_deinit_method
}

bool foo_comp (Foo * self, char * other_str) {
  FooClass * klass = (FooClass *) s_base_object_get_class (self);
  return klass->some_method (self->priv->my_str, other_str);
}

void foo_set_str (Foo * self, char * new_str) {
  free (self->priv->my_str);
  self->priv->my_str = s_string_new (new_str);
}

    </pre>
    <hr/>
    <p>
      Now we are almost done, the only things that remain is to write the
      methods. but first we have to go through what the code does.
    </p>
    <p>
      In the constructor <code> foo_new </code> we first create a pointer to 
      an instance of the object and an instance of the object class. The class
      is then set to the object instance using the
      <code> s_base_object_set_class </code> function. It is important to set
      the objects class <b> before </b> you initize the object.
    </p>
    <p>
      After the class is set the object is initized, this adds the standard
      <code> deinit, ref, unref </code> and <code> to_string </code> methods.
    </p>
    <p>
      We then have to set our own <code> to_string </code> and <code> deinit
      </code> methods using the <code> s_base_object_set_to_string_method,
      </code> and <code> s_base_object_set_deinit_method </code>.
    </p>
    <p>
      In the start of the <code>C</code>-file we wrote some prototypes or
      declarations of these methods, now it is time to implement them.
    </p>
    <hr/>
    <pre>
void foo_deinit_method (Foo * self) {
  FooClass klass = (FooClass *) s_base_object_get_class (self);
  free(self->priv->my_str);
  free(klass);
  free(self);
}

void foo_to_string_method (Foo * self) {
  return self->priv->my_str;
}

bool foo_comp_method (char * str1, char * str2) {
  if ( strcmp ( str1, str2 ) == 0 ) {
    return true;
  } else {
    return false;
  }
}
    </pre>
    <hr/>
    <p>
      And that is it.
    </p>
    <p>
      Please note that not any of the objects in the system are thread-safe,
      and probably never will be.
    </p>
    <footer style="padding-top: 10em">
      <!-- <A name="anchor"><p>  * Note </p></A> -->
    </footer>
  </body>
</html>