Skip to content

Commit ca4517c

Browse files
committed
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.
1 parent d688f1c commit ca4517c

File tree

4 files changed

+54
-80
lines changed

4 files changed

+54
-80
lines changed

Engine/source/T3D/assets/SoundAsset.cpp

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,6 @@ SoundAsset::SoundAsset()
189189

190190
SoundAsset::~SoundAsset()
191191
{
192-
193-
for (U32 i = 0; i < SFXPlayList::NUM_SLOTS; i++)
194-
{
195-
if(mSFXProfile[i].isProperlyAdded() && !mSFXProfile[i].isDeleted())
196-
mSFXProfile[i].unregisterObject();
197-
}
198192

199193
if (mPlaylist.isProperlyAdded() && !mPlaylist.isDeleted())
200194
mPlaylist.unregisterObject();
@@ -404,22 +398,17 @@ U32 SoundAsset::load()
404398
{
405399
Con::errorf("SoundAsset::initializeAsset: Attempted to load file %s but it was not valid!", mSoundFile[i]);
406400
mLoadedState = BadFileReference;
407-
mSFXProfile[i].setDescription(NULL);
408-
mSFXProfile[i].setSoundFileName(StringTable->insert(StringTable->EmptyString()));
409-
mSFXProfile[i].setPreload(false);
410401
return mLoadedState;
411402
}
412403
else
413404
{
414-
SFXProfile* trackProfile = new SFXProfile();
415-
trackProfile->setDescription(&mProfileDesc);
416-
trackProfile->setSoundFileName(mSoundPath[i]);
417-
trackProfile->setPreload(mPreload);
405+
mSFXProfile[i] = new SFXProfile;
406+
mSFXProfile[i]->setDescription(&mProfileDesc);
407+
mSFXProfile[i]->setSoundFileName(mSoundPath[i]);
408+
mSFXProfile[i]->setPreload(mPreload);
409+
mSFXProfile[i]->registerObject(String::ToString("%s_profile_track%d", getAssetName()).c_str());
418410

419-
mSFXProfile[i] = *trackProfile;
420-
mSFXProfile[i].registerObject(String::ToString("%s_profile_track%d", getAssetName()).c_str());
421-
422-
mPlaylist.mSlots.mTrack[i] = trackProfile;
411+
mPlaylist.mSlots.mTrack[i] = mSFXProfile[i];
423412

424413
}
425414
}
@@ -436,18 +425,15 @@ U32 SoundAsset::load()
436425
{
437426
Con::errorf("SoundAsset::initializeAsset: Attempted to load file %s but it was not valid!", mSoundFile[0]);
438427
mLoadedState = BadFileReference;
439-
mSFXProfile[0].setDescription(NULL);
440-
mSFXProfile[0].setSoundFileName(StringTable->insert(StringTable->EmptyString()));
441-
mSFXProfile[0].setPreload(false);
442428
return mLoadedState;
443429
}
444430
else
445431
{
446-
mSFXProfile[0].setDescription(&mProfileDesc);
447-
mSFXProfile[0].setSoundFileName(mSoundPath[0]);
448-
mSFXProfile[0].setPreload(mPreload);
449-
450-
mSFXProfile[0].registerObject(String::ToString("%s_profile", getAssetName()).c_str());
432+
mSFXProfile[0] = new SFXProfile;
433+
mSFXProfile[0]->setDescription(&mProfileDesc);
434+
mSFXProfile[0]->setSoundFileName(mSoundPath[0]);
435+
mSFXProfile[0]->setPreload(mPreload);
436+
mSFXProfile[0]->registerObject(String::ToString("%s_profile", getAssetName()).c_str());
451437
}
452438

453439
}

Engine/source/T3D/assets/SoundAsset.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ class SoundAsset : public AssetBase
8787
typedef AssetPtr<SoundAsset> ConcreteAssetPtr;
8888

