AMPS C/C++ Client Class Reference
AMPS C/C++ Client Version 5.3.5.17
MemoryStoreBuffer.hpp
Go to the documentation of this file.
1 //
3 // Copyright (c) 2010-2026 60East Technologies Inc., All Rights Reserved.
4 //
5 // This computer software is owned by 60East Technologies Inc. and is
6 // protected by U.S. copyright laws and other laws and by international
7 // treaties. This computer software is furnished by 60East Technologies
8 // Inc. pursuant to a written license agreement and may be used, copied,
9 // transmitted, and stored only in accordance with the terms of such
10 // license agreement and with the inclusion of the above copyright notice.
11 // This computer software or any other copies thereof may not be provided
12 // or otherwise made available to any other person.
13 //
14 // U.S. Government Restricted Rights. This computer software: (a) was
15 // developed at private expense and is in all respects the proprietary
16 // information of 60East Technologies Inc.; (b) was not developed with
17 // government funds; (c) is a trade secret of 60East Technologies Inc.
18 // for all purposes of the Freedom of Information Act; and (d) is a
19 // commercial item and thus, pursuant to Section 12.212 of the Federal
20 // Acquisition Regulations (FAR) and DFAR Supplement Section 227.7202,
21 // Government's use, duplication or disclosure of the computer software
22 // is subject to the restrictions set forth by 60East Technologies Inc..
23 //
25 
26 #ifndef _MEMORYSTOREBUFFER_H_
27 #define _MEMORYSTOREBUFFER_H_
28 
29 #include <amps/ampsplusplus.hpp>
30 #include <amps/Buffer.hpp>
31 #include <string>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #ifdef _WIN32
36  #include <windows.h>
37 #else
38  #include <sys/mman.h>
39 #endif
40 
44 
45 
46 namespace AMPS
47 {
50  class MemoryStoreBuffer : public Buffer
51  {
52  public:
53  MemoryStoreBuffer() : _buffer(new char[AMPS_MEMORYBUFFER_DEFAULT_LENGTH])
54  , _bufferLen(AMPS_MEMORYBUFFER_DEFAULT_LENGTH), _bufferPos(0)
55  {}
56 
58  {
59  delete[] _buffer;
60  }
61 
62  virtual size_t getSize() const
63  {
64  return _buffer == NULL ? 0 : _bufferLen;
65  }
66 
67  virtual void setSize(size_t newSize_)
68  {
69  if (_buffer != NULL && newSize_ > _bufferLen)
70  {
71  char* _oldBuffer = _buffer;
72  _buffer = new char[newSize_];
73  memcpy(_buffer, _oldBuffer, _bufferLen);
74  delete[] _oldBuffer;
75  }
76  else if (_buffer == NULL)
77  {
78  _buffer = new char[newSize_];
79  }
80  _bufferLen = newSize_;
81  }
82 
83  virtual size_t getPosition() const
84  {
85  return _bufferPos;
86  }
87 
88  virtual void setPosition(size_t position_)
89  {
90  while (position_ > _bufferLen) // -V776 _bufferLen should update in setSize
91  {
92  setSize(_bufferLen * 2);
93  }
94  _bufferPos = position_;
95 
96  }
97 
98  virtual void putByte(char byte_)
99  {
100  _buffer[_bufferPos++] = byte_;
101  }
102 
103  virtual char getByte()
104  {
105  return _buffer[_bufferPos++];
106  }
107 
108  virtual void putLong(long l_)
109  {
110  memcpy(_buffer + _bufferPos, &l_, sizeof(l_));
111  _bufferPos += sizeof(long);
112  }
113 
114  virtual long getLong()
115  {
116  long l;
117  memcpy(&l, _buffer + _bufferPos, sizeof(l));
118  _bufferPos += sizeof(long);
119  return l;
120  }
121 
122  virtual void putUnsignedLong(unsigned long l_)
123  {
124  memcpy(_buffer + _bufferPos, &l_, sizeof(l_));
125  _bufferPos += sizeof(unsigned long);
126  }
127 
128  virtual unsigned long getUnsignedLong()
129  {
130  unsigned long l;
131  memcpy(&l, _buffer + _bufferPos, sizeof(l));
132  _bufferPos += sizeof(unsigned long);
133  return l;
134  }
135 
136  virtual void putSizet(size_t s_)
137  {
138  memcpy(_buffer + _bufferPos, &s_, sizeof(s_));
139  _bufferPos += sizeof(size_t);
140  }
141 
142  virtual size_t getSizet()
143  {
144  size_t l;
145  memcpy(&l, _buffer + _bufferPos, sizeof(l));
146  _bufferPos += sizeof(size_t);
147  return l;
148  }
149 
150  virtual void putInt32(amps_int32_t s_)
151  {
152  memcpy(_buffer + _bufferPos, &s_, sizeof(s_));
153  _bufferPos += sizeof(amps_int32_t);
154  }
155 
156  virtual amps_int32_t getInt32()
157  {
158  amps_int32_t l;
159  memcpy(&l, _buffer + _bufferPos, sizeof(l));
160  _bufferPos += sizeof(amps_int32_t);
161  return l;
162  }
163 
164  virtual void putUint32(amps_uint32_t s_)
165  {
166  memcpy(_buffer + _bufferPos, &s_, sizeof(s_));
167  _bufferPos += sizeof(amps_uint32_t);
168  }
169 
170  virtual amps_uint32_t getUint32()
171  {
172  amps_uint32_t l;
173  memcpy(&l, _buffer + _bufferPos, sizeof(l));
174  _bufferPos += sizeof(amps_uint32_t);
175  return l;
176  }
177 
178  virtual void putInt64(amps_int64_t s_)
179  {
180  memcpy(_buffer + _bufferPos, &s_, sizeof(s_));
181  _bufferPos += sizeof(amps_int64_t);
182  }
183 
184  virtual amps_int64_t getInt64()
185  {
186  amps_int64_t l;
187  memcpy(&l, _buffer + _bufferPos, sizeof(l));
188  _bufferPos += sizeof(amps_int64_t);
189  return l;
190  }
191 
192  virtual void putUint64(amps_uint64_t s_)
193  {
194  memcpy(_buffer + _bufferPos, &s_, sizeof(s_));
195  _bufferPos += sizeof(amps_uint64_t);
196  }
197 
198  virtual amps_uint64_t getUint64()
199  {
200  amps_uint64_t l;
201  memcpy(&l, _buffer + _bufferPos, sizeof(l));
202  _bufferPos += sizeof(amps_uint64_t);
203  return l;
204  }
205 
206  virtual void putBytes(const char* data_, size_t dataLength_)
207  {
208  memcpy(_buffer + _bufferPos, data_, dataLength_);
209  _bufferPos += dataLength_;
210  }
211 
212  virtual void putBytes(const Buffer::ByteArray& bytes_)
213  {
214  memcpy(_buffer + _bufferPos, bytes_._data, bytes_._len);
215  _bufferPos += bytes_._len;
216  }
217 
218  virtual Buffer::ByteArray getBytes(size_t numBytes_)
219  {
220  Buffer::ByteArray retVal(_buffer + _bufferPos, numBytes_, false);
221  _bufferPos += numBytes_;
222  return retVal;
223  }
224 
225  // Copy bytes from here into buffer_
226  virtual void copyBytes(char* buffer_, size_t numBytes_)
227  {
228  memcpy(buffer_, _buffer + _bufferPos, numBytes_);
229  _bufferPos += numBytes_;
230  }
231 
232  virtual void zero(size_t offset_, size_t length_)
233  {
234  memset(_buffer + offset_, 0, length_);
235  }
236 
237  // Copy bytes from source_ location in the buffer to destination_ in buffer
238  virtual void copyBytes(size_t destination_, size_t source_, size_t number_)
239  {
240  // Could be overlap, should be infrequent, use memmove
241  memmove(_buffer + destination_, _buffer + source_, number_);
242  _bufferPos += number_;
243  }
244 
245  protected:
246  enum CtorFlag { EMPTY };
247  MemoryStoreBuffer(CtorFlag)
248  : _buffer(nullptr)
249  , _bufferLen(0)
250  , _bufferPos(0)
251  {}
252 
253  char* _buffer;
254  size_t _bufferLen;
255  size_t _bufferPos;
256  };
257 
258 } // end namespace AMPS
259 
260 #endif //_MEMORYSTOREBUFFER_H_
261 
Provides AMPS::Buffer, an abstract base class used by the store implementations in the AMPS client.
Core type, function, and class declarations for the AMPS C++ client.
Abstract base class for implementing a buffer to be used by a StoreImpl for storage of publish messag...
Definition: Buffer.hpp:41
A Buffer implementation that uses memory for storage.
Definition: MemoryStoreBuffer.hpp:51
virtual amps_uint64_t getUint64()
Get an unsigned 64-bit int value at the current buffer position and advance past it.
Definition: MemoryStoreBuffer.hpp:198
virtual amps_int32_t getInt32()
Get the 32-bit int value at the current buffer position and advance past it.
Definition: MemoryStoreBuffer.hpp:156
virtual char getByte()
Get the next byte from the buffer and advance.
Definition: MemoryStoreBuffer.hpp:103
virtual void putInt32(amps_int32_t s_)
Put a 32-bit int value into the buffer and advance past it.
Definition: MemoryStoreBuffer.hpp:150
virtual size_t getPosition() const
Get the current position in the buffer.
Definition: MemoryStoreBuffer.hpp:83
virtual void putSizet(size_t s_)
Put a size_t value into the buffer at the current position and advance past it.
Definition: MemoryStoreBuffer.hpp:136
virtual void putUint64(amps_uint64_t s_)
Put an amps_uint64_t value into the buffer at the current position and advance past it.
Definition: MemoryStoreBuffer.hpp:192
virtual void setPosition(size_t position_)
Set the buffer postion to a location.
Definition: MemoryStoreBuffer.hpp:88
virtual void zero(size_t offset_, size_t length_)
Set the given number of bytes in the buffer to zero starting at the given position.
Definition: MemoryStoreBuffer.hpp:232
virtual amps_uint32_t getUint32()
Get the unsigned 32-bit int value at the current buffer position and advance past it.
Definition: MemoryStoreBuffer.hpp:170
virtual amps_int64_t getInt64()
Get a 64-bit int value at the current buffer position and advance past it.
Definition: MemoryStoreBuffer.hpp:184
virtual void putInt64(amps_int64_t s_)
Put an amps_int64_t value into the buffer at the current position and advance past it.
Definition: MemoryStoreBuffer.hpp:178
virtual Buffer::ByteArray getBytes(size_t numBytes_)
Get the given number of bytes from the buffer.
Definition: MemoryStoreBuffer.hpp:218
virtual size_t getSizet()
Get a size_t value at the current buffer position and advance past it.
Definition: MemoryStoreBuffer.hpp:142
virtual void putBytes(const Buffer::ByteArray &bytes_)
Put the given bytes into the buffer at the current position and advance past them.
Definition: MemoryStoreBuffer.hpp:212
virtual void copyBytes(size_t destination_, size_t source_, size_t number_)
Move the given number of bytes at the given location to the new location Buffer should do this in the...
Definition: MemoryStoreBuffer.hpp:238
virtual size_t getSize() const
Get the current size of the Buffer in bytes.
Definition: MemoryStoreBuffer.hpp:62
virtual void putBytes(const char *data_, size_t dataLength_)
Put the given length of bytes in data into the buffer at the current position and advance past them.
Definition: MemoryStoreBuffer.hpp:206
virtual void putUint32(amps_uint32_t s_)
Put an unsigned 32-bit int value into the buffer at the current position and advance past the end of ...
Definition: MemoryStoreBuffer.hpp:164
virtual void putByte(char byte_)
Put a byte into the buffer at the current position and advance.
Definition: MemoryStoreBuffer.hpp:98
virtual void copyBytes(char *buffer_, size_t numBytes_)
Copy the given number of bytes from this buffer to the given buffer.
Definition: MemoryStoreBuffer.hpp:226
virtual void setSize(size_t newSize_)
Set the size for the buffer.
Definition: MemoryStoreBuffer.hpp:67