Cleaned up cheat classes, replacing pointers with smart_ptr.

git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@3078 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba
This commit is contained in:
stephena 2014-11-16 21:08:21 +00:00
parent 2707e4e4e0
commit a6eded9c65
11 changed files with 51 additions and 103 deletions

View File

@ -39,11 +39,6 @@ BankRomCheat::BankRomCheat(OSystem& os, const string& name, const string& code)
savedRom[i] = myOSystem.console().cartridge().peek(address + i);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
BankRomCheat::~BankRomCheat()
{
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool BankRomCheat::enable()
{

View File

@ -26,7 +26,7 @@ class BankRomCheat : public Cheat
{
public:
BankRomCheat(OSystem& os, const string& name, const string& code);
virtual ~BankRomCheat();
virtual ~BankRomCheat() { }
bool enable();
bool disable();

View File

@ -34,24 +34,17 @@
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CheatManager::CheatManager(OSystem& osystem)
: myOSystem(osystem),
myCurrentCheat(""),
myListIsDirty(false)
{
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CheatManager::~CheatManager()
bool CheatManager::add(const string& name, const string& code,
bool enable, int idx)
{
clear();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cheat* CheatManager::add(const string& name, const string& code,
bool enable, int idx)
{
Cheat* cheat = createCheat(name, code);
shared_ptr<Cheat> cheat = createCheat(name, code);
if(!cheat)
return nullptr;
return false;
// Delete duplicate entries
for(unsigned int i = 0; i < myCheatList.size(); i++)
@ -75,32 +68,36 @@ Cheat* CheatManager::add(const string& name, const string& code,
else
cheat->disable();
return cheat;
return true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CheatManager::remove(int idx)
{
if((unsigned int)idx >= myCheatList.size())
return;
if((unsigned int)idx < myCheatList.size())
{
// This will also remove it from the per-frame list (if applicable)
myCheatList[idx]->disable();
Cheat* c = myCheatList[idx];
// First remove it from the per-frame list
addPerFrame(c, false);
// Then remove it from the cheatlist entirely
Vec::removeAt(myCheatList, idx);
c->disable();
delete c;
// Then remove it from the cheatlist entirely
Vec::removeAt(myCheatList, idx);
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CheatManager::addPerFrame(Cheat* cheat, bool enable)
void CheatManager::addPerFrame(const string& name, const string& code, bool enable)
{
if(!cheat)
return;
// The actual cheat will always be in the main list; we look there first
shared_ptr<Cheat> cheat;
for(unsigned int i = 0; i < myCheatList.size(); i++)
{
if(myCheatList[i]->name() == name || myCheatList[i]->code() == code)
{
cheat = myCheatList[i];
break;
}
}
// Make sure there are no duplicates
bool found = false;
unsigned int i;
@ -128,7 +125,7 @@ void CheatManager::addPerFrame(Cheat* cheat, bool enable)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CheatManager::addOneShot(const string& name, const string& code)
{
unique_ptr<Cheat> cheat(createCheat(name, code));
shared_ptr<Cheat> cheat = createCheat(name, code);
// Evaluate this cheat once, and then immediately delete it
if(cheat)
@ -136,7 +133,7 @@ void CheatManager::addOneShot(const string& name, const string& code)
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cheat* CheatManager::createCheat(const string& name, const string& code) const
shared_ptr<Cheat> CheatManager::createCheat(const string& name, const string& code) const
{
if(!isValidCode(code))
return nullptr;
@ -145,15 +142,15 @@ Cheat* CheatManager::createCheat(const string& name, const string& code) const
switch(code.size())
{
case 4:
return new RamCheat(myOSystem, name, code);
return make_shared<RamCheat>(myOSystem, name, code);
break;
case 6:
return new CheetahCheat(myOSystem, name, code);
return make_shared<CheetahCheat>(myOSystem, name, code);
break;
case 8:
return new BankRomCheat(myOSystem, name, code);
return make_shared<BankRomCheat>(myOSystem, name, code);
break;
default:
@ -287,7 +284,8 @@ void CheatManager::saveCheatDatabase()
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CheatManager::loadCheats(const string& md5sum)
{
clear();
myPerFrameList.clear();
myCheatList.clear();
myCurrentCheat = "";
// Set up any cheatcodes that was on the command line
@ -338,25 +336,14 @@ void CheatManager::saveCheats(const string& md5sum)
// Update the dirty flag
myListIsDirty = myListIsDirty || changed;
clear();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CheatManager::clear()
{
// Don't delete the items from per-frame list, since it will be done in
// the following loop
myPerFrameList.clear();
for(auto cheat: myCheatList)
delete cheat;
myCheatList.clear();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool CheatManager::isValidCode(const string& code) const
{
for(auto c: code)
for(char c: code)
if(!isxdigit(c))
return false;

View File

@ -27,7 +27,7 @@ class OSystem;
#include "bspf.hxx"
typedef vector<Cheat*> CheatList;
typedef vector<shared_ptr<Cheat>> CheatList;
/**
This class provides an interface for performing all cheat operations
@ -41,7 +41,6 @@ class CheatManager
{
public:
CheatManager(OSystem& osystem);
~CheatManager();
/**
Adds the specified cheat to an internal list.
@ -51,10 +50,10 @@ class CheatManager
@param enable Whether to enable this cheat right away
@param idx Index at which to insert the cheat
@return The cheat (if was created), else nullptr.
@return Whether the cheat was created and enabled.
*/
Cheat* add(const string& name, const string& code,
bool enable = true, int idx = -1);
bool add(const string& name, const string& code,
bool enable = true, int idx = -1);
/**
Remove the cheat at 'idx' from the cheat list(s).
@ -68,10 +67,11 @@ class CheatManager
This method doesn't create a new cheat; it just adds/removes
an already created cheat to the per-frame list.
@param cheat The actual cheat object
@param name Name of the cheat
@param code The actual cheatcode
@param enable Add or remove the cheat to the per-frame list
*/
void addPerFrame(Cheat* cheat, bool enable);
void addPerFrame(const string& name, const string& code, bool enable);
/**
Creates and enables a one-shot cheat. One-shot cheats are the
@ -135,7 +135,7 @@ class CheatManager
@return The cheat (if was created), else nullptr.
*/
Cheat* createCheat(const string& name, const string& code) const;
shared_ptr<Cheat> createCheat(const string& name, const string& code) const;
/**
Parses a list of cheats and adds/enables each one.
@ -144,11 +144,6 @@ class CheatManager
*/
void parse(const string& cheats);
/**
Clear all per-ROM cheats lists.
*/
void clear();
private:
OSystem& myOSystem;

View File

@ -35,11 +35,6 @@ CheetahCheat::CheetahCheat(OSystem& os, const string& name, const string& code)
savedRom[i] = myOSystem.console().cartridge().peek(address + i);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CheetahCheat::~CheetahCheat()
{
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool CheetahCheat::enable()
{

View File

@ -26,7 +26,7 @@ class CheetahCheat : public Cheat
{
public:
CheetahCheat(OSystem& os, const string& name, const string& code);
virtual ~CheetahCheat();
virtual ~CheetahCheat() { }
bool enable();
bool disable();

View File

@ -32,18 +32,13 @@ RamCheat::RamCheat(OSystem& os, const string& name, const string& code)
value = (uInt8) unhex(myCode.substr(2, 2));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
RamCheat::~RamCheat()
{
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool RamCheat::enable()
{
if(!myEnabled)
{
myEnabled = true;
myOSystem.cheat().addPerFrame(this, myEnabled);
myOSystem.cheat().addPerFrame(name(), code(), myEnabled);
}
return myEnabled;
}
@ -54,7 +49,7 @@ bool RamCheat::disable()
if(myEnabled)
{
myEnabled = false;
myOSystem.cheat().addPerFrame(this, myEnabled);
myOSystem.cheat().addPerFrame(name(), code(), myEnabled);
}
return myEnabled;
}

View File

@ -26,7 +26,7 @@ class RamCheat : public Cheat
{
public:
RamCheat(OSystem& os, const string& name, const string& code);
virtual ~RamCheat();
virtual ~RamCheat() { }
bool enable();
bool disable();

View File

@ -729,8 +729,7 @@ void DebuggerParser::executeCheat()
for(int arg = 0; arg < argCount; arg++)
{
const string& cheat = argStrings[arg];
const Cheat* c = debugger.myOSystem.cheat().add("DBG", cheat);
if(c && c->enabled())
if(debugger.myOSystem.cheat().add("DBG", cheat))
commandResult << "Cheat code " << cheat << " enabled" << endl;
else
commandResult << red("Invalid cheat code ") << cheat << endl;

View File

@ -25,23 +25,6 @@
#include "GameList.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
GameList::GameList()
{
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
GameList::~GameList()
{
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void GameList::appendGame(const string& name, const string& path,
const string& md5, bool isDir)
{
myArray.emplace_back(name, path, md5, isDir);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void GameList::sortByName()
{

View File

@ -31,8 +31,7 @@
class GameList
{
public:
GameList();
~GameList();
GameList() { }
const string& name(uInt32 i) const
{ return i < myArray.size() ? myArray[i]._name : EmptyString; }
@ -50,7 +49,9 @@ class GameList
void clear() { myArray.clear(); }
void appendGame(const string& name, const string& path, const string& md5,
bool isDir = false);
bool isDir = false) {
myArray.emplace_back(name, path, md5, isDir);
};
void sortByName();
private:
@ -61,9 +62,7 @@ class GameList
bool _isdir;
Entry(string name, string path, string md5, bool isdir)
{
_name = name; _path = path; _md5 = md5; _isdir = isdir;
}
: _name(name), _path(path), _md5(md5), _isdir(isdir) { }
};
vector<Entry> myArray;
};