/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk

« back to all changes in this revision

Viewing changes to src/DynamicArray.h

  • Committer: Gustav Hartvigsson
  • Date: 2015-08-31 14:19:49 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20150831141949-pn1p83pgcbzbfm8x
* Hid internals of SDynamicArray.
* Added s_dynamic_array_serialize_json() and s_dynamic_array_deserialize_json()
* Added s_dynamic_array_new_json()
* Added _s_dynamic_array_new_full() internal function.

* Changed SOobject's base to_string method.
* Changed testcase for to_string method.

Show diffs side-by-side

added added

removed removed

Lines of Context:
56
56
 */
57
57
typedef struct SDynamicArray SDynamicArray;
58
58
 
59
 
typedef struct SDynamicArrayPrivate SDynamicArrayPrivate;
60
 
 
61
 
struct
62
 
SDynamicArray {
63
 
  SDynamicArrayPrivate * priv;
64
 
  size_t max_size;
65
 
  size_t last_item;
66
 
};
67
 
 
68
 
#define S_DYNAMIC_ARRAY(k) (SDynamicArray *)(k)
 
59
 
 
60
#define S_DYNAMIC_ARRAY(k) ((SDynamicArray *)(k))
69
61
 
70
62
/**
71
63
 * Create a new dynamic array.
72
64
 *
73
65
 * @param len The length of the initial array.
74
 
 * @param free_func The function to be used when freeing the items. Can be \c NULL.
 
66
 * @param free_func The function to be used when freeing the items. Can be NULL.
75
67
 *        If free_func is NULL, it will use the standard library's 
76
68
 *       <tt>free()</tt>.
77
 
 *        
78
 
 *        Normally a function with the signature <tt> (DynamicArray * self,
79
 
          spointer item, spointer data) </tt> should be used but cast to FreeFunc.
80
 
 */
81
 
SDynamicArray *
82
 
s_dynamic_array_new (size_t len, FreeFunc free_func);
 
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);
83
100
 
84
101
/**
85
102
 * Frees the dynamic array.
137
154
 */
138
155
void
139
156
s_dynamic_array_for_each (SDynamicArray * self, ForEachFunc func,
140
 
                             spointer data);
 
157
                          spointer data);
141
158
 
142
159
/** TODO
143
160
 * same as s_dynamic_array_for_each (), with the difference that it returns a new
145
162
 */
146
163
SDynamicArray *
147
164
s_dynamic_array_for_each_with_return (SDynamicArray * self,
148
 
                                                   ForEachFunc func,
149
 
                                                   spointer data);
 
165
                                      ForEachFunc func,
 
166
                                      spointer data);
 
167
 
 
168
/**
 
169
 * Returns a JSON representation of the array.
 
170
 *
 
171
 * If the to_json function is not set, this will return <tt>(pointer)</tt> for
 
172
 * each object.
 
173
 *
 
174
 * @param self The SDynamicArray to get the JSON representation from.
 
175
 *
 
176
 * @return a string with the JSON representation of the SDynamicArray.
 
177
 */
 
178
char *
 
179
s_dynamic_array_serialize_json (SDynamicArray * self);
 
180
 
 
181
/**
 
182
 * appends itmes to an the array 
 
183
 *
 
184
 * If the from_json function is not set, this will couse undefined behaviour.
 
185
 */
 
186
void
 
187
s_dynamic_array_deserialize_json (SDynamicArray * self, char * json);
150
188
 
151
189
/** @} */
152
190