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.

264 lines
11KB

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