summaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
author Vas Crabb <vas@vastheman.com>2026-01-13 03:16:26 +1100
committer Vas Crabb <vas@vastheman.com>2026-01-13 03:16:26 +1100
commit6907b0eb4dfcb0ad3b0cb17c86923f4d56608771 (patch)
tree0f49372404cdd55ef12377f9c8bb4821c4e22557 /src
parente25ef95c4628fcf2c712e82d7ba4475a4a6faa89 (diff)
-util/options.cpp: Shuffled numeric option conversions:
* Moved float/integer conversions into the option entries themselves. * Fixed another bug where float values would be converted to strings with the global local, but converted from strings using the "classic" locale. * frontend/mame/luaengine.cpp: Got rid of duplicated numeric options conversion code.
Diffstat (limited to 'src')
-rw-r--r--src/frontend/mame/luaengine.cpp36
-rw-r--r--src/lib/util/options.cpp82
-rw-r--r--src/lib/util/options.h5
3 files changed, 70 insertions, 53 deletions
diff --git a/src/frontend/mame/luaengine.cpp b/src/frontend/mame/luaengine.cpp
index 5b6bed871f8..fb6ec7c17e0 100644
--- a/src/frontend/mame/luaengine.cpp
+++ b/src/frontend/mame/luaengine.cpp
@@ -1358,21 +1358,21 @@ void lua_engine::initialize()
if (e.type() != core_options::option_type::BOOLEAN)
luaL_error(s, "Cannot set option to wrong type");
else
- e.set_value(val ? "1" : "0", OPTION_PRIORITY_CMDLINE);
+ e.set_value(val ? 1 : 0, OPTION_PRIORITY_CMDLINE);
},
[] (core_options::entry &e, sol::this_state s, int val)
{
if (e.type() != core_options::option_type::INTEGER)
luaL_error(s, "Cannot set option to wrong type");
else
- e.set_value(string_format(std::locale::classic(), "%d", val), OPTION_PRIORITY_CMDLINE);
+ e.set_value(val, OPTION_PRIORITY_CMDLINE);
},
[] (core_options::entry &e, sol::this_state s, float val)
{
if (e.type() != core_options::option_type::FLOAT)
luaL_error(s, "Cannot set option to wrong type");
else
- e.set_value(string_format(std::locale::classic(), "%f", val), OPTION_PRIORITY_CMDLINE);
+ e.set_value(val, OPTION_PRIORITY_CMDLINE);
},
[] (core_options::entry &e, sol::this_state s, const char *val)
{
@@ -1388,35 +1388,11 @@ void lua_engine::initialize()
switch (e.type())
{
case core_options::option_type::BOOLEAN:
- {
- std::istringstream str(e.value());
- str.imbue(std::locale::classic());
- int ival;
- if (str >> ival)
- return sol::make_object(s, ival != 0);
- else
- return sol::make_object(s, false);
- }
+ return sol::make_object(s, e.bool_value());
case core_options::option_type::INTEGER:
- {
- std::istringstream str(e.value());
- str.imbue(std::locale::classic());
- int ival;
- if (str >> ival)
- return sol::make_object(s, ival);
- else
- return sol::make_object(s, 0);
- }
+ return sol::make_object(s, e.int_value());
case core_options::option_type::FLOAT:
- {
- std::istringstream str(e.value());
- str.imbue(std::locale::classic());
- float fval;
- if (str >> fval)
- return sol::make_object(s, fval);
- else
- return sol::make_object(s, 0.0F);
- }
+ return sol::make_object(s, e.float_value());
default:
return sol::make_object(s, e.value());
}
diff --git a/src/lib/util/options.cpp b/src/lib/util/options.cpp
index 6155553d3c4..4f766aa1e00 100644
--- a/src/lib/util/options.cpp
+++ b/src/lib/util/options.cpp
@@ -162,6 +162,44 @@ const char *core_options::entry::value_unsubstituted() const noexcept
//-------------------------------------------------
+// entry::int_value
+//-------------------------------------------------
+
+int core_options::entry::int_value() const
+{
+ char const *const data = value();
+ if (!data)
+ return 0;
+ std::istringstream str(data);
+ str.imbue(std::locale::classic());
+ int ival;
+ if (str >> ival)
+ return ival;
+ else
+ return 0;
+}
+
+
+//-------------------------------------------------
+// entry::float_value
+//-------------------------------------------------
+
+float core_options::entry::float_value() const
+{
+ char const *const data = value();
+ if (!data)
+ return 0.0F;
+ std::istringstream str(data);
+ str.imbue(std::locale::classic());
+ float fval;
+ if (str >> fval)
+ return fval;
+ else
+ return 0.0F;
+}
+
+
+//-------------------------------------------------
// entry::copy_from
//-------------------------------------------------
@@ -208,6 +246,16 @@ void core_options::entry::set_value(std::string &&newvalue, int priority_value,
}
}
+void core_options::entry::set_value(int value, int priority)
+{
+ set_value(util::string_format(std::locale::classic(), "%d", value), priority, false, false);
+}
+
+void core_options::entry::set_value(float value, int priority)
+{
+ set_value(util::string_format(std::locale::classic(), "%f", value), priority, false, false);
+}
+
//-------------------------------------------------
// entry::set_default_value
@@ -1134,16 +1182,8 @@ const char *core_options::description(std::string_view option) const noexcept
int core_options::int_value(std::string_view option) const
{
- char const *const data = value(option);
- if (!data)
- return 0;
- std::istringstream str(data);
- str.imbue(std::locale::classic());
- int ival;
- if (str >> ival)
- return ival;
- else
- return 0;
+ auto const entry = get_entry(option);
+ return entry ? entry->int_value() : 0;
}
@@ -1153,16 +1193,8 @@ int core_options::int_value(std::string_view option) const
float core_options::float_value(std::string_view option) const
{
- char const *const data = value(option);
- if (!data)
- return 0.0F;
- std::istringstream str(data);
- str.imbue(std::locale::classic());
- float fval;
- if (str >> fval)
- return fval;
- else
- return 0.0F;
+ auto const entry = get_entry(option);
+ return entry ? entry->float_value() : 0.0F;
}
@@ -1187,18 +1219,22 @@ void core_options::set_value(std::string_view name, const char *value, int prior
void core_options::set_value(std::string_view name, std::string &&value, int priority)
{
auto entry = get_entry(name);
- assert(entry != nullptr);
+ assert(entry);
entry->set_value(std::move(value), priority, false, false);
}
void core_options::set_value(std::string_view name, int value, int priority)
{
- set_value(name, util::string_format("%d", value), priority);
+ auto entry = get_entry(name);
+ assert(entry);
+ entry->set_value(value, priority);
}
void core_options::set_value(std::string_view name, float value, int priority)
{
- set_value(name, util::string_format("%f", value), priority);
+ auto entry = get_entry(name);
+ assert(entry);
+ entry->set_value(value, priority);
}
diff --git a/src/lib/util/options.h b/src/lib/util/options.h
index 8b85b9f58e6..0dc4ddd2804 100644
--- a/src/lib/util/options.h
+++ b/src/lib/util/options.h
@@ -131,6 +131,9 @@ public:
const std::string &name() const noexcept { return m_names[0]; }
virtual const char *value() const noexcept;
virtual const char *value_unsubstituted() const noexcept;
+ bool bool_value() const { return int_value() != 0; }
+ int int_value() const;
+ float float_value() const;
int priority() const noexcept { return m_priority; }
void set_priority(int priority) noexcept { m_priority = priority; }
option_type type() const noexcept { return m_type; }
@@ -143,6 +146,8 @@ public:
// mutators
void copy_from(const entry &that, bool always_override);
void set_value(std::string &&newvalue, int priority, bool always_override = false, bool perform_substitutions = false);
+ void set_value(int newvalue, int priority);
+ void set_value(float newvalue, int priority);
virtual void set_default_value(std::string &&newvalue);
void set_description(const char *description) { m_description = description; }
void set_value_changed_handler(std::function<void(const char *)> &&handler) { m_value_changed_handler = std::move(handler); }