8989
protected:
90-
StringTableEntry mSoundFile[SFXPlayList::SFXPlaylistSettings::NUM_SLOTS];
91-
StringTableEntry mSoundPath[SFXPlayList::SFXPlaylistSettings::NUM_SLOTS];
92-
SFXProfile mSFXProfile[SFXPlayList::SFXPlaylistSettings::NUM_SLOTS];
90+
StringTableEntry mSoundFile[SFXPlayList::SFXPlaylistSettings::NUM_SLOTS];
91+
StringTableEntry mSoundPath[SFXPlayList::SFXPlaylistSettings::NUM_SLOTS];
92+
SimObjectPtr<SFXProfile> mSFXProfile[SFXPlayList::SFXPlaylistSettings::NUM_SLOTS];
9393

9494
SFXDescription mProfileDesc;
9595
SFXPlayList mPlaylist;
@@ -150,17 +150,17 @@ class SoundAsset : public AssetBase
150150
void copyTo(SimObject* object) override;
151151

152152
//SFXResource* getSound() { return mSoundResource; }
153-
Resource<SFXResource> getSoundResource(const U32 slotId = 0) { load(); return mSFXProfile[slotId].getResource(); }
153+
Resource<SFXResource> getSoundResource(const U32 slotId = 0) { load(); return mSFXProfile[slotId]->getResource(); }
154154

155155
/// Declare Console Object.
156156
DECLARE_CONOBJECT(SoundAsset);
157157

158158
static bool _setSoundFile(void* object, const char* index, const char* data);
159159
U32 load() override;
160160
inline StringTableEntry getSoundPath(const U32 slotId = 0) const { return mSoundPath[slotId]; };
161-
SFXProfile* getSfxProfile(const U32 slotId = 0) { return &mSFXProfile[slotId]; }
161+
SFXProfile* getSfxProfile(const U32 slotId = 0) { return mSFXProfile[slotId]; }
162162
SFXPlayList* getSfxPlaylist() { return &mPlaylist; }
163-
SFXTrack* getSFXTrack() { load(); return mIsPlaylist ? dynamic_cast<SFXTrack*>(&mPlaylist) : dynamic_cast<SFXTrack*>(&mSFXProfile[0]); }
163+
SFXTrack* getSFXTrack() { load(); return mIsPlaylist ? dynamic_cast<SFXTrack*>(&mPlaylist) : dynamic_cast<SFXTrack*>(mSFXProfile[0].getPointer()); }
164164
SFXDescription* getSfxDescription() { return &mProfileDesc; }
165165
bool isPlaylist(){ return mIsPlaylist; }
166166

Engine/source/console/simObject.h

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,30 +1054,16 @@ class SimObjectPtr : public WeakRefPtr< T >
10541054
typedef WeakRefPtr< T > Parent;
10551055

10561056
SimObjectPtr() = default;
1057-
SimObjectPtr(T *ptr) { set(ptr); }
1058-
SimObjectPtr( const SimObjectPtr& ref ) { this->mReference = ref.mReference; }
1059-
1060-
T* getObject() const { return Parent::getPointer(); }
1061-
1062-
~SimObjectPtr() { this->mReference = NULL; }
1063-
1064-
SimObjectPtr<T>& operator=(const SimObjectPtr ref)
1065-
{
1066-
this->mReference = ref.mReference;
1067-
return *this;
1068-
}
1069-
SimObjectPtr<T>& operator=(T *ptr)
1057+
SimObjectPtr(T* ptr) : Parent(ptr) {}
1058+
SimObjectPtr(const SimObjectPtr&) = default;
1059+
SimObjectPtr& operator=(const SimObjectPtr&) = default;
1060+
SimObjectPtr& operator=(T* ptr)
10701061
{
1071-
set(ptr);
1062+
Parent::operator=(ptr);
10721063
return *this;
10731064
}
10741065

1075-
protected:
1076-
1077-
void set(T * obj)
1078-
{
1079-
this->mReference = obj ? obj->getWeakReference() : NULL;
1080-
}
1066+
T* getObject() const { return Parent::getPointer(); }
10811067
};
10821068

