diff --git a/src/common/debugging.cpp b/src/common/debugging.cpp index c2dcbc4c9..b3357aa03 100644 --- a/src/common/debugging.cpp +++ b/src/common/debugging.cpp @@ -47,11 +47,15 @@ request_debugging(const std::string &options) { for (auto &one_option : all_options) { std::vector parts = split(one_option, "=", 2); + if (!parts[0].size()) + continue; if (parts[0] == "!") s_debugging_options.clear(); else s_debugging_options[parts[0]] = 1 == parts.size() ? std::string("") : parts[1]; } + + debugging_option_c::invalidate_cache(); } void @@ -64,3 +68,22 @@ init_debugging() { request_debugging(value); } } + +std::vector debugging_option_c::ms_registered_options; + +size_t +debugging_option_c::register_option(std::string const &option) { + auto itr = brng::find_if(ms_registered_options, [&option](option_c const &opt) { return opt.m_option == option; }); + if (itr != ms_registered_options.end()) + return std::distance(ms_registered_options.begin(), itr); + + ms_registered_options.emplace_back(option); + + return ms_registered_options.size() - 1; +} + +void +debugging_option_c::invalidate_cache() { + for (auto &opt : ms_registered_options) + opt.m_requested = boost::logic::indeterminate; +} diff --git a/src/common/debugging.h b/src/common/debugging.h index 4cceccc89..58eebffb1 100644 --- a/src/common/debugging.h +++ b/src/common/debugging.h @@ -24,23 +24,48 @@ void init_debugging(); int parse_debug_interval_arg(const std::string &option, int default_value = 1000, int invalid_value = -1); class debugging_option_c { + struct option_c { + boost::tribool m_requested; + std::string m_option; + + option_c(std::string const &option) + : m_requested{boost::logic::indeterminate} + , m_option{option} + { + } + + bool get() { + if (boost::logic::indeterminate(m_requested)) + m_requested = debugging_requested(m_option); + + return m_requested; + } + }; + protected: - boost::tribool m_requested; + size_t m_registered_idx; std::string m_option; +private: + static std::vector ms_registered_options; + public: debugging_option_c(std::string const &option) - : m_requested{boost::logic::indeterminate} + : m_registered_idx{std::numeric_limits::max()} , m_option{option} { } operator bool() { - if (boost::logic::indeterminate(m_requested)) - m_requested = debugging_requested(m_option); + if (m_registered_idx == std::numeric_limits::max()) + m_registered_idx = register_option(m_option); - return m_requested; + return ms_registered_options[m_registered_idx].get(); } + +public: + static size_t register_option(std::string const &option); + static void invalidate_cache(); }; #endif // MTX_COMMON_DEBUGGING_H