diff --git a/Changes.txt b/Changes.txt index 163285335..03b45ffd5 100644 --- a/Changes.txt +++ b/Changes.txt @@ -12,6 +12,16 @@ Release History =========================================================================== +4.2 to 4.3: (December xx, 2014) + + * The conversion to C++11 has begun :) From this point on, to build + Stella you will need a C++11 compatible compiler (Visual Studio 2013, + Clang 3.5, gcc 4.9, etc). Eventually, this will bring more bug-free + and (hopefully) faster code. + +-Have fun! + + 4.1.1 to 4.2: (October 28, 2014) * Text input from non-US keyboard layouts is now supported. Note that @@ -42,8 +52,6 @@ * The Linux port now uses an app-icon; this seems to be needed for some window managers. --Have fun! - 4.1 to 4.1.1: (September 14, 2014) diff --git a/Makefile b/Makefile index 2dadddb09..4c7ce484e 100644 --- a/Makefile +++ b/Makefile @@ -51,7 +51,7 @@ else endif CXXFLAGS+= -Wall ifdef HAVE_GCC - CXXFLAGS+= -Wno-multichar -Wunused -fno-rtti -Woverloaded-virtual + CXXFLAGS+= -Wno-multichar -Wunused -fno-rtti -Woverloaded-virtual -std=c++11 endif ifdef PROFILE diff --git a/src/cheat/CheatManager.cxx b/src/cheat/CheatManager.cxx index dd9320bf2..8e18656e8 100644 --- a/src/cheat/CheatManager.cxx +++ b/src/cheat/CheatManager.cxx @@ -281,11 +281,8 @@ void CheatManager::saveCheatDatabase() if(!out) return; - CheatCodeMap::iterator iter; - for(iter = myCheatMap.begin(); iter != myCheatMap.end(); ++iter) - out << "\"" << iter->first << "\" " - << "\"" << iter->second << "\"" - << endl; + for(const auto& iter: myCheatMap) + out << "\"" << iter.first << "\" " << "\"" << iter.second << "\"" << endl; out.close(); } @@ -302,7 +299,7 @@ void CheatManager::loadCheats(const string& md5sum) if(cheats != "") myOSystem.settings().setValue("cheat", ""); - CheatCodeMap::iterator iter = myCheatMap.find(md5sum); + const auto& iter = myCheatMap.find(md5sum); if(iter == myCheatMap.end() && cheats == "") return; diff --git a/src/common/SoundSDL2.cxx b/src/common/SoundSDL2.cxx index daf9df739..bba6d040e 100644 --- a/src/common/SoundSDL2.cxx +++ b/src/common/SoundSDL2.cxx @@ -99,8 +99,6 @@ SoundSDL2::~SoundSDL2() SDL_CloseAudio(); myIsEnabled = myIsInitializedFlag = false; } - - myOSystem.logMessage("SoundSDL2 destroyed", 2); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/common/bspf.hxx b/src/common/bspf.hxx index 302e92a81..a472828ab 100644 --- a/src/common/bspf.hxx +++ b/src/common/bspf.hxx @@ -66,6 +66,7 @@ #include #include #include +#include #include #include #include @@ -110,18 +111,22 @@ static const string EmptyString(""); ////////////////////////////////////////////////////////////////////// // Some convenience functions +// Initialize C++11 unique_ptr, at least until std::make_unique() +// becomes part of the standard (C++14) +template +std::unique_ptr make_ptr(Arguments && ... arguments_for_constructor) +{ + return std::unique_ptr( + new Value(std::forward(arguments_for_constructor)...) + ); +} + template inline void BSPF_swap(T& a, T& b) { T tmp = a; a = b; b = tmp; } template inline T BSPF_abs (T x) { return (x>=0) ? x : -x; } template inline T BSPF_min (T a, T b) { return (a inline T BSPF_max (T a, T b) { return (a>b) ? a : b; } template inline T BSPF_clamp (T a, T l, T u) { return (au) ? u : a; } -// Test whether two characters are equal (case insensitive) -static bool BSPF_equalsIgnoreCaseChar(char ch1, char ch2) -{ - return toupper((unsigned char)ch1) == toupper((unsigned char)ch2); -} - // Compare two strings, ignoring case inline int BSPF_compareIgnoreCase(const string& s1, const string& s2) { @@ -168,8 +173,10 @@ inline bool BSPF_equalsIgnoreCase(const string& s1, const string& s2) // starting from 'startpos' in the first string inline size_t BSPF_findIgnoreCase(const string& s1, const string& s2, int startpos = 0) { - string::const_iterator pos = std::search(s1.begin()+startpos, s1.end(), - s2.begin(), s2.end(), BSPF_equalsIgnoreCaseChar); + auto pos = std::search(s1.begin()+startpos, s1.end(), + s2.begin(), s2.end(), [](char ch1, char ch2) { + return toupper((unsigned char)ch1) == toupper((unsigned char)ch2); + }); return pos == s1.end() ? string::npos : pos - (s1.begin()+startpos); } diff --git a/src/common/mainSDL.cxx b/src/common/mainSDL.cxx index 44a37504b..0aaee877d 100644 --- a/src/common/mainSDL.cxx +++ b/src/common/mainSDL.cxx @@ -17,7 +17,6 @@ // $Id$ //============================================================================ -#include #include #include "bspf.hxx" @@ -42,7 +41,7 @@ #endif // Pointer to the main parent osystem object or the null pointer -OSystem* theOSystem = (OSystem*) NULL; +unique_ptr theOSystem; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Does general Cleanup in case any operation failed (or at end of program) @@ -51,12 +50,6 @@ int Cleanup() theOSystem->logMessage("Cleanup from mainSDL", 2); theOSystem->saveConfig(); - if(theOSystem) - delete theOSystem; - - if(SDL_WasInit(SDL_INIT_VIDEO) & SDL_INIT_VIDEO) - SDL_Quit(); - return 0; } @@ -164,13 +157,6 @@ int main(int argc, char* argv[]) #endif } -#if 0 - // Swallow any spurious events in the queue - // These are normally caused by joystick/mouse jitter - SDL_Event event; - while(SDL_PollEvent(&event)) /* swallow event */ ; -#endif - // Start the main loop, and don't exit until the user issues a QUIT command theOSystem->logMessage("Starting main loop ...", 2); theOSystem->mainLoop(); diff --git a/src/debugger/CartDebug.cxx b/src/debugger/CartDebug.cxx index 8ca128540..261dffb6a 100644 --- a/src/debugger/CartDebug.cxx +++ b/src/debugger/CartDebug.cxx @@ -260,15 +260,15 @@ bool CartDebug::disassemble(bool force) // $bxxx, it must be changed uInt16 offset = (PC - (PC % 0x1000)); AddressList& addresses = info.addressList; - for(list::iterator i = addresses.begin(); i != addresses.end(); ++i) - *i = (*i & 0xFFF) + offset; + for(auto& i: addresses) + i = (i & 0xFFF) + offset; // Only add addresses when absolutely necessary, to cut down on the // work that Distella has to do // Distella expects the addresses to be unique and in sorted order if(bankChanged || !pcfound) { - AddressList::iterator i; + AddressList::const_iterator i; for(i = addresses.begin(); i != addresses.end(); ++i) { if(PC < *i) @@ -339,7 +339,7 @@ int CartDebug::addressToLine(uInt16 address) const if(!myAddrToLineIsROM != !(address & 0x1000)) return -1; - map::const_iterator iter = myAddrToLineList.find(address & 0xFFF); + const auto& iter = myAddrToLineList.find(address & 0xFFF); return iter != myAddrToLineList.end() ? iter->second : -1; } @@ -390,16 +390,6 @@ string CartDebug::disassemble(uInt16 start, uInt16 lines) const bool CartDebug::addDirective(CartDebug::DisasmType type, uInt16 start, uInt16 end, int bank) { -#define PRINT_TAG(tag) \ - disasmTypeAsString(cerr, tag.type); \ - cerr << ": start = " << tag.start << ", end = " << tag.end << endl; - -#define PRINT_LIST(header) \ - cerr << header << endl; \ - for(DirectiveList::const_iterator d = list.begin(); d != list.end(); ++d) { \ - PRINT_TAG((*d)); } \ - cerr << endl; - if(end < start || start == 0 || end == 0) return false; @@ -525,24 +515,6 @@ bool CartDebug::addDirective(CartDebug::DisasmType type, return true; } -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -int CartDebug::getBank() -{ - return myConsole.cartridge().getBank(); -} - -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -int CartDebug::bankCount() const -{ - return myConsole.cartridge().bankCount(); -} - -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -string CartDebug::getCartType() const -{ - return myConsole.cartridge().name(); -} - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool CartDebug::addLabel(const string& label, uInt16 address) { @@ -814,7 +786,7 @@ string CartDebug::loadSymbolFile() { // Make sure the value doesn't represent a constant // For now, we simply ignore constants completely - AddrToLabel::const_iterator iter = myUserCLabels.find(value); + const auto& iter = myUserCLabels.find(value); if(iter == myUserCLabels.end() || !BSPF_equalsIgnoreCase(label, iter->second)) { // Check for period, and strip leading number @@ -865,11 +837,8 @@ string CartDebug::loadConfigFile() return "Unable to load directives from " + node.getPath(); // Erase all previous directives - for(Common::Array::iterator bi = myBankInfo.begin(); - bi != myBankInfo.end(); ++bi) - { - bi->directiveList.clear(); - } + for(auto& bi: myBankInfo) + bi.directiveList.clear(); int currentbank = 0; while(!in.eof()) @@ -1167,14 +1136,13 @@ string CartDebug::saveDisassembly() } } - AddrToLabel::const_iterator iter; if(myReserved.Label.size() > 0) { out << "\n;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n" << "; NON LOCATABLE\n" << ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n\n"; - for(iter = myReserved.Label.begin(); iter != myReserved.Label.end(); ++iter) - out << ALIGN(10) << iter->second << " = $" << iter->first << "\n"; + for(const auto& iter: myReserved.Label) + out << ALIGN(10) << iter.second << " = $" << iter.first << "\n"; } if(myUserLabels.size() > 0) @@ -1183,10 +1151,10 @@ string CartDebug::saveDisassembly() << "; USER DEFINED\n" << ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n\n"; int max_len = 0; - for(iter = myUserLabels.begin(); iter != myUserLabels.end(); ++iter) - max_len = BSPF_max(max_len, (int)iter->second.size()); - for(iter = myUserLabels.begin(); iter != myUserLabels.end(); ++iter) - out << ALIGN(max_len) << iter->second << " = $" << iter->first << "\n"; + for(const auto& iter: myUserLabels) + max_len = BSPF_max(max_len, (int)iter.second.size()); + for(const auto& iter: myUserLabels) + out << ALIGN(max_len) << iter.second << " = $" << iter.first << "\n"; } // And finally, output the disassembly @@ -1230,14 +1198,13 @@ string CartDebug::listConfig(int bank) { BankInfo& info = myBankInfo[b]; buf << "[" << b << "]" << endl; - for(DirectiveList::const_iterator i = info.directiveList.begin(); - i != info.directiveList.end(); ++i) + for(const auto& i: info.directiveList) { - if(i->type != CartDebug::NONE) + if(i.type != CartDebug::NONE) { buf << "(*) "; - disasmTypeAsString(buf, i->type); - buf << " " << Base::HEX4 << i->start << " " << Base::HEX4 << i->end << endl; + disasmTypeAsString(buf, i.type); + buf << " " << Base::HEX4 << i.start << " " << Base::HEX4 << i.end << endl; } } getBankDirectives(buf, info); @@ -1291,9 +1258,9 @@ void CartDebug::getCompletions(const char* in, StringList& completions) const // Now scan user-defined labels LabelToAddr::const_iterator iter; - for(iter = myUserAddresses.begin(); iter != myUserAddresses.end(); ++iter) + for(const auto& iter: myUserAddresses) { - const char* l = iter->first.c_str(); + const char* l = iter.first.c_str(); if(BSPF_startsWithIgnoreCase(l, in)) completions.push_back(l); } diff --git a/src/debugger/CartDebug.hxx b/src/debugger/CartDebug.hxx index 60a8c84f2..f24cbe064 100644 --- a/src/debugger/CartDebug.hxx +++ b/src/debugger/CartDebug.hxx @@ -182,19 +182,20 @@ class CartDebug : public DebuggerSystem // The following are convenience methods that query the cartridge object // for the desired information. /** - Get the current bank in use by the cartridge. + Get the current bank in use by the cartridge + (non-const because of use in YaccParser) */ - int getBank(); // non-const because of use in YaccParser + int getBank() { return myConsole.cartridge().getBank(); } /** Get the total number of banks supported by the cartridge. */ - int bankCount() const; + int bankCount() const { return myConsole.cartridge().bankCount(); } /** - Get the name/type of the cartridge. + Get the name/type of the cartridge. // FIXME - dead code */ - string getCartType() const; // FIXME - dead code + string getCartType() const { return myConsole.cartridge().name(); } /** Add a label and associated address. @@ -303,8 +304,8 @@ class CartDebug : public DebuggerSystem << endl << "addrlist: "; AddressList::const_iterator i; - for(i = b.addressList.begin(); i != b.addressList.end(); ++i) - os << HEX4 << *i << " "; + for(const auto& i: addressList) + os << HEX4 << i << " "; return os; } #endif diff --git a/src/debugger/Debugger.cxx b/src/debugger/Debugger.cxx index 975b6a27c..f0e714b20 100644 --- a/src/debugger/Debugger.cxx +++ b/src/debugger/Debugger.cxx @@ -557,14 +557,14 @@ bool Debugger::addFunction(const string& name, const string& definition, // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool Debugger::delFunction(const string& name) { - FunctionMap::iterator iter = functions.find(name); + const auto& iter = functions.find(name); if(iter == functions.end()) return false; functions.erase(name); delete iter->second; - FunctionDefMap::iterator def_iter = functionDefs.find(name); + const auto& def_iter = functionDefs.find(name); if(def_iter == functionDefs.end()) return false; @@ -575,21 +575,15 @@ bool Debugger::delFunction(const string& name) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - const Expression* Debugger::getFunction(const string& name) const { - FunctionMap::const_iterator iter = functions.find(name); - if(iter == functions.end()) - return 0; - else - return iter->second; + const auto& iter = functions.find(name); + return iter != functions.end() ? iter->second : nullptr; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - const string& Debugger::getFunctionDef(const string& name) const { - FunctionDefMap::const_iterator iter = functionDefs.find(name); - if(iter == functionDefs.end()) - return EmptyString; - else - return iter->second; + const auto& iter = functionDefs.find(name); + return iter != functionDefs.end() ? iter->second : EmptyString; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -647,10 +641,9 @@ string Debugger::builtinHelp() const // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Debugger::getCompletions(const char* in, StringList& list) const { - FunctionMap::const_iterator iter; - for(iter = functions.begin(); iter != functions.end(); ++iter) + for(const auto& iter: functions) { - const char* l = iter->first.c_str(); + const char* l = iter.first.c_str(); if(BSPF_equalsIgnoreCase(l, in)) list.push_back(l); } diff --git a/src/debugger/DebuggerParser.cxx b/src/debugger/DebuggerParser.cxx index 2d34ea992..82d36e770 100644 --- a/src/debugger/DebuggerParser.cxx +++ b/src/debugger/DebuggerParser.cxx @@ -602,17 +602,18 @@ bool DebuggerParser::saveScriptFile(string file) ofstream out(file.c_str()); FunctionDefMap funcs = debugger.getFunctionDefMap(); - for(FunctionDefMap::const_iterator i = funcs.begin(); i != funcs.end(); ++i) - out << "function " << i->first << " { " << i->second << " }" << endl; + for(const auto& i: funcs) + out << "function " << i.first << " { " << i.second << " }" << endl; - for(unsigned int i=0; i 0) { commandResult << "\nbreakifs:\n"; - for(unsigned int i = 0; i < conds.size(); i++) + for(uInt32 i = 0; i < conds.size(); i++) { commandResult << i << ": " << conds[i]; if(i != (conds.size() - 1)) commandResult << endl; @@ -1097,9 +1098,8 @@ void DebuggerParser::executeListfunctions() if(functions.size() > 0) { - FunctionDefMap::const_iterator iter; - for(iter = functions.begin(); iter != functions.end(); ++iter) - commandResult << iter->first << " -> " << iter->second << endl; + for(const auto& iter: functions) + commandResult << iter.first << " -> " << iter.second << endl; } else commandResult << "no user-defined functions"; @@ -1111,7 +1111,7 @@ void DebuggerParser::executeListtraps() { int count = 0; - for(unsigned int i=0; i<0x10000; i++) + for(uInt32 i = 0; i < 0x10000; ++i) { if(debugger.readTrap(i) || debugger.writeTrap(i)) { @@ -1231,7 +1231,7 @@ void DebuggerParser::executeRiot() void DebuggerParser::executeRom() { int addr = args[0]; - for(int i=1; i(myEvent, myProperties); // Construct the system and components - mySystem = new System(osystem); + mySystem = make_ptr(osystem); // The real controllers for this console will be added later // For now, we just add dummy joystick controllers, since autodetection @@ -176,8 +174,6 @@ Console::Console(OSystem& osystem, Cartridge& cart, const Properties& props) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Console::~Console() { - delete mySystem; - delete mySwitches; delete myCMHandler; delete myControllers[0]; delete myControllers[1]; diff --git a/src/emucore/Console.hxx b/src/emucore/Console.hxx index 04573aa6b..93d1c0a03 100644 --- a/src/emucore/Console.hxx +++ b/src/emucore/Console.hxx @@ -346,10 +346,10 @@ class Console : public Serializable TIA* myTIA; // Pointer to the switches on the front of the console - Switches* mySwitches; + unique_ptr mySwitches; // Pointer to the 6502 based system being emulated - System* mySystem; + unique_ptr mySystem; // Pointer to the 6532 (aka RIOT) (the debugger needs it) // A RIOT of my own! (...with apologies to The Clash...) diff --git a/src/emucore/EventHandler.cxx b/src/emucore/EventHandler.cxx index db24905ef..faaebd2ea 100644 --- a/src/emucore/EventHandler.cxx +++ b/src/emucore/EventHandler.cxx @@ -58,13 +58,11 @@ EventHandler::EventHandler(OSystem& osystem) : myOSystem(osystem), myOverlay(NULL), - myMouseControl(NULL), myState(S_NONE), myAllowAllDirectionsFlag(false), myFryingFlag(false), myUseCtrlKeyFlag(true), - mySkipMouseMotion(true), - myJoyHandler(NULL) + mySkipMouseMotion(true) { // Erase the key mapping array for(int i = 0; i < KBDK_LAST; ++i) @@ -77,7 +75,7 @@ EventHandler::EventHandler(OSystem& osystem) myComboTable[i][j] = Event::NoType; // Create joystick handler (to handle all joystick functionality) - myJoyHandler = new JoystickHandler(osystem); + myJoyHandler = make_ptr(osystem); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -90,9 +88,6 @@ EventHandler::~EventHandler() for(uInt32 i = 0; i < kMenuActionListSize; ++i) if(ourMenuActionList[i].key) free(ourMenuActionList[i].key); - - delete myMouseControl; myMouseControl = NULL; - delete myJoyHandler; myJoyHandler = NULL; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1196,7 +1191,7 @@ void EventHandler::setKeymap() if(event == Event::LastType && map.size() == KBDK_LAST * kNumModes) { // Fill the keymap table with events - IntArray::const_iterator event = map.begin(); + auto event = map.begin(); for(int mode = 0; mode < kNumModes; ++mode) for(int i = 0; i < KBDK_LAST; ++i) myKeyTable[i][mode] = (Event::Type) *event++; @@ -1781,8 +1776,6 @@ void EventHandler::setMouseControllerMode(const string& enable) { if(myOSystem.hasConsole()) { - delete myMouseControl; myMouseControl = NULL; - bool usemouse = false; if(BSPF_equalsIgnoreCase(enable, "always")) usemouse = true; @@ -1821,7 +1814,7 @@ void EventHandler::setMouseControllerMode(const string& enable) const string& control = usemouse ? myOSystem.console().properties().get(Controller_MouseAxis) : "none"; - myMouseControl = new MouseControl(myOSystem.console(), control); + myMouseControl = make_ptr(myOSystem.console(), control); myMouseControl->next(); // set first available mode } } diff --git a/src/emucore/EventHandler.hxx b/src/emucore/EventHandler.hxx index dd9e8b68b..7b3bce568 100644 --- a/src/emucore/EventHandler.hxx +++ b/src/emucore/EventHandler.hxx @@ -540,7 +540,7 @@ class EventHandler // MouseControl object, which takes care of switching the mouse between // all possible controller modes - MouseControl* myMouseControl; + unique_ptr myMouseControl; // Array of key events, indexed by StellaKey Event::Type myKeyTable[KBDK_LAST][kNumModes]; @@ -581,7 +581,7 @@ class EventHandler static const Event::Type SA_Key[2][12]; // Handler for all joystick addition/removal/mapping - JoystickHandler* myJoyHandler; + unique_ptr myJoyHandler; }; #endif diff --git a/src/emucore/EventJoyHandler.cxx b/src/emucore/EventJoyHandler.cxx index fec2c892c..4fb2f9bb3 100644 --- a/src/emucore/EventJoyHandler.cxx +++ b/src/emucore/EventJoyHandler.cxx @@ -139,7 +139,7 @@ bool EventHandler::StellaJoystick::setMap(const string& m) if((int)map.size() == numAxes * 2 * kNumModes) { // Fill the axes table with events - IntArray::const_iterator event = map.begin(); + auto event = map.begin(); for(int m = 0; m < kNumModes; ++m) for(int a = 0; a < numAxes; ++a) for(int k = 0; k < 2; ++k) @@ -148,7 +148,7 @@ bool EventHandler::StellaJoystick::setMap(const string& m) getValues(items[2], map); if((int)map.size() == numButtons * kNumModes) { - IntArray::const_iterator event = map.begin(); + auto event = map.begin(); for(int m = 0; m < kNumModes; ++m) for(int b = 0; b < numButtons; ++b) btnTable[b][m] = (Event::Type) *event++; @@ -156,7 +156,7 @@ bool EventHandler::StellaJoystick::setMap(const string& m) getValues(items[3], map); if((int)map.size() == numHats * 4 * kNumModes) { - IntArray::const_iterator event = map.begin(); + auto event = map.begin(); for(int m = 0; m < kNumModes; ++m) for(int h = 0; h < numHats; ++h) for(int k = 0; k < 4; ++k) @@ -260,9 +260,8 @@ EventHandler::JoystickHandler::JoystickHandler(OSystem& system) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - EventHandler::JoystickHandler::~JoystickHandler() { - map::const_iterator it; - for(it = myDatabase.begin(); it != myDatabase.end(); ++it) - delete it->second.joy; + for(const auto& i: myDatabase) + delete i.second.joy; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -270,14 +269,13 @@ void EventHandler::JoystickHandler::printDatabase() const { cerr << "---------------------------------------------------------" << endl << "joy database:" << endl; - map::const_iterator it; - for(it = myDatabase.begin(); it != myDatabase.end(); ++it) - cerr << it->first << endl << it->second << endl << endl; + for(const auto& i: myDatabase) + cerr << i.first << endl << i.second << endl << endl; cerr << "---------------------------------------------------------" << endl << "joy active:" << endl; - for(uInt32 i = 0; i < mySticks.size(); ++i) - cerr << *mySticks[i] << endl; + for(const auto& i: mySticks) + cerr << i << endl; cerr << endl; } @@ -291,7 +289,7 @@ int EventHandler::JoystickHandler::add(StellaJoystick* stick) // Figure out what type of joystick this is bool specialAdaptor = false; - if(stick->name.find("2600-daptor", 0) != string::npos) + if(BSPF_containsIgnoreCase(stick->name, "2600-daptor")) { // 2600-daptorII devices have 3 axes and 12 buttons, and the value of the z-axis // determines how those 12 buttons are used (not all buttons are used in all modes) @@ -306,7 +304,7 @@ int EventHandler::JoystickHandler::add(StellaJoystick* stick) specialAdaptor = true; } - else if(stick->name.find("Stelladaptor", 0) != string::npos) + else if(BSPF_containsIgnoreCase(stick->name, "Stelladaptor")) { stick->name = "Stelladaptor"; specialAdaptor = true; @@ -315,9 +313,8 @@ int EventHandler::JoystickHandler::add(StellaJoystick* stick) { // We need unique names for mappable devices int count = 0; - map::const_iterator c_it; - for(c_it = myDatabase.begin(); c_it != myDatabase.end(); ++c_it) - if(BSPF_startsWithIgnoreCase(c_it->first, stick->name)) + for(const auto& i: myDatabase) + if(BSPF_startsWithIgnoreCase(i.first, stick->name)) ++count; if(count > 1) @@ -335,7 +332,7 @@ int EventHandler::JoystickHandler::add(StellaJoystick* stick) mapStelladaptors(myOSystem.settings().getString("saport")); // Add stick to database - map::iterator it = myDatabase.find(stick->name); + auto it = myDatabase.find(stick->name); if(it != myDatabase.end()) // already present { it->second.joy = stick; @@ -364,7 +361,7 @@ int EventHandler::JoystickHandler::remove(int index) { StellaJoystick* stick = mySticks[index]; - map::iterator it = myDatabase.find(stick->name); + auto it = myDatabase.find(stick->name); if(it != myDatabase.end() && it->second.joy == stick) { ostringstream buf; @@ -398,33 +395,33 @@ void EventHandler::JoystickHandler::mapStelladaptors(const string& saport) saOrder[0] = 2; saOrder[1] = 1; } - for(uInt32 i = 0; i < mySticks.size(); ++i) + for(auto stick: mySticks) { - if(BSPF_startsWithIgnoreCase(mySticks[i]->name, "Stelladaptor")) + if(BSPF_startsWithIgnoreCase(stick->name, "Stelladaptor")) { if(saOrder[saCount] == 1) { - mySticks[i]->name += " (emulates left joystick port)"; - mySticks[i]->type = StellaJoystick::JT_STELLADAPTOR_LEFT; + stick->name += " (emulates left joystick port)"; + stick->type = StellaJoystick::JT_STELLADAPTOR_LEFT; } else if(saOrder[saCount] == 2) { - mySticks[i]->name += " (emulates right joystick port)"; - mySticks[i]->type = StellaJoystick::JT_STELLADAPTOR_RIGHT; + stick->name += " (emulates right joystick port)"; + stick->type = StellaJoystick::JT_STELLADAPTOR_RIGHT; } saCount++; } - else if(BSPF_startsWithIgnoreCase(mySticks[i]->name, "2600-daptor")) + else if(BSPF_startsWithIgnoreCase(stick->name, "2600-daptor")) { if(saOrder[saCount] == 1) { - mySticks[i]->name += " (emulates left joystick port)"; - mySticks[i]->type = StellaJoystick::JT_2600DAPTOR_LEFT; + stick->name += " (emulates left joystick port)"; + stick->type = StellaJoystick::JT_2600DAPTOR_LEFT; } else if(saOrder[saCount] == 2) { - mySticks[i]->name += " (emulates right joystick port)"; - mySticks[i]->type = StellaJoystick::JT_2600DAPTOR_RIGHT; + stick->name += " (emulates right joystick port)"; + stick->type = StellaJoystick::JT_2600DAPTOR_RIGHT; } saCount++; } @@ -531,13 +528,13 @@ void EventHandler::JoystickHandler::eraseMapping(Event::Type event, EventMode mo // Otherwise, only reset the given event if(event == Event::NoType) { - for(uInt32 i = 0; i < mySticks.size(); ++i) - mySticks[i]->eraseMap(mode); // erase all events + for(auto stick: mySticks) + stick->eraseMap(mode); // erase all events } else { - for(uInt32 i = 0; i < mySticks.size(); ++i) - mySticks[i]->eraseEvent(event, mode); // only reset the specific event + for(auto stick: mySticks) + stick->eraseEvent(event, mode); // only reset the specific event } } @@ -549,12 +546,9 @@ void EventHandler::JoystickHandler::saveMapping() ostringstream joybuf; joybuf << Event::LastType; - map::const_iterator it; - for(it = myDatabase.begin(); it != myDatabase.end(); ++it) + for(const auto& i: myDatabase) { - const string& map = it->second.joy ? - it->second.joy->getMap() : it->second.mapping; - + const string& map = i.second.joy ? i.second.joy->getMap() : i.second.mapping; if(map != "") joybuf << "^" << map; } diff --git a/src/emucore/FSNode.cxx b/src/emucore/FSNode.cxx index a7f4899ff..05052c092 100644 --- a/src/emucore/FSNode.cxx +++ b/src/emucore/FSNode.cxx @@ -70,8 +70,8 @@ bool FilesystemNode::getChildren(FSList& fslist, ListMode mode, bool hidden) con if (!_realNode->getChildren(tmp, mode, hidden)) return false; - for (AbstractFSList::iterator i = tmp.begin(); i != tmp.end(); ++i) - fslist.push_back(FilesystemNode(*i)); + for (const auto& i: tmp) + fslist.push_back(FilesystemNode(i)); return true; } diff --git a/src/emucore/FrameBuffer.cxx b/src/emucore/FrameBuffer.cxx index c7fd9c118..62028cb74 100644 --- a/src/emucore/FrameBuffer.cxx +++ b/src/emucore/FrameBuffer.cxx @@ -49,22 +49,15 @@ FrameBuffer::FrameBuffer(OSystem& osystem) : myOSystem(osystem), myInitializedCount(0), - myPausedCount(0), - myTIASurface(NULL) + myPausedCount(0) { - myMsg.surface = myStatsMsg.surface = NULL; + myMsg.surface = myStatsMsg.surface = nullptr; myMsg.enabled = myStatsMsg.enabled = false; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -FrameBuffer::~FrameBuffer(void) +FrameBuffer::~FrameBuffer() { - delete myFont; - delete myInfoFont; - delete mySmallFont; - delete myLauncherFont; - delete myTIASurface; - // Free all allocated surfaces while(!mySurfaceList.empty()) { @@ -107,15 +100,15 @@ bool FrameBuffer::initialize() // This font is used in a variety of situations when a really small // font is needed; we let the specific widget/dialog decide when to // use it - mySmallFont = new GUI::Font(GUI::stellaDesc); + mySmallFont = make_ptr(GUI::stellaDesc); // The general font used in all UI elements // This is determined by the size of the framebuffer - myFont = new GUI::Font(smallScreen ? GUI::stellaDesc : GUI::stellaMediumDesc); + myFont = make_ptr(smallScreen ? GUI::stellaDesc : GUI::stellaMediumDesc); // The info font used in all UI elements // This is determined by the size of the framebuffer - myInfoFont = new GUI::Font(smallScreen ? GUI::stellaDesc : GUI::consoleDesc); + myInfoFont = make_ptr(smallScreen ? GUI::stellaDesc : GUI::consoleDesc); // The font used by the ROM launcher // Normally, this is configurable by the user, except in the case of @@ -124,14 +117,14 @@ bool FrameBuffer::initialize() { const string& lf = myOSystem.settings().getString("launcherfont"); if(lf == "small") - myLauncherFont = new GUI::Font(GUI::consoleDesc); + myLauncherFont = make_ptr(GUI::consoleDesc); else if(lf == "medium") - myLauncherFont = new GUI::Font(GUI::stellaMediumDesc); + myLauncherFont = make_ptr(GUI::stellaMediumDesc); else - myLauncherFont = new GUI::Font(GUI::stellaLargeDesc); + myLauncherFont = make_ptr(GUI::stellaLargeDesc); } else - myLauncherFont = new GUI::Font(GUI::stellaDesc); + myLauncherFont = make_ptr(GUI::stellaDesc); // Determine possible TIA windowed zoom levels uInt32 maxZoom = maxWindowSizeForScreen((uInt32)kTIAMinW, (uInt32)kTIAMinH, @@ -160,7 +153,7 @@ bool FrameBuffer::initialize() FBSurface::setPalette(myPalette); // Create a TIA surface; we need it for rendering TIA images - myTIASurface = new TIASurface(myOSystem); + myTIASurface = make_ptr(myOSystem); return true; } @@ -241,12 +234,12 @@ FBInitStatus FrameBuffer::createDisplay(const string& title, myStatsMsg.w = infoFont().getMaxCharWidth() * 24 + 2; myStatsMsg.h = (infoFont().getFontHeight() + 2) * 2; - if(myStatsMsg.surface == NULL) + if(myStatsMsg.surface == nullptr) { uInt32 surfaceID = allocateSurface(myStatsMsg.w, myStatsMsg.h); myStatsMsg.surface = surface(surfaceID); } - if(myMsg.surface == NULL) + if(myMsg.surface == nullptr) { uInt32 surfaceID = allocateSurface((uInt32)kFBMinW, font().getFontHeight()+10); myMsg.surface = surface(surfaceID); @@ -589,8 +582,8 @@ uInt32 FrameBuffer::allocateSurface(int w, int h, const uInt32* data) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - FBSurface* FrameBuffer::surface(uInt32 id) const { - map::const_iterator iter = mySurfaceList.find(id); - return iter != mySurfaceList.end() ? iter->second : NULL; + const auto& iter = mySurfaceList.find(id); + return iter != mySurfaceList.end() ? iter->second : nullptr; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -602,11 +595,10 @@ void FrameBuffer::resetSurfaces() // Any derived FrameBuffer classes that call this method should be // aware of these restrictions, and act accordingly - map::iterator iter; - for(iter = mySurfaceList.begin(); iter != mySurfaceList.end(); ++iter) - iter->second->free(); - for(iter = mySurfaceList.begin(); iter != mySurfaceList.end(); ++iter) - iter->second->reload(); + for(auto& s: mySurfaceList) + s.second->free(); + for(auto& s: mySurfaceList) + s.second->reload(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -743,8 +735,8 @@ void FrameBuffer::setAvailableVidModes(uInt32 baseWidth, uInt32 baseHeight) { myWindowedModeList.clear(); - for(uInt32 i = 0; i < myFullscreenModeLists.size(); ++i) - myFullscreenModeLists[i].clear(); + for(auto& mode: myFullscreenModeLists) + mode.clear(); for(uInt32 i = myFullscreenModeLists.size(); i < myDisplays.size(); ++i) myFullscreenModeLists.push_back(VideoModeList()); diff --git a/src/emucore/FrameBuffer.hxx b/src/emucore/FrameBuffer.hxx index 8b45d2713..9e064a426 100644 --- a/src/emucore/FrameBuffer.hxx +++ b/src/emucore/FrameBuffer.hxx @@ -534,19 +534,19 @@ class FrameBuffer VariantList myRenderers; // The font object to use for the normal in-game GUI - GUI::Font* myFont; + unique_ptr myFont; // The info font object to use for the normal in-game GUI - GUI::Font* myInfoFont; + unique_ptr myInfoFont; // The font object to use when space is very limited - GUI::Font* mySmallFont; + unique_ptr mySmallFont; // The font object to use for the ROM launcher - GUI::Font* myLauncherFont; + unique_ptr myLauncherFont; // The TIASurface class takes responsibility for TIA rendering - TIASurface* myTIASurface; + unique_ptr myTIASurface; // Used for onscreen messages and frame statistics // (scanline count and framerate) diff --git a/src/emucore/MediaFactory.hxx b/src/emucore/MediaFactory.hxx index f65027286..d11ea322a 100644 --- a/src/emucore/MediaFactory.hxx +++ b/src/emucore/MediaFactory.hxx @@ -20,15 +20,21 @@ #ifndef MEDIA_FACTORY_HXX #define MEDIA_FACTORY_HXX +#include "bspf.hxx" + #include "OSystem.hxx" #include "Settings.hxx" +#include "SerialPort.hxx" #if defined(BSPF_UNIX) + #include "SerialPortUNIX.hxx" #include "SettingsUNIX.hxx" #include "OSystemUNIX.hxx" #elif defined(BSPF_WINDOWS) + #include "SerialPortWINDOWS.hxx" #include "SettingsWINDOWS.hxx" #include "OSystemWINDOWS.hxx" #elif defined(BSPF_MAC_OSX) + #include "SerialPortMACOSX.hxx" #include "SettingsMACOSX.hxx" #include "OSystemMACOSX.hxx" extern "C" { @@ -61,49 +67,62 @@ class MediaFactory { public: - static OSystem* createOSystem() + static unique_ptr createOSystem() { #if defined(BSPF_UNIX) - return new OSystemUNIX(); + return make_ptr(); #elif defined(BSPF_WINDOWS) - return new OSystemWINDOWS(); + return make_ptr(); #elif defined(BSPF_MAC_OSX) - return new OSystemMACOSX(); + return make_ptr(); #else #error Unsupported platform for OSystem! #endif } - static Settings* createSettings(OSystem& osystem) + static unique_ptr createSettings(OSystem& osystem) { #if defined(BSPF_UNIX) - return new SettingsUNIX(osystem); + return make_ptr(osystem); #elif defined(BSPF_WINDOWS) - return new SettingsWINDOWS(osystem); + return make_ptr(osystem); #elif defined(BSPF_MAC_OSX) - return new SettingsMACOSX(osystem); + return make_ptr(osystem); #else #error Unsupported platform for Settings! #endif } - static FrameBuffer* createVideo(OSystem& osystem) + static unique_ptr createSerialPort() { - return new FrameBufferSDL2(osystem); - } - - static Sound* createAudio(OSystem& osystem) - { - #ifdef SOUND_SUPPORT - return new SoundSDL2(osystem); + #if defined(BSPF_UNIX) + return make_ptr(); + #elif defined(BSPF_WINDOWS) + return make_ptr(); + #elif defined(BSPF_MAC_OSX) + return make_ptr(); #else - return new SoundNull(osystem); + return make_ptr(); #endif } - static EventHandler* createEventHandler(OSystem& osystem) + static unique_ptr createVideo(OSystem& osystem) { - return new EventHandlerSDL2(osystem); + return make_ptr(osystem); + } + + static unique_ptr createAudio(OSystem& osystem) + { + #ifdef SOUND_SUPPORT + return make_ptr(osystem); + #else + return make_ptr(osystem); + #endif + } + + static unique_ptr createEventHandler(OSystem& osystem) + { + return make_ptr(osystem); } }; diff --git a/src/emucore/OSystem.cxx b/src/emucore/OSystem.cxx index 2b51e1723..77956c16d 100644 --- a/src/emucore/OSystem.cxx +++ b/src/emucore/OSystem.cxx @@ -39,15 +39,6 @@ #include "CheatManager.hxx" #endif -#include "SerialPort.hxx" -#if defined(BSPF_UNIX) - #include "SerialPortUNIX.hxx" -#elif defined(BSPF_WINDOWS) - #include "SerialPortWINDOWS.hxx" -#elif defined(BSPF_MAC_OSX) - #include "SerialPortMACOSX.hxx" -#endif - #include "FSNode.hxx" #include "MD5.hxx" #include "Cart.hxx" @@ -60,6 +51,7 @@ #include "Widget.hxx" #include "Console.hxx" #include "Random.hxx" +#include "SerialPort.hxx" #include "StateManager.hxx" #include "Version.hxx" @@ -67,21 +59,9 @@ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - OSystem::OSystem() - : myEventHandler(NULL), - myFrameBuffer(NULL), - mySound(NULL), - mySettings(NULL), - myPropSet(NULL), - myConsole(NULL), - mySerialPort(NULL), - myMenu(NULL), - myCommandMenu(NULL), - myLauncher(NULL), + : myConsole(nullptr), myLauncherUsed(false), - myDebugger(NULL), - myCheatManager(NULL), - myStateManager(NULL), - myPNGLib(NULL), + myDebugger(nullptr), myQuitLoop(false), myRomFile(""), myRomMD5(""), @@ -116,43 +96,17 @@ OSystem::OSystem() myBuildInfo = info.str(); mySettings = MediaFactory::createSettings(*this); - myRandom = new Random(*this); + myRandom = make_ptr(*this); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - OSystem::~OSystem() { - delete myMenu; - delete myCommandMenu; - delete myLauncher; - // Remove any game console that is currently attached deleteConsole(); - - // These must be deleted after all the others - // This is a bit hacky, since it depends on ordering - // of d'tor calls #ifdef DEBUGGER_SUPPORT delete myDebugger; #endif -#ifdef CHEATCODE_SUPPORT - delete myCheatManager; -#endif - - delete myStateManager; - delete myPropSet; - - delete mySerialPort; - delete myPNGLib; - delete myZipHandler; - - // OSystem takes responsibility for these, since it called MediaFactory - // to create them - delete myFrameBuffer; - delete mySound; - delete myEventHandler; - delete myRandom; - delete mySettings; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -185,18 +139,18 @@ bool OSystem::create() myEventHandler->initialize(); // Create a properties set for us to use and set it up - myPropSet = new PropertiesSet(propertiesFile()); + myPropSet = make_ptr(propertiesFile()); #ifdef CHEATCODE_SUPPORT - myCheatManager = new CheatManager(*this); + myCheatManager = make_ptr(*this); myCheatManager->loadCheatDatabase(); #endif // Create menu and launcher GUI objects - myMenu = new Menu(*this); - myCommandMenu = new CommandMenu(*this); - myLauncher = new Launcher(*this); - myStateManager = new StateManager(*this); + myMenu = make_ptr(*this); + myCommandMenu = make_ptr(*this); + myLauncher = make_ptr(*this); + myStateManager = make_ptr(*this); // Create the sound object; the sound subsystem isn't actually // opened until needed, so this is non-blocking (on those systems @@ -206,25 +160,16 @@ bool OSystem::create() // Create the serial port object // This is used by any controller that wants to directly access // a real serial port on the system -#if defined(BSPF_UNIX) - mySerialPort = new SerialPortUNIX(); -#elif defined(BSPF_WINDOWS) - mySerialPort = new SerialPortWINDOWS(); -#elif defined(BSPF_MAC_OSX) - mySerialPort = new SerialPortMACOSX(); -#else - // Create an 'empty' serial port - mySerialPort = new SerialPort(); -#endif + mySerialPort = MediaFactory::createSerialPort(); // Re-initialize random seed myRandom->initSeed(); // Create PNG handler - myPNGLib = new PNGLibrary(*myFrameBuffer); + myPNGLib = make_ptr(*myFrameBuffer); // Create ZIP handler - myZipHandler = new ZipHandler(); + myZipHandler = make_ptr(); return true; } @@ -777,4 +722,4 @@ void OSystem::mainLoop() } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -ZipHandler* OSystem::myZipHandler = 0; +unique_ptr OSystem::myZipHandler = unique_ptr(); diff --git a/src/emucore/OSystem.hxx b/src/emucore/OSystem.hxx index b9737ec11..a420fb654 100644 --- a/src/emucore/OSystem.hxx +++ b/src/emucore/OSystem.hxx @@ -458,50 +458,50 @@ class OSystem protected: // Pointer to the EventHandler object - EventHandler* myEventHandler; + unique_ptr myEventHandler; // Pointer to the FrameBuffer object - FrameBuffer* myFrameBuffer; + unique_ptr myFrameBuffer; // Pointer to the Sound object - Sound* mySound; + unique_ptr mySound; // Pointer to the Settings object - Settings* mySettings; + unique_ptr mySettings; // Pointer to the Random object - Random* myRandom; + unique_ptr myRandom; // Pointer to the PropertiesSet object - PropertiesSet* myPropSet; + unique_ptr myPropSet; // Pointer to the (currently defined) Console object Console* myConsole; // Pointer to the serial port object - SerialPort* mySerialPort; + unique_ptr mySerialPort; // Pointer to the Menu object - Menu* myMenu; + unique_ptr myMenu; // Pointer to the CommandMenu object - CommandMenu* myCommandMenu; + unique_ptr myCommandMenu; // Pointer to the Launcher object - Launcher* myLauncher; + unique_ptr myLauncher; bool myLauncherUsed; // Pointer to the Debugger object Debugger* myDebugger; // Pointer to the CheatManager object - CheatManager* myCheatManager; + unique_ptr myCheatManager; // Pointer to the StateManager object - StateManager* myStateManager; + unique_ptr myStateManager; // PNG object responsible for loading/saving PNG images - PNGLibrary* myPNGLib; + unique_ptr myPNGLib; // The list of log messages string myLogMessages; @@ -519,7 +519,7 @@ class OSystem bool myQuitLoop; // ZIP static reference variable responsible for accessing ZIP files - static ZipHandler* myZipHandler; + static unique_ptr myZipHandler; private: string myBaseDir; diff --git a/src/emucore/PropsSet.cxx b/src/emucore/PropsSet.cxx index 802568abb..d955903bb 100644 --- a/src/emucore/PropsSet.cxx +++ b/src/emucore/PropsSet.cxx @@ -73,9 +73,8 @@ bool PropertiesSet::save(const string& filename) const return false; // Only save those entries in the external list - for(PropsList::const_iterator i = myExternalProps.begin(); - i != myExternalProps.end(); ++i) - i->second.save(out); + for(const auto& i: myExternalProps) + i.second.save(out); return true; } @@ -99,18 +98,18 @@ bool PropertiesSet::getMD5(const string& md5, Properties& properties, if(!useDefaults) { // Check external list - PropsList::const_iterator iter = myExternalProps.find(md5); - if(iter != myExternalProps.end()) + const auto ext = myExternalProps.find(md5); + if(ext != myExternalProps.end()) { - properties = iter->second; + properties = ext->second; found = true; } else // Search temp list { - iter = myTempProps.find(md5); - if(iter != myTempProps.end()) + const auto tmp = myTempProps.find(md5); + if(tmp != myTempProps.end()) { - properties = iter->second; + properties = tmp->second; found = true; } } @@ -178,8 +177,7 @@ void PropertiesSet::insert(const Properties& properties, bool save) // The status of 'save' determines which list to save to PropsList& list = save ? myExternalProps : myTempProps; - pair ret; - ret = list.insert(make_pair(md5, properties)); + auto ret = list.insert(make_pair(md5, properties)); if(ret.second == false) { // Remove old item and insert again @@ -227,6 +225,6 @@ void PropertiesSet::print() const // Now, print the resulting list Properties::printHeader(); - for(PropsList::const_iterator i = list.begin(); i != list.end(); ++i) - i->second.print(); + for(const auto& i: list) + i.second.print(); } diff --git a/src/gui/GameList.cxx b/src/gui/GameList.cxx index c7104dc6d..ccd47a875 100644 --- a/src/gui/GameList.cxx +++ b/src/gui/GameList.cxx @@ -40,37 +40,35 @@ GameList::~GameList() void GameList::appendGame(const string& name, const string& path, const string& md5, bool isDir) { - myArray.push_back(Entry(name, path, md5, isDir)); + myArray.emplace_back(name, path, md5, isDir); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void GameList::sortByName() { - if(myArray.size() <= 1) + if(myArray.size() < 2) return; - sort(myArray.begin(), myArray.end()); -} - -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -bool GameList::Entry::operator< (const Entry& g) const -{ - string::const_iterator it1 = _name.begin(); - string::const_iterator it2 = g._name.begin(); - - // Account for ending ']' character in directory entries - string::const_iterator end1 = _isdir ? _name.end() - 1 : _name.end(); - string::const_iterator end2 = g._isdir ? g._name.end() - 1 : g._name.end(); - - // Stop when either string's end has been reached - while((it1 != end1) && (it2 != end2)) - { - if(toupper(*it1) != toupper(*it2)) // letters differ? - return toupper(*it1) < toupper(*it2); - - // proceed to the next character in each string - ++it1; - ++it2; - } - return _name.size() < g._name.size(); + auto cmp = [](const Entry& a, const Entry& b) + { + auto it1 = a._name.begin(), it2 = b._name.begin(); + + // Account for ending ']' character in directory entries + auto end1 = a._isdir ? a._name.end() - 1 : a._name.end(); + auto end2 = b._isdir ? b._name.end() - 1 : b._name.end(); + + // Stop when either string's end has been reached + while((it1 != end1) && (it2 != end2)) + { + if(toupper(*it1) != toupper(*it2)) // letters differ? + return toupper(*it1) < toupper(*it2); + + // proceed to the next character in each string + ++it1; + ++it2; + } + return a._name.size() < b._name.size(); + }; + + sort(myArray.begin(), myArray.end(), cmp); } diff --git a/src/gui/GameList.hxx b/src/gui/GameList.hxx index d74488102..5e29bf1b4 100644 --- a/src/gui/GameList.hxx +++ b/src/gui/GameList.hxx @@ -65,8 +65,6 @@ class GameList { _name = name; _path = path; _md5 = md5; _isdir = isdir; } - - bool operator < (const Entry& a) const; }; vector myArray; }; diff --git a/src/gui/ListWidget.cxx b/src/gui/ListWidget.cxx index 5d1ca5c14..0960aa9a0 100644 --- a/src/gui/ListWidget.cxx +++ b/src/gui/ListWidget.cxx @@ -95,14 +95,14 @@ void ListWidget::setSelected(const string& item) else { uInt32 itemToSelect = 0; - StringList::const_iterator iter; - for(iter = _list.begin(); iter != _list.end(); ++iter, ++itemToSelect) + for(const auto& iter: _list) { - if(item == *iter) + if(item == iter) { selected = itemToSelect; break; } + ++itemToSelect; } if(itemToSelect > _list.size() || selected == -1) selected = 0; @@ -264,9 +264,9 @@ bool ListWidget::handleText(char text) // quite big lists to deal with -- so for now we can live with this lazy // implementation :-) int newSelectedItem = 0; - for (StringList::const_iterator i = _list.begin(); i != _list.end(); ++i) + for(const auto& i: _list) { - if(BSPF_startsWithIgnoreCase(*i, _quickSelectStr)) + if(BSPF_startsWithIgnoreCase(i, _quickSelectStr)) { _selectedItem = newSelectedItem; break; diff --git a/src/tools/check-sig.cxx b/src/tools/check-sig.cxx index de7f66d57..d8f0a0bcd 100644 --- a/src/tools/check-sig.cxx +++ b/src/tools/check-sig.cxx @@ -76,8 +76,8 @@ int main(int ac, char* av[]) if(result > 0) { cout << setw(3) << result << " hits: \'" << av[2] << "\' - \"" << av[1] << "\" @"; - for(list::iterator it = locations.begin(); it != locations.end(); ++it) - cout << ' ' << hex << ((int)*it + offset); + for(const auto& it: locations) + cout << ' ' << hex << (it + offset); cout << endl; }