Loading...
Searching...
No Matches
juce_SharedResourcePointer.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 <typename SharedObjectType>
class SharedResourcePointer
{
public:
SharedResourcePointer() = default;
SharedResourcePointer (const SharedResourcePointer&) = default;
SharedResourcePointer (SharedResourcePointer&&) noexcept = default;
~SharedResourcePointer() = default;
operator SharedObjectType*() const noexcept { return sharedObject.get(); }
SharedObjectType& get() const noexcept { return *sharedObject; }
SharedObjectType& getObject() const noexcept { return *sharedObject; }
SharedObjectType* operator->() const noexcept { return sharedObject.get(); }
SharedObjectType& operator*() const noexcept { return *sharedObject; }
#ifndef DOXYGEN
[[deprecated ("If you are relying on this function please inform the JUCE team as we are planing on removing this in a subsequent release")]]
int getReferenceCount() const noexcept { return (int) sharedObject.use_count(); }
#endif
static std::optional<SharedResourcePointer> getSharedObjectWithoutCreating()
{
if (auto sharedPtr = weak().lock())
return SharedResourcePointer { std::move (sharedPtr) };
return {};
}
private:
explicit SharedResourcePointer (std::shared_ptr<SharedObjectType>&& other) noexcept
: sharedObject (std::move (other))
{
jassert (sharedObject != nullptr);
}
class Weak
{
public:
std::shared_ptr<SharedObjectType> lock()
{
const SpinLock::ScopedLockType lock { mutex };
return ptr.lock();
}
std::shared_ptr<SharedObjectType> lockOrCreate()
{
const SpinLock::ScopedLockType lock { mutex };
if (auto locked = ptr.lock())
return locked;
const std::shared_ptr<SharedObjectType> shared (new SharedObjectType());
ptr = shared;
return shared;
}
private:
SpinLock mutex;
std::weak_ptr<SharedObjectType> ptr;
};
inline static Weak& weak()
{
static Weak weak;
return weak;
}
std::shared_ptr<SharedObjectType> sharedObject = weak().lockOrCreate();
// There's no need to assign to a SharedResourcePointer because every
// instance of the class is exactly the same!
SharedResourcePointer& operator= (const SharedResourcePointer&) = delete;
SharedResourcePointer& operator= (SharedResourcePointer&&) noexcept = delete;
JUCE_LEAK_DETECTOR (SharedResourcePointer)
};
} // namespace juce
juce::SharedResourcePointer::SharedResourcePointer
SharedResourcePointer(SharedResourcePointer &&) noexcept=default
juce::SharedResourcePointer::getObject
SharedObjectType & getObject() const noexcept
Definition juce_SharedResourcePointer.h:111
juce::SharedResourcePointer::SharedResourcePointer
SharedResourcePointer()=default
juce::SharedResourcePointer::SharedResourcePointer
SharedResourcePointer(const SharedResourcePointer &)=default
juce::SharedResourcePointer::operator*
SharedObjectType & operator*() const noexcept
Definition juce_SharedResourcePointer.h:117
juce::SharedResourcePointer::get
SharedObjectType & get() const noexcept
Definition juce_SharedResourcePointer.h:108
juce::SharedResourcePointer::getSharedObjectWithoutCreating
static std::optional< SharedResourcePointer > getSharedObjectWithoutCreating()
Definition juce_SharedResourcePointer.h:125
juce::SharedResourcePointer::operator->
SharedObjectType * operator->() const noexcept
Definition juce_SharedResourcePointer.h:114
juce::SpinLock::ScopedLockType
GenericScopedLock< SpinLock > ScopedLockType
Definition juce_SpinLock.h:73