The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

248 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_MEMORYBLOCK_JUCEHEADER__
  19. #define __JUCE_MEMORYBLOCK_JUCEHEADER__
  20. #include "../text/juce_String.h"
  21. #include "juce_HeapBlock.h"
  22. //==============================================================================
  23. /**
  24. A class to hold a resizable block of raw data.
  25. */
  26. class JUCE_API MemoryBlock
  27. {
  28. public:
  29. //==============================================================================
  30. /** Create an uninitialised block with 0 size. */
  31. MemoryBlock() throw();
  32. /** Creates a memory block with a given initial size.
  33. @param initialSize the size of block to create
  34. @param initialiseToZero whether to clear the memory or just leave it uninitialised
  35. */
  36. MemoryBlock (const size_t initialSize,
  37. const bool initialiseToZero = false) throw();
  38. /** Creates a copy of another memory block. */
  39. MemoryBlock (const MemoryBlock& other) throw();
  40. /** Creates a memory block using a copy of a block of data.
  41. @param dataToInitialiseFrom some data to copy into this block
  42. @param sizeInBytes how much space to use
  43. */
  44. MemoryBlock (const void* const dataToInitialiseFrom,
  45. const size_t sizeInBytes) throw();
  46. /** Destructor. */
  47. ~MemoryBlock() throw();
  48. /** Copies another memory block onto this one.
  49. This block will be resized and copied to exactly match the other one.
  50. */
  51. MemoryBlock& operator= (const MemoryBlock& other) throw();
  52. //==============================================================================
  53. /** Compares two memory blocks.
  54. @returns true only if the two blocks are the same size and have identical contents.
  55. */
  56. bool operator== (const MemoryBlock& other) const throw();
  57. /** Compares two memory blocks.
  58. @returns true if the two blocks are different sizes or have different contents.
  59. */
  60. bool operator!= (const MemoryBlock& other) const throw();
  61. //==============================================================================
  62. /** Returns a pointer to the data, casting it to any type of primitive data required.
  63. Note that the pointer returned will probably become invalid when the
  64. block is resized.
  65. */
  66. template <class DataType>
  67. operator DataType*() const throw() { return (DataType*) data; }
  68. /** Returns a void pointer to the data.
  69. Note that the pointer returned will probably become invalid when the
  70. block is resized.
  71. */
  72. void* getData() const throw() { return data; }
  73. /** Returns a byte from the memory block.
  74. This returns a reference, so you can also use it to set a byte.
  75. */
  76. template <typename Type>
  77. char& operator[] (const Type offset) const throw() { return data [offset]; }
  78. //==============================================================================
  79. /** Returns the block's current allocated size, in bytes. */
  80. size_t getSize() const throw() { return size; }
  81. /** Resizes the memory block.
  82. This will try to keep as much of the block's current content as it can,
  83. and can optionally be made to clear any new space that gets allocated at
  84. the end of the block.
  85. @param newSize the new desired size for the block
  86. @param initialiseNewSpaceToZero if the block gets enlarged, this determines
  87. whether to clear the new section or just leave it
  88. uninitialised
  89. @see ensureSize
  90. */
  91. void setSize (const size_t newSize,
  92. const bool initialiseNewSpaceToZero = false) throw();
  93. /** Increases the block's size only if it's smaller than a given size.
  94. @param minimumSize if the block is already bigger than this size, no action
  95. will be taken; otherwise it will be increased to this size
  96. @param initialiseNewSpaceToZero if the block gets enlarged, this determines
  97. whether to clear the new section or just leave it
  98. uninitialised
  99. @see setSize
  100. */
  101. void ensureSize (const size_t minimumSize,
  102. const bool initialiseNewSpaceToZero = false) throw();
  103. //==============================================================================
  104. /** Fills the entire memory block with a repeated byte value.
  105. This is handy for clearing a block of memory to zero.
  106. */
  107. void fillWith (const uint8 valueToUse) throw();
  108. /** Adds another block of data to the end of this one.
  109. This block's size will be increased accordingly.
  110. */
  111. void append (const void* const data,
  112. const size_t numBytes) throw();
  113. /** Exchanges the contents of this and another memory block.
  114. No actual copying is required for this, so it's very fast.
  115. */
  116. void swapWith (MemoryBlock& other) throw();
  117. //==============================================================================
  118. /** Copies data into this MemoryBlock from a memory address.
  119. @param srcData the memory location of the data to copy into this block
  120. @param destinationOffset the offset in this block at which the data being copied should begin
  121. @param numBytes how much to copy in (if this goes beyond the size of the memory block,
  122. it will be clipped so not to do anything nasty)
  123. */
  124. void copyFrom (const void* srcData,
  125. int destinationOffset,
  126. size_t numBytes) throw();
  127. /** Copies data from this MemoryBlock to a memory address.
  128. @param destData the memory location to write to
  129. @param sourceOffset the offset within this block from which the copied data will be read
  130. @param numBytes how much to copy (if this extends beyond the limits of the memory block,
  131. zeros will be used for that portion of the data)
  132. */
  133. void copyTo (void* destData,
  134. int sourceOffset,
  135. size_t numBytes) const throw();
  136. /** Chops out a section of the block.
  137. This will remove a section of the memory block and close the gap around it,
  138. shifting any subsequent data downwards and reducing the size of the block.
  139. If the range specified goes beyond the size of the block, it will be clipped.
  140. */
  141. void removeSection (size_t startByte, size_t numBytesToRemove) throw();
  142. //==============================================================================
  143. /** Attempts to parse the contents of the block as a zero-terminated string of 8-bit
  144. characters in the system's default encoding. */
  145. const String toString() const throw();
  146. //==============================================================================
  147. /** Parses a string of hexadecimal numbers and writes this data into the memory block.
  148. The block will be resized to the number of valid bytes read from the string.
  149. Non-hex characters in the string will be ignored.
  150. @see String::toHexString()
  151. */
  152. void loadFromHexString (const String& sourceHexString) throw();
  153. //==============================================================================
  154. /** Sets a number of bits in the memory block, treating it as a long binary sequence. */
  155. void setBitRange (size_t bitRangeStart,
  156. size_t numBits,
  157. int binaryNumberToApply) throw();
  158. /** Reads a number of bits from the memory block, treating it as one long binary sequence */
  159. int getBitRange (size_t bitRangeStart,
  160. size_t numBitsToRead) const throw();
  161. //==============================================================================
  162. /** Returns a string of characters that represent the binary contents of this block.
  163. Uses a 64-bit encoding system to allow binary data to be turned into a string
  164. of simple non-extended characters, e.g. for storage in XML.
  165. @see fromBase64Encoding
  166. */
  167. const String toBase64Encoding() const throw();
  168. /** Takes a string of encoded characters and turns it into binary data.
  169. The string passed in must have been created by to64BitEncoding(), and this
  170. block will be resized to recreate the original data block.
  171. @see toBase64Encoding
  172. */
  173. bool fromBase64Encoding (const String& encodedString) throw();
  174. //==============================================================================
  175. juce_UseDebuggingNewOperator
  176. private:
  177. //==============================================================================
  178. HeapBlock <char> data;
  179. size_t size;
  180. };
  181. #endif // __JUCE_MEMORYBLOCK_JUCEHEADER__