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