Skip to content

Commit 4fe0dd0

Browse files
authored
Merge pull request #1682 from marauder2k9-torque/RefBase-refactor
RefBase classes refactored to use smart pointers
2 parents ed5f684 + 8c8c259 commit 4fe0dd0

File tree

5 files changed

+211
-313
lines changed

5 files changed

+211
-313
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: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@
4040
#ifndef _TAML_CALLBACKS_H_
4141
#include "persistence/taml/tamlCallbacks.h"
4242
#endif
43+
44+
#ifndef _OBJECTTYPES_H_
4345
#include "T3D/objectTypes.h"
46+
#endif
4447

4548
class Stream;
4649
class LightManager;
@@ -606,6 +609,7 @@ class SimObject: public ConsoleObject, public TamlCallbacks
606609

607610
/// @name Events
608611
/// @{
612+
//virtual void onPrepare();
609613

610614
/// Called when the object is added to the sim.
611615
virtual bool onAdd();
@@ -1045,56 +1049,47 @@ DefineBitfieldType(GameTypeMasksType);
10451049
template< typename T >
10461050
class SimObjectPtr : public WeakRefPtr< T >
10471051
{
1048-
public:
1052+
public:
10491053

1050-
typedef WeakRefPtr< T > Parent;
1054+
typedef WeakRefPtr< T > Parent;
10511055

1052-
SimObjectPtr() {}
1053-
SimObjectPtr(T *ptr) { this->mReference = NULL; set(ptr); }
1054-
SimObjectPtr( const SimObjectPtr& ref ) { this->mReference = NULL; set(ref.mReference); }
1055-
1056-
T* getObject() const { return Parent::getPointer(); }
1056+
SimObjectPtr() = default;
1057+
SimObjectPtr(T* ptr) { set(ptr); }
1058+
SimObjectPtr(const SimObjectPtr&) = default;
1059+
SimObjectPtr& operator=(const SimObjectPtr&) = default;
1060+
SimObjectPtr& operator=(T* ptr)
1061+
{
1062+
set(ptr);
1063+
return *this;
1064+
}
1065+
1066+
T* getObject() const { return Parent::getPointer(); }
10571067

1058-
~SimObjectPtr() { set((WeakRefBase::WeakReference*)NULL); }
1059-
1060-
SimObjectPtr<T>& operator=(const SimObjectPtr ref)
1061-
{
1062-
set(ref.mReference);
1063-
return *this;
1064-
}
1065-
SimObjectPtr<T>& operator=(T *ptr)
1066-
{
1067-
set(ptr);
1068-
return *this;
1069-
}
1070-
1071-
protected:
1072-
void set(WeakRefBase::WeakReference * ref)
1068+
protected:
1069+
void set(T* obj)
1070+
{
1071+
// Nothing to do if same object
1072+
if (obj && this->mWeak.lock() == obj->getWeakControl().lock())
1073+
return;
1074+
1075+
// Before overwriting, check old object for auto-delete
1076+
if (auto old_ctrl = this->mWeak.lock())
10731077
{
1074-
if( ref == this->mReference )
1075-
return;
1076-
1077-
if( this->mReference )
1078-
{
1079-
// Auto-delete
1080-
T* obj = this->getPointer();
1081-
if ( this->mReference->getRefCount() == 2 && obj && obj->isAutoDeleted() )
1082-
obj->deleteObject();
1083-
1084-
this->mReference->decRefCount();
1085-
}
1086-
this->mReference = NULL;
1087-
if( ref )
1078+
T* old_obj = getObject();
1079+
if (this->mWeak.use_count() == 1 && old_obj && old_obj->isAutoDeleted())
10881080
{
1089-
this->mReference = ref;
1090-
this->mReference->incRefCount();
1081+
old_obj->destroySelf();
10911082
}
10921083
}
10931084

1094-
void set(T * obj)
1085+
// Assign new weak reference
1086+
this->mWeak.reset();
1087+
if (obj)
10951088
{
1096-
set(obj ? obj->getWeakReference() : (WeakRefBase::WeakReference *)NULL);
1089+
auto obj_ctrl = obj->getWeakControl().lock();
1090+
this->mWeak = obj_ctrl;
10971091
}
1092+
}
10981093
};
10991094

11001095
#endif // _SIMOBJECT_H_
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "refBase.h"
2+
3+
WeakRefBase::~WeakRefBase()
4+
{
5+
if (mControl)
6+
mControl->object = NULL;
7+
}
8+
9+
WeakControlBlock::WeakControlBlock(WeakRefBase* obj)
10+
: object(obj)
11+
{
12+
}
13+
14+
WeakControlBlock::~WeakControlBlock()
15+
{
16+
17+
}
18+

0 commit comments

Comments
 (0)