Loading...
Searching...
No Matches
juce_OptionalScopedPointer.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
template <class ObjectType>
class OptionalScopedPointer
{
public:
//==============================================================================
OptionalScopedPointer() = default;
OptionalScopedPointer (ObjectType* objectToHold, bool takeOwnership)
: object (objectToHold),
shouldDelete (takeOwnership)
{
}
OptionalScopedPointer (OptionalScopedPointer&& other) noexcept
: object (std::move (other.object)),
shouldDelete (std::move (other.shouldDelete))
{
}
explicit OptionalScopedPointer (std::unique_ptr<ObjectType>&& ptr) noexcept
: OptionalScopedPointer (ptr.release(), true)
{
}
explicit OptionalScopedPointer (ObjectType& ref) noexcept
: OptionalScopedPointer (std::addressof (ref), false)
{
}
OptionalScopedPointer& operator= (OptionalScopedPointer&& other) noexcept
{
swapWith (other);
other.reset();
return *this;
}
~OptionalScopedPointer() noexcept
{
reset();
}
//==============================================================================
operator ObjectType*() const noexcept { return object.get(); }
ObjectType* get() const noexcept { return object.get(); }
ObjectType& operator*() const noexcept { return *object; }
ObjectType* operator->() const noexcept { return object.get(); }
//==============================================================================
ObjectType* release() noexcept { return object.release(); }
void reset() noexcept
{
if (! shouldDelete)
object.release();
else
object.reset();
}
void clear() { reset(); }
void set (ObjectType* newObject, bool takeOwnership)
{
if (object.get() != newObject)
{
reset();
object.reset (newObject);
}
shouldDelete = takeOwnership;
}
void setOwned (ObjectType* newObject)
{
set (newObject, true);
}
void setNonOwned (ObjectType* newObject)
{
set (newObject, false);
}
bool willDeleteObject() const noexcept { return shouldDelete; }
//==============================================================================
void swapWith (OptionalScopedPointer<ObjectType>& other) noexcept
{
std::swap (other.object, object);
std::swap (other.shouldDelete, shouldDelete);
}
private:
//==============================================================================
std::unique_ptr<ObjectType> object;
bool shouldDelete = false;
};
} // namespace juce
juce::OptionalScopedPointer::OptionalScopedPointer
OptionalScopedPointer(ObjectType *objectToHold, bool takeOwnership)
Definition juce_OptionalScopedPointer.h:51
juce::OptionalScopedPointer::setNonOwned
void setNonOwned(ObjectType *newObject)
Definition juce_OptionalScopedPointer.h:165
juce::OptionalScopedPointer::set
void set(ObjectType *newObject, bool takeOwnership)
Definition juce_OptionalScopedPointer.h:147
juce::OptionalScopedPointer::operator=
OptionalScopedPointer & operator=(OptionalScopedPointer &&other) noexcept
Definition juce_OptionalScopedPointer.h:91
juce::OptionalScopedPointer::clear
void clear()
Definition juce_OptionalScopedPointer.h:138
juce::OptionalScopedPointer::reset
void reset() noexcept
Definition juce_OptionalScopedPointer.h:129
juce::OptionalScopedPointer::OptionalScopedPointer
OptionalScopedPointer(std::unique_ptr< ObjectType > &&ptr) noexcept
Definition juce_OptionalScopedPointer.h:72
juce::OptionalScopedPointer::OptionalScopedPointer
OptionalScopedPointer()=default
juce::OptionalScopedPointer::get
ObjectType * get() const noexcept
Definition juce_OptionalScopedPointer.h:112
juce::OptionalScopedPointer::OptionalScopedPointer
OptionalScopedPointer(ObjectType &ref) noexcept
Definition juce_OptionalScopedPointer.h:78
juce::OptionalScopedPointer::operator*
ObjectType & operator*() const noexcept
Definition juce_OptionalScopedPointer.h:115
juce::OptionalScopedPointer::release
ObjectType * release() noexcept
Definition juce_OptionalScopedPointer.h:124
juce::OptionalScopedPointer::setOwned
void setOwned(ObjectType *newObject)
Definition juce_OptionalScopedPointer.h:159
juce::OptionalScopedPointer::swapWith
void swapWith(OptionalScopedPointer< ObjectType > &other) noexcept
Definition juce_OptionalScopedPointer.h:179
juce::OptionalScopedPointer::willDeleteObject
bool willDeleteObject() const noexcept
Definition juce_OptionalScopedPointer.h:173
juce::OptionalScopedPointer::operator->
ObjectType * operator->() const noexcept
Definition juce_OptionalScopedPointer.h:118
juce::OptionalScopedPointer::OptionalScopedPointer
OptionalScopedPointer(OptionalScopedPointer &&other) noexcept
Definition juce_OptionalScopedPointer.h:65
juce::OptionalScopedPointer::~OptionalScopedPointer
~OptionalScopedPointer() noexcept
Definition juce_OptionalScopedPointer.h:102