1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

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

This commit is contained in:
jules 2011-11-20 21:55:35 +00:00
parent 2ae2f8da30
commit 6b7496c893
6 changed files with 15 additions and 6 deletions

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();
}
//==============================================================================

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

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;

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

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;
}

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())
{
}