From 4e68bd517bbaad5b606d9fc8afe13cc8a3c620fc Mon Sep 17 00:00:00 2001 From: Julian Storer Date: Thu, 26 Aug 2010 16:00:17 +0100 Subject: [PATCH] Small tweaks for OwnedArray, and some minor clean-ups. --- juce_amalgamated.cpp | 29 +++---- juce_amalgamated.h | 94 +++++++++++++---------- src/audio/synthesisers/juce_Sampler.h | 1 + src/containers/juce_MemoryBlock.cpp | 29 +++---- src/containers/juce_MemoryBlock.h | 28 ++++--- src/containers/juce_OwnedArray.h | 67 +++++++++------- src/gui/graphics/contexts/juce_Graphics.h | 1 + 7 files changed, 132 insertions(+), 117 deletions(-) diff --git a/juce_amalgamated.cpp b/juce_amalgamated.cpp index ef1196b900..3681fd7033 100644 --- a/juce_amalgamated.cpp +++ b/juce_amalgamated.cpp @@ -3416,8 +3416,7 @@ MemoryBlock::MemoryBlock() throw() { } -MemoryBlock::MemoryBlock (const size_t initialSize, - const bool initialiseToZero) throw() +MemoryBlock::MemoryBlock (const size_t initialSize, const bool initialiseToZero) { if (initialSize > 0) { @@ -3430,7 +3429,7 @@ MemoryBlock::MemoryBlock (const size_t initialSize, } } -MemoryBlock::MemoryBlock (const MemoryBlock& other) throw() +MemoryBlock::MemoryBlock (const MemoryBlock& other) : size (other.size) { if (size > 0) @@ -3441,8 +3440,7 @@ MemoryBlock::MemoryBlock (const MemoryBlock& other) throw() } } -MemoryBlock::MemoryBlock (const void* const dataToInitialiseFrom, - const size_t sizeInBytes) throw() +MemoryBlock::MemoryBlock (const void* const dataToInitialiseFrom, const size_t sizeInBytes) : size (jmax ((size_t) 0, sizeInBytes)) { jassert (sizeInBytes >= 0); @@ -3464,7 +3462,7 @@ MemoryBlock::~MemoryBlock() throw() jassert (size == 0 || data != 0); // non-zero size but no data allocated? } -MemoryBlock& MemoryBlock::operator= (const MemoryBlock& other) throw() +MemoryBlock& MemoryBlock::operator= (const MemoryBlock& other) { if (this != &other) { @@ -3492,8 +3490,7 @@ bool MemoryBlock::matches (const void* dataToCompare, size_t dataSize) const thr } // this will resize the block to this size -void MemoryBlock::setSize (const size_t newSize, - const bool initialiseToZero) throw() +void MemoryBlock::setSize (const size_t newSize, const bool initialiseToZero) { if (size != newSize) { @@ -3521,8 +3518,7 @@ void MemoryBlock::setSize (const size_t newSize, } } -void MemoryBlock::ensureSize (const size_t minimumSize, - const bool initialiseToZero) throw() +void MemoryBlock::ensureSize (const size_t minimumSize, const bool initialiseToZero) { if (size < minimumSize) setSize (minimumSize, initialiseToZero); @@ -3539,8 +3535,7 @@ void MemoryBlock::fillWith (const uint8 value) throw() memset (data, (int) value, size); } -void MemoryBlock::append (const void* const srcData, - const size_t numBytes) throw() +void MemoryBlock::append (const void* const srcData, const size_t numBytes) { if (numBytes > 0) { @@ -3592,7 +3587,7 @@ void MemoryBlock::copyTo (void* const dst, int offset, size_t num) const throw() memcpy (d, data + offset, num); } -void MemoryBlock::removeSection (size_t startByte, size_t numBytesToRemove) throw() +void MemoryBlock::removeSection (size_t startByte, size_t numBytesToRemove) { if (startByte < 0) { @@ -3614,7 +3609,7 @@ void MemoryBlock::removeSection (size_t startByte, size_t numBytesToRemove) thro } } -const String MemoryBlock::toString() const throw() +const String MemoryBlock::toString() const { return String (static_cast (getData()), size); } @@ -3666,7 +3661,7 @@ void MemoryBlock::setBitRange (const size_t bitRangeStart, size_t numBits, int b } } -void MemoryBlock::loadFromHexString (const String& hex) throw() +void MemoryBlock::loadFromHexString (const String& hex) { ensureSize (hex.length() >> 1); char* dest = data; @@ -3713,7 +3708,7 @@ void MemoryBlock::loadFromHexString (const String& hex) throw() const char* const MemoryBlock::encodingTable = ".ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+"; -const String MemoryBlock::toBase64Encoding() const throw() +const String MemoryBlock::toBase64Encoding() const { const size_t numChars = ((size << 3) + 5) / 6; @@ -3733,7 +3728,7 @@ const String MemoryBlock::toBase64Encoding() const throw() return destString; } -bool MemoryBlock::fromBase64Encoding (const String& s) throw() +bool MemoryBlock::fromBase64Encoding (const String& s) { const int startPos = s.indexOfChar ('.') + 1; diff --git a/juce_amalgamated.h b/juce_amalgamated.h index e8eed827c5..0e87cecb43 100644 --- a/juce_amalgamated.h +++ b/juce_amalgamated.h @@ -4869,18 +4869,17 @@ public: @param initialiseToZero whether to clear the memory or just leave it uninitialised */ MemoryBlock (const size_t initialSize, - const bool initialiseToZero = false) throw(); + bool initialiseToZero = false); /** Creates a copy of another memory block. */ - MemoryBlock (const MemoryBlock& other) throw(); + MemoryBlock (const MemoryBlock& other); /** Creates a memory block using a copy of a block of data. @param dataToInitialiseFrom some data to copy into this block @param sizeInBytes how much space to use */ - MemoryBlock (const void* dataToInitialiseFrom, - const size_t sizeInBytes) throw(); + MemoryBlock (const void* dataToInitialiseFrom, size_t sizeInBytes); /** Destructor. */ ~MemoryBlock() throw(); @@ -4889,7 +4888,7 @@ public: This block will be resized and copied to exactly match the other one. */ - MemoryBlock& operator= (const MemoryBlock& other) throw(); + MemoryBlock& operator= (const MemoryBlock& other); /** Compares two memory blocks. @@ -4937,7 +4936,7 @@ public: @see ensureSize */ void setSize (const size_t newSize, - const bool initialiseNewSpaceToZero = false) throw(); + bool initialiseNewSpaceToZero = false); /** Increases the block's size only if it's smaller than a given size. @@ -4949,20 +4948,19 @@ public: @see setSize */ void ensureSize (const size_t minimumSize, - const bool initialiseNewSpaceToZero = false) throw(); + bool initialiseNewSpaceToZero = false); /** Fills the entire memory block with a repeated byte value. This is handy for clearing a block of memory to zero. */ - void fillWith (const uint8 valueToUse) throw(); + void fillWith (uint8 valueToUse) throw(); /** Adds another block of data to the end of this one. This block's size will be increased accordingly. */ - void append (const void* data, - const size_t numBytes) throw(); + void append (const void* data, size_t numBytes); /** Exchanges the contents of this and another memory block. No actual copying is required for this, so it's very fast. @@ -4998,11 +4996,11 @@ public: If the range specified goes beyond the size of the block, it will be clipped. */ - void removeSection (size_t startByte, size_t numBytesToRemove) throw(); + void removeSection (size_t startByte, size_t numBytesToRemove); /** Attempts to parse the contents of the block as a zero-terminated string of 8-bit characters in the system's default encoding. */ - const String toString() const throw(); + const String toString() const; /** Parses a string of hexadecimal numbers and writes this data into the memory block. @@ -5011,7 +5009,7 @@ public: @see String::toHexString() */ - void loadFromHexString (const String& sourceHexString) throw(); + void loadFromHexString (const String& sourceHexString); /** Sets a number of bits in the memory block, treating it as a long binary sequence. */ void setBitRange (size_t bitRangeStart, @@ -5029,7 +5027,7 @@ public: @see fromBase64Encoding */ - const String toBase64Encoding() const throw(); + const String toBase64Encoding() const; /** Takes a string of encoded characters and turns it into binary data. @@ -5038,7 +5036,7 @@ public: @see toBase64Encoding */ - bool fromBase64Encoding (const String& encodedString) throw(); + bool fromBase64Encoding (const String& encodedString); juce_UseDebuggingNewOperator @@ -7024,26 +7022,33 @@ public: { if (indexToChange >= 0) { - ScopedPointer toDelete; - const ScopedLockType lock (getLock()); + ObjectClass* toDelete = 0; - if (indexToChange < numUsed) { - if (deleteOldElement) + const ScopedLockType lock (getLock()); + + if (indexToChange < numUsed) { - toDelete = data.elements [indexToChange]; + if (deleteOldElement) + { + toDelete = data.elements [indexToChange]; - if (toDelete == newObject) - toDelete = 0; + if (toDelete == newObject) + toDelete = 0; + } + + data.elements [indexToChange] = const_cast (newObject); } + else + { + data.ensureAllocatedSize (numUsed + 1); + data.elements [numUsed++] = const_cast (newObject); + } + } - data.elements [indexToChange] = const_cast (newObject); - } - else - { - data.ensureAllocatedSize (numUsed + 1); - data.elements [numUsed++] = const_cast (newObject); - } + delete toDelete; // don't want to use a ScopedPointer here because if the + // object has a private destructor, both OwnedArray and + // ScopedPointer would need to be friend classes.. } } @@ -7203,25 +7208,32 @@ public: void remove (const int indexToRemove, const bool deleteObject = true) { - ScopedPointer toDelete; - const ScopedLockType lock (getLock()); + ObjectClass* toDelete = 0; - if (((unsigned int) indexToRemove) < (unsigned int) numUsed) { - ObjectClass** const e = data.elements + indexToRemove; + const ScopedLockType lock (getLock()); - if (deleteObject) - toDelete = *e; + if (((unsigned int) indexToRemove) < (unsigned int) numUsed) + { + ObjectClass** const e = data.elements + indexToRemove; - --numUsed; - const int numToShift = numUsed - indexToRemove; + if (deleteObject) + toDelete = *e; - if (numToShift > 0) - memmove (e, e + 1, numToShift * sizeof (ObjectClass*)); + --numUsed; + const int numToShift = numUsed - indexToRemove; - if ((numUsed << 1) < data.numAllocated) - minimiseStorageOverheads(); + if (numToShift > 0) + memmove (e, e + 1, numToShift * sizeof (ObjectClass*)); + } } + + delete toDelete; // don't want to use a ScopedPointer here because if the + // object has a private destructor, both OwnedArray and + // ScopedPointer would need to be friend classes.. + + if ((numUsed << 1) < data.numAllocated) + minimiseStorageOverheads(); } /** Removes and returns an object from the array without deleting it. diff --git a/src/audio/synthesisers/juce_Sampler.h b/src/audio/synthesisers/juce_Sampler.h index f6f7768c7d..86445f0769 100644 --- a/src/audio/synthesisers/juce_Sampler.h +++ b/src/audio/synthesisers/juce_Sampler.h @@ -27,6 +27,7 @@ #define __JUCE_SAMPLER_JUCEHEADER__ #include "../../containers/juce_BigInteger.h" +#include "../../containers/juce_ScopedPointer.h" #include "juce_Synthesiser.h" diff --git a/src/containers/juce_MemoryBlock.cpp b/src/containers/juce_MemoryBlock.cpp index 273aa5540e..ebfb704543 100644 --- a/src/containers/juce_MemoryBlock.cpp +++ b/src/containers/juce_MemoryBlock.cpp @@ -36,8 +36,7 @@ MemoryBlock::MemoryBlock() throw() { } -MemoryBlock::MemoryBlock (const size_t initialSize, - const bool initialiseToZero) throw() +MemoryBlock::MemoryBlock (const size_t initialSize, const bool initialiseToZero) { if (initialSize > 0) { @@ -50,7 +49,7 @@ MemoryBlock::MemoryBlock (const size_t initialSize, } } -MemoryBlock::MemoryBlock (const MemoryBlock& other) throw() +MemoryBlock::MemoryBlock (const MemoryBlock& other) : size (other.size) { if (size > 0) @@ -61,8 +60,7 @@ MemoryBlock::MemoryBlock (const MemoryBlock& other) throw() } } -MemoryBlock::MemoryBlock (const void* const dataToInitialiseFrom, - const size_t sizeInBytes) throw() +MemoryBlock::MemoryBlock (const void* const dataToInitialiseFrom, const size_t sizeInBytes) : size (jmax ((size_t) 0, sizeInBytes)) { jassert (sizeInBytes >= 0); @@ -84,7 +82,7 @@ MemoryBlock::~MemoryBlock() throw() jassert (size == 0 || data != 0); // non-zero size but no data allocated? } -MemoryBlock& MemoryBlock::operator= (const MemoryBlock& other) throw() +MemoryBlock& MemoryBlock::operator= (const MemoryBlock& other) { if (this != &other) { @@ -114,8 +112,7 @@ bool MemoryBlock::matches (const void* dataToCompare, size_t dataSize) const thr //============================================================================== // this will resize the block to this size -void MemoryBlock::setSize (const size_t newSize, - const bool initialiseToZero) throw() +void MemoryBlock::setSize (const size_t newSize, const bool initialiseToZero) { if (size != newSize) { @@ -143,8 +140,7 @@ void MemoryBlock::setSize (const size_t newSize, } } -void MemoryBlock::ensureSize (const size_t minimumSize, - const bool initialiseToZero) throw() +void MemoryBlock::ensureSize (const size_t minimumSize, const bool initialiseToZero) { if (size < minimumSize) setSize (minimumSize, initialiseToZero); @@ -162,8 +158,7 @@ void MemoryBlock::fillWith (const uint8 value) throw() memset (data, (int) value, size); } -void MemoryBlock::append (const void* const srcData, - const size_t numBytes) throw() +void MemoryBlock::append (const void* const srcData, const size_t numBytes) { if (numBytes > 0) { @@ -215,7 +210,7 @@ void MemoryBlock::copyTo (void* const dst, int offset, size_t num) const throw() memcpy (d, data + offset, num); } -void MemoryBlock::removeSection (size_t startByte, size_t numBytesToRemove) throw() +void MemoryBlock::removeSection (size_t startByte, size_t numBytesToRemove) { if (startByte < 0) { @@ -237,7 +232,7 @@ void MemoryBlock::removeSection (size_t startByte, size_t numBytesToRemove) thro } } -const String MemoryBlock::toString() const throw() +const String MemoryBlock::toString() const { return String (static_cast (getData()), size); } @@ -291,7 +286,7 @@ void MemoryBlock::setBitRange (const size_t bitRangeStart, size_t numBits, int b } //============================================================================== -void MemoryBlock::loadFromHexString (const String& hex) throw() +void MemoryBlock::loadFromHexString (const String& hex) { ensureSize (hex.length() >> 1); char* dest = data; @@ -339,7 +334,7 @@ void MemoryBlock::loadFromHexString (const String& hex) throw() //============================================================================== const char* const MemoryBlock::encodingTable = ".ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+"; -const String MemoryBlock::toBase64Encoding() const throw() +const String MemoryBlock::toBase64Encoding() const { const size_t numChars = ((size << 3) + 5) / 6; @@ -359,7 +354,7 @@ const String MemoryBlock::toBase64Encoding() const throw() return destString; } -bool MemoryBlock::fromBase64Encoding (const String& s) throw() +bool MemoryBlock::fromBase64Encoding (const String& s) { const int startPos = s.indexOfChar ('.') + 1; diff --git a/src/containers/juce_MemoryBlock.h b/src/containers/juce_MemoryBlock.h index 10390880a9..ab3184160b 100644 --- a/src/containers/juce_MemoryBlock.h +++ b/src/containers/juce_MemoryBlock.h @@ -48,18 +48,17 @@ public: @param initialiseToZero whether to clear the memory or just leave it uninitialised */ MemoryBlock (const size_t initialSize, - const bool initialiseToZero = false) throw(); + bool initialiseToZero = false); /** Creates a copy of another memory block. */ - MemoryBlock (const MemoryBlock& other) throw(); + MemoryBlock (const MemoryBlock& other); /** Creates a memory block using a copy of a block of data. @param dataToInitialiseFrom some data to copy into this block @param sizeInBytes how much space to use */ - MemoryBlock (const void* dataToInitialiseFrom, - const size_t sizeInBytes) throw(); + MemoryBlock (const void* dataToInitialiseFrom, size_t sizeInBytes); /** Destructor. */ ~MemoryBlock() throw(); @@ -68,7 +67,7 @@ public: This block will be resized and copied to exactly match the other one. */ - MemoryBlock& operator= (const MemoryBlock& other) throw(); + MemoryBlock& operator= (const MemoryBlock& other); //============================================================================== /** Compares two memory blocks. @@ -120,7 +119,7 @@ public: @see ensureSize */ void setSize (const size_t newSize, - const bool initialiseNewSpaceToZero = false) throw(); + bool initialiseNewSpaceToZero = false); /** Increases the block's size only if it's smaller than a given size. @@ -132,21 +131,20 @@ public: @see setSize */ void ensureSize (const size_t minimumSize, - const bool initialiseNewSpaceToZero = false) throw(); + bool initialiseNewSpaceToZero = false); //============================================================================== /** Fills the entire memory block with a repeated byte value. This is handy for clearing a block of memory to zero. */ - void fillWith (const uint8 valueToUse) throw(); + void fillWith (uint8 valueToUse) throw(); /** Adds another block of data to the end of this one. This block's size will be increased accordingly. */ - void append (const void* data, - const size_t numBytes) throw(); + void append (const void* data, size_t numBytes); /** Exchanges the contents of this and another memory block. No actual copying is required for this, so it's very fast. @@ -183,12 +181,12 @@ public: If the range specified goes beyond the size of the block, it will be clipped. */ - void removeSection (size_t startByte, size_t numBytesToRemove) throw(); + void removeSection (size_t startByte, size_t numBytesToRemove); //============================================================================== /** Attempts to parse the contents of the block as a zero-terminated string of 8-bit characters in the system's default encoding. */ - const String toString() const throw(); + const String toString() const; //============================================================================== /** Parses a string of hexadecimal numbers and writes this data into the memory block. @@ -198,7 +196,7 @@ public: @see String::toHexString() */ - void loadFromHexString (const String& sourceHexString) throw(); + void loadFromHexString (const String& sourceHexString); //============================================================================== /** Sets a number of bits in the memory block, treating it as a long binary sequence. */ @@ -218,7 +216,7 @@ public: @see fromBase64Encoding */ - const String toBase64Encoding() const throw(); + const String toBase64Encoding() const; /** Takes a string of encoded characters and turns it into binary data. @@ -227,7 +225,7 @@ public: @see toBase64Encoding */ - bool fromBase64Encoding (const String& encodedString) throw(); + bool fromBase64Encoding (const String& encodedString); //============================================================================== diff --git a/src/containers/juce_OwnedArray.h b/src/containers/juce_OwnedArray.h index d98c013626..ed295312b6 100644 --- a/src/containers/juce_OwnedArray.h +++ b/src/containers/juce_OwnedArray.h @@ -29,7 +29,6 @@ #include "juce_ArrayAllocationBase.h" #include "juce_ElementComparator.h" #include "../threads/juce_CriticalSection.h" -#include "../containers/juce_ScopedPointer.h" //============================================================================== @@ -301,26 +300,33 @@ public: { if (indexToChange >= 0) { - ScopedPointer toDelete; - const ScopedLockType lock (getLock()); + ObjectClass* toDelete = 0; - if (indexToChange < numUsed) { - if (deleteOldElement) + const ScopedLockType lock (getLock()); + + if (indexToChange < numUsed) { - toDelete = data.elements [indexToChange]; + if (deleteOldElement) + { + toDelete = data.elements [indexToChange]; - if (toDelete == newObject) - toDelete = 0; + if (toDelete == newObject) + toDelete = 0; + } + + data.elements [indexToChange] = const_cast (newObject); } + else + { + data.ensureAllocatedSize (numUsed + 1); + data.elements [numUsed++] = const_cast (newObject); + } + } - data.elements [indexToChange] = const_cast (newObject); - } - else - { - data.ensureAllocatedSize (numUsed + 1); - data.elements [numUsed++] = const_cast (newObject); - } + delete toDelete; // don't want to use a ScopedPointer here because if the + // object has a private destructor, both OwnedArray and + // ScopedPointer would need to be friend classes.. } } @@ -481,25 +487,32 @@ public: void remove (const int indexToRemove, const bool deleteObject = true) { - ScopedPointer toDelete; - const ScopedLockType lock (getLock()); + ObjectClass* toDelete = 0; - if (((unsigned int) indexToRemove) < (unsigned int) numUsed) { - ObjectClass** const e = data.elements + indexToRemove; + const ScopedLockType lock (getLock()); - if (deleteObject) - toDelete = *e; + if (((unsigned int) indexToRemove) < (unsigned int) numUsed) + { + ObjectClass** const e = data.elements + indexToRemove; - --numUsed; - const int numToShift = numUsed - indexToRemove; + if (deleteObject) + toDelete = *e; - if (numToShift > 0) - memmove (e, e + 1, numToShift * sizeof (ObjectClass*)); + --numUsed; + const int numToShift = numUsed - indexToRemove; - if ((numUsed << 1) < data.numAllocated) - minimiseStorageOverheads(); + if (numToShift > 0) + memmove (e, e + 1, numToShift * sizeof (ObjectClass*)); + } } + + delete toDelete; // don't want to use a ScopedPointer here because if the + // object has a private destructor, both OwnedArray and + // ScopedPointer would need to be friend classes.. + + if ((numUsed << 1) < data.numAllocated) + minimiseStorageOverheads(); } /** Removes and returns an object from the array without deleting it. diff --git a/src/gui/graphics/contexts/juce_Graphics.h b/src/gui/graphics/contexts/juce_Graphics.h index cd5cf34e01..9cb08becb8 100644 --- a/src/gui/graphics/contexts/juce_Graphics.h +++ b/src/gui/graphics/contexts/juce_Graphics.h @@ -33,6 +33,7 @@ #include "../colour/juce_Colours.h" #include "../colour/juce_ColourGradient.h" #include "juce_RectanglePlacement.h" +#include "../../../containers/juce_ScopedPointer.h" class LowLevelGraphicsContext; class Image; class FillType;