Browse Source

Handy new functions: createCopyIfNotNull(), and ScopedPointer::createCopy()

tags/2021-05-28
jules 14 years ago
parent
commit
6b7496c893
6 changed files with 15 additions and 6 deletions
  1. +1
    -1
      modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.cpp
  2. +5
    -0
      modules/juce_core/memory/juce_Memory.h
  3. +5
    -0
      modules/juce_core/memory/juce_ScopedPointer.h
  4. +1
    -1
      modules/juce_data_structures/values/juce_ValueTree.cpp
  5. +2
    -2
      modules/juce_graphics/colour/juce_FillType.cpp
  6. +1
    -2
      modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.cpp

+ 1
- 1
modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.cpp View File

@@ -248,7 +248,7 @@ void AudioDeviceManager::insertDefaultDeviceNames (AudioDeviceSetup& setup) cons
XmlElement* AudioDeviceManager::createStateXml() const
{
return lastExplicitSettings != nullptr ? new XmlElement (*lastExplicitSettings) : nullptr;
return lastExplicitSettings.createCopy();
}
//==============================================================================


+ 5
- 0
modules/juce_core/memory/juce_Memory.h View File

@@ -75,6 +75,11 @@ inline Type* addBytesToPointer (Type* pointer, int bytes) noexcept { return (Ty
template <typename Type1, typename Type2>
inline int getAddressDifference (Type1* pointer1, Type2* pointer2) noexcept { return (int) (((const char*) pointer1) - (const char*) pointer2); }
/** If a pointer is non-null, this returns a new copy of the object that it points to, or safely returns
nullptr if the pointer is null.
*/
template <class Type>
inline Type* createCopyIfNotNull (Type* pointer) { return pointer != nullptr ? new Type (*pointer) : nullptr; }
//==============================================================================
/* In a Windows DLL build, we'll expose some malloc/free functions that live inside the DLL, and use these for


+ 5
- 0
modules/juce_core/memory/juce_ScopedPointer.h View File

@@ -169,6 +169,11 @@ public:
std::swap (object, other.object);
}
/** If the pointer is non-null, this will attempt to return a new copy of the object that is pointed to.
If the pointer is null, this will safely return a nullptr.
*/
inline ObjectType* createCopy() const { return createCopyIfNotNull (object); }
private:
//==============================================================================
ObjectType* object;


+ 1
- 1
modules/juce_data_structures/values/juce_ValueTree.cpp View File

@@ -708,7 +708,7 @@ bool ValueTree::isEquivalentTo (const ValueTree& other) const
ValueTree ValueTree::createCopy() const
{
return ValueTree (object != nullptr ? new SharedObject (*object) : nullptr);
return ValueTree (createCopyIfNotNull (object.getObject()));
}
bool ValueTree::hasType (const Identifier& typeName) const


+ 2
- 2
modules/juce_graphics/colour/juce_FillType.cpp View File

@@ -48,7 +48,7 @@ FillType::FillType (const Image& image_, const AffineTransform& transform_) noex
FillType::FillType (const FillType& other)
: colour (other.colour),
gradient (other.gradient != nullptr ? new ColourGradient (*other.gradient) : nullptr),
gradient (other.gradient.createCopy()),
image (other.image),
transform (other.transform)
{
@@ -59,7 +59,7 @@ FillType& FillType::operator= (const FillType& other)
if (this != &other)
{
colour = other.colour;
gradient = (other.gradient != nullptr ? new ColourGradient (*other.gradient) : nullptr);
gradient = other.gradient.createCopy();
image = other.image;
transform = other.transform;
}


+ 1
- 2
modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.cpp View File

@@ -1873,8 +1873,7 @@ public:
: clip (other.clip), transform (other.transform), font (other.font),
fillType (other.fillType), interpolationQuality (other.interpolationQuality),
state (other.state), transparencyLayerAlpha (other.transparencyLayerAlpha),
transparencyLayer (other.transparencyLayer),
previousTarget (other.previousTarget != nullptr ? new OpenGLTarget (*other.previousTarget) : nullptr)
transparencyLayer (other.transparencyLayer), previousTarget (other.previousTarget.createCopy())
{
}


Loading…
Cancel
Save