WSF
UtBuffer.hpp
Go to the documentation of this file.
1// ****************************************************************************
2// CUI
3//
4// The Advanced Framework for Simulation, Integration, and Modeling (AFSIM)
5//
6// Copyright 2003-2015 The Boeing Company. All rights reserved.
7//
8// The use, dissemination or disclosure of data in this file is subject to
9// limitation or restriction. See accompanying README and LICENSE for details.
10// ****************************************************************************
11// ****************************************************************************
12// Updated by Infoscitex, a DCS Company.
13// ****************************************************************************
14
15#ifndef UTBUFFER_HPP
16#define UTBUFFER_HPP
17
18#include <algorithm>
19#include <cstring>
20
21#include "WsfUtExport.hpp"
22
28{
29public:
31 UtBuffer(int aBytes)
32 : mBuffer(nullptr)
33 , mCanGrow(false)
34 {
36 SetBuffer(new char[aBytes], aBytes, true);
37 }
38
40 UtBuffer(char* aData, int aBytes, bool aCanGrow = false)
41 : mBuffer(nullptr)
42 , mCanGrow(false)
43 {
45 SetBuffer(aData, aBytes, aCanGrow);
46 }
47
48 static const int cDEFAULT_BUFFER_SIZE = 1024;
49
51 UtBuffer(const UtBuffer& aSrc)
52 : mBuffer(new char[aSrc.mBytes])
53 , mBytes(aSrc.mBytes)
54 , mGetPos(aSrc.mGetPos)
55 , mPutPos()
56 , mCanGrow(true)
58 {
59 if (aSrc.mPutPos > mGetPos)
60 {
61 PutRaw(aSrc.mBuffer, aSrc.mPutPos);
62 }
63 }
64
67 : mBuffer(nullptr)
68 , mCanGrow(false)
69 {
72 mCanGrow = true;
73 }
74
75 UtBuffer& operator=(const UtBuffer&) = delete;
76
78
80 void EnableByteSwap(bool aSwapBytes) { mSwapBytes = aSwapBytes; }
82 void SetBigEndian();
84 void SetLittleEndian();
86 void SetNativeByteOrder();
87
88 void Dispose()
89 {
90 if (mCanGrow)
91 {
92 delete[] mBuffer;
93 }
94 mBuffer = nullptr;
95 }
96
102 void SetBuffer(char* aData, size_t aBytes, bool aCanGrow = false)
103 {
104 Dispose();
105 mBuffer = aData;
106 mBytes = aBytes;
107 mPutPos = 0;
108 mGetPos = 0;
109 mCanGrow = aCanGrow;
110 }
111
112 void SetPutPos(size_t aPos) { mPutPos = aPos; }
114 size_t& GetPutPos() { return mPutPos; }
116 size_t GetPutPos() const { return mPutPos; }
118 void SetGetPos(size_t aPos) { mGetPos = aPos; }
120 size_t& GetGetPos() { return mGetPos; }
122 size_t GetGetPos() const { return mGetPos; }
123
125 char* GetBuffer() { return mBuffer; }
127 const char* GetBuffer() const { return mBuffer; }
128
130 size_t GetBytes() const { return mBytes; }
131
133 void PutRaw(const char* aBuffer, size_t aBytes)
134 {
135 CheckPutSpace(aBytes);
136 memcpy(mBuffer + mPutPos, aBuffer, aBytes);
137 mPutPos += aBytes;
138 }
139
140 void GetRaw(char* aBuffer, size_t aBytes)
141 {
142 memcpy(aBuffer, mBuffer + mGetPos, aBytes);
143 mGetPos += aBytes;
144 }
145
147 void Reset() { mGetPos = mPutPos = 0; }
148
149 void Move(size_t aBeginOffset, size_t aEndOffset, size_t aNewBegin);
150
152 void Grow(size_t aNewSize)
153 {
154 if (aNewSize >= mBytes)
155 {
156 GrowBy(aNewSize - mBytes);
157 }
158 }
159
160 void GrowBy(size_t aBytes);
162 size_t GetValidBytes() const { return mPutPos - mGetPos; }
165 {
167 std::swap(mBytes, aRhs.mBytes);
171 }
172
174 {
175 void* bufferPtr = mBuffer;
176 mBuffer = nullptr;
177 mBytes = 0;
178 return bufferPtr;
179 }
180
183 template<typename T>
184 void Put(const T& aVal)
185 {
186 PutP<sizeof(T)>(reinterpret_cast<const char*>(&aVal));
187 }
188
190 template<typename T>
191 void Get(T& aVal)
192 {
193 GetP<sizeof(T)>(reinterpret_cast<char*>(&aVal));
194 }
195
196 bool CheckPutSpace(size_t aPutSize)
197 {
198 bool needsToGrow = (aPutSize + mPutPos >= mBytes);
199 if (mCanGrow & needsToGrow)
200 {
201 GrowBy(aPutSize);
202 }
203 return needsToGrow & !mCanGrow;
204 }
205
206protected:
207 template<int BYTES>
208 void PutP(const char* aBuffer)
209 {
210 CheckPutSpace(BYTES);
211 if (mSwapBytes)
212 {
213 for (size_t i = 0; i < BYTES; ++i)
214 {
215 *(mBuffer + mPutPos + i) = aBuffer[BYTES - 1 - i];
216 }
217 }
218 else
219 {
220 memcpy(mBuffer + mPutPos, aBuffer, BYTES);
221 }
222 mPutPos += BYTES;
223 }
224
225 template<int BYTES>
226 void GetP(char* aBuffer)
227 {
228 if (mSwapBytes)
229 {
230 char* bufPtr = mBuffer + mGetPos;
231 for (size_t i = 0; i < BYTES; ++i)
232 {
233 *(aBuffer + i) = bufPtr[BYTES - 1 - i];
234 }
235 }
236 else
237 {
238 memcpy(aBuffer, mBuffer + mGetPos, BYTES);
239 }
240 mGetPos += BYTES;
241 }
242
243 char* mBuffer;
244 size_t mBytes;
245 size_t mGetPos;
246 size_t mPutPos;
249};
250
251#endif
#define WSF_UT_EXPORT
Definition WsfUtExport.hpp:32
void GetRaw(char *aBuffer, size_t aBytes)
Reads a sequence of aBytes to the buffer.
Definition UtBuffer.hpp:140
void PutP(const char *aBuffer)
Definition UtBuffer.hpp:208
size_t mBytes
Definition UtBuffer.hpp:244
void SetGetPos(size_t aPos)
Set the current offset used to get data.
Definition UtBuffer.hpp:118
void GetP(char *aBuffer)
Definition UtBuffer.hpp:226
const char * GetBuffer() const
Returns a pointer to the internal buffer.
Definition UtBuffer.hpp:127
size_t GetPutPos() const
Set the current offset used to put data.
Definition UtBuffer.hpp:116
void Reset()
Set the put/get positions to the start of the buffer.
Definition UtBuffer.hpp:147
void EnableByteSwap(bool aSwapBytes)
Disable or Enable byte swapping.
Definition UtBuffer.hpp:80
void Dispose()
Definition UtBuffer.hpp:88
UtBuffer()
Construct the buffer with a default size, and allow growth.
Definition UtBuffer.hpp:66
void SetPutPos(size_t aPos)
Set the current offset used to put data.
Definition UtBuffer.hpp:112
static const int cDEFAULT_BUFFER_SIZE
Definition UtBuffer.hpp:48
size_t & GetPutPos()
Get the current offset used to put data.
Definition UtBuffer.hpp:114
size_t GetGetPos() const
Get the current offset used to get data.
Definition UtBuffer.hpp:122
size_t GetBytes() const
Returns the size of the internal buffer.
Definition UtBuffer.hpp:130
UtBuffer(const UtBuffer &aSrc)
Construct a copy of aSrc.
Definition UtBuffer.hpp:51
void SetBigEndian()
Enable byte swapping if the local machine is little-endian.
Definition UtBuffer.cpp:15
void Put(const T &aVal)
Definition UtBuffer.hpp:184
void * ReleaseBuffer()
Definition UtBuffer.hpp:173
void SwapBuffer(UtBuffer &aRhs)
Swap the internal buffers between this and aRhs.
Definition UtBuffer.hpp:164
UtBuffer(char *aData, int aBytes, bool aCanGrow=false)
Construct the buffer with a user buffer, see SetBuffer().
Definition UtBuffer.hpp:40
bool CheckPutSpace(size_t aPutSize)
Definition UtBuffer.hpp:196
void Get(T &aVal)
Definition UtBuffer.hpp:191
~UtBuffer()
Definition UtBuffer.hpp:77
size_t mPutPos
Definition UtBuffer.hpp:246
size_t mGetPos
Definition UtBuffer.hpp:245
size_t & GetGetPos()
Get the current offset used to get data.
Definition UtBuffer.hpp:120
bool mCanGrow
Definition UtBuffer.hpp:247
void PutRaw(const char *aBuffer, size_t aBytes)
Writes a sequence of aBytes to the buffer.
Definition UtBuffer.hpp:133
void SetBuffer(char *aData, size_t aBytes, bool aCanGrow=false)
Definition UtBuffer.hpp:102
char * mBuffer
Definition UtBuffer.hpp:243
void Grow(size_t aNewSize)
Grow the buffer so it is at least as large as aNewSize.
Definition UtBuffer.hpp:152
char * GetBuffer()
Returns a pointer to the internal buffer.
Definition UtBuffer.hpp:125
void GrowBy(size_t aBytes)
Grow the buffer by an increment of at least aBytes.
Definition UtBuffer.cpp:36
UtBuffer(int aBytes)
Construct the buffer with a grow-able buffer of size aBytes.
Definition UtBuffer.hpp:31
bool mSwapBytes
Definition UtBuffer.hpp:248
UtBuffer & operator=(const UtBuffer &)=delete
size_t GetValidBytes() const
Return the difference between the write position and the read position.
Definition UtBuffer.hpp:162
void swap(WsfRoute &aLhs, WsfRoute &aRhs)
Definition WsfRoute.hpp:294
Copyrights Multiple, All Rights Reserved