diff options
Diffstat (limited to 'src/lib/util/options.h')
-rw-r--r-- | src/lib/util/options.h | 294 |
1 files changed, 117 insertions, 177 deletions
diff --git a/src/lib/util/options.h b/src/lib/util/options.h index a36ff95cab6..edf7f8600c8 100644 --- a/src/lib/util/options.h +++ b/src/lib/util/options.h @@ -13,13 +13,26 @@ #include "corefile.h" #include <unordered_map> -#include <sstream> + //************************************************************************** // CONSTANTS //************************************************************************** +// option types +const uint32_t OPTION_TYPE_MASK = 0x0007; // up to 8 different types +enum +{ + OPTION_INVALID, // invalid + OPTION_HEADER, // a header item + OPTION_COMMAND, // a command + OPTION_BOOLEAN, // boolean option + OPTION_INTEGER, // integer option + OPTION_FLOAT, // floating-point option + OPTION_STRING // string option +}; + // option priorities const int OPTION_PRIORITY_DEFAULT = 0; // defaults are at 0 priority const int OPTION_PRIORITY_LOW = 50; // low priority @@ -27,55 +40,20 @@ const int OPTION_PRIORITY_NORMAL = 100; // normal priority const int OPTION_PRIORITY_HIGH = 150; // high priority const int OPTION_PRIORITY_MAXIMUM = 255; // maximum priority +const uint32_t OPTION_FLAG_INTERNAL = 0x40000000; + //************************************************************************** // TYPE DEFINITIONS //************************************************************************** -struct options_entry; - -// exception thrown by core_options when an illegal request is made -class options_exception : public std::exception -{ -public: - const std::string &message() const { return m_message; } - virtual const char *what() const noexcept override { return message().c_str(); } - -protected: - options_exception(std::string &&message); - -private: - std::string m_message; -}; - -class options_warning_exception : public options_exception -{ -public: - template <typename... Params> options_warning_exception(const char *fmt, Params &&...args) - : options_warning_exception(util::string_format(fmt, std::forward<Params>(args)...)) - { - } - - options_warning_exception(std::string &&message); - options_warning_exception(const options_warning_exception &) = default; - options_warning_exception(options_warning_exception &&) = default; - options_warning_exception& operator=(const options_warning_exception &) = default; - options_warning_exception& operator=(options_warning_exception &&) = default; -}; - -class options_error_exception : public options_exception +// static structure describing a single option with its description and default value +struct options_entry { -public: - template <typename... Params> options_error_exception(const char *fmt, Params &&...args) - : options_error_exception(util::string_format(fmt, std::forward<Params>(args)...)) - { - } - - options_error_exception(std::string &&message); - options_error_exception(const options_error_exception &) = default; - options_error_exception(options_error_exception &&) = default; - options_error_exception& operator=(const options_error_exception &) = default; - options_error_exception& operator=(options_error_exception &&) = default; + const char * name; // name on the command line + const char * defvalue; // default value of this argument + uint32_t flags; // flags to describe the option + const char * description; // description for -showusage }; @@ -85,95 +63,94 @@ class core_options static const int MAX_UNADORNED_OPTIONS = 16; public: - enum class option_type - { - INVALID, // invalid - HEADER, // a header item - COMMAND, // a command - BOOLEAN, // boolean option - INTEGER, // integer option - FLOAT, // floating-point option - STRING // string option - }; - // information about a single entry in the options class entry { - public: - typedef std::shared_ptr<entry> shared_ptr; - typedef std::weak_ptr<entry> weak_ptr; - - // constructor/destructor - entry(std::vector<std::string> &&names, option_type type = option_type::STRING, const char *description = nullptr); - entry(std::string &&name, option_type type = option_type::STRING, const char *description = nullptr); - entry(const entry &) = delete; - entry(entry &&) = delete; - entry& operator=(const entry &) = delete; - entry& operator=(entry &&) = delete; - virtual ~entry(); - - // accessors - const std::vector<std::string> &names() const { return m_names; } - const std::string &name() const { return m_names[0]; } - virtual const char *value() const; - int priority() const { return m_priority; } - option_type type() const { return m_type; } - const char *description() const { return m_description; } - virtual const std::string &default_value() const; - virtual const char *minimum() const; - virtual const char *maximum() const; - bool has_range() const; + friend class core_options; + friend class simple_list<entry>; - // mutators - void set_value(std::string &&newvalue, int priority, bool always_override = false); - 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); } + // construction/destruction + entry(const char *name, const char *description, uint32_t flags = 0, const char *defvalue = nullptr); - protected: - virtual void internal_set_value(std::string &&newvalue) = 0; + public: + // getters + entry *next() const { return m_next; } + const char *name(int index = 0) const { return (index < ARRAY_LENGTH(m_name) && !m_name[index].empty()) ? m_name[index].c_str() : nullptr; } + const char *description() const { return m_description; } + const char *value() const { return m_data.c_str(); } + const char *default_value() const { return m_defdata.c_str(); } + const char *minimum() const { return m_minimum.c_str(); } + const char *maximum() const { return m_maximum.c_str(); } + int type() const { return (m_flags & OPTION_TYPE_MASK); } + uint32_t flags() const { return m_flags; } + bool is_header() const { return type() == OPTION_HEADER; } + bool is_command() const { return type() == OPTION_COMMAND; } + bool is_internal() const { return m_flags & OPTION_FLAG_INTERNAL; } + bool has_range() const { return (!m_minimum.empty() && !m_maximum.empty()); } + int priority() const { return m_priority; } + bool is_changed() const { return m_changed; } + + // setters + void set_value(const char *newvalue, int priority); + void set_default_value(const char *defvalue); + void set_description(const char *description); + void set_flag(uint32_t mask, uint32_t flag); + void mark_changed() { m_changed = true; } + void revert(int priority_hi, int priority_lo); private: - void validate(const std::string &value); - - std::vector<std::string> m_names; - int m_priority; - core_options::option_type m_type; - const char * m_description; - std::function<void(const char *)> m_value_changed_handler; + // internal state + entry * m_next; // link to the next data + uint32_t m_flags; // flags from the entry + bool m_error_reported; // have we reported an error on this option yet? + int m_priority; // priority of the data set + const char * m_description; // description for this item + std::string m_name[4]; // up to 4 names for the item + std::string m_data; // data for this item + std::string m_defdata; // default data for this item + std::string m_minimum; // minimum value + std::string m_maximum; // maximum value + bool m_changed; // changed flag }; // construction/destruction core_options(); - core_options(const core_options &) = delete; - core_options(core_options &&) = delete; - core_options& operator=(const core_options &) = delete; - core_options& operator=(core_options &&) = delete; + core_options(const options_entry *entrylist); + core_options(const options_entry *entrylist1, const options_entry *entrylist2); + core_options(const options_entry *entrylist1, const options_entry *entrylist2, const options_entry *entrylist3); + core_options(const core_options &src); virtual ~core_options(); + // operators + core_options &operator=(const core_options &rhs); + bool operator==(const core_options &rhs); + bool operator!=(const core_options &rhs); + // getters + entry *first() const { return m_entrylist.first(); } const std::string &command() const { return m_command; } - const entry::shared_ptr get_entry(const std::string &name) const; - entry::shared_ptr get_entry(const std::string &name); - const std::vector<entry::shared_ptr> &entries() const { return m_entries; } - bool exists(const std::string &name) const { return get_entry(name) != nullptr; } - bool header_exists(const char *description) const; + entry *get_entry(const char *name) const; + + // range iterators + using auto_iterator = simple_list<entry>::auto_iterator; + auto_iterator begin() const { return m_entrylist.begin(); } + auto_iterator end() const { return m_entrylist.end(); } // configuration - void add_entry(entry::shared_ptr &&entry, const char *after_header = nullptr); - void add_entry(const options_entry &entry, bool override_existing = false); - void add_entry(std::vector<std::string> &&names, const char *description, option_type type, std::string &&default_value = "", std::string &&minimum = "", std::string &&maximum = ""); - void add_header(const char *description); + void add_entry(const char *name, const char *description, uint32_t flags = 0, const char *defvalue = nullptr, bool override_existing = false); + void add_entry(const options_entry &data, bool override_existing = false) { add_entry(data.name, data.description, data.flags, data.defvalue, override_existing); } void add_entries(const options_entry *entrylist, bool override_existing = false); void set_default_value(const char *name, const char *defvalue); void set_description(const char *name, const char *description); void remove_entry(entry &delentry); - void set_value_changed_handler(const std::string &name, std::function<void(const char *)> &&handler); // parsing/input - void parse_command_line(std::vector<std::string> &args, int priority); - void parse_ini_file(util::core_file &inifile, int priority, bool always_override); - void copy_from(const core_options &that); + bool parse_command_line(std::vector<std::string> &args, int priority, std::string &error_string); + bool parse_ini_file(util::core_file &inifile, int priority, int ignore_priority, std::string &error_string); + bool pluck_from_command_line(std::vector<std::string> &args, const std::string &name, std::string &result); + + // reverting + void revert(int priority_hi = OPTION_PRIORITY_MAXIMUM, int priority_lo = OPTION_PRIORITY_DEFAULT); // output std::string output_ini(const core_options *diff = nullptr) const; @@ -182,88 +159,51 @@ public: // reading const char *value(const char *option) const; const char *description(const char *option) const; - bool bool_value(const char *option) const { return int_value(option) != 0; } - int int_value(const char *option) const { return atoi(value(option)); } - float float_value(const char *option) const { return atof(value(option)); } + int priority(const char *option) const; + bool bool_value(const char *name) const { return (atoi(value(name)) != 0); } + int int_value(const char *name) const { return atoi(value(name)); } + float float_value(const char *name) const { return atof(value(name)); } + bool exists(const char *name) const; // setting - void set_value(const std::string &name, const std::string &value, int priority); - void set_value(const std::string &name, std::string &&value, int priority); - void set_value(const std::string &name, int value, int priority); - void set_value(const std::string &name, float value, int priority); + bool set_value(const char *name, const char *value, int priority, std::string &error_string); + bool set_value(const char *name, int value, int priority, std::string &error_string); + bool set_value(const char *name, float value, int priority, std::string &error_string); + void set_flag(const char *name, uint32_t mask, uint32_t flags); + void mark_changed(const char *name); // misc static const char *unadorned(int x = 0) { return s_option_unadorned[std::min(x, MAX_UNADORNED_OPTIONS - 1)]; } + int options_count() const { return m_entrylist.count(); } -private: - class simple_entry : public entry - { - public: - // construction/destruction - simple_entry(std::vector<std::string> &&names, const char *description, core_options::option_type type, std::string &&defdata, std::string &&minimum, std::string &&maximum); - simple_entry(const simple_entry &) = delete; - simple_entry(simple_entry &&) = delete; - simple_entry& operator=(const simple_entry &) = delete; - simple_entry& operator=(simple_entry &&) = delete; - ~simple_entry(); - - // getters - virtual const char *value() const override; - virtual const char *minimum() const override; - virtual const char *maximum() const override; - virtual const std::string &default_value() const override; - - virtual void set_default_value(std::string &&newvalue) override; - - protected: - virtual void internal_set_value(std::string &&newvalue) override; - - private: - // internal state - std::string m_data; // data for this item - std::string m_defdata; // default data for this item - std::string m_minimum; // minimum value - std::string m_maximum; // maximum value - }; - - // used internally in core_options - enum class condition_type +protected: + // This is a hook to allow option value retrieval to be overridden for various reasons; this is a crude + // extensibility mechanism that should really be replaced by something better + enum class override_get_value_result { NONE, - WARN, - ERR + OVERRIDE, + SKIP }; + virtual void value_changed(const std::string &name, const std::string &value) {} + virtual override_get_value_result override_get_value(const char *name, std::string &value) const { return override_get_value_result::NONE; } + virtual bool override_set_value(const char *name, const std::string &value) { return false; } + +private: // internal helpers - void add_to_entry_map(std::string &&name, entry::shared_ptr &entry); - void prettify_and_set_value(entry &curentry, std::string &&data, int priority, std::ostream &error_stream, condition_type &condition); - void throw_options_exception_if_appropriate(condition_type condition, std::ostringstream &error_stream); + void reset(); + void append_entry(entry &newentry); + void copyfrom(const core_options &src); + bool validate_and_set_data(entry &curentry, std::string &&newdata, int priority, std::string &error_string); // internal state - std::vector<entry::shared_ptr> m_entries; // cannonical list of entries - std::unordered_map<std::string, entry::weak_ptr> m_entrymap; // map for fast lookup - std::string m_command; // command found - static const char *const s_option_unadorned[]; // array of unadorned option "names" -}; - - -// static structure describing a single option with its description and default value -struct options_entry -{ - const char * name; // name on the command line - const char * defvalue; // default value of this argument - core_options::option_type type; // type of option - const char * description; // description for -showusage + simple_list<entry> m_entrylist; // head of list of entries + std::unordered_map<std::string,entry *> m_entrymap; // map for fast lookup + std::string m_command; // command found + static const char *const s_option_unadorned[]; // array of unadorned option "names" }; -// legacy option types -const core_options::option_type OPTION_INVALID = core_options::option_type::INVALID; -const core_options::option_type OPTION_HEADER = core_options::option_type::HEADER; -const core_options::option_type OPTION_COMMAND = core_options::option_type::COMMAND; -const core_options::option_type OPTION_BOOLEAN = core_options::option_type::BOOLEAN; -const core_options::option_type OPTION_INTEGER = core_options::option_type::INTEGER; -const core_options::option_type OPTION_FLOAT = core_options::option_type::FLOAT; -const core_options::option_type OPTION_STRING = core_options::option_type::STRING; #endif // MAME_LIB_UTIL_OPTIONS_H |