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