/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
 */
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
85
SMatrix *
100 by Gustav Hartvigsson
* Fixed README.
86
s_matrix_new (size_t width,
87
              size_t height,
88
              SMatrixRowInformation * row_information);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
89
100 by Gustav Hartvigsson
* Fixed README.
90
/**
91
 * Free a SMatrix.
92
 *
93
 * @param self The SMatrix to free.
94
 * @param free_data Should the data also be freed with the data structure?
95
 */
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
96
void
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
97
s_matrix_free (SMatrix * self, sboolean free_data);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
98
99
/**
100
 * Reallocate a SMatrix from one tuple width to an other.
101
 *
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
102
 * @warning The new tuple size must be larger then the last, or it will be
103
 * turnicated, but not freed.
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
104
 *
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
105
 * @note You need to free the old SMatrix yourself
106
 * <em>(but not the data stored)</em>, this is to avoid memory leaks.
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
107
 */
108
SMatrix *
100 by Gustav Hartvigsson
* Fixed README.
109
s_matrix_realloc (SMatrix * self,
110
                  size_t width,
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
111
                  SMatrixRowInformation * new_row_information);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
112
113
/**
114
 * Get element y in tuple x.
115
 *
62 by Gustav Hartvigsson
* General documentation clean up.
116
 * Equivalent to matrix[x][y] would be in static 2d arrays.
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
117
 */
118
spointer
100 by Gustav Hartvigsson
* Fixed README.
119
s_matrix_get (SMatrix * self,
120
              size_t x,
121
              size_t y);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
122
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
123
/**
124
 * Returns the number representing the last tuple.
125
 * IE: it the last tuple has the x position of 10, this will return 10.
126
 *
127
 * @param self The SMatrix to get the x coordinate of the last tuple.
128
 *
129
 * @return The x coordinate of the last tuple.
130
 *
131
 * @note The value way be wrong.
132
 */
133
size_t
134
s_matrix_get_last_tuple_n (SMatrix self);
135
136
/**
137
 * @brief Set data at a point [x,y] in the array.
138
 *
139
 * @param self The SMatrix to operate on.
140
 * @param x The x coordinates of where to put the data.
141
 *          (What tuple to put it in).
142
 * @param y The y coordinates to put the data in.
143
 *          (What position in the tuple to put it in).
144
 * @param data The data to be placed in the SMatrix.
145
 */
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
146
void
100 by Gustav Hartvigsson
* Fixed README.
147
s_matrix_set (SMatrix * self,
148
              size_t x,
149
              size_t y,
150
              spointer data);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
151
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
152
/**
153
 * Gets tuple x in SMatrix self.
154
 * 
155
 * @param self the matrix to perform the operation on.
156
 * @param x the tuple to get.
157
 *
158
 * @return a pointer to an array congaing painters to the data in the tuple.
159
 *
160
 * @see s_matrix_get_tuple_as_dynamic_array()
161
 */
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
162
spointer *
100 by Gustav Hartvigsson
* Fixed README.
163
s_matrix_get_tuple (SMatrix * self,
164
                    size_t x);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
165
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
166
/**
167
 * Append a tuple to a SMatrix.
168
 *
169
 * @param self The SMatrix to perform the operation on.
170
 * @param tuple The <em>allocated</em> data tuple to be copied into the SMatrix.
171
 *
172
 * @note This function will consume (free) allocated data for the tuple.
173
 *
174
 * @note The function assumes that the tuple has the same width as specified
175
 *       during construction or reallocation. Any dangling pointers will
176
 *       <em>not</em> be freed.
177
 */
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
178
void
100 by Gustav Hartvigsson
* Fixed README.
179
s_matrix_append (SMatrix * self,
180
                 spointer * tuple);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
181
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
182
/**
183
 * Get tuple x as a SDynamicArray.
184
 *
185
 * @param self the SMatrix to perform the operation on.
186
 * @param x the tuple to get.
187
 *
188
 * @return An SDynamicArray containing pointers to the data in the tuple.
189
 *
190
 * @see s_matrix_get_tuple
191
 */
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
192
SDynamicArray *
100 by Gustav Hartvigsson
* Fixed README.
193
s_matrix_get_tuple_as_dynamic_array (SMatrix * self,
194
                                     size_t x);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
195
196
/**
62 by Gustav Hartvigsson
* General documentation clean up.
197
 * This iterates over each tuple. giving the tuple as the item in the
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
198
 * callback function.
199
 */
200
void
100 by Gustav Hartvigsson
* Fixed README.
201
s_matrix_for_each (SMatrix * self,
202
                  ForEachFunc callback,
203
                  spointer data);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
204
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
205
206
/**
207
 * @TODO
208
 * @warning NOT IMPLIED
209
 *
210
 * Get the matrix as JSON.
211
 * @param self The SMatrix to get the JSON from.
212
 *
213
 * @return a null-terminated JSON string representing the matrix.
214
 *
215
 * @note This only works on primative types. complex types (IE: structs and
216
 * such) can not be gotten any information from.
217
 *
218
 *The outputted JSON will have the format:
219
 @code{.js}
220
{
221
  { // Tuple
222
    { // Item
223
      name: "Name"
224
      type: "STRING",
225
      data: "John Smith"
226
    },
227
    { // Item
228
      name: "Age",
229
      type: "UINT",
230
      data: 32,
231
    },
232
    { // Item
233
      name: "Height",
234
      type: "UINT",
235
      data: 189
236
    }
237
  },
238
  { //tuple
239
    // ...
240
  }
241
}
242
 @endcode
243
 */
244
char *
245
s_matrix_serialize_json (SMatrix * self);
246
247
/**
248
 * @TODO
249
 * @warning NOT IMPLIED
250
 *
251
 * Deselialize JSON into an SMatrix.
252
 *
253
 * @param self The SMatrix to write to.
254
 * @param data the JSON data to be deselialised.
255
 *
256
 * @note The SMatrix must be empty, but initialised.
257
 *
258
 * @note As with s_matrix_serialize_json(), only primative types work. Complex
259
 * types will not work.
260
 */
261
void
100 by Gustav Hartvigsson
* Fixed README.
262
s_matrix_deserialize_json (SMatrix * self,
263
                           const schar * data);
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
264
62 by Gustav Hartvigsson
* General documentation clean up.
265
/** @} */
266
110 by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub.
267
S_END_DECLS
268
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
269
#endif /*__GAME_MATRIX__ */