/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
5.2.9 by Gustav Hartvigsson
* Added license to files
1
/*
2
Copyright (c) 2013-2014 Gustav Hartvigsson
3
4
Permission is hereby granted, free of charge, to any person obtaining a copy
5
of this software and associated documentation files (the "Software"), to deal
6
in the Software without restriction, including without limitation the rights
7
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
copies of the Software, and to permit persons to whom the Software is
9
furnished to do so, subject to the following conditions:
10
11
The above copyright notice and this permission notice shall be included in
12
all copies or substantial portions of the Software.
13
14
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
THE SOFTWARE.
21
*/
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
22
23
#ifndef __H_DYNAMIC_ARRAY__
24
#define __H_DYNAMIC_ARRAY__
25
#include <stdlib.h>
26
#include "defs.h"
30 by Gustav Hartvigsson
* Made the code compile using CMake.
27
#include "Func.h"
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
28
5.2.9 by Gustav Hartvigsson
* Added license to files
29
BEGIN_DECLS
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
30
31
/** @file
46 by Gustav Hartvigsson
* Renamed DynamicArray to SDynamicArray.
32
 * @defgroup SDynamicArray SDynamicArray
33
 * @addtogroup SDynamicArray SDynamicArray
34
 * @{
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
35
 *
46 by Gustav Hartvigsson
* Renamed DynamicArray to SDynamicArray.
36
 * SDynamicArray is an imlpementation of a dynamic array, it is usefule when
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
37
 * dealing with lare amounts of data that may change over time, or with an
38
 * unknowned numebr of items.
39
 * 
40
 * Note that accsess time is constant, but write time is not guarenteed to be.
41
 * 
42
 * When the size of the array is equal to the number of elements in it, it will
43
 * re-allocate the array with a larger size.
44
 */
45
46
/**
47
 * The padding that is added when expanding the array.
48
 */
49
#define ARRAY_PADDING 8
50
51
/**
46 by Gustav Hartvigsson
* Renamed DynamicArray to SDynamicArray.
52
 * An SDynamicArray is the standard implementation of a Dynamic Array in SSTS.
53
 *
54
 * It does not depend an SObject, because it should be albe to be used in,
55
 * SObject and SObject based classes.
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
56
 */
46 by Gustav Hartvigsson
* Renamed DynamicArray to SDynamicArray.
57
typedef struct SDynamicArray SDynamicArray;
58
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
59
60
#define S_DYNAMIC_ARRAY(k) ((SDynamicArray *)(k))
53 by Gustav Hartvigsson
* Finnished up s_map_add () ???
61
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
62
/**
63
 * Create a new dynamic array.
64
 *
65
 * @param len The length of the initial array.
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
66
 * @param free_func The function to be used when freeing the items. Can be NULL.
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
67
 *        If free_func is NULL, it will use the standard library's 
68
 *       <tt>free()</tt>.
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
69
 *
70
 * The free function should have the the signature <tt> (DynamicArray * self,
71
 * spointer item, spointer data) </tt> should be used but cast to FreeFunc.
72
 */
73
SDynamicArray *
74
s_dynamic_array_new (size_t len,
75
                     FreeFunc free_func);
76
77
/**
78
 * Same as s_dynamic_array_new() but with support for to_json and from_json
79
 * methods.
80
 *
81
 * @param to_json This function is used, on an item basis, to serialise the data
82
 *        in that item into a json representation.
83
 *
84
 * @param from_json This function talkes a string and marchal it into an object.
85
 *
86
 * The to_json function should have the signature:
87
 * <tt>char * name (spointer item)</tt> and return a string.
88
 * If this is not set, it will return <tt>"(pointer)"</tt>.
89
 *
90
 * The from_json function should have the signature:
91
 * <tt>spointer name (char * json)</tt>.
92
 * If this function is not set, the s_matrix_deserialize_json() function will
93
 * not work.
94
 */
