From dcc93232dbc0d7c2685025de566f42287a44faed Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Thu, 2 Jun 2016 20:21:11 +0200 Subject: [PATCH] using std::vector> instead of simple_list in cheat engine (nw) --- src/frontend/mame/cheat.cpp | 116 +++++++++++++++--------------- src/frontend/mame/cheat.h | 41 +++-------- src/frontend/mame/luaengine.cpp | 4 +- src/frontend/mame/ui/cheatopt.cpp | 10 +-- 4 files changed, 76 insertions(+), 95 deletions(-) diff --git a/src/frontend/mame/cheat.cpp b/src/frontend/mame/cheat.cpp index f3005ab33e9..a2d4eeadc1f 100644 --- a/src/frontend/mame/cheat.cpp +++ b/src/frontend/mame/cheat.cpp @@ -156,10 +156,12 @@ cheat_parameter::cheat_parameter(cheat_manager &manager, symbol_table &symbols, int format = xml_get_attribute_int_format(itemnode, "value"); // allocate and append a new item - item &curitem = m_itemlist.append(*global_alloc(item(itemnode->value, value, format))); + auto curitem = std::make_unique(itemnode->value, value, format); // ensure the maximum expands to suit - m_maxval = MAX(m_maxval, curitem.value()); + m_maxval = MAX(m_maxval, curitem->value()); + + m_itemlist.push_back(std::move(curitem)); } // add a variable to the symbol table for our value @@ -182,10 +184,10 @@ const char *cheat_parameter::text() { // if not, we're an item cheat m_curtext = string_format("??? (%d)", UINT32(m_value)); - for (item &curitem : m_itemlist) - if (curitem.value() == m_value) + for (auto &curitem : m_itemlist) + if (curitem->value() == m_value) { - m_curtext.assign(curitem.text()); + m_curtext.assign(curitem->text()); break; } } @@ -217,8 +219,8 @@ void cheat_parameter::save(emu_file &cheatfile) const // iterate over items else { - for (const item &curitem : m_itemlist) - cheatfile.printf("\t\t\t%s\n", curitem.value().format().c_str(), curitem.text()); + for (auto &curitem : m_itemlist) + cheatfile.printf("\t\t\t%s\n", curitem->value().format().c_str(), curitem->text()); cheatfile.printf("\t\t\n"); } } @@ -233,7 +235,7 @@ bool cheat_parameter::set_minimum_state() UINT64 origvalue = m_value; // set based on whether we have an item list - m_value = (!has_itemlist()) ? m_minval : m_itemlist.first()->value(); + m_value = (!has_itemlist()) ? m_minval : m_itemlist.front()->value(); return (m_value != origvalue); } @@ -259,10 +261,12 @@ bool cheat_parameter::set_prev_state() // if not, we're an item cheat else { - item *curitem, *previtem = nullptr; - for (curitem = m_itemlist.first(); curitem != nullptr; previtem = curitem, curitem = curitem->next()) + item *previtem = nullptr; + for (auto &curitem : m_itemlist) { if (curitem->value() == m_value) break; + previtem = curitem.get(); + } if (previtem != nullptr) m_value = previtem->value(); } @@ -291,12 +295,12 @@ bool cheat_parameter::set_next_state() // if not, we're an item cheat else { - item *curitem; - for (curitem = m_itemlist.first(); curitem != nullptr; curitem = curitem->next()) - if (curitem->value() == m_value) + std::vector>::iterator it; + for (it = m_itemlist.begin(); it != m_itemlist.end(); ++it) + if (it->get()->value() == m_value) break; - if (curitem != nullptr && curitem->next() != nullptr) - m_value = curitem->next()->value(); + if (it != m_itemlist.end() && (++it != m_itemlist.end())) + m_value = (++it)->get()->value(); } return (m_value != origvalue); @@ -331,11 +335,11 @@ cheat_script::cheat_script(cheat_manager &manager, symbol_table &symbols, const { // handle action nodes if (strcmp(entrynode->name, "action") == 0) - m_entrylist.append(*global_alloc(script_entry(manager, symbols, filename, *entrynode, true))); + m_entrylist.push_back(std::make_unique(manager, symbols, filename, *entrynode, true)); // handle output nodes else if (strcmp(entrynode->name, "output") == 0) - m_entrylist.append(*global_alloc(script_entry(manager, symbols, filename, *entrynode, false))); + m_entrylist.push_back(std::make_unique(manager, symbols, filename, *entrynode, false)); // anything else is ignored else @@ -358,8 +362,8 @@ void cheat_script::execute(cheat_manager &manager, UINT64 &argindex) return; // iterate over entries - for (script_entry &entry : m_entrylist) - entry.execute(manager, argindex); + for (auto &entry : m_entrylist) + entry->execute(manager, argindex); } @@ -382,8 +386,8 @@ void cheat_script::save(emu_file &cheatfile) const cheatfile.printf(">\n"); // output entries - for (const script_entry &entry : m_entrylist) - entry.save(cheatfile); + for (auto &entry : m_entrylist) + entry->save(cheatfile); // close the tag cheatfile.printf("\t\t\n"); @@ -395,9 +399,8 @@ void cheat_script::save(emu_file &cheatfile) const //------------------------------------------------- cheat_script::script_entry::script_entry(cheat_manager &manager, symbol_table &symbols, const char *filename, xml_data_node &entrynode, bool isaction) - : m_next(nullptr), - m_condition(&symbols), - m_expression(&symbols) + : m_condition(&symbols), + m_expression(&symbols) { const char *expression = nullptr; try @@ -440,10 +443,12 @@ cheat_script::script_entry::script_entry(cheat_manager &manager, symbol_table &s int totalargs = 0; for (xml_data_node *argnode = xml_get_sibling(entrynode.child, "argument"); argnode != nullptr; argnode = xml_get_sibling(argnode->next, "argument")) { - output_argument &curarg = m_arglist.append(*global_alloc(output_argument(manager, symbols, filename, *argnode))); - + auto curarg = std::make_unique(manager, symbols, filename, *argnode); // verify we didn't overrun the argument count - totalargs += curarg.count(); + totalargs += curarg->count(); + + m_arglist.push_back(std::move(curarg)); + if (totalargs > MAX_ARGUMENTS) throw emu_fatalerror("%s.xml(%d): too many arguments (found %d, max is %d)\n", filename, argnode->line, totalargs, MAX_ARGUMENTS); } @@ -500,8 +505,8 @@ void cheat_script::script_entry::execute(cheat_manager &manager, UINT64 &arginde // iterate over arguments and evaluate them UINT64 params[MAX_ARGUMENTS]; int curarg = 0; - for (output_argument &arg : m_arglist) - curarg += arg.values(argindex, ¶ms[curarg]); + for (auto &arg : m_arglist) + curarg += arg->values(argindex, ¶ms[curarg]); // generate the astring manager.get_output_astring(m_line, m_justify) = string_format(m_format, @@ -544,15 +549,15 @@ void cheat_script::script_entry::save(emu_file &cheatfile) const cheatfile.printf(" align=\"center\""); else if (m_justify == JUSTIFY_RIGHT) cheatfile.printf(" align=\"right\""); - if (m_arglist.count() == 0) + if (m_arglist.size() == 0) cheatfile.printf(" />\n"); // output arguments else { cheatfile.printf(">\n"); - for (const output_argument &curarg : m_arglist) - curarg.save(cheatfile); + for (auto &curarg : m_arglist) + curarg->save(cheatfile); cheatfile.printf("\t\t\t\n"); } } @@ -568,8 +573,8 @@ void cheat_script::script_entry::validate_format(const char *filename, int line) { // first count arguments int argsprovided = 0; - for (const output_argument &curarg : m_arglist) - argsprovided += curarg.count(); + for (auto &curarg : m_arglist) + argsprovided += curarg->count(); // now scan the string for valid argument usage const char *p = strchr(m_format.c_str(), '%'); @@ -603,9 +608,8 @@ void cheat_script::script_entry::validate_format(const char *filename, int line) //------------------------------------------------- cheat_script::script_entry::output_argument::output_argument(cheat_manager &manager, symbol_table &symbols, const char *filename, xml_data_node &argnode) - : m_next(nullptr), - m_expression(&symbols), - m_count(0) + : m_expression(&symbols), + m_count(0) { // first extract attributes m_count = xml_get_attribute_int(&argnode, "count", 1); @@ -673,7 +677,6 @@ void cheat_script::script_entry::output_argument::save(emu_file &cheatfile) cons cheat_entry::cheat_entry(cheat_manager &manager, symbol_table &globaltable, const char *filename, xml_data_node &cheatnode) : m_manager(manager), - m_next(nullptr), m_symbols(&manager.machine(), &globaltable), m_state(SCRIPT_STATE_OFF), m_numtemp(DEFAULT_TEMP_VARIABLES), @@ -1034,8 +1037,8 @@ std::unique_ptr &cheat_entry::script_for_state(script_state state) bool cheat_entry::is_duplicate() const { - for (cheat_entry &scannode : manager().entries()) - if (strcmp(scannode.description(), description()) == 0) + for (auto &scannode : manager().entries()) + if (strcmp(scannode->description(), description()) == 0) return true; return false; } @@ -1100,9 +1103,9 @@ void cheat_manager::set_enable(bool enable) if (!m_disabled && !enable) { // iterate over running cheats and execute any OFF Scripts - for (cheat_entry &cheat : m_cheatlist) - if (cheat.state() == SCRIPT_STATE_RUN) - cheat.execute_off_script(); + for (auto &cheat : m_cheatlist) + if (cheat->state() == SCRIPT_STATE_RUN) + cheat->execute_off_script(); machine().popmessage("Cheats Disabled"); m_disabled = true; } @@ -1112,9 +1115,9 @@ void cheat_manager::set_enable(bool enable) { // iterate over running cheats and execute any ON Scripts m_disabled = false; - for (cheat_entry &cheat : m_cheatlist) - if (cheat.state() == SCRIPT_STATE_RUN) - cheat.execute_on_script(); + for (auto &cheat : m_cheatlist) + if (cheat->state() == SCRIPT_STATE_RUN) + cheat->execute_on_script(); machine().popmessage("Cheats Enabled"); } } @@ -1132,7 +1135,7 @@ void cheat_manager::reload() return; // free everything - m_cheatlist.reset(); + m_cheatlist.clear(); // reset state m_framecount = 0; @@ -1166,11 +1169,11 @@ void cheat_manager::reload() } // if we haven't found the cheats yet, load by basename - if (m_cheatlist.count() == 0) + if (m_cheatlist.size() == 0) load_cheats(machine().basename()); // temporary: save the file back out as output.xml for comparison - if (m_cheatlist.count() != 0) + if (m_cheatlist.size() != 0) save_all("output"); } @@ -1199,8 +1202,8 @@ bool cheat_manager::save_all(const char *filename) cheatfile.printf("\n", CHEAT_VERSION); // iterate over cheats in the list and save them - for (cheat_entry &cheat : m_cheatlist) - cheat.save(cheatfile); + for (auto &cheat : m_cheatlist) + cheat->save(cheatfile); // close out the file cheatfile.printf("\n"); @@ -1358,8 +1361,8 @@ void cheat_manager::frame_update() elem.clear(); // iterate over running cheats and execute them - for (cheat_entry &cheat : m_cheatlist) - cheat.frame_update(); + for (auto &cheat : m_cheatlist) + cheat->frame_update(); // increment the frame counter m_framecount++; @@ -1416,16 +1419,15 @@ void cheat_manager::load_cheats(const char *filename) for (xml_data_node *cheatnode = xml_get_sibling(mamecheatnode->child, "cheat"); cheatnode != nullptr; cheatnode = xml_get_sibling(cheatnode->next, "cheat")) { // load this entry - cheat_entry *curcheat = global_alloc(cheat_entry(*this, m_symtable, filename, *cheatnode)); + auto curcheat = std::make_unique(*this, m_symtable, filename, *cheatnode); // make sure we're not a duplicate if (REMOVE_DUPLICATE_CHEATS && curcheat->is_duplicate()) { osd_printf_verbose("Ignoring duplicate cheat '%s' from file %s\n", curcheat->description(), cheatfile.fullpath()); - global_free(curcheat); } else // add to the end of the list - m_cheatlist.append(*curcheat); + m_cheatlist.push_back(std::move(curcheat)); } // free the file and loop for the next one @@ -1440,7 +1442,7 @@ void cheat_manager::load_cheats(const char *filename) catch (emu_fatalerror &err) { osd_printf_error("%s\n", err.string()); - m_cheatlist.reset(); + m_cheatlist.clear(); if (rootnode != nullptr) xml_file_free(rootnode); } diff --git a/src/frontend/mame/cheat.h b/src/frontend/mame/cheat.h index 71cb149c62c..cc6dcf14e59 100644 --- a/src/frontend/mame/cheat.h +++ b/src/frontend/mame/cheat.h @@ -76,9 +76,9 @@ public: // queries const char *text(); - bool has_itemlist() const { return (m_itemlist.count() != 0); } - bool is_minimum() const { return (m_value == ((m_itemlist.count() == 0) ? m_minval : m_itemlist.first()->value())); } - bool is_maximum() const { return (m_value == ((m_itemlist.count() == 0) ? m_maxval : m_itemlist.last()->value())); } + bool has_itemlist() const { return (m_itemlist.size() != 0); } + bool is_minimum() const { return (m_value == ((m_itemlist.size() == 0) ? m_minval : m_itemlist.front()->value())); } + bool is_maximum() const { return (m_value == ((m_itemlist.size() == 0) ? m_maxval : m_itemlist.back()->value())); } // state setters bool set_minimum_state(); @@ -92,23 +92,18 @@ private: // a single item in a parameter item list class item { - friend class simple_list; - public: // construction/destruction item(const char *text, UINT64 value, int valformat) - : m_next(nullptr), - m_text(text), - m_value(value, valformat) { } + : m_text(text), + m_value(value, valformat) { } // getters - item *next() const { return m_next; } const number_and_format &value() const { return m_value; } const char *text() const { return m_text.c_str(); } private: // internal state - item * m_next; // next item in list std::string m_text; // name of the item number_and_format m_value; // value of the item }; @@ -119,7 +114,7 @@ private: number_and_format m_stepval; // step value UINT64 m_value; // live value of the parameter std::string m_curtext; // holding for a value string - simple_list m_itemlist; // list of items + std::vector> m_itemlist; // list of items }; @@ -128,8 +123,6 @@ private: // a script entry, specifying which state to execute under class cheat_script { - friend class simple_list; - public: // construction/destruction cheat_script(cheat_manager &manager, symbol_table &symbols, const char *filename, xml_data_node &scriptnode); @@ -145,15 +138,10 @@ private: // an entry within the script class script_entry { - friend class simple_list; - public: // construction/destruction script_entry(cheat_manager &manager, symbol_table &symbols, const char *filename, xml_data_node &entrynode, bool isaction); - // getters - script_entry *next() const { return m_next; } - // actions void execute(cheat_manager &manager, UINT64 &argindex); void save(emu_file &cheatfile) const; @@ -162,14 +150,11 @@ private: // an argument for output class output_argument { - friend class simple_list; - public: // construction/destruction output_argument(cheat_manager &manager, symbol_table &symbols, const char *filename, xml_data_node &argnode); // getters - output_argument *next() const { return m_next; } int count() const { return m_count; } int values(UINT64 &argindex, UINT64 *result); @@ -178,7 +163,6 @@ private: private: // internal state - output_argument * m_next; // link to next argument parsed_expression m_expression; // expression for argument UINT64 m_count; // number of repetitions }; @@ -187,11 +171,10 @@ private: void validate_format(const char *filename, int line); // internal state - script_entry * m_next; // link to next entry parsed_expression m_condition; // condition under which this is executed parsed_expression m_expression; // expression to execute std::string m_format; // string format to print - simple_list m_arglist; // list of arguments + std::vector> m_arglist; // list of arguments INT8 m_line; // which line to print on UINT8 m_justify; // justification when printing @@ -200,7 +183,7 @@ private: }; // internal state - simple_list m_entrylist; // list of actions to perform + std::vector> m_entrylist; // list of actions to perform script_state m_state; // which state this script is for }; @@ -210,8 +193,6 @@ private: // a single cheat class cheat_entry { - friend class simple_list; - public: // construction/destruction cheat_entry(cheat_manager &manager, symbol_table &globaltable, const char *filename, xml_data_node &cheatnode); @@ -219,7 +200,6 @@ public: // getters cheat_manager &manager() const { return m_manager; } - cheat_entry *next() const { return m_next; } script_state state() const { return m_state; } const char *description() const { return m_description.c_str(); } const char *comment() const { return m_comment.c_str(); } @@ -265,7 +245,6 @@ private: // internal state cheat_manager & m_manager; // reference to our manager - cheat_entry * m_next; // next cheat entry std::string m_description; // string description/menu title std::string m_comment; // comment data std::unique_ptr m_parameter; // parameter @@ -295,7 +274,7 @@ public: // getters running_machine &machine() const { return m_machine; } bool enabled() const { return !m_disabled; } - const simple_list &entries() const { return m_cheatlist; } + const std::vector> &entries() const { return m_cheatlist; } // setters void set_enable(bool enable = true); @@ -320,7 +299,7 @@ private: // internal state running_machine & m_machine; // reference to our machine - simple_list m_cheatlist; // cheat list + std::vector> m_cheatlist; // cheat list UINT64 m_framecount; // frame count std::vector m_output; // array of output strings std::vector m_justify; // justification for each string diff --git a/src/frontend/mame/luaengine.cpp b/src/frontend/mame/luaengine.cpp index 3606b5322a2..1ab9ba8cc1a 100644 --- a/src/frontend/mame/luaengine.cpp +++ b/src/frontend/mame/luaengine.cpp @@ -605,8 +605,8 @@ luabridge::LuaRef lua_engine::l_cheat_get_entries(const cheat_manager *c) luabridge::LuaRef entry_table = luabridge::LuaRef::newTable(L); int cheatnum = 0; - for (cheat_entry &entry : cm->entries()) { - entry_table[cheatnum++] = &entry; + for (auto &entry : cm->entries()) { + entry_table[cheatnum++] = entry.get(); } return entry_table; diff --git a/src/frontend/mame/ui/cheatopt.cpp b/src/frontend/mame/ui/cheatopt.cpp index 34a4f253c6d..b194510e1c9 100644 --- a/src/frontend/mame/ui/cheatopt.cpp +++ b/src/frontend/mame/ui/cheatopt.cpp @@ -52,8 +52,8 @@ void menu_cheat::handle() /* handle reset all + reset all cheats for reload all option */ if ((menu_event->itemref == ITEMREF_CHEATS_RESET_ALL || menu_event->itemref == ITEMREF_CHEATS_RELOAD_ALL) && menu_event->iptkey == IPT_UI_SELECT) { - for (cheat_entry &curcheat : mame_machine_manager::instance()->cheat().entries()) - if (curcheat.select_default_state()) + for (auto &curcheat : mame_machine_manager::instance()->cheat().entries()) + if (curcheat->select_default_state()) changed = true; } @@ -141,11 +141,11 @@ void menu_cheat::populate() // add other cheats if (!mame_machine_manager::instance()->cheat().entries().empty()) { - for (cheat_entry &curcheat : mame_machine_manager::instance()->cheat().entries()) + for (auto &curcheat : mame_machine_manager::instance()->cheat().entries()) { UINT32 flags; - curcheat.menu_text(text, subtext, flags); - item_append(text.c_str(), subtext.c_str(), flags, &curcheat); + curcheat->menu_text(text, subtext, flags); + item_append(text.c_str(), subtext.c_str(), flags, curcheat.get()); } /* add a separator */