/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
1
#ifndef __GAME_MATRIX__
2
#define __GAME_MATRIX__
3
4
#include <string.h>
5
#include <stdlib.h>
6
#include <stdbool.h>
7
#include <stdio.h>
8
9
10
#include "defs.h"
11
#include "DynamicArray.h"
12
13
/**
62 by Gustav Hartvigsson
* General documentation clean up.
14
 * @file
15
 * @defgroup SMatrix SMatrix
16
 * @addtogroup SMatrix
17
 * @{
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
18
 * A SMatrix is a 2d, rectangular array.
19
 */
71 by Gustav Hartvigsson
* Implemented s_matrix_set and s_matrix_append
20
110 by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub.
21
S_BEGIN_DECLS
71 by Gustav Hartvigsson
* Implemented s_matrix_set and s_matrix_append
22
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
23
/**
24
 * An SMatrix is an opaque data structure representing a simple rectangular
25
 * matrix.
26
 *
27
 * An SMatrix Stores pointers to data, and not the data itself.
28
 * So when using functions like s_matrix_realloc, it only copies the pointers
29
 * over from the old SMatrix to the new.
30
 */
31
typedef struct SMatrix SMatrix;
32
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
33
#define S_MATRIX(k) ((SMatrix *) (k))
34
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
35
/**
36
 * convenience structure to store type into and name of a tuple.
37
 * Example:
38
 @code{.c}
39
typedef enum {
40
  MY_TUPLE_ENUM_NAME,
41
  MY_TUPLE_ENUM_AGE,
42
  MY_TUPLE_ENUM_HEIGHT,
43
  MY_TUPLE_ENUM_END_OF_TUPLE
44
} MyRowInfoEnum;
45
46
const SMatrixRowInformation my_row_info[] = {
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
47
  {"Name", S_TYPE_STRING, NULL, NULL, NULL},
48
  {"Age", S_TYPE_UINT, NULL, NULL, NULL},
49
  {"Height", S_TYPE_UINT, NULL, NULL, NULL}
50
  {NULL, NULL, NULL, NULL, NULL}
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
51
};
52
 @endcode
53
 * The information can then be used like this:
54
 @code{.c}
55
for (int i = 0, i <= s_matrix_get_last_tuple_n (my_matrix), i++) {
56
  spointer tmp_tuple = s_matrix_get_tuple (my_matrix, i);
57
  for (int j = 0, j <= MY_TUPLE_ENUM_END_OF_TUPLE, j++) {
58
    s_print ("%s: %s", my_row_info[j], tmp_tuple[j]);
59
  }
60
  s_print ("\n");
61
  free (tmp_tuple);
62
}
63
 @endcode
64
 *
65
 */