95
SDynamicArray *
96
s_dynamic_array_new_json (size_t len,
97
                          FreeFunc free_func,
98
                          FuncPointer to_json,
99
                          FuncPointer from_json);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
100
101
/**
102
 * Frees the dynamic array.
103
 * 
104
 * after this is run the data will be lost.
105
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
106
void
107
s_dynamic_array_free (SDynamicArray * self, sboolean free_data);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
108
109
110
/**
111
 * Get an item from the array.
112
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
113
spointer
114
s_dynamic_array_get (SDynamicArray * self, size_t index);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
115
116
/**
47 by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h
117
 *
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
118
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
119
void
120
s_dynamic_array_set (SDynamicArray * self, size_t index, spointer item);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
121
122
/**
123
 * Get the size of the array, this is not the same as the length of the array.
124
 * The size is the number of elements that can be allocated without resizing
125
 * the array.
126
 * 
46 by Gustav Hartvigsson
* Renamed DynamicArray to SDynamicArray.
127
 * To get the length of the array use s_dynamic_array_len ().
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
128
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
129
size_t
130
s_dynamic_array_size (SDynamicArray * self);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
131
132
/**
47 by Gustav Hartvigsson
* Added a few skeletal functions to Callback.h
133
 * Get the index of the last item in the array.
134
 *
135
 * @note
136
 * This is not the last item added to the array, necessary, it is the index
69 by Gustav Hartvigsson
* Finished of SMap... Sort of...
137
 * of the item that has the highest position in the array.
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
138
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
139
size_t
140
s_dynamic_array_last_item (SDynamicArray * self);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
141
142
/**
143
 * Dumps a copy of the array. Must be cast.
144
 * 
145
 * Is not freed when the dynamic array is freed.
146
 * 
147
 * Is null-terminated.
148
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
149
spointer *
150
s_dynamic_array_dump_array (SDynamicArray * self);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
151
152
/**
153
 * Use a function on the array.
154
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
155
void
156
s_dynamic_array_for_each (SDynamicArray * self, ForEachFunc func,
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
157
                          spointer data);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
158
70 by Gustav Hartvigsson
* removed s_dynamic_array_foreach_with_return.
159
#if 0
160
This function is removed, it would have been a pain in the arse to implement.
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
161
/** TODO
46 by Gustav Hartvigsson
* Renamed DynamicArray to SDynamicArray.
162
 * same as s_dynamic_array_for_each (), with the difference that it returns a new
163
 * SDynamicArray.
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
164
 */
61 by Gustav Hartvigsson
* Made the code more easy to read.
165
SDynamicArray *
166
s_dynamic_array_for_each_with_return (SDynamicArray * self,
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
167
                                      ForEachFunc func,
168
                                      spointer data);
70 by Gustav Hartvigsson
* removed s_dynamic_array_foreach_with_return.
169
#endif
68 by Gustav Hartvigsson
* Hid internals of SDynamicArray.
170
171
/**
172
 * Returns a JSON representation of the array.
173
 *
174
 * If the to_json function is not set, this will return <tt>(pointer)</tt> for
175
 * each object.
176
 *
177
 * @param self The SDynamicArray to get the JSON representation from.
178
 *
179
 * @return a string with the JSON representation of the SDynamicArray.
180
 */
181
char *
182
s_dynamic_array_serialize_json (SDynamicArray * self);
183
184
/**
185
 * appends itmes to an the array 
186
 *
187
 * If the from_json function is not set, this will couse undefined behaviour.
188
 */
189
void
190
s_dynamic_array_deserialize_json (SDynamicArray * self, char * json);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
191
46 by Gustav Hartvigsson
* Renamed DynamicArray to SDynamicArray.
192
/** @} */
193
5.2.9 by Gustav Hartvigsson
* Added license to files
194
END_DECLS
195
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
196
#endif /* #define __H_DYNAMIC_ARRAY__ */