10831069
#endif // _SIMOBJECT_H_

Engine/source/core/util/refBase.h

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,10 @@ class WeakRefBase
7979
virtual ~WeakRefBase();
8080

8181
// Copy constructor
82-
WeakRefBase(const WeakRefBase& other)
83-
{
84-
mControl = other.mControl;
85-
mReference = std::make_unique<WeakReference>(mControl);
86-
}
87-
88-
// Copy assignment
89-
WeakRefBase& operator=(const WeakRefBase& other)
90-
{
91-
if (this != &other)
92-
{
93-
mControl = other.mControl;
94-
mReference = std::make_unique<WeakReference>(mControl);
95-
}
96-
return *this;
97-
}
82+
WeakRefBase(const WeakRefBase&) = delete;
83+
WeakRefBase& operator=(const WeakRefBase&) = delete;
84+
WeakRefBase(WeakRefBase&&) = delete;
85+
WeakRefBase& operator=(WeakRefBase&&) = delete;
9886

9987
std::shared_ptr<WeakReference> getWeakReference()
10088
{
@@ -103,6 +91,12 @@ class WeakRefBase
10391
return mReference;
10492
}
10593

94+
std::weak_ptr<WeakControlBlock> getWeakControl()
95+
{
96+
ensureControl();
97+
return mControl;
98+
}
99+
106100
protected:
107101

108102
void ensureControl()
@@ -127,40 +121,48 @@ template< typename T > class SimObjectPtr;
127121
template <class T> class WeakRefPtr
128122
{
129123
public:
130-
WeakRefPtr() = default;
131-
WeakRefPtr(T* obj) { set(obj); }
132-
WeakRefPtr(const WeakRefPtr& other) { mReference = other.mReference; }
124+
WeakRefPtr() = default;
125+
WeakRefPtr(T* obj) { set(obj); }
133126

134-
WeakRefPtr& operator=(const WeakRefPtr& other)
135-
{
136-
mReference = other.mReference;
137-
return *this;
138-
}
127+
WeakRefPtr(const WeakRefPtr&) = default;
128+
WeakRefPtr& operator=(const WeakRefPtr&) = default;
139129

140130
WeakRefPtr& operator=(T* obj)
141131
{
142132
set(obj);
143133
return *this;
144134
}
145135

146-
bool isValid() const { return mReference && mReference->get(); }
147-
bool isNull() const { return !isValid(); }
136+
bool isValid() const { return getPointer() != NULL; }
137+
bool isNull() const { return getPointer() == NULL; }
148138

149139
[[nodiscard]] constexpr T* operator->() const { return getPointer(); }
150140
[[nodiscard]] constexpr T& operator*() const { return *getPointer(); }
151141
[[nodiscard]] constexpr operator T*() const { return getPointer(); }
152142

153143
/// Returns the pointer.
154-
[[nodiscard]] constexpr T* getPointer() const { return mReference ? (T*)mReference->get() : NULL; }
144+
[[nodiscard]] constexpr T* getPointer() const
145+
{
146+
auto obj_ctrl = mWeak.lock();
147+
if (!obj_ctrl) return NULL;
148+
return (T*)obj_ctrl->object;
149+
}
155150

156151
protected:
157152
void set(T* obj)
158153
{
159-
mReference = obj ? obj->getWeakReference() : NULL;
154+
if (!obj)
155+
{
156+
mWeak.reset();
157+
return;
158+
}
159+
160+
auto obj_ctrl = obj->getWeakControl().lock();
161+
mWeak = obj_ctrl;
160162
}
161163
private:
162164
template< typename > friend class SimObjectPtr;
163-
std::shared_ptr<WeakRefBase::WeakReference> mReference;
165+
std::weak_ptr<WeakControlBlock> mWeak;
164166
};
165167

166168
/// Union of an arbitrary type with a WeakRefBase. The exposed type will

0 commit comments

Comments
 (0)