fork(1) download
  1. #ifndef COMMON_ASSORTED_BYTEBUFFER_H
  2. #define COMMON_ASSORTED_BYTEBUFFER_H
  3.  
  4. #include <string>
  5. typedef unsigned char Byte;
  6.  
  7. //A simple wrapper (with lots of convenience functions) for managing a block of memory.
  8. class ByteBuffer
  9. {
  10. public:
  11. static const size_t NoPos = ((size_t)-1);
  12. public:
  13. ByteBuffer() = default;
  14. ByteBuffer(size_t bytesToAllocate); //Calls 'AllocateData()'.
  15. ByteBuffer(const ByteBuffer &) = delete; //Disabled copy constructor. This class might contain alot of data, and should be explicitely copied only.
  16. ByteBuffer(ByteBuffer &&other); //Move constructor works though.
  17. ByteBuffer &operator=(const ByteBuffer &) = delete;
  18. ByteBuffer &operator=(ByteBuffer &&other);
  19. ~ByteBuffer();
  20.  
  21. //========================================================
  22. //Named constructors, for convience:
  23. //========================================================
  24.  
  25. //Creates a ByteBuffer that owns 'data', and will delete it when destroyed.
  26. static ByteBuffer MakeOwnerOf(Byte *data, size_t bytes);
  27. static ByteBuffer MakeOwnerOf(ByteBuffer &other);
  28.  
  29. //Creates a ByteBuffer that copies 'data'. When destroyed, this ByteBuffer will delete the copied memory but not the original.
  30. static ByteBuffer MakeCopiedFrom(const Byte *data, size_t bytes);
  31. static ByteBuffer MakeCopiedFrom(const ByteBuffer &other, size_t pos = 0, size_t bytes = NoPos);
  32.  
  33. //Creates a ByteBuffer that points to 'data', without copying it, and will not delete the memory when destroyed.
  34. static ByteBuffer MakePointedAt(Byte *data, size_t bytes);
  35. static ByteBuffer MakePointedAt(ByteBuffer &other, size_t pos = 0, size_t bytes = NoPos);
  36.  
  37. //Creates a ByteBuffer that allocates a new block of memory of size 'bytes', and that will delete the memory when destroyed.
  38. static ByteBuffer MakeAllocated(size_t bytes);
  39.  
  40. public:
  41. //========================================================
  42. //Access functions:
  43. //========================================================
  44.  
  45. //Returns a reference to the bytes starting at 'pos'.
  46. //Doesn't check if 'pos' is inbounds or not.
  47. Byte &operator[](size_t pos);
  48. const Byte &operator[](size_t pos) const;
  49.  
  50. //Returns a reference to the bytes starting at 'pos'.
  51. //This is the same as buffer[pos], except it _does_ check if pos is in-bounds, by asserting
  52. Byte &At(size_t pos);
  53. const Byte &At(size_t pos) const;
  54.  
  55. //Returns a pointer to the first byte of the entire buffer. (The same as buffer[0])
  56. Byte *GetData();
  57. const Byte *GetData() const;
  58.  
  59. //Creates a non-owning ByteBuffer that points to the memory starting at 'pos', and containing the specified length of bytes.
  60. //This is similar to getting a substring of a string.
  61. //This ByteBuffer still owns and data and is responsible for freeing it. The returned buffer merely points to the data, but doesn't own it.
  62. ByteBuffer MakeDataPortion(size_t pos, size_t bytes);
  63. //The same as 'MakeDataPortion()', but copies the data.
  64. ByteBuffer DuplicateDataPortion(size_t pos, size_t bytes);
  65.  
  66. //========================================================
  67. //Querying functions:
  68. //========================================================
  69.  
  70. //Returns the size of the buffer, in bytes.
  71. size_t GetSize() const;
  72.  
  73. //True if this ByteBuffer owns the memory it points to.
  74. bool IsOwner() const;
  75. //True if this ByteBuffer points to nullptr, or is of size 0.
  76. bool IsEmpty() const;
  77.  
  78. //========================================================
  79. //Freeing functions: (deletes or releases the data)
  80. //========================================================
  81.  
  82. //Releases ownership of the data (if it owned it), and returns a pointer to the data.
  83. //This ByteBuffer still points to the data, but will never delete it.
  84. //If 'bytes' is not Null, it will be set to the size of this buffer.
  85. Byte *ReleaseData(size_t *bytes = nullptr);
  86.  
  87. //Releases ownership of the data (if it owned it), and returns a pointer to the data.
  88. //This ByteBuffer no longer points to the data, and will never delete it.
  89. //If 'bytes' is not Null, it will be set to the size of this buffer.
  90. //This is the same as calling ReleaseData() followed by Clear().
  91. Byte *ReleaseAndReset(size_t *bytes = nullptr);
  92.  
  93. //Clears this ByteBuffer, setting the data pointer to nullptr and the size to 0.
  94. //If this ByteBuffer owns the memory it points to, that memory will now get deleted.
  95. void Clear();
  96.  
  97. //========================================================
  98. //Re-assignment functions
  99. //========================================================
  100.  
  101. //Takes ownership of 'data' without copying it.
  102. //When this ByteBuffer is destroyed, it will delete 'data' also.
  103. //Any old data this ByteBuffer already points to will be freed (if this buffer is the owner of that data).
  104. void TakeData(Byte *data, size_t bytes);
  105. void TakeData(ByteBuffer &other); //'other' is made to Release() the data.
  106. //Copies 'data'. When this ByteBuffer is destroyed, it will delete the copied memory but not the original.
  107. //Any old data this ByteBuffer already points to will be freed (if this buffer is the owner of that data).
  108. void CopyData(const Byte *data, size_t bytes);
  109. void CopyData(const ByteBuffer &other, size_t pos = 0, size_t bytes = NoPos);
  110. //Points to 'data', without copying it, and when this ByteBuffer is destroyed, will not delete the memory.
  111. //Any old data this ByteBuffer already points to will be freed (if this buffer is the owner of that data).
  112. void PointToData(Byte *data, size_t bytes);
  113. void PointToData(ByteBuffer &other, size_t pos = 0, size_t bytes = NoPos);
  114. //Allocates a new block of memory of size 'bytes'.
  115. //Any old data this ByteBuffer already points to will be freed (if this buffer is the owner of that data).
  116. void AllocateData(size_t bytes);
  117.  
  118. private:
  119. //Clears the existing data (freeing it if this ByteBuffer is the owner),
  120. //and assigning 'data' as the new memory pointer and 'bytes' as the new size.
  121. void resetTo(Byte *data, size_t bytes, bool isOwner);
  122.  
  123. private:
  124. Byte *data = nullptr;
  125. size_t bytes = 0;
  126. bool isOwner = true; //True if this ByteBuffer owns 'data', and is responsible for deleting it.
  127. };
  128.  
  129. //================================================================================
  130.  
  131. //A constant value
  132. const size_t ByteBuffer_SuggestedResize = size_t(-1);
  133.  
  134. //Resizes 'byteBuffer' by allocating a new buffer of size 'newSize', and copying the old bytes to it, before deleting the original buffer.
  135. //If 'offset' is specified, offsets the position to place the the old memory in the new buffer. This allows you to grow the buffer's front as well as the back.
  136. //e.g. to grow a buffer's front and back by 4 bytes each, you can do: ResizeByteBuffer(buffer, (buffer.GetSize() + 8), 4);
  137. //This can shrink or grow the buffer.
  138. //
  139. //If 'newSize' is ResizeByteBuffer_SuggestedSize, automaticly grows the buffer by an internally-decided reasonable amount (35% or a minimum of 128 bytes).
  140. void ResizeByteBuffer(ByteBuffer &byteBuffer, size_t newSize = ByteBuffer_SuggestedResize, size_t offset = 0);
  141. //Starting at 'pos', fills a number of bytes (bytes) in byteBuffer with 'fillValue'.
  142. void FillByteBuffer(ByteBuffer &byteBuffer, Byte fillValue = 0, size_t pos = 0, size_t bytes = ByteBuffer::NoPos);
  143.  
  144. //================================================================================
  145.  
  146. //Loads a file entirely into a bytebuffer. Returns an empty ByteBuffer if the load failed or if the file was empty.
  147. ByteBuffer LoadFileAsByteBuffer(const std::string &filename);
  148. //Writes the entire bytebuffer to a file, and returns true if it succeeded.
  149. //If 'bytes' is specified, and if it's less than the bytebuffer's size, only writes that number of bytes.
  150. bool SaveByteBufferToFile(const ByteBuffer &byteBuffer, const std::string &filename, size_t bytes = static_cast<size_t>(-1));
  151.  
  152. ByteBuffer StringToByteBuffer(const std::string &str);
  153. std::string ByteBufferToString(const ByteBuffer &byteBuffer);
  154.  
  155. //================================================================================
  156.  
  157. //Note yet written:
  158.  
  159. //ByteBuffer CompressByteBufferWith_AlgorithmName(const ByteBuffer &byteBuffer);
  160. //ByteBuffer UncompressByteBufferWith_AlgorithmName(const ByteBuffer &byteBuffer);
  161.  
  162. //================================================================================
  163.  
  164. //ByteBuffer EncryptByteBufferWith_AlgorithmName(const ByteBuffer &byteBuffer);
  165. //ByteBuffer UnencryptByteBufferWith_AlgorithmName(const ByteBuffer &byteBuffer);
  166.  
  167. //================================================================================
  168.  
  169. #endif // COMMON_ASSORTED_BYTEBUFFER_H
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/usr/lib/gcc/i486-linux-gnu/4.8/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty