kodi
Buffer.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include <memory>
12 #include <string.h>
13 #include <string>
14 
15 namespace XbmcCommons
16 {
17  class BufferException final
18  {
19  std::string message;
20 
21  public:
22  explicit BufferException(const char* message_) : message(message_) {}
23  };
24 
79  class Buffer
80  {
81  std::shared_ptr<unsigned char> bufferRef;
82  unsigned char* buffer = nullptr;
83  size_t mposition = 0;
84  size_t mcapacity = 0;
85  size_t mlimit = 0;
86 
87  inline void check(size_t count) const
88  {
89  if ((mposition + count) > mlimit)
90  throw BufferException("Buffer buffer overflow: Cannot add more data to the Buffer's buffer.");
91  }
92 
93  public:
97  inline Buffer() { clear(); }
98 
111  inline Buffer(void* buffer_, size_t bufferSize) : buffer((unsigned char*)buffer_), mcapacity(bufferSize)
112  {
113  clear();
114  }
115 
122  inline explicit Buffer(size_t bufferSize) : buffer(bufferSize ? new unsigned char[bufferSize] : NULL), mcapacity(bufferSize)
123  {
124  clear();
125  bufferRef.reset(buffer, std::default_delete<unsigned char[]>());
126  }
127 
135  inline Buffer(const Buffer& buf) = default;
136 
144  inline Buffer& operator=(const Buffer& buf)
145  {
146  buffer = buf.buffer;
147  bufferRef = buf.bufferRef;
148  mcapacity = buf.mcapacity;
149  mlimit = buf.mlimit;
150  return *this;
151  }
152 
153  inline Buffer& allocate(size_t bufferSize)
154  {
155  buffer = bufferSize ? new unsigned char[bufferSize] : NULL;
156  bufferRef.reset(buffer, std::default_delete<unsigned char[]>());
157  mcapacity = bufferSize;
158  clear();
159  return *this;
160  }
161 
178  inline Buffer& flip() { mlimit = mposition; mposition = 0; return *this; }
179 
194  inline Buffer& clear() { mlimit = mcapacity; mposition = 0; return *this; }
195 
200  inline Buffer& rewind() { mposition = 0; return *this; }
201 
207  inline size_t remaining() const { return mlimit - mposition; }
208 
209  inline Buffer& put(const void* src, size_t bytes)
210  { check(bytes); memcpy( buffer + mposition, src, bytes); mposition += bytes; return *this; }
211  inline Buffer& get(void* dest, size_t bytes)
212  { check(bytes); memcpy( dest, buffer + mposition, bytes); mposition += bytes; return *this; }
213 
214  inline unsigned char* data() const { return buffer; }
215  inline unsigned char* curPosition() const { return buffer + mposition; }
216  inline Buffer& setPosition(size_t position) { mposition = position; return *this; }
217  inline Buffer& forward(size_t positionIncrement)
218  { check(positionIncrement); mposition += positionIncrement; return *this; }
219 
220  inline size_t limit() const { return mlimit; }
221  inline size_t capacity() const { return mcapacity; }
222  inline size_t position() const { return mposition; }
223 
224 #define DEFAULTBUFFERRELATIVERW(name,type) \
225  inline Buffer& put##name(const type & val) { return put(&val, sizeof(type)); } \
226  inline type get##name() { type ret; get(&ret, sizeof(type)); return ret; }
227 
228  DEFAULTBUFFERRELATIVERW(Bool,bool);
229  DEFAULTBUFFERRELATIVERW(Int,int);
230  DEFAULTBUFFERRELATIVERW(Char,char);
231  DEFAULTBUFFERRELATIVERW(Long,long);
232  DEFAULTBUFFERRELATIVERW(Float,float);
233  DEFAULTBUFFERRELATIVERW(Double,double);
234  DEFAULTBUFFERRELATIVERW(Pointer,void*);
235  DEFAULTBUFFERRELATIVERW(LongLong,long long);
236 #undef DEFAULTBUFFERRELATIVERW
237 
238  inline Buffer& putString(const char* str) { size_t len = strlen(str) + 1; check(len); put(str, len); return (*this); }
239  inline Buffer& putString(const std::string& str) { size_t len = str.length() + 1; check(len); put(str.c_str(), len); return (*this); }
240 
241  inline std::string getString() { std::string ret((const char*)(buffer + mposition)); size_t len = ret.length() + 1; check(len); mposition += len; return ret; }
242  inline std::string getString(size_t length)
243  {
244  check(length);
245  std::string ret((const char*)(buffer + mposition),length);
246  mposition += length;
247  return ret;
248  }
249  inline char* getCharPointerDirect() { char* ret = (char*)(buffer + mposition); size_t len = strlen(ret) + 1; check(len); mposition += len; return ret; }
250 
251  };
252 
253 }
254 
Buffer & operator=(const Buffer &buf)
Copy another buffer.
Definition: Buffer.h:144
Definition: Buffer.h:17
size_t remaining() const
This method provides for the remaining number of bytes that can be read out of the buffer or written ...
Definition: Buffer.h:207
Definition: Buffer.h:15
Buffer & flip()
Flips this buffer.
Definition: Buffer.h:178
Buffer & rewind()
This method resets the position to the beginning of the buffer so that it can be either reread or wri...
Definition: Buffer.h:200
Buffer(void *buffer_, size_t bufferSize)
Construct a buffer given an externally managed memory buffer.
Definition: Buffer.h:111
Buffer(size_t bufferSize)
Construct a buffer buffer using the size buffer provided.
Definition: Buffer.h:122
This class is based on the java java.nio.Buffer class however, it does not implement the &#39;mark&#39; funct...
Definition: Buffer.h:79
A class representing a touch pointer interaction consisting of an down touch, the last touch and the ...
Definition: TouchTypes.h:49
#define check(A)
disappears in normal production mode
Definition: bigint.c:90
Buffer()
Construct an uninitialized buffer instance, perhaps as an lvalue.
Definition: Buffer.h:97
Buffer & clear()
Clears this buffer.
Definition: Buffer.h:194