EpcTools
An event based multi-threaded C++ development framework.
ecbuf.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2009-2019 Brian Waters
3 * Copyright (c) 2019 Sprint
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 
18 #ifndef __ecbuf_h_included
19 #define __ecbuf_h_included
20 
23 
24 #include "ebase.h"
25 #include "esynch.h"
26 #include "eerror.h"
27 
30 
32 DECLARE_ERROR(ECircularBufferError_HeadAndTailOutOfSync);
33 DECLARE_ERROR(ECircularBufferError_UsedLessThanZero);
34 DECLARE_ERROR(ECircularBufferError_TailExceededCapacity);
35 DECLARE_ERROR(ECircularBufferError_AttemptToExceedCapacity);
36 DECLARE_ERROR(ECircularBufferError_BufferSizeHasBeenExceeded);
37 DECLARE_ERROR(ECircularBufferError_HeadHasExceededCapacity);
38 DECLARE_ERROR(ECircularBufferError_AttemptToModifyDataOutsideBoundsOfCurrentBuffer);
40 
43 
46 {
47 public:
53 
55  Void initialize();
56 
58  Bool isEmpty() { return m_used == 0; }
60  Int capacity() { return m_capacity; }
62  Int used() { return m_used; }
64  Int free() { return m_capacity - m_used; }
65 
73  Int peekData(pUChar dest, Int offset, Int length)
74  {
75  return readData(dest, offset, length, true);
76  }
77 
82  Int readData(pUChar dest, Int offset, Int length)
83  {
84  return readData(dest, offset, length, false);
85  }
86 
96  void writeData(pUChar src, Int offset, Int length, Bool nolock=False);
110  void modifyData(pUChar src, Int offset, Int length, Bool nolock=False);
111 
114  EMutexPrivate &getMutex() { return m_mutex; }
115 
116 private:
117  Int readData(pUChar dest, Int offset, Int length, Bool peek);
118 
119  ECircularBuffer();
120 
121  pUChar m_data;
122  Int m_capacity;
123  Int m_head;
124  Int m_tail;
125  Int m_used;
126 
127  EMutexPrivate m_mutex;
128 };
129 
130 #endif // #define __ecbuf_h_included
ECircularBuffer::~ECircularBuffer
~ECircularBuffer()
Class destructor.
Definition: ecbuf.cpp:28
DECLARE_ERROR
#define DECLARE_ERROR(__e__)
Declares exception class derived from EError with no constructor parameters.
Definition: eerror.h:53
eerror.h
Defines base class for exceptions and declaration helper macros.
ECircularBuffer::writeData
void writeData(pUChar src, Int offset, Int length, Bool nolock=False)
Writes data to the circular buffer.
Definition: ecbuf.cpp:102
ECircularBuffer::getMutex
EMutexPrivate & getMutex()
Retrieves the mutex;.
Definition: ecbuf.h:114
ebase.h
Macros for various standard C library functions and standard includes.
EMutexPrivate
A private mutex (the mutex data is allocated from either the heap or stack).
Definition: esynch.h:175
ECircularBuffer::isEmpty
Bool isEmpty()
True - the buffer is empty, False - there is data in the buffer.
Definition: ecbuf.h:58
esynch.h
Contains definitions for synchronization objects.
ECircularBuffer::initialize
Void initialize()
Initializes the circular buffer.
Definition: ecbuf.cpp:37
ECircularBuffer::free
Int free()
Returns the number of bytes that are free in the buffer.
Definition: ecbuf.h:64
False
#define False
False.
Definition: ebase.h:27
ECircularBuffer::used
Int used()
Returns the number of bytes in use in the buffer.
Definition: ecbuf.h:62
ECircularBuffer
Implements a circular buffer.
Definition: ecbuf.h:45
ECircularBuffer::capacity
Int capacity()
Returns the maximum capacity of the circular buffer.
Definition: ecbuf.h:60
ECircularBuffer::modifyData
void modifyData(pUChar src, Int offset, Int length, Bool nolock=False)
Modifies data within the buffer.
Definition: ecbuf.cpp:135
ECircularBuffer::readData
Int readData(pUChar dest, Int offset, Int length)
Reads and removes data from the buffer.
Definition: ecbuf.h:82
ECircularBuffer::peekData
Int peekData(pUChar dest, Int offset, Int length)
Reads data from the buffer without removing it from the buffer.
Definition: ecbuf.h:73