AMPS C/C++ Client Class Reference
AMPS C/C++ Client Version 5.3.4.4
MemoryStoreBuffer.hpp
Go to the documentation of this file.
1 //
3 // Copyright (c) 2010-2024 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)
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  *((long*)(_buffer + _bufferPos)) = l_;
111  _bufferPos += sizeof(long);
112  }
113 
114  virtual long getLong()
115  {
116  _bufferPos += sizeof(long);
117  return *((long*)(_buffer + _bufferPos - sizeof(long)));
118  }
119 
120  virtual void putUnsignedLong(unsigned long l_)
121  {
122  *((unsigned long*)(_buffer + _bufferPos)) = l_;
123  _bufferPos += sizeof(unsigned long);
124  }
125 
126  virtual unsigned long getUnsignedLong()
127  {
128  _bufferPos += sizeof(unsigned long);
129  return *((unsigned long*)(_buffer + _bufferPos - sizeof(unsigned long)));
130  }
131 
132  virtual void putSizet(size_t s_)
133  {
134  *((size_t*)(_buffer + _bufferPos)) = s_;
135  _bufferPos += sizeof(size_t);
136  }
137 
138  virtual size_t getSizet()
139  {
140  _bufferPos += sizeof(size_t);
141  return *((size_t*)(_buffer + _bufferPos - sizeof(size_t)));
142  }
143 
144  virtual void putInt32(amps_int32_t s_)
145  {
146  *((amps_int32_t*)(_buffer + _bufferPos)) = s_;
147  _bufferPos += sizeof(amps_int32_t);
148  }
149 
150  virtual amps_int32_t getInt32()
151  {
152  _bufferPos += sizeof(amps_int32_t);
153  return *((amps_int32_t*)(_buffer + _bufferPos - sizeof(amps_int32_t)));
154  }
155 
156  virtual void putUint32(amps_uint32_t s_)
157  {
158  *((amps_uint32_t*)(_buffer + _bufferPos)) = s_;
159  _bufferPos += sizeof(amps_uint32_t);
160  }
161 
162  virtual amps_uint32_t getUint32()
163  {
164  _bufferPos += sizeof(amps_uint32_t);
165  return *((amps_uint32_t*)(_buffer + _bufferPos - sizeof(amps_uint32_t)));
166  }
167 
168  virtual void putInt64(amps_int64_t s_)
169  {
170  *((amps_int64_t*)(_buffer + _bufferPos)) = s_;
171  _bufferPos += sizeof(amps_int64_t);
172  }
173 
174  virtual amps_int64_t getInt64()
175  {
176  _bufferPos += sizeof(amps_int64_t);
177  return *((amps_int64_t*)(_buffer + _bufferPos - sizeof(amps_int64_t)));
178  }
179 
180  virtual void putUint64(amps_uint64_t s_)
181  {
182  *((amps_uint64_t*)(_buffer + _bufferPos)) = s_;
183  _bufferPos += sizeof(amps_uint64_t);
184  }
185 
186  virtual amps_uint64_t getUint64()
187  {
188  _bufferPos += sizeof(amps_uint64_t);
189  return *((amps_uint64_t*)(_buffer + _bufferPos - sizeof(amps_uint64_t)));
190  }
191 
192  virtual void putBytes(const char* data_, size_t dataLength_)
193  {
194  memcpy(_buffer + _bufferPos, data_, dataLength_);
195  _bufferPos += dataLength_;
196  }
197 
198  virtual void putBytes(const Buffer::ByteArray& bytes_)
199  {
200  memcpy(_buffer + _bufferPos, bytes_._data, bytes_._len);
201  _bufferPos += bytes_._len;
202  }
203 
204  virtual Buffer::ByteArray getBytes(size_t numBytes_)
205  {
206  Buffer::ByteArray retVal(_buffer + _bufferPos, numBytes_, false);
207  _bufferPos += numBytes_;
208  return retVal;
209  }
210 
211  // Copy bytes from here into buffer_
212  virtual void copyBytes(char* buffer_, size_t numBytes_)
213  {
214  memcpy(buffer_, _buffer + _bufferPos, numBytes_);
215  _bufferPos += numBytes_;
216  }
217 
218  virtual void zero(size_t offset_, size_t length_)
219  {
220  memset(_buffer + offset_, 0, length_);
221  }
222 
223  // Copy bytes from source_ location in the buffer to destination_ in buffer
224  virtual void copyBytes(size_t destination_, size_t source_, size_t number_)
225  {
226  // Could be overlap, should be infrequent, use memmove
227  memmove(_buffer + destination_, _buffer + source_, number_);
228  _bufferPos += number_;
229  }
230 
231  protected:
232  enum CtorFlag { EMPTY };
233  MemoryStoreBuffer(CtorFlag)
234  : _buffer(nullptr)
235  , _bufferLen(0)
236  , _bufferPos(0)
237  {}
238 
239  char* _buffer;
240  size_t _bufferLen;
241  size_t _bufferPos;
242  };
243 
244 } // end namespace AMPS
245 
246 #endif //_MEMORYSTOREBUFFER_H_
247 
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:156
virtual char getByte()
Get the next byte from the buffer and advance.
Definition: MemoryStoreBuffer.hpp:103
virtual amps_uint32_t getUint32()
Get the unsigned 32-bit int value at the current buffer position and advance past it...
Definition: MemoryStoreBuffer.hpp:162
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:212
virtual void putInt32(amps_int32_t s_)
Put a 32-bit int value into the buffer and advance past it.
Definition: MemoryStoreBuffer.hpp:144
virtual size_t getSize() const
Get the current size of the Buffer in bytes.
Definition: MemoryStoreBuffer.hpp:62
virtual amps_int64_t getInt64()
Get a 64-bit int value at the current buffer position and advance past it.
Definition: MemoryStoreBuffer.hpp:174
virtual amps_uint64_t getUint64()
Get an unsigned 64-bit int value at the current buffer position and advance past it.
Definition: MemoryStoreBuffer.hpp:186
A Buffer implementation that uses memory for storage.
Definition: MemoryStoreBuffer.hpp:50
virtual void setSize(size_t newSize_)
Set the size for the buffer.
Definition: MemoryStoreBuffer.hpp:67
Core type, function, and class declarations for the AMPS C++ client.
Provides AMPS::Buffer, an abstract base class used by the store implementations in the AMPS client...
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:224
virtual size_t getPosition() const
Get the current position in the buffer.
Definition: MemoryStoreBuffer.hpp:83
virtual size_t getSizet()
Get a size_t value at the current buffer position and advance past it.
Definition: MemoryStoreBuffer.hpp:138
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:218
virtual Buffer::ByteArray getBytes(size_t numBytes_)
Get the given number of bytes from the buffer.
Definition: MemoryStoreBuffer.hpp:204
virtual void setPosition(size_t position_)
Set the buffer postion to a location.
Definition: MemoryStoreBuffer.hpp:88
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:198
Abstract base class for implementing a buffer to be used by a StoreImpl for storage of publish messag...
Definition: Buffer.hpp:40
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:132
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:192
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:180
virtual amps_int32_t getInt32()
Get the 32-bit int value at the current buffer position and advance past it.
Definition: MemoryStoreBuffer.hpp:150
Definition: ampsplusplus.hpp:102
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:168