diff --git a/src/cheat/BankRomCheat.cxx b/src/cheat/BankRomCheat.cxx index a3f1a022a..3fca11f52 100644 --- a/src/cheat/BankRomCheat.cxx +++ b/src/cheat/BankRomCheat.cxx @@ -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() { diff --git a/src/cheat/BankRomCheat.hxx b/src/cheat/BankRomCheat.hxx index 646d0ded5..f6174c96b 100644 --- a/src/cheat/BankRomCheat.hxx +++ b/src/cheat/BankRomCheat.hxx @@ -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(); diff --git a/src/cheat/CheatManager.cxx b/src/cheat/CheatManager.cxx index 3f9536954..b7771e4be 100644 --- a/src/cheat/CheatManager.cxx +++ b/src/cheat/CheatManager.cxx @@ -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 = 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; + 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(createCheat(name, code)); + shared_ptr 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 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(myOSystem, name, code); break; case 6: - return new CheetahCheat(myOSystem, name, code); + return make_shared(myOSystem, name, code); break; case 8: - return new BankRomCheat(myOSystem, name, code); + return make_shared(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; diff --git a/src/cheat/CheatManager.hxx b/src/cheat/CheatManager.hxx index ee7d9d537..e38c2ff53 100644 --- a/src/cheat/CheatManager.hxx +++ b/src/cheat/CheatManager.hxx @@ -27,7 +27,7 @@ class OSystem; #include "bspf.hxx" -typedef vector CheatList; +typedef vector> 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 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; diff --git a/src/cheat/CheetahCheat.cxx b/src/cheat/CheetahCheat.cxx index b446fa3f5..ccf820ed9 100644 --- a/src/cheat/CheetahCheat.cxx +++ b/src/cheat/CheetahCheat.cxx @@ -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() { diff --git a/src/cheat/CheetahCheat.hxx b/src/cheat/CheetahCheat.hxx index c51a7a76c..89a044224 100644 --- a/src/cheat/CheetahCheat.hxx +++ b/src/cheat/CheetahCheat.hxx @@ -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(); diff --git a/src/cheat/RamCheat.cxx b/src/cheat/RamCheat.cxx index 1770a1afa..6b34de676 100644 --- a/src/cheat/RamCheat.cxx +++ b/src/cheat/RamCheat.cxx @@ -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; } diff --git a/src/cheat/RamCheat.hxx b/src/cheat/RamCheat.hxx index 90443427d..15f0ca79a 100644 --- a/src/cheat/RamCheat.hxx +++ b/src/cheat/RamCheat.hxx @@ -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(); diff --git a/src/debugger/DebuggerParser.cxx b/src/debugger/DebuggerParser.cxx index b41b26880..9656e80f0 100644 --- a/src/debugger/DebuggerParser.cxx +++ b/src/debugger/DebuggerParser.cxx @@ -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; diff --git a/src/gui/GameList.cxx b/src/gui/GameList.cxx index 753a4697a..d03b8a623 100644 --- a/src/gui/GameList.cxx +++ b/src/gui/GameList.cxx @@ -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() { diff --git a/src/gui/GameList.hxx b/src/gui/GameList.hxx index d2661a30b..5c16136c1 100644 --- a/src/gui/GameList.hxx +++ b/src/gui/GameList.hxx @@ -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 myArray; };