/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 docs/inheritance.html

  • Committer: Gustav Hartvigsson
  • Date: 2015-10-05 12:21:17 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20151005122117-x3t9p6n2qwvop7an
* Removed outdated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
    <hr/>
72
 
    <pre>
73
 
#include "Foo.h"
74
 
 
75
 
struct _FooPrivate {
76
 
  char * my_str; //The private data.
77
 
};
78
 
 
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
82
 
 
83
 
//The constructor
84
 
Foo * foo_new (char * my_str) {
85
 
  Foo * self = malloc ( sizeof (Foo));
86
 
  FooClass * klass = malloc( sizeof (FooClass));
87
 
  
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;
93
 
  
94
 
  return self;
95
 
}
96
 
 
97
 
void foo_free (Foo * self) {
98
 
  s_base_object_free (self); // Calls foo_deinit_method
99
 
}
100
 
 
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);
104
 
}
105
 
 
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);
109
 
}
110
 
 
111
 
    </pre>
112
 
    <hr/>
113
 
    <p>
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.
116
 
    </p>
117
 
    <p>
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.
123
 
    </p>
124
 
    <p>
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.
127
 
    </p>
128
 
    <p>
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>.
132
 
    </p>
133
 
    <p>
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.
136
 
    </p>
137
 
    <hr/>
138
 
    <pre>
139
 
void foo_deinit_method (Foo * self) {
140
 
  FooClass klass = (FooClass *) s_base_object_get_class (self);
141
 
  free(self->priv->my_str);
142
 
  free(klass);
143
 
  free(self);
144
 
}
145
 
 
146
 
void foo_to_string_method (Foo * self) {
147
 
  return self->priv->my_str;
148
 
}
149
 
 
150
 
bool foo_comp_method (char * str1, char * str2) {
151
 
  if ( strcmp ( str1, str2 ) == 0 ) {
152
 
    return true;
153
 
  } else {
154
 
    return false;
155
 
  }
156
 
}
157
 
    </pre>
158
 
    <hr/>
159
 
    <p>
160
 
      And that is it.
161
 
    </p>
162
 
    <p>
163
 
      Please note that not any of the objects in the system are thread-safe,
164
 
      and probably never will be.
165
 
    </p>
166
 
    <footer style="padding-top: 10em">
167
 
      <!-- <A name="anchor"><p>  * Note </p></A> -->
168
 
    </footer>
169
 
  </body>
170
 
</html>