Refactoring/cleanup to state load/save handling

- Changed running_machine::schedule_[load|save]() to take 'std::string &&' instead of 'const char *'
- Changed running_machine::saveload_schedule to be 'enum class'
This commit is contained in:
Nathan Woods 2017-05-06 08:24:51 -04:00 committed by Vas Crabb
parent 36a43a078b
commit c5ef33acdc
3 changed files with 35 additions and 35 deletions

View File

@ -125,7 +125,7 @@ running_machine::running_machine(const machine_config &_config, machine_manager
m_ui_active(_config.options().ui_active()), m_ui_active(_config.options().ui_active()),
m_basename(_config.gamedrv().name), m_basename(_config.gamedrv().name),
m_sample_rate(_config.options().sample_rate()), m_sample_rate(_config.options().sample_rate()),
m_saveload_schedule(SLS_NONE), m_saveload_schedule(saveload_schedule::NONE),
m_saveload_schedule_time(attotime::zero), m_saveload_schedule_time(attotime::zero),
m_saveload_searchpath(nullptr), m_saveload_searchpath(nullptr),
@ -335,7 +335,7 @@ int running_machine::run(bool quiet)
soft_reset(); soft_reset();
// handle initial load // handle initial load
if (m_saveload_schedule != SLS_NONE) if (m_saveload_schedule != saveload_schedule::NONE)
handle_saveload(); handle_saveload();
export_http_api(); export_http_api();
@ -344,7 +344,7 @@ int running_machine::run(bool quiet)
// run the CPUs until a reset or exit // run the CPUs until a reset or exit
m_hard_reset_pending = false; m_hard_reset_pending = false;
while ((!m_hard_reset_pending && !m_exit_pending) || m_saveload_schedule != SLS_NONE) while ((!m_hard_reset_pending && !m_exit_pending) || m_saveload_schedule != saveload_schedule::NONE)
{ {
g_profiler.start(PROFILER_EXTRA); g_profiler.start(PROFILER_EXTRA);
@ -361,7 +361,7 @@ int running_machine::run(bool quiet)
m_video->frame_update(); m_video->frame_update();
// handle save/load // handle save/load
if (m_saveload_schedule != SLS_NONE) if (m_saveload_schedule != saveload_schedule::NONE)
handle_saveload(); handle_saveload();
g_profiler.stop(); g_profiler.stop();
@ -571,7 +571,7 @@ std::string running_machine::get_statename(const char *option) const
// for state loading/saving // for state loading/saving
//------------------------------------------------- //-------------------------------------------------
std::string running_machine::compose_saveload_filename(const char *filename, const char **searchpath) std::string running_machine::compose_saveload_filename(std::string &&filename, const char **searchpath)
{ {
std::string result; std::string result;
@ -581,7 +581,7 @@ std::string running_machine::compose_saveload_filename(const char *filename, con
// if so, this is easy // if so, this is easy
if (searchpath != nullptr) if (searchpath != nullptr)
*searchpath = nullptr; *searchpath = nullptr;
result = filename; result = std::move(filename);
} }
else else
{ {
@ -603,10 +603,10 @@ std::string running_machine::compose_saveload_filename(const char *filename, con
// for state loading/saving // for state loading/saving
//------------------------------------------------- //-------------------------------------------------
void running_machine::set_saveload_filename(const char *filename) void running_machine::set_saveload_filename(std::string &&filename)
{ {
// compose the save/load filename and persist it // compose the save/load filename and persist it
m_saveload_pending_file = compose_saveload_filename(filename, &m_saveload_searchpath); m_saveload_pending_file = compose_saveload_filename(std::move(filename), &m_saveload_searchpath);
} }
@ -615,13 +615,13 @@ void running_machine::set_saveload_filename(const char *filename)
// soon as possible // soon as possible
//------------------------------------------------- //-------------------------------------------------
void running_machine::schedule_save(const char *filename) void running_machine::schedule_save(std::string &&filename)
{ {
// specify the filename to save or load // specify the filename to save or load
set_saveload_filename(filename); set_saveload_filename(std::move(filename));
// note the start time and set a timer for the next timeslice to actually schedule it // note the start time and set a timer for the next timeslice to actually schedule it
m_saveload_schedule = SLS_SAVE; m_saveload_schedule = saveload_schedule::SAVE;
m_saveload_schedule_time = this->time(); m_saveload_schedule_time = this->time();
// we can't be paused since we need to clear out anonymous timers // we can't be paused since we need to clear out anonymous timers
@ -639,7 +639,7 @@ void running_machine::immediate_save(const char *filename)
set_saveload_filename(filename); set_saveload_filename(filename);
// set up some parameters for handle_saveload() // set up some parameters for handle_saveload()
m_saveload_schedule = SLS_SAVE; m_saveload_schedule = saveload_schedule::SAVE;
m_saveload_schedule_time = this->time(); m_saveload_schedule_time = this->time();
// jump right into the save, anonymous timers can't hurt us! // jump right into the save, anonymous timers can't hurt us!
@ -652,13 +652,13 @@ void running_machine::immediate_save(const char *filename)
// soon as possible // soon as possible
//------------------------------------------------- //-------------------------------------------------
void running_machine::schedule_load(const char *filename) void running_machine::schedule_load(std::string &&filename)
{ {
// specify the filename to save or load // specify the filename to save or load
set_saveload_filename(filename); set_saveload_filename(std::move(filename));
// note the start time and set a timer for the next timeslice to actually schedule it // note the start time and set a timer for the next timeslice to actually schedule it
m_saveload_schedule = SLS_LOAD; m_saveload_schedule = saveload_schedule::LOAD;
m_saveload_schedule_time = this->time(); m_saveload_schedule_time = this->time();
// we can't be paused since we need to clear out anonymous timers // we can't be paused since we need to clear out anonymous timers
@ -676,7 +676,7 @@ void running_machine::immediate_load(const char *filename)
set_saveload_filename(filename); set_saveload_filename(filename);
// set up some parameters for handle_saveload() // set up some parameters for handle_saveload()
m_saveload_schedule = SLS_LOAD; m_saveload_schedule = saveload_schedule::LOAD;
m_saveload_schedule_time = this->time(); m_saveload_schedule_time = this->time();
// jump right into the load, anonymous timers can't hurt us // jump right into the load, anonymous timers can't hurt us
@ -860,7 +860,7 @@ void running_machine::handle_saveload()
// if no name, bail // if no name, bail
if (!m_saveload_pending_file.empty()) if (!m_saveload_pending_file.empty())
{ {
const char *const opname = (m_saveload_schedule == SLS_LOAD) ? "load" : "save"; const char *const opname = (m_saveload_schedule == saveload_schedule::LOAD) ? "load" : "save";
// if there are anonymous timers, we can't save just yet, and we can't load yet either // if there are anonymous timers, we can't save just yet, and we can't load yet either
// because the timers might overwrite data we have loaded // because the timers might overwrite data we have loaded
@ -874,17 +874,17 @@ void running_machine::handle_saveload()
} }
else else
{ {
u32 const openflags = (m_saveload_schedule == SLS_LOAD) ? OPEN_FLAG_READ : (OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS); u32 const openflags = (m_saveload_schedule == saveload_schedule::LOAD) ? OPEN_FLAG_READ : (OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS);
// open the file // open the file
emu_file file(m_saveload_searchpath, openflags); emu_file file(m_saveload_searchpath, openflags);
auto const filerr = file.open(m_saveload_pending_file.c_str()); auto const filerr = file.open(m_saveload_pending_file.c_str());
if (filerr == osd_file::error::NONE) if (filerr == osd_file::error::NONE)
{ {
const char *const opnamed = (m_saveload_schedule == SLS_LOAD) ? "loaded" : "saved"; const char *const opnamed = (m_saveload_schedule == saveload_schedule::LOAD) ? "loaded" : "saved";
// read/write the save state // read/write the save state
save_error saverr = (m_saveload_schedule == SLS_LOAD) ? m_save.read_file(file) : m_save.write_file(file); save_error saverr = (m_saveload_schedule == saveload_schedule::LOAD) ? m_save.read_file(file) : m_save.write_file(file);
// handle the result // handle the result
switch (saverr) switch (saverr)
@ -918,7 +918,7 @@ void running_machine::handle_saveload()
} }
// close and perhaps delete the file // close and perhaps delete the file
if (saverr != STATERR_NONE && m_saveload_schedule == SLS_SAVE) if (saverr != STATERR_NONE && m_saveload_schedule == saveload_schedule::SAVE)
file.remove_on_close(); file.remove_on_close();
} }
else else
@ -929,7 +929,7 @@ void running_machine::handle_saveload()
// unschedule the operation // unschedule the operation
m_saveload_pending_file.clear(); m_saveload_pending_file.clear();
m_saveload_searchpath = nullptr; m_saveload_searchpath = nullptr;
m_saveload_schedule = SLS_NONE; m_saveload_schedule = saveload_schedule::NONE;
} }

View File

@ -257,8 +257,8 @@ public:
void schedule_exit(); void schedule_exit();
void schedule_hard_reset(); void schedule_hard_reset();
void schedule_soft_reset(); void schedule_soft_reset();
void schedule_save(const char *filename); void schedule_save(std::string &&filename);
void schedule_load(const char *filename); void schedule_load(std::string &&filename);
// date & time // date & time
void base_datetime(system_time &systime); void base_datetime(system_time &systime);
@ -273,7 +273,7 @@ public:
void strlog(const char *str) const; void strlog(const char *str) const;
u32 rand(); u32 rand();
const char *describe_context(); const char *describe_context();
std::string compose_saveload_filename(const char *base_filename, const char **searchpath = nullptr); std::string compose_saveload_filename(std::string &&base_filename, const char **searchpath = nullptr);
// CPU information // CPU information
cpu_device * firstcpu; // first CPU cpu_device * firstcpu; // first CPU
@ -315,7 +315,7 @@ private:
template <typename T> struct is_null { template <typename U> static bool value(U &&x) { return false; } }; template <typename T> struct is_null { template <typename U> static bool value(U &&x) { return false; } };
template <typename T> struct is_null<T *> { template <typename U> static bool value(U &&x) { return !x; } }; template <typename T> struct is_null<T *> { template <typename U> static bool value(U &&x) { return !x; } };
void start(); void start();
void set_saveload_filename(const char *filename); void set_saveload_filename(std::string &&filename);
std::string get_statename(const char *statename_opt) const; std::string get_statename(const char *statename_opt) const;
void handle_saveload(); void handle_saveload();
void soft_reset(void *ptr = nullptr, s32 param = 0); void soft_reset(void *ptr = nullptr, s32 param = 0);
@ -374,11 +374,11 @@ private:
std::unique_ptr<emu_file> m_logfile; // pointer to the active log file std::unique_ptr<emu_file> m_logfile; // pointer to the active log file
// load/save management // load/save management
enum saveload_schedule enum class saveload_schedule
{ {
SLS_NONE, NONE,
SLS_SAVE, SAVE,
SLS_LOAD LOAD
}; };
saveload_schedule m_saveload_schedule; saveload_schedule m_saveload_schedule;
attotime m_saveload_schedule_time; attotime m_saveload_schedule_time;

View File

@ -1276,7 +1276,7 @@ uint32_t mame_ui_manager::handler_ingame(render_container &container)
uint32_t mame_ui_manager::handler_load_save(render_container &container, uint32_t state) uint32_t mame_ui_manager::handler_load_save(render_container &container, uint32_t state)
{ {
char filename[20]; std::string filename;
char file = 0; char file = 0;
// if we're not in the middle of anything, skip // if we're not in the middle of anything, skip
@ -1340,7 +1340,7 @@ uint32_t mame_ui_manager::handler_load_save(render_container &container, uint32_
for (input_item_id id = ITEM_ID_BUTTON1; id <= ITEM_ID_BUTTON32; ++id) for (input_item_id id = ITEM_ID_BUTTON1; id <= ITEM_ID_BUTTON32; ++id)
if (machine().input().code_pressed_once(input_code(DEVICE_CLASS_JOYSTICK, joy_index, ITEM_CLASS_SWITCH, ITEM_MODIFIER_NONE, id))) if (machine().input().code_pressed_once(input_code(DEVICE_CLASS_JOYSTICK, joy_index, ITEM_CLASS_SWITCH, ITEM_MODIFIER_NONE, id)))
{ {
snprintf(filename, sizeof(filename), "joy%i-%i", joy_index, id - ITEM_ID_BUTTON1 + 1); filename = util::string_format("joy%i-%i", joy_index, id - ITEM_ID_BUTTON1 + 1);
found = true; found = true;
break; break;
} }
@ -1350,19 +1350,19 @@ uint32_t mame_ui_manager::handler_load_save(render_container &container, uint32_
} }
else else
{ {
sprintf(filename, "%c", file); filename = util::string_format("%c", file);
} }
// display a popup indicating that the save will proceed // display a popup indicating that the save will proceed
if (state == LOADSAVE_SAVE) if (state == LOADSAVE_SAVE)
{ {
machine().popmessage(_("Save to position %s"), filename); machine().popmessage(_("Save to position %s"), filename);
machine().schedule_save(filename); machine().schedule_save(std::move(filename));
} }
else else
{ {
machine().popmessage(_("Load from position %s"), filename); machine().popmessage(_("Load from position %s"), filename);
machine().schedule_load(filename); machine().schedule_load(std::move(filename));
} }
// avoid handling the name of the save state slot as a seperate input // avoid handling the name of the save state slot as a seperate input