diff --git a/juce_amalgamated.cpp b/juce_amalgamated.cpp index eaec33c9aa..6816d42c5f 100644 --- a/juce_amalgamated.cpp +++ b/juce_amalgamated.cpp @@ -435,45 +435,42 @@ #pragma warning (pop) #endif -template +template class ComSmartPtr { public: - ComSmartPtr() throw() : p (0) {} - ComSmartPtr (T* const p_) : p (p_) { if (p_ != 0) p_->AddRef(); } - ComSmartPtr (const ComSmartPtr& p_) : p (p_.p) { if (p != 0) p->AddRef(); } - ~ComSmartPtr() { if (p != 0) p->Release(); } + ComSmartPtr() throw() : p (0) {} + ComSmartPtr (ComClass* const p_) : p (p_) { if (p_ != 0) p_->AddRef(); } + ComSmartPtr (const ComSmartPtr& p_) : p (p_.p) { if (p != 0) p->AddRef(); } + ~ComSmartPtr() { if (p != 0) p->Release(); } - operator T*() const throw() { return p; } - T& operator*() const throw() { return *p; } - T** operator&() throw() { return &p; } - T* operator->() const throw() { return p; } + operator ComClass*() const throw() { return p; } + ComClass& operator*() const throw() { return *p; } + ComClass** operator&() throw() { return &p; } + ComClass* operator->() const throw() { return p; } - T* operator= (T* const newP) + ComClass* operator= (ComClass* const newP) { - if (newP != 0) - newP->AddRef(); - - if (p != 0) - p->Release(); - + if (newP != 0) newP->AddRef(); + if (p != 0) p->Release(); p = newP; return newP; } - T* operator= (const ComSmartPtr& newP) { return operator= (newP.p); } + ComClass* operator= (const ComSmartPtr& newP) { return operator= (newP.p); } - HRESULT CoCreateInstance (REFCLSID rclsid, DWORD dwClsContext) + HRESULT CoCreateInstance (REFCLSID rclsid, DWORD dwClsContext = CLSCTX_INPROC_SERVER) { #ifndef __MINGW32__ operator= (0); - return ::CoCreateInstance (rclsid, 0, dwClsContext, __uuidof(T), (void**) &p); + return ::CoCreateInstance (rclsid, 0, dwClsContext, __uuidof (ComClass), (void**) &p); #else return S_FALSE; #endif } - T* p; +private: + ComClass* p; }; template @@ -12155,6 +12152,12 @@ const String& StringArray::operator[] (const int index) const throw() return String::empty; } +String& StringArray::getReference (const int index) throw() +{ + jassert (((unsigned int) index) < (unsigned int) strings.size()); + return strings.getReference (index); +} + void StringArray::add (const String& newString) { strings.add (newString); @@ -46807,7 +46810,7 @@ public: { if (isEnabled() && owner.getModel() != 0 && ! (e.mouseWasClicked() || isDragging)) { - const SparseSet selectedRows (owner.getSelectedRows()); + const SparseSet selectedRows (owner.getSelectedRows()); if (selectedRows.size() > 0) { @@ -47097,7 +47100,7 @@ void ListBox::updateContent() if (selected [selected.size() - 1] >= totalItems) { - selected.removeRange (totalItems, std::numeric_limits::max() - totalItems); + selected.removeRange (Range (totalItems, std::numeric_limits::max())); lastRowSelected = getSelectedRow (0); selectionChanged = true; } @@ -47132,7 +47135,7 @@ void ListBox::selectRowInternal (const int row, if (deselectOthersFirst) selected.clear(); - selected.addRange (row, 1); + selected.addRange (Range (row, row + 1)); if (getHeight() == 0 || getWidth() == 0) dontScroll = true; @@ -47181,7 +47184,7 @@ void ListBox::deselectRow (const int row) { if (selected.contains (row)) { - selected.removeRange (row, 1); + selected.removeRange (Range (row, row + 1)); if (row == lastRowSelected) lastRowSelected = getSelectedRow (0); @@ -47195,7 +47198,7 @@ void ListBox::setSelectedRows (const SparseSet& setOfRowsToBeSelected, const bool sendNotificationEventToModel) { selected = setOfRowsToBeSelected; - selected.removeRange (totalItems, std::numeric_limits::max() - totalItems); + selected.removeRange (Range (totalItems, std::numeric_limits::max())); if (! isRowSelected (lastRowSelected)) lastRowSelected = getSelectedRow (0); @@ -47219,10 +47222,10 @@ void ListBox::selectRangeOfRows (int firstRow, int lastRow) firstRow = jlimit (0, jmax (0, numRows), firstRow); lastRow = jlimit (0, jmax (0, numRows), lastRow); - selected.addRange (jmin (firstRow, lastRow), - abs (firstRow - lastRow) + 1); + selected.addRange (Range (jmin (firstRow, lastRow), + jmax (firstRow, lastRow) + 1)); - selected.removeRange (lastRow, 1); + selected.removeRange (Range (lastRow, lastRow + 1)); } selectRowInternal (lastRow, false, false, true); @@ -50202,7 +50205,7 @@ public: { if (isEnabled() && owner.getModel() != 0 && ! (e.mouseWasClicked() || isDragging)) { - const SparseSet selectedRows (owner.getSelectedRows()); + const SparseSet selectedRows (owner.getSelectedRows()); if (selectedRows.size() > 0) { @@ -90835,7 +90838,7 @@ void Image::createSolidAreaMask (RectangleList& result, const float alphaThresho if (hasAlphaChannel()) { const uint8 threshold = (uint8) jlimit (0, 255, roundToInt (alphaThreshold * 255.0f)); - SparseSet pixelsOnRow; + SparseSet pixelsOnRow; const BitmapData srcData (*this, 0, 0, getWidth(), getHeight()); @@ -90849,7 +90852,7 @@ void Image::createSolidAreaMask (RectangleList& result, const float alphaThresho for (int x = 0; x < imageWidth; ++x) { if (((const PixelARGB*) lineData)->getAlpha() >= threshold) - pixelsOnRow.addRange (x, 1); + pixelsOnRow.addRange (Range (x, x + 1)); lineData += srcData.pixelStride; } @@ -90859,7 +90862,7 @@ void Image::createSolidAreaMask (RectangleList& result, const float alphaThresho for (int x = 0; x < imageWidth; ++x) { if (*lineData >= threshold) - pixelsOnRow.addRange (x, 1); + pixelsOnRow.addRange (Range (x, x + 1)); lineData += srcData.pixelStride; } @@ -90867,10 +90870,8 @@ void Image::createSolidAreaMask (RectangleList& result, const float alphaThresho for (int i = 0; i < pixelsOnRow.getNumRanges(); ++i) { - int x, w; - - if (pixelsOnRow.getRange (i, x, w)) - result.add (Rectangle (x, y, w, 1)); + const Range range (pixelsOnRow.getRange (i)); + result.add (Rectangle (range.getStart(), y, range.getLength(), 1)); } result.consolidate(); @@ -212224,7 +212225,7 @@ const File File::getLinkedTarget() const return result; ComSmartPtr shellLink; - if (SUCCEEDED (shellLink.CoCreateInstance (CLSID_ShellLink, CLSCTX_INPROC_SERVER))) + if (SUCCEEDED (shellLink.CoCreateInstance (CLSID_ShellLink))) { ComSmartPtr persistFile; if (SUCCEEDED (shellLink->QueryInterface (IID_IPersistFile, (LPVOID*) &persistFile))) @@ -226110,7 +226111,7 @@ private: bool createDevices() { ComSmartPtr enumerator; - if (! OK (enumerator.CoCreateInstance (__uuidof (MMDeviceEnumerator), CLSCTX_INPROC_SERVER))) + if (! OK (enumerator.CoCreateInstance (__uuidof (MMDeviceEnumerator)))) return false; ComSmartPtr deviceCollection; @@ -226170,7 +226171,7 @@ public: inputDeviceIds.clear(); ComSmartPtr enumerator; - if (! OK (enumerator.CoCreateInstance (__uuidof (MMDeviceEnumerator), CLSCTX_INPROC_SERVER))) + if (! OK (enumerator.CoCreateInstance (__uuidof (MMDeviceEnumerator)))) return; const String defaultRenderer = getDefaultEndpoint (enumerator, false); @@ -226352,7 +226353,7 @@ public: activeImage (0), loadingImage (0) { - HRESULT hr = graphBuilder.CoCreateInstance (CLSID_FilterGraph, CLSCTX_INPROC); + HRESULT hr = graphBuilder.CoCreateInstance (CLSID_FilterGraph); if (FAILED (hr)) return; @@ -226383,7 +226384,7 @@ public: if (FAILED (hr)) return; - hr = smartTee.CoCreateInstance (CLSID_SmartTee, CLSCTX_INPROC_SERVER); + hr = smartTee.CoCreateInstance (CLSID_SmartTee); if (FAILED (hr)) return; @@ -226395,7 +226396,7 @@ public: return; ComSmartPtr sampleGrabberBase; - hr = sampleGrabberBase.CoCreateInstance (CLSID_SampleGrabber, CLSCTX_INPROC_SERVER); + hr = sampleGrabberBase.CoCreateInstance (CLSID_SampleGrabber); if (FAILED (hr)) return; @@ -226434,7 +226435,7 @@ public: height = pVih->bmiHeader.biHeight; ComSmartPtr nullFilter; - hr = nullFilter.CoCreateInstance (CLSID_NullRenderer, CLSCTX_INPROC_SERVER); + hr = nullFilter.CoCreateInstance (CLSID_NullRenderer); hr = graphBuilder->AddFilter (nullFilter, _T("Null Renderer")); if (connectFilters (sampleGrabberBase, nullFilter) @@ -226562,7 +226563,7 @@ public: firstRecordedTime = Time(); recordNextFrameTime = true; - HRESULT hr = asfWriter.CoCreateInstance (CLSID_WMAsfWriter, CLSCTX_INPROC_SERVER); + HRESULT hr = asfWriter.CoCreateInstance (CLSID_WMAsfWriter); if (SUCCEEDED (hr)) { @@ -227029,7 +227030,7 @@ static ComSmartPtr enumerateCameras (StringArray* const names, ComSmartPtr result; ComSmartPtr pDevEnum; - HRESULT hr = pDevEnum.CoCreateInstance (CLSID_SystemDeviceEnum, CLSCTX_INPROC); + HRESULT hr = pDevEnum.CoCreateInstance (CLSID_SystemDeviceEnum); if (SUCCEEDED (hr)) { @@ -227100,7 +227101,7 @@ CameraDevice* CameraDevice::openDevice (int index, int maxWidth, int maxHeight) { ComSmartPtr captureGraphBuilder; - HRESULT hr = captureGraphBuilder.CoCreateInstance (CLSID_CaptureGraphBuilder2, CLSCTX_INPROC); + HRESULT hr = captureGraphBuilder.CoCreateInstance (CLSID_CaptureGraphBuilder2); if (SUCCEEDED (hr)) { diff --git a/juce_amalgamated.h b/juce_amalgamated.h index 8fddfdb549..42545efd05 100644 --- a/juce_amalgamated.h +++ b/juce_amalgamated.h @@ -1868,7 +1868,7 @@ private: public: static int compareElements (ParameterType first, ParameterType second) { - return (first < second) ? -1 : ((first < second) ? 1 : 0); + return (first < second) ? -1 : ((second < first) ? 1 : 0); } }; @@ -3997,6 +3997,8 @@ public: const String& operator[] (int index) const throw(); + String& getReference (int index) throw(); + bool contains (const String& stringToLookFor, bool ignoreCase = false) const; @@ -4886,7 +4888,7 @@ public: void setStart (const ValueType newStart) throw() { start = newStart; - if (newStart > end) + if (end < newStart) end = newStart; } @@ -4956,7 +4958,7 @@ public: bool contains (const ValueType position) const throw() { - return position >= start && position < end; + return start <= position && position < end; } ValueType clipValue (const ValueType value) const throw() @@ -4966,7 +4968,7 @@ public: bool intersects (const Range& other) const throw() { - return other.start < end && other.end > start; + return other.start < end && start < other.end; } const Range getIntersectionWith (const Range& other) const throw() @@ -4984,7 +4986,7 @@ public: const Range constrainRange (const Range& rangeToConstrain) const throw() { const ValueType otherLen = rangeToConstrain.getLength(); - return otherLen >= getLength() + return getLength() <= otherLen ? *this : rangeToConstrain.movedToStartAt (jlimit (start, end - otherLen, rangeToConstrain.getStart())); } @@ -5802,20 +5804,20 @@ class SparseSet { public: - SparseSet() throw() + SparseSet() { } - SparseSet (const SparseSet& other) throw() + SparseSet (const SparseSet& other) : values (other.values) { } - ~SparseSet() throw() + ~SparseSet() { } - void clear() throw() + void clear() { values.clear(); } @@ -5825,43 +5827,37 @@ public: return values.size() == 0; } - Type size() const throw() + Type size() const { - Type num = 0; + Type total (0); for (int i = 0; i < values.size(); i += 2) - num += values[i + 1] - values[i]; + total += values.getUnchecked (i + 1) - values.getUnchecked (i); - return num; + return total; } - Type operator[] (int index) const throw() + Type operator[] (Type index) const { for (int i = 0; i < values.size(); i += 2) { - const Type s = values.getUnchecked(i); - const Type e = values.getUnchecked(i + 1); + const Type start (values.getUnchecked (i)); + const Type len (values.getUnchecked (i + 1) - start); - if (index < e - s) - return s + index; + if (index < len) + return start + index; - index -= e - s; + index -= len; } - return (Type) 0; + return Type (0); } - bool contains (const Type valueToLookFor) const throw() + bool contains (const Type valueToLookFor) const { - bool on = false; - for (int i = 0; i < values.size(); ++i) - { - if (values.getUnchecked(i) > valueToLookFor) - return on; - - on = ! on; - } + if (valueToLookFor < values.getUnchecked(i)) + return (i & 1) != 0; return false; } @@ -5871,67 +5867,64 @@ public: return values.size() >> 1; } - bool getRange (const int rangeIndex, - Type& startValue, - Type& numValues) const throw() + const Range getRange (const int rangeIndex) const { if (((unsigned int) rangeIndex) < (unsigned int) getNumRanges()) - { - startValue = values [rangeIndex << 1]; - numValues = values [(rangeIndex << 1) + 1] - startValue; - - return true; - } - - return false; + return Range (values.getUnchecked (rangeIndex << 1), + values.getUnchecked ((rangeIndex << 1) + 1)); + else + return Range(); } - bool getTotalRange (Type& lowestValue, - Type& highestValue) const throw() + const Range getTotalRange() const { if (values.size() > 0) { - lowestValue = values.getUnchecked (0); - highestValue = values.getUnchecked (values.size() - 1); - return true; + jassert ((values.size() & 1) == 0); + return Range (values.getUnchecked (0), + values.getUnchecked (values.size() - 1)); } - return false; + return Range(); } - void addRange (const Type firstValue, - const Type numValuesToAdd) throw() + void addRange (const Range& range) { - jassert (numValuesToAdd >= 0); - - if (numValuesToAdd > 0) + jassert (range.getLength() >= 0); + if (range.getLength() > 0) { - removeRange (firstValue, numValuesToAdd); + removeRange (range); - values.addUsingDefaultSort (firstValue); - values.addUsingDefaultSort (firstValue + numValuesToAdd); + values.addUsingDefaultSort (range.getStart()); + values.addUsingDefaultSort (range.getEnd()); simplify(); } + + String s; + for (int i = 0; i < values.size(); ++i) + s << values[i] << " "; + DBG (s); } - void removeRange (const Type firstValue, - const Type numValuesToRemove) throw() + void removeRange (const Range& rangeToRemove) { - jassert (numValuesToRemove >= 0); + jassert (rangeToRemove.getLength() >= 0); - if (numValuesToRemove >= 0 - && firstValue < values.getLast()) + if (rangeToRemove.getLength() > 0 + && values.size() > 0 + && rangeToRemove.getStart() < values.getUnchecked (values.size() - 1) + && values.getUnchecked(0) < rangeToRemove.getEnd()) { - const bool onAtStart = contains (firstValue - 1); - const Type lastValue = firstValue + jmin (numValuesToRemove, values.getLast() - firstValue); + const bool onAtStart = contains (rangeToRemove.getStart() - 1); + const Type lastValue (jmin (rangeToRemove.getEnd(), values.getLast())); const bool onAtEnd = contains (lastValue); for (int i = values.size(); --i >= 0;) { if (values.getUnchecked(i) <= lastValue) { - while (values.getUnchecked(i) >= firstValue) + while (values.getUnchecked(i) >= rangeToRemove.getStart()) { values.remove (i); @@ -5943,55 +5936,38 @@ public: } } - if (onAtStart) - values.addUsingDefaultSort (firstValue); - - if (onAtEnd) - values.addUsingDefaultSort (lastValue); + if (onAtStart) values.addUsingDefaultSort (rangeToRemove.getStart()); + if (onAtEnd) values.addUsingDefaultSort (lastValue); simplify(); } } - void invertRange (const Type firstValue, - const Type numValues) + void invertRange (const Range& range) { SparseSet newItems; - newItems.addRange (firstValue, numValues); + newItems.addRange (range); int i; for (i = getNumRanges(); --i >= 0;) - { - const int start = values [i << 1]; - const int end = values [(i << 1) + 1]; + newItems.removeRange (getRange (i)); - newItems.removeRange (start, end); - } - - removeRange (firstValue, numValues); + removeRange (range); for (i = newItems.getNumRanges(); --i >= 0;) - { - const int start = newItems.values [i << 1]; - const int end = newItems.values [(i << 1) + 1]; - - addRange (start, end); - } + addRange (newItems.getRange(i)); } - bool overlapsRange (const Type firstValue, - const Type numValues) throw() + bool overlapsRange (const Range& range) { - jassert (numValues >= 0); - - if (numValues > 0) + if (range.getLength() > 0) { for (int i = getNumRanges(); --i >= 0;) { - if (firstValue >= values.getUnchecked ((i << 1) + 1)) + if (values.getUnchecked ((i << 1) + 1) <= range.getStart()) return false; - if (firstValue + numValues > values.getUnchecked (i << 1)) + if (values.getUnchecked (i << 1) < range.getEnd()) return true; } } @@ -5999,20 +5975,17 @@ public: return false; } - bool containsRange (const Type firstValue, - const Type numValues) throw() + bool containsRange (const Range& range) { - jassert (numValues >= 0); - - if (numValues > 0) + if (range.getLength() > 0) { for (int i = getNumRanges(); --i >= 0;) { - if (firstValue >= values.getUnchecked ((i << 1) + 1)) + if (values.getUnchecked ((i << 1) + 1) <= range.getStart()) return false; - if (firstValue >= values.getUnchecked (i << 1) - && firstValue + numValues <= values.getUnchecked ((i << 1) + 1)) + if (values.getUnchecked (i << 1) <= range.getStart() + && range.getEnd() <= values.getUnchecked ((i << 1) + 1)) return true; } } @@ -6036,13 +6009,13 @@ private: // alternating start/end values of ranges of values that are present. Array values; - void simplify() throw() + void simplify() { jassert ((values.size() & 1) == 0); for (int i = values.size(); --i > 0;) if (values.getUnchecked(i) == values.getUnchecked (i - 1)) - values.removeRange (i - 1, 2); + values.removeRange (--i, 2); } }; diff --git a/src/containers/juce_ElementComparator.h b/src/containers/juce_ElementComparator.h index 930fef71fe..8e28417c81 100644 --- a/src/containers/juce_ElementComparator.h +++ b/src/containers/juce_ElementComparator.h @@ -279,7 +279,7 @@ private: public: static int compareElements (ParameterType first, ParameterType second) { - return (first < second) ? -1 : ((first < second) ? 1 : 0); + return (first < second) ? -1 : ((second < first) ? 1 : 0); } }; diff --git a/src/containers/juce_Range.h b/src/containers/juce_Range.h index b69aa9d00e..3fe17d6990 100644 --- a/src/containers/juce_Range.h +++ b/src/containers/juce_Range.h @@ -104,7 +104,7 @@ public: void setStart (const ValueType newStart) throw() { start = newStart; - if (newStart > end) + if (end < newStart) end = newStart; } @@ -204,7 +204,7 @@ public: /** Returns true if the given position lies inside this range. */ bool contains (const ValueType position) const throw() { - return position >= start && position < end; + return start <= position && position < end; } /** Returns the nearest value to the one supplied, which lies within the range. */ @@ -216,7 +216,7 @@ public: /** Returns true if the given range intersects this one. */ bool intersects (const Range& other) const throw() { - return other.start < end && other.end > start; + return other.start < end && start < other.end; } /** Returns the range that is the intersection of the two ranges, or an empty range @@ -247,7 +247,7 @@ public: const Range constrainRange (const Range& rangeToConstrain) const throw() { const ValueType otherLen = rangeToConstrain.getLength(); - return otherLen >= getLength() + return getLength() <= otherLen ? *this : rangeToConstrain.movedToStartAt (jlimit (start, end - otherLen, rangeToConstrain.getStart())); } diff --git a/src/containers/juce_SparseSet.h b/src/containers/juce_SparseSet.h index 01536dbf61..42ef1601f8 100644 --- a/src/containers/juce_SparseSet.h +++ b/src/containers/juce_SparseSet.h @@ -27,6 +27,7 @@ #define __JUCE_SPARSESET_JUCEHEADER__ #include "juce_ArrayAllocationBase.h" +#include "juce_Range.h" #include "../threads/juce_CriticalSection.h" @@ -47,24 +48,24 @@ class SparseSet public: //============================================================================== /** Creates a new empty set. */ - SparseSet() throw() + SparseSet() { } /** Creates a copy of another SparseSet. */ - SparseSet (const SparseSet& other) throw() + SparseSet (const SparseSet& other) : values (other.values) { } /** Destructor. */ - ~SparseSet() throw() + ~SparseSet() { } //============================================================================== /** Clears the set. */ - void clear() throw() + void clear() { values.clear(); } @@ -84,14 +85,14 @@ public: are a lot of items in the set. Use isEmpty() for a quick test of whether there are any items. */ - Type size() const throw() + Type size() const { - Type num = 0; + Type total (0); for (int i = 0; i < values.size(); i += 2) - num += values[i + 1] - values[i]; + total += values.getUnchecked (i + 1) - values.getUnchecked (i); - return num; + return total; } /** Returns one of the values in the set. @@ -99,41 +100,34 @@ public: @param index the index of the value to retrieve, in the range 0 to (size() - 1). @returns the value at this index, or 0 if it's out-of-range */ - Type operator[] (int index) const throw() + Type operator[] (Type index) const { for (int i = 0; i < values.size(); i += 2) { - const Type s = values.getUnchecked(i); - const Type e = values.getUnchecked(i + 1); + const Type start (values.getUnchecked (i)); + const Type len (values.getUnchecked (i + 1) - start); - if (index < e - s) - return s + index; + if (index < len) + return start + index; - index -= e - s; + index -= len; } - return (Type) 0; + return Type (0); } /** Checks whether a particular value is in the set. */ - bool contains (const Type valueToLookFor) const throw() + bool contains (const Type valueToLookFor) const { - bool on = false; - for (int i = 0; i < values.size(); ++i) - { - if (values.getUnchecked(i) > valueToLookFor) - return on; - - on = ! on; - } + if (valueToLookFor < values.getUnchecked(i)) + return (i & 1) != 0; return false; } //============================================================================== /** Returns the number of contiguous blocks of values. - @see getRange */ int getNumRanges() const throw() @@ -142,94 +136,78 @@ public: } /** Returns one of the contiguous ranges of values stored. - @param rangeIndex the index of the range to look up, between 0 and (getNumRanges() - 1) - @param startValue on return, the value at the start of the range - @param numValues on return, the number of values in the range - @see getTotalRange */ - bool getRange (const int rangeIndex, - Type& startValue, - Type& numValues) const throw() + const Range getRange (const int rangeIndex) const { if (((unsigned int) rangeIndex) < (unsigned int) getNumRanges()) - { - startValue = values [rangeIndex << 1]; - numValues = values [(rangeIndex << 1) + 1] - startValue; - - return true; - } - - return false; + return Range (values.getUnchecked (rangeIndex << 1), + values.getUnchecked ((rangeIndex << 1) + 1)); + else + return Range(); } - /** Returns the lowest and highest values in the set. - + /** Returns the range between the lowest and highest values in the set. @see getRange */ - bool getTotalRange (Type& lowestValue, - Type& highestValue) const throw() + const Range getTotalRange() const { if (values.size() > 0) { - lowestValue = values.getUnchecked (0); - highestValue = values.getUnchecked (values.size() - 1); - return true; + jassert ((values.size() & 1) == 0); + return Range (values.getUnchecked (0), + values.getUnchecked (values.size() - 1)); } - return false; + return Range(); } //============================================================================== /** Adds a range of contiguous values to the set. - - e.g. addRange (10, 4) will add (10, 11, 12, 13) to the set. - - @param firstValue the start of the range of values to add - @param numValuesToAdd how many values to add + e.g. addRange (Range \ (10, 14)) will add (10, 11, 12, 13) to the set. */ - void addRange (const Type firstValue, - const Type numValuesToAdd) throw() + void addRange (const Range& range) { - jassert (numValuesToAdd >= 0); - - if (numValuesToAdd > 0) + jassert (range.getLength() >= 0); + if (range.getLength() > 0) { - removeRange (firstValue, numValuesToAdd); + removeRange (range); - values.addUsingDefaultSort (firstValue); - values.addUsingDefaultSort (firstValue + numValuesToAdd); + values.addUsingDefaultSort (range.getStart()); + values.addUsingDefaultSort (range.getEnd()); simplify(); } + + String s; + for (int i = 0; i < values.size(); ++i) + s << values[i] << " "; + DBG (s); } /** Removes a range of values from the set. - - e.g. removeRange (10, 4) will remove (10, 11, 12, 13) from the set. - - @param firstValue the start of the range of values to remove - @param numValuesToRemove how many values to remove + e.g. removeRange (Range\ (10, 14)) will remove (10, 11, 12, 13) from the set. */ - void removeRange (const Type firstValue, - const Type numValuesToRemove) throw() + void removeRange (const Range& rangeToRemove) { - jassert (numValuesToRemove >= 0); + jassert (rangeToRemove.getLength() >= 0); - if (numValuesToRemove >= 0 - && firstValue < values.getLast()) + if (rangeToRemove.getLength() > 0 + && values.size() > 0 + && rangeToRemove.getStart() < values.getUnchecked (values.size() - 1) + && values.getUnchecked(0) < rangeToRemove.getEnd()) { - const bool onAtStart = contains (firstValue - 1); - const Type lastValue = firstValue + jmin (numValuesToRemove, values.getLast() - firstValue); + const bool onAtStart = contains (rangeToRemove.getStart() - 1); + const Type lastValue (jmin (rangeToRemove.getEnd(), values.getLast())); const bool onAtEnd = contains (lastValue); for (int i = values.size(); --i >= 0;) { if (values.getUnchecked(i) <= lastValue) { - while (values.getUnchecked(i) >= firstValue) + while (values.getUnchecked(i) >= rangeToRemove.getStart()) { values.remove (i); @@ -241,57 +219,40 @@ public: } } - if (onAtStart) - values.addUsingDefaultSort (firstValue); - - if (onAtEnd) - values.addUsingDefaultSort (lastValue); + if (onAtStart) values.addUsingDefaultSort (rangeToRemove.getStart()); + if (onAtEnd) values.addUsingDefaultSort (lastValue); simplify(); } } /** Does an XOR of the values in a given range. */ - void invertRange (const Type firstValue, - const Type numValues) + void invertRange (const Range& range) { SparseSet newItems; - newItems.addRange (firstValue, numValues); + newItems.addRange (range); int i; for (i = getNumRanges(); --i >= 0;) - { - const int start = values [i << 1]; - const int end = values [(i << 1) + 1]; + newItems.removeRange (getRange (i)); - newItems.removeRange (start, end); - } - - removeRange (firstValue, numValues); + removeRange (range); for (i = newItems.getNumRanges(); --i >= 0;) - { - const int start = newItems.values [i << 1]; - const int end = newItems.values [(i << 1) + 1]; - - addRange (start, end); - } + addRange (newItems.getRange(i)); } - /** Checks whether any part of a given range overlaps any part of this one. */ - bool overlapsRange (const Type firstValue, - const Type numValues) throw() + /** Checks whether any part of a given range overlaps any part of this set. */ + bool overlapsRange (const Range& range) { - jassert (numValues >= 0); - - if (numValues > 0) + if (range.getLength() > 0) { for (int i = getNumRanges(); --i >= 0;) { - if (firstValue >= values.getUnchecked ((i << 1) + 1)) + if (values.getUnchecked ((i << 1) + 1) <= range.getStart()) return false; - if (firstValue + numValues > values.getUnchecked (i << 1)) + if (values.getUnchecked (i << 1) < range.getEnd()) return true; } } @@ -300,20 +261,17 @@ public: } /** Checks whether the whole of a given range is contained within this one. */ - bool containsRange (const Type firstValue, - const Type numValues) throw() + bool containsRange (const Range& range) { - jassert (numValues >= 0); - - if (numValues > 0) + if (range.getLength() > 0) { for (int i = getNumRanges(); --i >= 0;) { - if (firstValue >= values.getUnchecked ((i << 1) + 1)) + if (values.getUnchecked ((i << 1) + 1) <= range.getStart()) return false; - if (firstValue >= values.getUnchecked (i << 1) - && firstValue + numValues <= values.getUnchecked ((i << 1) + 1)) + if (values.getUnchecked (i << 1) <= range.getStart() + && range.getEnd() <= values.getUnchecked ((i << 1) + 1)) return true; } } @@ -339,13 +297,13 @@ private: // alternating start/end values of ranges of values that are present. Array values; - void simplify() throw() + void simplify() { jassert ((values.size() & 1) == 0); for (int i = values.size(); --i > 0;) if (values.getUnchecked(i) == values.getUnchecked (i - 1)) - values.removeRange (i - 1, 2); + values.removeRange (--i, 2); } }; diff --git a/src/gui/components/controls/juce_ListBox.cpp b/src/gui/components/controls/juce_ListBox.cpp index 745b0419f7..05a01bca33 100644 --- a/src/gui/components/controls/juce_ListBox.cpp +++ b/src/gui/components/controls/juce_ListBox.cpp @@ -128,7 +128,7 @@ public: { if (isEnabled() && owner.getModel() != 0 && ! (e.mouseWasClicked() || isDragging)) { - const SparseSet selectedRows (owner.getSelectedRows()); + const SparseSet selectedRows (owner.getSelectedRows()); if (selectedRows.size() > 0) { @@ -426,7 +426,7 @@ void ListBox::updateContent() if (selected [selected.size() - 1] >= totalItems) { - selected.removeRange (totalItems, std::numeric_limits::max() - totalItems); + selected.removeRange (Range (totalItems, std::numeric_limits::max())); lastRowSelected = getSelectedRow (0); selectionChanged = true; } @@ -462,7 +462,7 @@ void ListBox::selectRowInternal (const int row, if (deselectOthersFirst) selected.clear(); - selected.addRange (row, 1); + selected.addRange (Range (row, row + 1)); if (getHeight() == 0 || getWidth() == 0) dontScroll = true; @@ -511,7 +511,7 @@ void ListBox::deselectRow (const int row) { if (selected.contains (row)) { - selected.removeRange (row, 1); + selected.removeRange (Range (row, row + 1)); if (row == lastRowSelected) lastRowSelected = getSelectedRow (0); @@ -525,7 +525,7 @@ void ListBox::setSelectedRows (const SparseSet& setOfRowsToBeSelected, const bool sendNotificationEventToModel) { selected = setOfRowsToBeSelected; - selected.removeRange (totalItems, std::numeric_limits::max() - totalItems); + selected.removeRange (Range (totalItems, std::numeric_limits::max())); if (! isRowSelected (lastRowSelected)) lastRowSelected = getSelectedRow (0); @@ -549,10 +549,10 @@ void ListBox::selectRangeOfRows (int firstRow, int lastRow) firstRow = jlimit (0, jmax (0, numRows), firstRow); lastRow = jlimit (0, jmax (0, numRows), lastRow); - selected.addRange (jmin (firstRow, lastRow), - abs (firstRow - lastRow) + 1); + selected.addRange (Range (jmin (firstRow, lastRow), + jmax (firstRow, lastRow) + 1)); - selected.removeRange (lastRow, 1); + selected.removeRange (Range (lastRow, lastRow + 1)); } selectRowInternal (lastRow, false, false, true); diff --git a/src/gui/components/controls/juce_TableListBox.cpp b/src/gui/components/controls/juce_TableListBox.cpp index 454721f068..7486ea4a55 100644 --- a/src/gui/components/controls/juce_TableListBox.cpp +++ b/src/gui/components/controls/juce_TableListBox.cpp @@ -191,7 +191,7 @@ public: { if (isEnabled() && owner.getModel() != 0 && ! (e.mouseWasClicked() || isDragging)) { - const SparseSet selectedRows (owner.getSelectedRows()); + const SparseSet selectedRows (owner.getSelectedRows()); if (selectedRows.size() > 0) { diff --git a/src/gui/graphics/imaging/juce_Image.cpp b/src/gui/graphics/imaging/juce_Image.cpp index e4b275d427..aa77798f9c 100644 --- a/src/gui/graphics/imaging/juce_Image.cpp +++ b/src/gui/graphics/imaging/juce_Image.cpp @@ -372,7 +372,7 @@ void Image::createSolidAreaMask (RectangleList& result, const float alphaThresho if (hasAlphaChannel()) { const uint8 threshold = (uint8) jlimit (0, 255, roundToInt (alphaThreshold * 255.0f)); - SparseSet pixelsOnRow; + SparseSet pixelsOnRow; const BitmapData srcData (*this, 0, 0, getWidth(), getHeight()); @@ -386,7 +386,7 @@ void Image::createSolidAreaMask (RectangleList& result, const float alphaThresho for (int x = 0; x < imageWidth; ++x) { if (((const PixelARGB*) lineData)->getAlpha() >= threshold) - pixelsOnRow.addRange (x, 1); + pixelsOnRow.addRange (Range (x, x + 1)); lineData += srcData.pixelStride; } @@ -396,7 +396,7 @@ void Image::createSolidAreaMask (RectangleList& result, const float alphaThresho for (int x = 0; x < imageWidth; ++x) { if (*lineData >= threshold) - pixelsOnRow.addRange (x, 1); + pixelsOnRow.addRange (Range (x, x + 1)); lineData += srcData.pixelStride; } @@ -404,10 +404,8 @@ void Image::createSolidAreaMask (RectangleList& result, const float alphaThresho for (int i = 0; i < pixelsOnRow.getNumRanges(); ++i) { - int x, w; - - if (pixelsOnRow.getRange (i, x, w)) - result.add (Rectangle (x, y, w, 1)); + const Range range (pixelsOnRow.getRange (i)); + result.add (Rectangle (range.getStart(), y, range.getLength(), 1)); } result.consolidate(); diff --git a/src/native/windows/juce_win32_CameraDevice.cpp b/src/native/windows/juce_win32_CameraDevice.cpp index 9376e20cb5..1eba9366f3 100644 --- a/src/native/windows/juce_win32_CameraDevice.cpp +++ b/src/native/windows/juce_win32_CameraDevice.cpp @@ -49,7 +49,7 @@ public: activeImage (0), loadingImage (0) { - HRESULT hr = graphBuilder.CoCreateInstance (CLSID_FilterGraph, CLSCTX_INPROC); + HRESULT hr = graphBuilder.CoCreateInstance (CLSID_FilterGraph); if (FAILED (hr)) return; @@ -80,7 +80,7 @@ public: if (FAILED (hr)) return; - hr = smartTee.CoCreateInstance (CLSID_SmartTee, CLSCTX_INPROC_SERVER); + hr = smartTee.CoCreateInstance (CLSID_SmartTee); if (FAILED (hr)) return; @@ -92,7 +92,7 @@ public: return; ComSmartPtr sampleGrabberBase; - hr = sampleGrabberBase.CoCreateInstance (CLSID_SampleGrabber, CLSCTX_INPROC_SERVER); + hr = sampleGrabberBase.CoCreateInstance (CLSID_SampleGrabber); if (FAILED (hr)) return; @@ -131,7 +131,7 @@ public: height = pVih->bmiHeader.biHeight; ComSmartPtr nullFilter; - hr = nullFilter.CoCreateInstance (CLSID_NullRenderer, CLSCTX_INPROC_SERVER); + hr = nullFilter.CoCreateInstance (CLSID_NullRenderer); hr = graphBuilder->AddFilter (nullFilter, _T("Null Renderer")); if (connectFilters (sampleGrabberBase, nullFilter) @@ -259,7 +259,7 @@ public: firstRecordedTime = Time(); recordNextFrameTime = true; - HRESULT hr = asfWriter.CoCreateInstance (CLSID_WMAsfWriter, CLSCTX_INPROC_SERVER); + HRESULT hr = asfWriter.CoCreateInstance (CLSID_WMAsfWriter); if (SUCCEEDED (hr)) { @@ -737,7 +737,7 @@ static ComSmartPtr enumerateCameras (StringArray* const names, ComSmartPtr result; ComSmartPtr pDevEnum; - HRESULT hr = pDevEnum.CoCreateInstance (CLSID_SystemDeviceEnum, CLSCTX_INPROC); + HRESULT hr = pDevEnum.CoCreateInstance (CLSID_SystemDeviceEnum); if (SUCCEEDED (hr)) { @@ -808,7 +808,7 @@ CameraDevice* CameraDevice::openDevice (int index, int maxWidth, int maxHeight) { ComSmartPtr captureGraphBuilder; - HRESULT hr = captureGraphBuilder.CoCreateInstance (CLSID_CaptureGraphBuilder2, CLSCTX_INPROC); + HRESULT hr = captureGraphBuilder.CoCreateInstance (CLSID_CaptureGraphBuilder2); if (SUCCEEDED (hr)) { diff --git a/src/native/windows/juce_win32_Files.cpp b/src/native/windows/juce_win32_Files.cpp index 1de88c9182..f7701a7a3f 100644 --- a/src/native/windows/juce_win32_Files.cpp +++ b/src/native/windows/juce_win32_Files.cpp @@ -502,7 +502,7 @@ const File File::getLinkedTarget() const return result; ComSmartPtr shellLink; - if (SUCCEEDED (shellLink.CoCreateInstance (CLSID_ShellLink, CLSCTX_INPROC_SERVER))) + if (SUCCEEDED (shellLink.CoCreateInstance (CLSID_ShellLink))) { ComSmartPtr persistFile; if (SUCCEEDED (shellLink->QueryInterface (IID_IPersistFile, (LPVOID*) &persistFile))) diff --git a/src/native/windows/juce_win32_NativeIncludes.h b/src/native/windows/juce_win32_NativeIncludes.h index db1abf18ec..d513fb5806 100644 --- a/src/native/windows/juce_win32_NativeIncludes.h +++ b/src/native/windows/juce_win32_NativeIncludes.h @@ -187,48 +187,47 @@ /** A simple COM smart pointer. Avoids having to include ATL just to get one of these. */ -template +template class ComSmartPtr { public: - ComSmartPtr() throw() : p (0) {} - ComSmartPtr (T* const p_) : p (p_) { if (p_ != 0) p_->AddRef(); } - ComSmartPtr (const ComSmartPtr& p_) : p (p_.p) { if (p != 0) p->AddRef(); } - ~ComSmartPtr() { if (p != 0) p->Release(); } + ComSmartPtr() throw() : p (0) {} + ComSmartPtr (ComClass* const p_) : p (p_) { if (p_ != 0) p_->AddRef(); } + ComSmartPtr (const ComSmartPtr& p_) : p (p_.p) { if (p != 0) p->AddRef(); } + ~ComSmartPtr() { if (p != 0) p->Release(); } - operator T*() const throw() { return p; } - T& operator*() const throw() { return *p; } - T** operator&() throw() { return &p; } - T* operator->() const throw() { return p; } + operator ComClass*() const throw() { return p; } + ComClass& operator*() const throw() { return *p; } + ComClass** operator&() throw() { return &p; } + ComClass* operator->() const throw() { return p; } - T* operator= (T* const newP) + ComClass* operator= (ComClass* const newP) { - if (newP != 0) - newP->AddRef(); - - if (p != 0) - p->Release(); - + if (newP != 0) newP->AddRef(); + if (p != 0) p->Release(); p = newP; return newP; } - T* operator= (const ComSmartPtr& newP) { return operator= (newP.p); } + ComClass* operator= (const ComSmartPtr& newP) { return operator= (newP.p); } - HRESULT CoCreateInstance (REFCLSID rclsid, DWORD dwClsContext) + HRESULT CoCreateInstance (REFCLSID rclsid, DWORD dwClsContext = CLSCTX_INPROC_SERVER) { #ifndef __MINGW32__ operator= (0); - return ::CoCreateInstance (rclsid, 0, dwClsContext, __uuidof(T), (void**) &p); + return ::CoCreateInstance (rclsid, 0, dwClsContext, __uuidof (ComClass), (void**) &p); #else return S_FALSE; #endif } - T* p; +private: + ComClass* p; }; //============================================================================== +/** Handy base class for writing COM objects, providing ref-counting and a basic QueryInterface method. +*/ template class ComBaseClassHelper : public ComClass { diff --git a/src/native/windows/juce_win32_WASAPI.cpp b/src/native/windows/juce_win32_WASAPI.cpp index c4b6fa54fa..b35bc5ede3 100644 --- a/src/native/windows/juce_win32_WASAPI.cpp +++ b/src/native/windows/juce_win32_WASAPI.cpp @@ -912,7 +912,7 @@ private: bool createDevices() { ComSmartPtr enumerator; - if (! OK (enumerator.CoCreateInstance (__uuidof (MMDeviceEnumerator), CLSCTX_INPROC_SERVER))) + if (! OK (enumerator.CoCreateInstance (__uuidof (MMDeviceEnumerator)))) return false; ComSmartPtr deviceCollection; @@ -976,7 +976,7 @@ public: inputDeviceIds.clear(); ComSmartPtr enumerator; - if (! OK (enumerator.CoCreateInstance (__uuidof (MMDeviceEnumerator), CLSCTX_INPROC_SERVER))) + if (! OK (enumerator.CoCreateInstance (__uuidof (MMDeviceEnumerator)))) return; const String defaultRenderer = getDefaultEndpoint (enumerator, false); diff --git a/src/text/juce_StringArray.cpp b/src/text/juce_StringArray.cpp index c27a6f5995..917497c09b 100644 --- a/src/text/juce_StringArray.cpp +++ b/src/text/juce_StringArray.cpp @@ -116,6 +116,12 @@ const String& StringArray::operator[] (const int index) const throw() return String::empty; } +String& StringArray::getReference (const int index) throw() +{ + jassert (((unsigned int) index) < (unsigned int) strings.size()); + return strings.getReference (index); +} + void StringArray::add (const String& newString) { strings.add (newString); diff --git a/src/text/juce_StringArray.h b/src/text/juce_StringArray.h index 5509babb68..e835b8b46e 100644 --- a/src/text/juce_StringArray.h +++ b/src/text/juce_StringArray.h @@ -108,6 +108,12 @@ public: */ const String& operator[] (int index) const throw(); + /** Returns a reference to one of the strings in the array. + This lets you modify a string in-place in the array, but you must be sure that + the index is in-range. + */ + String& getReference (int index) throw(); + /** Searches for a string in the array. The comparison will be case-insensitive if the ignoreCase parameter is true.