make deep copy simpler by not holding a pointer

This commit is contained in:
Chris Danford
2005-03-08 21:17:21 +00:00
parent 9cb560ae03
commit 494ae0b74d
2 changed files with 50 additions and 42 deletions
+44 -37
View File
@@ -40,14 +40,14 @@ Steps::Steps()
m_Difficulty = DIFFICULTY_INVALID; m_Difficulty = DIFFICULTY_INVALID;
m_iMeter = 0; m_iMeter = 0;
notes = NULL; m_NoteData.Init();
notes_comp = ""; m_bNoteDataIsFilled = false;
m_sNoteDataCompressed = "";
parent = NULL; parent = NULL;
} }
Steps::~Steps() Steps::~Steps()
{ {
SAFE_DELETE( notes );
} }
void Steps::SetNoteData( const NoteData& noteDataNew ) void Steps::SetNoteData( const NoteData& noteDataNew )
@@ -56,11 +56,11 @@ void Steps::SetNoteData( const NoteData& noteDataNew )
DeAutogen(); DeAutogen();
SAFE_DELETE( notes ); m_NoteData = noteDataNew;
notes = new NoteData( noteDataNew ); m_bNoteDataIsFilled = true;
NoteDataUtil::GetSMNoteDataString( *notes, notes_comp ); NoteDataUtil::GetSMNoteDataString( m_NoteData, m_sNoteDataCompressed );
m_uHash = GetHashForString( notes_comp ); m_uHash = GetHashForString( m_sNoteDataCompressed );
} }
void Steps::GetNoteData( NoteData& noteDataOut ) const void Steps::GetNoteData( NoteData& noteDataOut ) const
@@ -69,8 +69,10 @@ void Steps::GetNoteData( NoteData& noteDataOut ) const
Decompress(); Decompress();
if( notes != NULL ) if( m_bNoteDataIsFilled )
noteDataOut = *notes; {
noteDataOut = m_NoteData;
}
else else
{ {
noteDataOut.ClearAll(); noteDataOut.ClearAll();
@@ -80,28 +82,29 @@ void Steps::GetNoteData( NoteData& noteDataOut ) const
void Steps::SetSMNoteData( const CString &notes_comp_ ) void Steps::SetSMNoteData( const CString &notes_comp_ )
{ {
SAFE_DELETE( notes ); m_NoteData.Init();
m_bNoteDataIsFilled = false;
notes_comp = notes_comp_; m_sNoteDataCompressed = notes_comp_;
m_uHash = GetHashForString( notes_comp ); m_uHash = GetHashForString( m_sNoteDataCompressed );
} }
/* XXX: this function should pull data from cache, like Decompress() */ /* XXX: this function should pull data from cache, like Decompress() */
void Steps::GetSMNoteData( CString &notes_comp_out ) const void Steps::GetSMNoteData( CString &notes_comp_out ) const
{ {
if( notes_comp.empty() ) if( m_sNoteDataCompressed.empty() )
{ {
if( !notes ) if( !m_bNoteDataIsFilled )
{ {
/* no data is no data */ /* no data is no data */
notes_comp_out = ""; notes_comp_out = "";
return; return;
} }
NoteDataUtil::GetSMNoteDataString( *notes, notes_comp ); NoteDataUtil::GetSMNoteDataString( m_NoteData, m_sNoteDataCompressed );
} }
notes_comp_out = notes_comp; notes_comp_out = m_sNoteDataCompressed;
} }
float Steps::PredictMeter() const float Steps::PredictMeter() const
@@ -150,31 +153,33 @@ void Steps::TidyUpData()
void Steps::Decompress() const void Steps::Decompress() const
{ {
if(notes) if( m_bNoteDataIsFilled )
return; // already decompressed return; // already decompressed
if(parent) if(parent)
{ {
// get autogen notes // get autogen m_NoteData
NoteData notedata; NoteData notedata;
parent->GetNoteData( notedata ); parent->GetNoteData( notedata );
notes = new NoteData; m_bNoteDataIsFilled = true;
int iNewTracks = GameManager::StepsTypeToNumTracks(m_StepsType); int iNewTracks = GameManager::StepsTypeToNumTracks(m_StepsType);
if( this->m_StepsType == STEPS_TYPE_LIGHTS_CABINET ) if( this->m_StepsType == STEPS_TYPE_LIGHTS_CABINET )
{ {
NoteDataUtil::LoadTransformedLights( notedata, *notes, iNewTracks ); NoteDataUtil::LoadTransformedLights( notedata, m_NoteData, iNewTracks );
} else { }
NoteDataUtil::LoadTransformedSlidingWindow( notedata, *notes, iNewTracks ); else
{
NoteDataUtil::LoadTransformedSlidingWindow( notedata, m_NoteData, iNewTracks );
NoteDataUtil::FixImpossibleRows( *notes, m_StepsType ); NoteDataUtil::FixImpossibleRows( m_NoteData, m_StepsType );
} }
return; return;
} }
if( !m_sFilename.empty() && notes_comp.empty() ) if( !m_sFilename.empty() && m_sNoteDataCompressed.empty() )
{ {
/* We have data on disk and not in memory. Load it. */ /* We have data on disk and not in memory. Load it. */
Song s; Song s;
@@ -196,20 +201,20 @@ void Steps::Decompress() const
return; return;
} }
pSteps->GetSMNoteData( notes_comp ); pSteps->GetSMNoteData( m_sNoteDataCompressed );
} }
if( notes_comp.empty() ) if( m_sNoteDataCompressed.empty() )
{ {
/* there is no data, do nothing */ /* there is no data, do nothing */
} }
else else
{ {
// load from compressed // load from compressed
notes = new NoteData; m_bNoteDataIsFilled = true;
notes->SetNumTracks( GameManager::StepsTypeToNumTracks(m_StepsType) ); m_NoteData.SetNumTracks( GameManager::StepsTypeToNumTracks(m_StepsType) );
NoteDataUtil::LoadFromSMNoteDataString( *notes, notes_comp ); NoteDataUtil::LoadFromSMNoteDataString( m_NoteData, m_sNoteDataCompressed );
} }
} }
@@ -218,20 +223,22 @@ void Steps::Compress() const
if( !m_sFilename.empty() ) if( !m_sFilename.empty() )
{ {
/* We have a file on disk; clear all data in memory. */ /* We have a file on disk; clear all data in memory. */
SAFE_DELETE( notes ); m_NoteData.Init();
m_bNoteDataIsFilled = false;
/* Be careful; 'x = ""', notes_comp.clear() and notes_comp.reserve(0) /* Be careful; 'x = ""', m_sNoteDataCompressed.clear() and m_sNoteDataCompressed.reserve(0)
* don't always free the alocated memory. */ * don't always free the alocated memory. */
notes_comp = CString(""); m_sNoteDataCompressed = CString("");
} }
if( notes_comp.empty() ) if( m_sNoteDataCompressed.empty() )
{ {
if(!notes) return; /* no data is no data */ if(!m_bNoteDataIsFilled) return; /* no data is no data */
NoteDataUtil::GetSMNoteDataString( *notes, notes_comp ); NoteDataUtil::GetSMNoteDataString( m_NoteData, m_sNoteDataCompressed );
} }
SAFE_DELETE( notes ); m_NoteData.Init();
m_bNoteDataIsFilled = false;
} }
/* Copy our parent's data. This is done when we're being changed from autogen /* Copy our parent's data. This is done when we're being changed from autogen
@@ -241,7 +248,7 @@ void Steps::DeAutogen()
if(!parent) if(!parent)
return; /* OK */ return; /* OK */
Decompress(); // fills in notes with sliding window transform Decompress(); // fills in m_NoteData with sliding window transform
m_sDescription = Real()->m_sDescription; m_sDescription = Real()->m_sDescription;
m_Difficulty = Real()->m_Difficulty; m_Difficulty = Real()->m_Difficulty;
+6 -5
View File
@@ -8,7 +8,7 @@
#include "Grade.h" #include "Grade.h"
#include "RadarValues.h" #include "RadarValues.h"
#include "Difficulty.h" #include "Difficulty.h"
class NoteData; #include "NoteData.h"
class Profile; class Profile;
struct lua_State; struct lua_State;
@@ -67,14 +67,15 @@ public:
protected: protected:
/* If this Steps is autogenerated, this will point to the autogen /* If this Steps is autogenerated, this will point to the autogen
* source. If this is true, notes_comp will always be NULL. */ * source. If this is true, m_sNoteDataCompressed will always be empty. */
const Steps *parent; const Steps *parent;
/* We can have one or both of these; if we have both, they're always identical. /* We can have one or both of these; if we have both, they're always identical.
* Call Compress() to force us to only have notes_comp; otherwise, creation of * Call Compress() to force us to only have m_sNoteDataCompressed; otherwise, creation of
* these is transparent. */ * these is transparent. */
mutable NoteData *notes; mutable NoteData m_NoteData;
mutable CString notes_comp; mutable bool m_bNoteDataIsFilled;
mutable CString m_sNoteDataCompressed;
const Steps *Real() const; const Steps *Real() const;