Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #ifndef WATER_MEMORYBLOCK_H_INCLUDED
  21. #define WATER_MEMORYBLOCK_H_INCLUDED
  22. #include "HeapBlock.h"
  23. namespace water {
  24. //==============================================================================
  25. /**
  26. A class to hold a resizable block of raw data.
  27. */
  28. class MemoryBlock
  29. {
  30. public:
  31. //==============================================================================
  32. /** Create an uninitialised block with 0 size. */
  33. MemoryBlock() noexcept;
  34. /** Creates a memory block with a given initial size.
  35. @param initialSize the size of block to create
  36. @param initialiseToZero whether to clear the memory or just leave it uninitialised
  37. */
  38. MemoryBlock (const size_t initialSize,
  39. bool initialiseToZero = false);
  40. /** Creates a copy of another memory block. */
  41. MemoryBlock (const MemoryBlock&);
  42. /** Creates a memory block using a copy of a block of data.
  43. @param dataToInitialiseFrom some data to copy into this block
  44. @param sizeInBytes how much space to use
  45. */
  46. MemoryBlock (const void* dataToInitialiseFrom, size_t sizeInBytes);
  47. /** Destructor. */
  48. ~MemoryBlock() noexcept;
  49. /** Copies another memory block onto this one.
  50. This block will be resized and copied to exactly match the other one.
  51. */
  52. MemoryBlock& operator= (const MemoryBlock&);
  53. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  54. MemoryBlock (MemoryBlock&&) noexcept;
  55. MemoryBlock& operator= (MemoryBlock&&) noexcept;
  56. #endif
  57. //==============================================================================
  58. /** Compares two memory blocks.
  59. @returns true only if the two blocks are the same size and have identical contents.
  60. */
  61. bool operator== (const MemoryBlock& other) const noexcept;
  62. /** Compares two memory blocks.
  63. @returns true if the two blocks are different sizes or have different contents.
  64. */
  65. bool operator!= (const MemoryBlock& other) const noexcept;
  66. /** Returns true if the data in this MemoryBlock matches the raw bytes passed-in. */
  67. bool matches (const void* data, size_t dataSize) const noexcept;
  68. //==============================================================================
  69. /** Returns a void pointer to the data.
  70. Note that the pointer returned will probably become invalid when the
  71. block is resized.
  72. */
  73. void* getData() const noexcept { return data; }
  74. /** Returns a byte from the memory block.
  75. This returns a reference, so you can also use it to set a byte.
  76. */
  77. template <typename Type>
  78. char& operator[] (const Type offset) const noexcept { return data [offset]; }
  79. //==============================================================================
  80. /** Returns the block's current allocated size, in bytes. */
  81. size_t getSize() const noexcept { return size; }
  82. /** Resizes the memory block.
  83. Any data that is present in both the old and new sizes will be retained.
  84. When enlarging the block, the new space that is allocated at the end can either be
  85. cleared, or left uninitialised.
  86. @param newSize the new desired size for the block
  87. @param initialiseNewSpaceToZero if the block gets enlarged, this determines
  88. whether to clear the new section or just leave it
  89. uninitialised
  90. @see ensureSize
  91. */
  92. void setSize (const size_t newSize,
  93. bool initialiseNewSpaceToZero = false);
  94. /** Increases the block's size only if it's smaller than a given size.
  95. @param minimumSize if the block is already bigger than this size, no action
  96. will be taken; otherwise it will be increased to this size
  97. @param initialiseNewSpaceToZero if the block gets enlarged, this determines
  98. whether to clear the new section or just leave it
  99. uninitialised
  100. @see setSize
  101. */
  102. void ensureSize (const size_t minimumSize,
  103. bool initialiseNewSpaceToZero = false);
  104. /** Frees all the blocks data, setting its size to 0. */
  105. void reset();
  106. //==============================================================================
  107. /** Fills the entire memory block with a repeated byte value.
  108. This is handy for clearing a block of memory to zero.
  109. */
  110. void fillWith (uint8 valueToUse) noexcept;
  111. /** Adds another block of data to the end of this one.
  112. The data pointer must not be null. This block's size will be increased accordingly.
  113. */
  114. void append (const void* data, size_t numBytes);
  115. /** Resizes this block to the given size and fills its contents from the supplied buffer.
  116. The data pointer must not be null.
  117. */
  118. void replaceWith (const void* data, size_t numBytes);
  119. /** Inserts some data into the block.
  120. The dataToInsert pointer must not be null. This block's size will be increased accordingly.
  121. If the insert position lies outside the valid range of the block, it will be clipped to
  122. within the range before being used.
  123. */
  124. void insert (const void* dataToInsert, size_t numBytesToInsert, size_t insertPosition);
  125. /** Chops out a section of the block.
  126. This will remove a section of the memory block and close the gap around it,
  127. shifting any subsequent data downwards and reducing the size of the block.
  128. If the range specified goes beyond the size of the block, it will be clipped.
  129. */
  130. void removeSection (size_t startByte, size_t numBytesToRemove);
  131. //==============================================================================
  132. /** Copies data into this MemoryBlock from a memory address.
  133. @param srcData the memory location of the data to copy into this block
  134. @param destinationOffset the offset in this block at which the data being copied should begin
  135. @param numBytes how much to copy in (if this goes beyond the size of the memory block,
  136. it will be clipped so not to do anything nasty)
  137. */
  138. void copyFrom (const void* srcData,
  139. int destinationOffset,
  140. size_t numBytes) noexcept;
  141. /** Copies data from this MemoryBlock to a memory address.
  142. @param destData the memory location to write to
  143. @param sourceOffset the offset within this block from which the copied data will be read
  144. @param numBytes how much to copy (if this extends beyond the limits of the memory block,
  145. zeros will be used for that portion of the data)
  146. */
  147. void copyTo (void* destData,
  148. int sourceOffset,
  149. size_t numBytes) const noexcept;
  150. //==============================================================================
  151. /** Exchanges the contents of this and another memory block.
  152. No actual copying is required for this, so it's very fast.
  153. */
  154. void swapWith (MemoryBlock& other) noexcept;
  155. //==============================================================================
  156. /** Release this object's data ownership, returning the data pointer. */
  157. void* release () noexcept;
  158. //==============================================================================
  159. /** Attempts to parse the contents of the block as a zero-terminated UTF8 string. */
  160. String toString() const;
  161. //==============================================================================
  162. /** Parses a string of hexadecimal numbers and writes this data into the memory block.
  163. The block will be resized to the number of valid bytes read from the string.
  164. Non-hex characters in the string will be ignored.
  165. @see String::toHexString()
  166. */
  167. void loadFromHexString (StringRef sourceHexString);
  168. //==============================================================================
  169. /** Sets a number of bits in the memory block, treating it as a long binary sequence. */
  170. void setBitRange (size_t bitRangeStart,
  171. size_t numBits,
  172. int binaryNumberToApply) noexcept;
  173. /** Reads a number of bits from the memory block, treating it as one long binary sequence */
  174. int getBitRange (size_t bitRangeStart,
  175. size_t numBitsToRead) const noexcept;
  176. private:
  177. //==============================================================================
  178. HeapBlock<char> data;
  179. size_t size;
  180. };
  181. }
  182. #endif // WATER_MEMORYBLOCK_H_INCLUDED