summaryrefslogtreecommitdiff
path: root/src/stable/ringbuffer.h
blob: d4b5993414880b580e840b0ddf4d6ccc6d9f6d14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * Copyright (C) 2007-2012 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#ifndef BU_RING_BUFFER_H
#define BU_RING_BUFFER_H

#include <memory>
#include "bu/exceptionbase.h"
#include "bu/queue.h"
#include "bu/sharedcore.h"

namespace Bu
{
	template<typename value, typename valuealloc> class RingBuffer;

	/** @cond DEVEL */
	template<typename value, typename valuealloc>
	class RingBufferCore
	{
	friend class RingBuffer<value, valuealloc>;
	friend class SharedCore<RingBuffer<value, valuealloc>,
		   RingBufferCore<value, valuealloc> >;
	private:
		RingBufferCore() :
			iCapacity( 0 ),
			iStart( -1 ),
			iEnd( -2 ),
			aData( NULL )
		{
		}

		virtual ~RingBufferCore()
		{
			clear();
		}

		void init( int iNewCapacity )
		{
			if( iCapacity > 0 )
				return;

			iCapacity = iNewCapacity;
			iStart = -1;
			iEnd = -2;
			aData = va.allocate( iCapacity );
		}

		void clear()
		{
			for( int j = iStart; j < iEnd; j=(j+1%iCapacity) )
			{
				va.destroy( &aData[j] );
			}
			va.deallocate( aData, iCapacity );
			aData = NULL;
			iCapacity = 0;
		}
		
		void enqueue( const value &v )
		{
			if( iStart == -1 )
			{
				iStart = 0;
				iEnd = 1;
				va.construct( &aData[0], v );
			}
			else if( iStart == iEnd )
			{
				throw ExceptionBase("Hey, it's full!");
			}
			else
			{
				va.construct( &aData[iEnd], v );
				iEnd = (iEnd+1)%iCapacity;
			}
		}

		value dequeue()
		{
			if( iStart == -1 )
			{
				throw ExceptionBase("No data");
			}
			else
			{
				value &v = aData[iStart];
				va.destroy( &aData[iStart] );
				iStart = (iStart+1)%iCapacity;
				if( iStart == iEnd )
				{
					iStart = -1;
					iEnd = -2;
				}
				return v;
			}
		}

		value &get( int iIndex )
		{
			return aData[(iIndex+iStart)%iCapacity];
		}

		int getSize()
		{
			if( iStart < 0 )
				return 0;
			if( iEnd == iStart )
				return iCapacity;
			if( iEnd < iStart )
				return iEnd-iStart;
			return iCapacity-(iEnd-iStart);
		}

		int iCapacity;
		int iStart, iEnd;
		value *aData;
		valuealloc va;
	};
	/** @endcond */

	/**
	 *@ingroup Containers
	 */
	template<typename value, typename valuealloc=std::allocator<value> >
	class RingBuffer : public Queue<value>, public SharedCore<
					   RingBuffer<value, valuealloc>,
					   RingBufferCore<value, valuealloc>
					   >
	{
	private:
		typedef RingBuffer<value, valuealloc> MyType;
		typedef RingBufferCore<value, valuealloc> Core;

	protected:
		using SharedCore<MyType, Core>::core;
		using SharedCore<MyType, Core>::_hardCopy;
		using SharedCore<MyType, Core>::_allocateCore;

	public:
		RingBuffer( int iCapacity )
		{
			core->init( iCapacity );
		}

		RingBuffer( const RingBuffer &rSrc ) :
			SharedCore<MyType, Core>( rSrc )
		{
		}

		virtual ~RingBuffer()
		{
		}

		int getCapacity() const
		{
			return core->iCapacity;
		}

		bool isFilled() const
		{
			return (core->iStart == core->iEnd);
		}

		bool isEmpty() const
		{
			return (core->iStart == -1);
		}

		virtual void enqueue( const value &v )
		{
			_hardCopy();

			core->enqueue( v );
		}

		virtual value dequeue()
		{
			_hardCopy();

			return core->dequeue();
		}

		virtual int getSize() const
		{
			return core->getSize();
		}

		virtual value &peek()
		{
			_hardCopy();

			return core->get( 0 );
		}

		virtual const value &peek() const
		{
			return core->get( 0 );
		}

		value &operator[]( int iIndex )
		{
			_hardCopy();

			return core->get( iIndex );
		}

	protected:
		virtual Core *_copyCore( Core *src )
		{
			Core *pRet = _allocateCore();

			pRet->init( src->iCapacity );
			int iSize = src->getSize();
			for( int j = 0; j < iSize; j++ )
			{
				pRet->enqueue( src->get( j ) );
			}

			return pRet;
		}
	};
}

#endif