/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
126.1.1 by Gustav Hartvigsson
* Using
1
#pragma once
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
2
3
#include "defs.h"
4
#include "Func.h"
5
110 by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub.
6
S_BEGIN_DECLS
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
7
8
9
/**
10
 * @file
11
 * @defgroup SRingBuffer SRingBuffer
12
 * @addtogroup SRingBuffer
13
 * @{
14
 * An SRingBuffer is a special data structure that is circular in nature.
15
 *
139 by Gustav Hartvigsson
* Started work on the ring buffer, still untested and may contain
16
 * This is not a represent for a queue, this is only for byte sized data
17
 * elements, like text and such.
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
18
 *
19
 * @section Theory
20
 * The theory behind a circular structure is simple: Never have to deal with
21
 * empty slots in an array, only permit indirect, non-indexed access.
22
 *
23
 <pre>
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
24
    [b|b|b|b|b|b|b|b|b|b| | | | ]
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
25
     ^                 ^
26
     start             end
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
27
     front             back
28
29
30
31
    [ | | | | | |b|b|b|b| | | | ]
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
32
                 ^     ^
33
             start     end
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
34
             front     back
35
36
37
38
    [b|b| | | | | | | |b|b|b|b|b]
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
39
       ^               ^
40
     end             start
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
41
     back            front
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
42
 </pre>
139 by Gustav Hartvigsson
* Started work on the ring buffer, still untested and may contain
43
 *
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
44
 */
45
46
/**
47
 * The SRingBuffer is an opaque data structure that represent an array that
48
 * is circular in nature.
49
 */
50
typedef struct SRingBuffer SRingBuffer;
51
52
/**
53
 * Create a new SRingBuffer objects.
54
 *
139 by Gustav Hartvigsson
* Started work on the ring buffer, still untested and may contain
55
 * @param size The number of items to allocate space for.
56
 * @param free_func The function that is used to free the data.
57
 *
58
 * @note It is recomended to have a big size of the buffer to make room for
59
 *       each item that should be stored plus a bit extra. (if you expect to
60
 *       have \f$n\f$ items allocate \f$n + \frac{n}{2}\f$ slots.)
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
61
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
62
S_EXPORTED
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
63
SRingBuffer *
139 by Gustav Hartvigsson
* Started work on the ring buffer, still untested and may contain
64
s_ring_buffer_new (size_t size);
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
65
66
/**
67
 * Free an SRingBuffer.
68
 *
69
 * @param self The SRingBuffer to free.
139 by Gustav Hartvigsson
* Started work on the ring buffer, still untested and may contain
70
 * @param free_data Set to @c TRUE to free the data.
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
71
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
72
S_EXPORTED
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
73
void
74
s_ring_buffer_free (SRingBuffer * self);
75
76
/**
139 by Gustav Hartvigsson
* Started work on the ring buffer, still untested and may contain
77
 * determine if buffer is empty.
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
78
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
79
S_EXPORTED
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
80
sboolean
81
s_ring_buffer_is_empty (SRingBuffer * self);
82
83
/**
84
 * Add data to the front of the ringbuffer.
139 by Gustav Hartvigsson
* Started work on the ring buffer, still untested and may contain
85
 *
86
 * @param err Set to TRUE on error, FALSE otherwise.
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
87
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
88
S_EXPORTED
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
89
void
90
s_ring_buffer_push (SRingBuffer * self,
139 by Gustav Hartvigsson
* Started work on the ring buffer, still untested and may contain
91
                    sbyte data,
92
                    sboolean * err);
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
93
94
/**
139 by Gustav Hartvigsson
* Started work on the ring buffer, still untested and may contain
95
 * add an item to the front of the ringbuffer.
96
97
 * @param err Set to TRUE on error, FALSE otherwise.
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
98
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
99
S_EXPORTED
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
100
void
139 by Gustav Hartvigsson
* Started work on the ring buffer, still untested and may contain
101
s_ring_buffer_push_front (SRingBuffer * self,
102
                          subyte data,
103
                          sboolean * err);
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
104
105
/**
106
 * Pop an item from the front of the array.
139 by Gustav Hartvigsson
* Started work on the ring buffer, still untested and may contain
107
108
 * @param err Set to TRUE on error, FALSE otherwise.
109
 */
110
S_EXPORTED
111
spointer
112
s_ring_buffer_pop (SRingBuffer * self,
113
                   sboolean * err);
114
115
/**
116
 * Pop the front of the array.
117
118
 * @param err Set to TRUE on error, FALSE otherwise.
119
 */
120
S_EXPORTED
121
spointer
122
s_ring_buffer_pop_front (SRingBuffer * self,
123
                         sboolean * err);
124
125
/**
126
 * Peek at the byte at the end of the buffer.
127
128
 * @param err Set to TRUE on error, FALSE otherwise.
129
 */
130
S_EXPORTED
131
sbyte
132
s_ring_buffer_peek (SRingBuffer * self,
133
                    sboolean * err);
134
135
/**
136
 * Peek at the byte at the front of the buffer.
137
 *
138
 * @param err Set to TRUE on error, FALSE otherwise.
139
 */
140
S_EXPORTED
141
sbyte
142
s_ring_buffer_peek_front (SRingBuffer * self,
143
                          sboolean * err);
144
145
/**
146
 * Reallocate an SRingBuffer to a new size for the internal array.
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
147
 *
148
 * @param self The SRingBuffer to reallocate.
149
 * @param len The new length.
139 by Gustav Hartvigsson
* Started work on the ring buffer, still untested and may contain
150
 * @param err Set to TRUE on error, FALSE otherwise.
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
151
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
152
S_EXPORTED
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
153
void
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
154
s_ring_buffer_realloc (SRingBuffer * self,
139 by Gustav Hartvigsson
* Started work on the ring buffer, still untested and may contain
155
                       size_t len
156
                       sboolean * err);
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
157
158
/**
159
 * Get the current length of the ring buffer.
160
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
161
S_EXPORTED
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
162
size_t
163
s_ring_buffer_len (SRingBuffer * self);
164
165
/**
166
 * Get the total size of the internal array.
167
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
168
S_EXPORTED
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
169
size_t
170
s_ring_buffer_size (SRingBuffer * self);
171
172
/**
173
 * Iterate over the bytes in the SRingBuffer.
174
 *
175
 * This is the only valid way to iterate over the bytes in an SRingBuffer.
176
 *
177
 * @param self The SRingBuffer to iterate over.
178
 * @param func Pointer to a function that is run on each byte.
179
 * @param user_data Data that is provided to the function when doing the
180
 *                  iteration.
139 by Gustav Hartvigsson
* Started work on the ring buffer, still untested and may contain
181
 * @param err Set to TRUE on error, FALSE otherwise.
182
 *
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
183
 *
184
 * @note
185
 * This does not use pop or push operations, and is thus the only way to keep
186
 * the data and still be able to iterate over it.
187
 *
188
 * The signature of the function provided should be as follows
189
 @code{C}
190
void
191
my_for_each_func (SRingBuffer * buffer,
192
                  sbyte item,
193
                  spointer user_data) {
194
  // code here
195
}
196
 @endcode
197
 *
198
 * To prevent warnings cast the function using the @c FOREACHFUNC() macro.
199
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
200
S_EXPORTED
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
201
void
202
s_ring_buffer_for_each (SRingBuffer * self,
203
                        ForEachFunc func,
139 by Gustav Hartvigsson
* Started work on the ring buffer, still untested and may contain
204
                        spointer user_data,
205
                        sboolean * err);
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
206
207
/**
208
 * @}
209
 */
210
110 by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub.
211
S_END_DECLS