/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
1
#ifndef _H_RING_BUFFER_
2
#define _H_RING_BUFFER_
3
4
#include "defs.h"
5
#include "Func.h"
6
110 by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub.
7
S_BEGIN_DECLS
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
8
9
10
/**
11
 * @file
12
 * @defgroup SRingBuffer SRingBuffer
13
 * @addtogroup SRingBuffer
14
 * @{
15
 * An SRingBuffer is a special data structure that is circular in nature.
16
 * It only holds bytes and as such is useful when dealing with strings and
17
 * other eight-bit data.
18
 *
19
 * If you are looking for a circular array, this is not
20
 * such a structure.
21
 *
22
 * @section Theory
23
 * The theory behind a circular structure is simple: Never have to deal with
24
 * empty slots in an array, only permit indirect, non-indexed access.
25
 *
26
 <pre>
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
27
    [b|b|b|b|b|b|b|b|b|b| | | | ]
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
28
     ^                 ^
29
     start             end
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
30
     front             back
31
32
33
34
    [ | | | | | |b|b|b|b| | | | ]
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
35
                 ^     ^
36
             start     end
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
37
             front     back
38
39
40
41
    [b|b| | | | | | | |b|b|b|b|b]
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
42
       ^               ^
43
     end             start
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
44
     back            front
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
45
 </pre>
46
 */
47
48
/**
49
 * The SRingBuffer is an opaque data structure that represent an array that
50
 * is circular in nature.
51
 */
52
typedef struct SRingBuffer SRingBuffer;
53
54
/**
55
 * Create a new SRingBuffer objects.
56
 *
57
 * @param len The number of items to 
58
 */
59
SRingBuffer *
60
s_ring_buffer_new (size_t len);
61
62
/**
63
 * Free an SRingBuffer.
64
 *
65
 * @param self The SRingBuffer to free.
66
 */
67
void
68
s_ring_buffer_free (SRingBuffer * self);
69
70
/**
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
71
 * determine 
72
 */
73
sboolean
74
s_ring_buffer_is_empty (SRingBuffer * self);
75
76
/**
77
 * Add data to the front of the ringbuffer.
78
 * 
79
 */
80
void
81
s_ring_buffer_push (SRingBuffer * self,
82
                    sbyte data);
83
84
/**
85
 * add an item to the back of the ringbuffer.
86
 */
87
void
88
s_ring_buffer_push_back (SRingBuffer * self,
89
                         sbyte data);
90
91
/**
92
 * Pop an item from the front of the array.
93
 */
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
94
sbyte
95
s_ring_buffer_pop (SRingBuffer * self);
96
97
/**
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
98
 * Pop the back of the array.
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
99
 */
100
sbyte
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
101
s_ring_buffer_pop_back (SRingBuffer * self);
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
102
103
/**
104
 * Reallocate an SRingBuffer to a now size for the internal array.
105
 *
106
 * @param self The SRingBuffer to reallocate.
107
 * @param len The new length.
108
 */
109
void
99 by Gustav Hartvigsson
* Working on RingBuffer and Stream.
110
s_ring_buffer_realloc (SRingBuffer * self,
111
                       size_t len);
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
112
113
/**
114
 * Get the current length of the ring buffer.
115
 */
116
size_t
117
s_ring_buffer_len (SRingBuffer * self);
118
119
/**
120
 * Get the total size of the internal array.
121
 */
122
size_t
123
s_ring_buffer_size (SRingBuffer * self);
124
125
/**
126
 * Iterate over the bytes in the SRingBuffer.
127
 *
128
 * This is the only valid way to iterate over the bytes in an SRingBuffer.
129
 *
130
 * @param self The SRingBuffer to iterate over.
131
 * @param func Pointer to a function that is run on each byte.
132
 * @param user_data Data that is provided to the function when doing the
133
 *                  iteration.
134
 *
135
 * @note
136
 * This does not use pop or push operations, and is thus the only way to keep
137
 * the data and still be able to iterate over it.
138
 *
139
 * The signature of the function provided should be as follows
140
 @code{C}
141
void
142
my_for_each_func (SRingBuffer * buffer,
143
                  sbyte item,
144
                  spointer user_data) {
145
  // code here
146
}
147
 @endcode
148
 *
149
 * To prevent warnings cast the function using the @c FOREACHFUNC() macro.
150
 */
151
void
152
s_ring_buffer_for_each (SRingBuffer * self,
153
                        ForEachFunc func,
154
                        spointer user_data);
155
156
/**
157
 * @}
158
 */
159
110 by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub.
160
S_END_DECLS
98 by Gustav Hartvigsson
* Started work on SRingBuffer to be used in SStream.
161
162
#endif /* _H_RING_BUFFER_ */