mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-07-12 23:24:41 +00:00
working weak ref
WeakRefPtr now actually acts like a weak reference backend of weakRefPtr is now a weak_ptr so once the main shared_ptr is freed, they all get freed.
This commit is contained in:
parent
d688f1cfde
commit
ca4517c076
4 changed files with 56 additions and 82 deletions
|
|
@ -190,12 +190,6 @@ SoundAsset::SoundAsset()
|
||||||
SoundAsset::~SoundAsset()
|
SoundAsset::~SoundAsset()
|
||||||
{
|
{
|
||||||
|
|
||||||
for (U32 i = 0; i < SFXPlayList::NUM_SLOTS; i++)
|
|
||||||
{
|
|
||||||
if(mSFXProfile[i].isProperlyAdded() && !mSFXProfile[i].isDeleted())
|
|
||||||
mSFXProfile[i].unregisterObject();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mPlaylist.isProperlyAdded() && !mPlaylist.isDeleted())
|
if (mPlaylist.isProperlyAdded() && !mPlaylist.isDeleted())
|
||||||
mPlaylist.unregisterObject();
|
mPlaylist.unregisterObject();
|
||||||
}
|
}
|
||||||
|
|
@ -404,22 +398,17 @@ U32 SoundAsset::load()
|
||||||
{
|
{
|
||||||
Con::errorf("SoundAsset::initializeAsset: Attempted to load file %s but it was not valid!", mSoundFile[i]);
|
Con::errorf("SoundAsset::initializeAsset: Attempted to load file %s but it was not valid!", mSoundFile[i]);
|
||||||
mLoadedState = BadFileReference;
|
mLoadedState = BadFileReference;
|
||||||
mSFXProfile[i].setDescription(NULL);
|
|
||||||
mSFXProfile[i].setSoundFileName(StringTable->insert(StringTable->EmptyString()));
|
|
||||||
mSFXProfile[i].setPreload(false);
|
|
||||||
return mLoadedState;
|
return mLoadedState;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
SFXProfile* trackProfile = new SFXProfile();
|
mSFXProfile[i] = new SFXProfile;
|
||||||
trackProfile->setDescription(&mProfileDesc);
|
mSFXProfile[i]->setDescription(&mProfileDesc);
|
||||||
trackProfile->setSoundFileName(mSoundPath[i]);
|
mSFXProfile[i]->setSoundFileName(mSoundPath[i]);
|
||||||
trackProfile->setPreload(mPreload);
|
mSFXProfile[i]->setPreload(mPreload);
|
||||||
|
mSFXProfile[i]->registerObject(String::ToString("%s_profile_track%d", getAssetName()).c_str());
|
||||||
|
|
||||||
mSFXProfile[i] = *trackProfile;
|
mPlaylist.mSlots.mTrack[i] = mSFXProfile[i];
|
||||||
mSFXProfile[i].registerObject(String::ToString("%s_profile_track%d", getAssetName()).c_str());
|
|
||||||
|
|
||||||
mPlaylist.mSlots.mTrack[i] = trackProfile;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -436,18 +425,15 @@ U32 SoundAsset::load()
|
||||||
{
|
{
|
||||||
Con::errorf("SoundAsset::initializeAsset: Attempted to load file %s but it was not valid!", mSoundFile[0]);
|
Con::errorf("SoundAsset::initializeAsset: Attempted to load file %s but it was not valid!", mSoundFile[0]);
|
||||||
mLoadedState = BadFileReference;
|
mLoadedState = BadFileReference;
|
||||||
mSFXProfile[0].setDescription(NULL);
|
|
||||||
mSFXProfile[0].setSoundFileName(StringTable->insert(StringTable->EmptyString()));
|
|
||||||
mSFXProfile[0].setPreload(false);
|
|
||||||
return mLoadedState;
|
return mLoadedState;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mSFXProfile[0].setDescription(&mProfileDesc);
|
mSFXProfile[0] = new SFXProfile;
|
||||||
mSFXProfile[0].setSoundFileName(mSoundPath[0]);
|
mSFXProfile[0]->setDescription(&mProfileDesc);
|
||||||
mSFXProfile[0].setPreload(mPreload);
|
mSFXProfile[0]->setSoundFileName(mSoundPath[0]);
|
||||||
|
mSFXProfile[0]->setPreload(mPreload);
|
||||||
mSFXProfile[0].registerObject(String::ToString("%s_profile", getAssetName()).c_str());
|
mSFXProfile[0]->registerObject(String::ToString("%s_profile", getAssetName()).c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -87,9 +87,9 @@ class SoundAsset : public AssetBase
|
||||||
typedef AssetPtr<SoundAsset> ConcreteAssetPtr;
|
typedef AssetPtr<SoundAsset> ConcreteAssetPtr;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
StringTableEntry mSoundFile[SFXPlayList::SFXPlaylistSettings::NUM_SLOTS];
|
StringTableEntry mSoundFile[SFXPlayList::SFXPlaylistSettings::NUM_SLOTS];
|
||||||
StringTableEntry mSoundPath[SFXPlayList::SFXPlaylistSettings::NUM_SLOTS];
|
StringTableEntry mSoundPath[SFXPlayList::SFXPlaylistSettings::NUM_SLOTS];
|
||||||
SFXProfile mSFXProfile[SFXPlayList::SFXPlaylistSettings::NUM_SLOTS];
|
SimObjectPtr<SFXProfile> mSFXProfile[SFXPlayList::SFXPlaylistSettings::NUM_SLOTS];
|
||||||
|
|
||||||
SFXDescription mProfileDesc;
|
SFXDescription mProfileDesc;
|
||||||
SFXPlayList mPlaylist;
|
SFXPlayList mPlaylist;
|
||||||
|
|
@ -150,7 +150,7 @@ public:
|
||||||
void copyTo(SimObject* object) override;
|
void copyTo(SimObject* object) override;
|
||||||
|
|
||||||
//SFXResource* getSound() { return mSoundResource; }
|
//SFXResource* getSound() { return mSoundResource; }
|
||||||
Resource<SFXResource> getSoundResource(const U32 slotId = 0) { load(); return mSFXProfile[slotId].getResource(); }
|
Resource<SFXResource> getSoundResource(const U32 slotId = 0) { load(); return mSFXProfile[slotId]->getResource(); }
|
||||||
|
|
||||||
/// Declare Console Object.
|
/// Declare Console Object.
|
||||||
DECLARE_CONOBJECT(SoundAsset);
|
DECLARE_CONOBJECT(SoundAsset);
|
||||||
|
|
@ -158,9 +158,9 @@ public:
|
||||||
static bool _setSoundFile(void* object, const char* index, const char* data);
|
static bool _setSoundFile(void* object, const char* index, const char* data);
|
||||||
U32 load() override;
|
U32 load() override;
|
||||||
inline StringTableEntry getSoundPath(const U32 slotId = 0) const { return mSoundPath[slotId]; };
|
inline StringTableEntry getSoundPath(const U32 slotId = 0) const { return mSoundPath[slotId]; };
|
||||||
SFXProfile* getSfxProfile(const U32 slotId = 0) { return &mSFXProfile[slotId]; }
|
SFXProfile* getSfxProfile(const U32 slotId = 0) { return mSFXProfile[slotId]; }
|
||||||
SFXPlayList* getSfxPlaylist() { return &mPlaylist; }
|
SFXPlayList* getSfxPlaylist() { return &mPlaylist; }
|
||||||
SFXTrack* getSFXTrack() { load(); return mIsPlaylist ? dynamic_cast<SFXTrack*>(&mPlaylist) : dynamic_cast<SFXTrack*>(&mSFXProfile[0]); }
|
SFXTrack* getSFXTrack() { load(); return mIsPlaylist ? dynamic_cast<SFXTrack*>(&mPlaylist) : dynamic_cast<SFXTrack*>(mSFXProfile[0].getPointer()); }
|
||||||
SFXDescription* getSfxDescription() { return &mProfileDesc; }
|
SFXDescription* getSfxDescription() { return &mProfileDesc; }
|
||||||
bool isPlaylist(){ return mIsPlaylist; }
|
bool isPlaylist(){ return mIsPlaylist; }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1054,30 +1054,16 @@ public:
|
||||||
typedef WeakRefPtr< T > Parent;
|
typedef WeakRefPtr< T > Parent;
|
||||||
|
|
||||||
SimObjectPtr() = default;
|
SimObjectPtr() = default;
|
||||||
SimObjectPtr(T *ptr) { set(ptr); }
|
SimObjectPtr(T* ptr) : Parent(ptr) {}
|
||||||
SimObjectPtr( const SimObjectPtr& ref ) { this->mReference = ref.mReference; }
|
SimObjectPtr(const SimObjectPtr&) = default;
|
||||||
|
SimObjectPtr& operator=(const SimObjectPtr&) = default;
|
||||||
|
SimObjectPtr& operator=(T* ptr)
|
||||||
|
{
|
||||||
|
Parent::operator=(ptr);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
T* getObject() const { return Parent::getPointer(); }
|
T* getObject() const { return Parent::getPointer(); }
|
||||||
|
|
||||||
~SimObjectPtr() { this->mReference = NULL; }
|
|
||||||
|
|
||||||
SimObjectPtr<T>& operator=(const SimObjectPtr ref)
|
|
||||||
{
|
|
||||||
this->mReference = ref.mReference;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
SimObjectPtr<T>& operator=(T *ptr)
|
|
||||||
{
|
|
||||||
set(ptr);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
|
|
||||||
void set(T * obj)
|
|
||||||
{
|
|
||||||
this->mReference = obj ? obj->getWeakReference() : NULL;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // _SIMOBJECT_H_
|
#endif // _SIMOBJECT_H_
|
||||||
|
|
|
||||||
|
|
@ -79,22 +79,10 @@ public:
|
||||||
virtual ~WeakRefBase();
|
virtual ~WeakRefBase();
|
||||||
|
|
||||||
// Copy constructor
|
// Copy constructor
|
||||||
WeakRefBase(const WeakRefBase& other)
|
WeakRefBase(const WeakRefBase&) = delete;
|
||||||
{
|
WeakRefBase& operator=(const WeakRefBase&) = delete;
|
||||||
mControl = other.mControl;
|
WeakRefBase(WeakRefBase&&) = delete;
|
||||||
mReference = std::make_unique<WeakReference>(mControl);
|
WeakRefBase& operator=(WeakRefBase&&) = delete;
|
||||||
}
|
|
||||||
|
|
||||||
// Copy assignment
|
|
||||||
WeakRefBase& operator=(const WeakRefBase& other)
|
|
||||||
{
|
|
||||||
if (this != &other)
|
|
||||||
{
|
|
||||||
mControl = other.mControl;
|
|
||||||
mReference = std::make_unique<WeakReference>(mControl);
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<WeakReference> getWeakReference()
|
std::shared_ptr<WeakReference> getWeakReference()
|
||||||
{
|
{
|
||||||
|
|
@ -103,6 +91,12 @@ public:
|
||||||
return mReference;
|
return mReference;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::weak_ptr<WeakControlBlock> getWeakControl()
|
||||||
|
{
|
||||||
|
ensureControl();
|
||||||
|
return mControl;
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
void ensureControl()
|
void ensureControl()
|
||||||
|
|
@ -127,15 +121,11 @@ template< typename T > class SimObjectPtr;
|
||||||
template <class T> class WeakRefPtr
|
template <class T> class WeakRefPtr
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
WeakRefPtr() = default;
|
WeakRefPtr() = default;
|
||||||
WeakRefPtr(T* obj) { set(obj); }
|
WeakRefPtr(T* obj) { set(obj); }
|
||||||
WeakRefPtr(const WeakRefPtr& other) { mReference = other.mReference; }
|
|
||||||
|
|
||||||
WeakRefPtr& operator=(const WeakRefPtr& other)
|
WeakRefPtr(const WeakRefPtr&) = default;
|
||||||
{
|
WeakRefPtr& operator=(const WeakRefPtr&) = default;
|
||||||
mReference = other.mReference;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
WeakRefPtr& operator=(T* obj)
|
WeakRefPtr& operator=(T* obj)
|
||||||
{
|
{
|
||||||
|
|
@ -143,24 +133,36 @@ public:
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isValid() const { return mReference && mReference->get(); }
|
bool isValid() const { return getPointer() != NULL; }
|
||||||
bool isNull() const { return !isValid(); }
|
bool isNull() const { return getPointer() == NULL; }
|
||||||
|
|
||||||
[[nodiscard]] constexpr T* operator->() const { return getPointer(); }
|
[[nodiscard]] constexpr T* operator->() const { return getPointer(); }
|
||||||
[[nodiscard]] constexpr T& operator*() const { return *getPointer(); }
|
[[nodiscard]] constexpr T& operator*() const { return *getPointer(); }
|
||||||
[[nodiscard]] constexpr operator T*() const { return getPointer(); }
|
[[nodiscard]] constexpr operator T*() const { return getPointer(); }
|
||||||
|
|
||||||
/// Returns the pointer.
|
/// Returns the pointer.
|
||||||
[[nodiscard]] constexpr T* getPointer() const { return mReference ? (T*)mReference->get() : NULL; }
|
[[nodiscard]] constexpr T* getPointer() const
|
||||||
|
{
|
||||||
|
auto obj_ctrl = mWeak.lock();
|
||||||
|
if (!obj_ctrl) return NULL;
|
||||||
|
return (T*)obj_ctrl->object;
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void set(T* obj)
|
void set(T* obj)
|
||||||
{
|
{
|
||||||
mReference = obj ? obj->getWeakReference() : NULL;
|
if (!obj)
|
||||||
|
{
|
||||||
|
mWeak.reset();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto obj_ctrl = obj->getWeakControl().lock();
|
||||||
|
mWeak = obj_ctrl;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
template< typename > friend class SimObjectPtr;
|
template< typename > friend class SimObjectPtr;
|
||||||
std::shared_ptr<WeakRefBase::WeakReference> mReference;
|
std::weak_ptr<WeakControlBlock> mWeak;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Union of an arbitrary type with a WeakRefBase. The exposed type will
|
/// Union of an arbitrary type with a WeakRefBase. The exposed type will
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue