debugging: make debugging_option_c instances re-evaluate their result upon invalidation

This commit is contained in:
Moritz Bunkus 2013-12-08 13:15:23 +01:00
parent 5654172dfd
commit 605bae5bf5
2 changed files with 53 additions and 5 deletions

View File

@ -47,11 +47,15 @@ request_debugging(const std::string &options) {
for (auto &one_option : all_options) {
std::vector<std::string> 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::option_c> 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;
}

View File

@ -24,18 +24,17 @@ void init_debugging();
int parse_debug_interval_arg(const std::string &option, int default_value = 1000, int invalid_value = -1);
class debugging_option_c {
protected:
struct option_c {
boost::tribool m_requested;
std::string m_option;
public:
debugging_option_c(std::string const &option)
option_c(std::string const &option)
: m_requested{boost::logic::indeterminate}
, m_option{option}
{
}
operator bool() {
bool get() {
if (boost::logic::indeterminate(m_requested))
m_requested = debugging_requested(m_option);
@ -43,4 +42,30 @@ public:
}
};
protected:
size_t m_registered_idx;
std::string m_option;
private:
static std::vector<option_c> ms_registered_options;
public:
debugging_option_c(std::string const &option)
: m_registered_idx{std::numeric_limits<size_t>::max()}
, m_option{option}
{
}
operator bool() {
if (m_registered_idx == std::numeric_limits<size_t>::max())
m_registered_idx = register_option(m_option);
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