66
typedef struct SMatrixRowInformation {
100 by Gustav Hartvigsson
* Fixed README.
67
  schar *       name; /**< Name of the row. */
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
68
  FreeFunc      free_func; /**< Free function to be used with complex objects.*/
69
  FuncPointer   to_json; /**< Used to get some sort of JSON information out of
70
                              a complex object. (Can be NULL). */
71
  FuncPointer   from_json; /**< When constructing an object of this type from
72
                            * some JSON representation of the complex object.
100 by Gustav Hartvigsson
* Fixed README.
73
                            * This functons should be used. (Can be NULL)*/
81 by Gustav Hartvigsson
* Re arranged members of structs to prevent struct fragmentation and padding.
74
  SType         type; /**< Type of the row. */
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
75
} SMatrixRowInformation;
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
76
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
77
/**
100 by Gustav Hartvigsson
* Fixed README.
78
 * Create a new SMatrix.
79
 *
80
 * @param width The width of the matrix. This can be seen as the number of
81
 *              columns each tuple has.
82
 * @param height The initial height of the matrix. The initial number of rows
83
 *               the matrix has.
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
84
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
85
S_EXPORTED
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
86
SMatrix *
100 by Gustav Hartvigsson
* Fixed README.
87
s_matrix_new (size_t width,
88
              size_t height,
89
              SMatrixRowInformation * row_information);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
90
100 by Gustav Hartvigsson
* Fixed README.
91
/**
92
 * Free a SMatrix.
93
 *
94
 * @param self The SMatrix to free.
95
 * @param free_data Should the data also be freed with the data structure?
96
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
97
S_EXPORTED
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
98
void
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
99
s_matrix_free (SMatrix * self, sboolean free_data);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
100
101
/**
102
 * Reallocate a SMatrix from one tuple width to an other.
103
 *
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
104
 * @warning The new tuple size must be larger then the last, or it will be
105
 * turnicated, but not freed.
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
106
 *
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
107
 * @note You need to free the old SMatrix yourself
108
 * <em>(but not the data stored)</em>, this is to avoid memory leaks.
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
109
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
110
S_EXPORTED
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
111
SMatrix *
100 by Gustav Hartvigsson
* Fixed README.
112
s_matrix_realloc (SMatrix * self,
113
                  size_t width,
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
114
                  SMatrixRowInformation * new_row_information);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
115
116
/**
117
 * Get element y in tuple x.
118
 *
62 by Gustav Hartvigsson
* General documentation clean up.
119
 * Equivalent to matrix[x][y] would be in static 2d arrays.
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
120
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
121
S_EXPORTED
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
122
spointer
100 by Gustav Hartvigsson
* Fixed README.
123
s_matrix_get (SMatrix * self,
124
              size_t x,
125
              size_t y);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
126
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
127
/**
128
 * Returns the number representing the last tuple.
129
 * IE: it the last tuple has the x position of 10, this will return 10.
130
 *
131
 * @param self The SMatrix to get the x coordinate of the last tuple.
132
 *
133
 * @return The x coordinate of the last tuple.
134
 *
135
 * @note The value way be wrong.
136
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
137
S_EXPORTED
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
138
size_t
139
s_matrix_get_last_tuple_n (SMatrix self);
140
141
/**
142
 * @brief Set data at a point [x,y] in the array.
143
 *
144
 * @param self The SMatrix to operate on.
145
 * @param x The x coordinates of where to put the data.
146
 *          (What tuple to put it in).
147
 * @param y The y coordinates to put the data in.
148
 *          (What position in the tuple to put it in).
149
 * @param data The data to be placed in the SMatrix.
150
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
151
S_EXPORTED
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
152
void
100 by Gustav Hartvigsson
* Fixed README.
153
s_matrix_set (SMatrix * self,
154
              size_t x,
155
              size_t y,
156
              spointer data);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
157
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
158
/**
159
 * Gets tuple x in SMatrix self.
160
 * 
161
 * @param self the matrix to perform the operation on.
162
 * @param x the tuple to get.
163
 *
164
 * @return a pointer to an array congaing painters to the data in the tuple.
165
 *
166
 * @see s_matrix_get_tuple_as_dynamic_array()
167
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
168
S_EXPORTED
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
169
spointer *
100 by Gustav Hartvigsson
* Fixed README.
170
s_matrix_get_tuple (SMatrix * self,
171
                    size_t x);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
172
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
173
/**
174
 * Append a tuple to a SMatrix.
175
 *
176
 * @param self The SMatrix to perform the operation on.
177
 * @param tuple The <em>allocated</em> data tuple to be copied into the SMatrix.
178
 *
179
 * @note This function will consume (free) allocated data for the tuple.
180
 *
181
 * @note The function assumes that the tuple has the same width as specified
182
 *       during construction or reallocation. Any dangling pointers will
183
 *       <em>not</em> be freed.
184
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
185
S_EXPORTED
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
186
void
100 by Gustav Hartvigsson
* Fixed README.
187
s_matrix_append (SMatrix * self,
188
                 spointer * tuple);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
189
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
190
/**
191
 * Get tuple x as a SDynamicArray.
192
 *
193
 * @param self the SMatrix to perform the operation on.
194
 * @param x the tuple to get.
195
 *
196
 * @return An SDynamicArray containing pointers to the data in the tuple.
197
 *
198
 * @see s_matrix_get_tuple
199
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
200
S_EXPORTED
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
201
SDynamicArray *
100 by Gustav Hartvigsson
* Fixed README.
202
s_matrix_get_tuple_as_dynamic_array (SMatrix * self,
203
                                     size_t x);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
204
205
/**
62 by Gustav Hartvigsson
* General documentation clean up.
206
 * This iterates over each tuple. giving the tuple as the item in the
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
207
 * callback function.
208
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
209
S_EXPORTED
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
210
void
100 by Gustav Hartvigsson
* Fixed README.
211
s_matrix_for_each (SMatrix * self,
212
                  ForEachFunc callback,
213
                  spointer data);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
214
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
215
216
/**
217
 * @TODO
218
 * @warning NOT IMPLIED
219
 *
220
 * Get the matrix as JSON.
221
 * @param self The SMatrix to get the JSON from.
222
 *
223
 * @return a null-terminated JSON string representing the matrix.
224
 *
225
 * @note This only works on primative types. complex types (IE: structs and
226
 * such) can not be gotten any information from.
227
 *
228
 *The outputted JSON will have the format:
229
 @code{.js}
230
{
231
  { // Tuple
232
    { // Item
233
      name: "Name"
234
      type: "STRING",
235
      data: "John Smith"
236
    },
237
    { // Item
238
      name: "Age",
239
      type: "UINT",
240
      data: 32,
241
    },
242
    { // Item
243
      name: "Height",
244
      type: "UINT",
245
      data: 189
246
    }
247
  },
248
  { //tuple
249
    // ...
250
  }
251
}
252
 @endcode
253
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
254
S_EXPORTED
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
255
char *
256
s_matrix_serialize_json (SMatrix * self);
257
258
/**
259
 * @TODO
260
 * @warning NOT IMPLIED
261
 *
262
 * Deselialize JSON into an SMatrix.
263
 *
264
 * @param self The SMatrix to write to.
265
 * @param data the JSON data to be deselialised.
266
 *
267
 * @note The SMatrix must be empty, but initialised.
268
 *
269
 * @note As with s_matrix_serialize_json(), only primative types work. Complex
270
 * types will not work.
271
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
272
S_EXPORTED
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
273
void
100 by Gustav Hartvigsson
* Fixed README.
274
s_matrix_deserialize_json (SMatrix * self,
275
                           const schar * data);
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
276
62 by Gustav Hartvigsson
* General documentation clean up.
277
/** @} */
278
110 by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub.
279
S_END_DECLS
280
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
281
#endif /*__GAME_MATRIX__ */