Remove AutoRegister util template

This commit is contained in:
Maxime Coste 2014-11-02 16:04:24 +00:00
parent b494b873b1
commit abfc016321
4 changed files with 16 additions and 91 deletions

View File

@ -217,9 +217,15 @@ InsertCompletion complete_line(const Buffer& buffer, ByteCoord cursor_pos)
} }
InsertCompleter::InsertCompleter(const Context& context) InsertCompleter::InsertCompleter(const Context& context)
: OptionManagerWatcher_AutoRegister(context.options()), : m_context(context)
m_context(context) {
{} context.options().register_watcher(*this);
}
InsertCompleter::~InsertCompleter()
{
m_context.options().unregister_watcher(*this);
}
void InsertCompleter::select(int offset) void InsertCompleter::select(int offset)
{ {
@ -315,19 +321,19 @@ bool InsertCompleter::setup_ifn()
using namespace std::placeholders; using namespace std::placeholders;
if (not m_completions.is_valid()) if (not m_completions.is_valid())
{ {
auto& completers = options()["completers"].get<InsertCompleterDescList>(); auto& completers = m_context.options()["completers"].get<InsertCompleterDescList>();
for (auto& completer : completers) for (auto& completer : completers)
{ {
if (completer.mode == InsertCompleterDesc::Filename and if (completer.mode == InsertCompleterDesc::Filename and
try_complete([this](const Buffer& buffer, ByteCoord cursor_pos) { try_complete([this](const Buffer& buffer, ByteCoord cursor_pos) {
return complete_filename<true>(buffer, cursor_pos, return complete_filename<true>(buffer, cursor_pos,
options()); m_context.options());
})) }))
return true; return true;
if (completer.mode == InsertCompleterDesc::Option and if (completer.mode == InsertCompleterDesc::Option and
try_complete([&,this](const Buffer& buffer, ByteCoord cursor_pos) { try_complete([&,this](const Buffer& buffer, ByteCoord cursor_pos) {
return complete_option(buffer, cursor_pos, return complete_option(buffer, cursor_pos,
options(), *completer.param); m_context.options(), *completer.param);
})) }))
return true; return true;
if (completer.mode == InsertCompleterDesc::Word and if (completer.mode == InsertCompleterDesc::Word and
@ -368,7 +374,7 @@ void InsertCompleter::menu_show()
void InsertCompleter::on_option_changed(const Option& opt) void InsertCompleter::on_option_changed(const Option& opt)
{ {
auto& completers = options()["completers"].get<InsertCompleterDescList>(); auto& completers = m_context.options()["completers"].get<InsertCompleterDescList>();
std::vector<StringView> option_names; std::vector<StringView> option_names;
for (auto& completer : completers) for (auto& completer : completers)
{ {
@ -410,7 +416,7 @@ bool InsertCompleter::try_complete(CompleteFunc complete_func)
void InsertCompleter::explicit_file_complete() void InsertCompleter::explicit_file_complete()
{ {
try_complete([this](const Buffer& buffer, ByteCoord cursor_pos) { try_complete([this](const Buffer& buffer, ByteCoord cursor_pos) {
return complete_filename<false>(buffer, cursor_pos, options()); return complete_filename<false>(buffer, cursor_pos, m_context.options());
}); });
} }

View File

@ -49,12 +49,13 @@ struct InsertCompletion
bool is_valid() const { return not candidates.empty(); } bool is_valid() const { return not candidates.empty(); }
}; };
class InsertCompleter : public OptionManagerWatcher_AutoRegister class InsertCompleter : public OptionManagerWatcher
{ {
public: public:
InsertCompleter(const Context& context); InsertCompleter(const Context& context);
InsertCompleter(const InsertCompleter&) = delete; InsertCompleter(const InsertCompleter&) = delete;
InsertCompleter& operator=(const InsertCompleter&) = delete; InsertCompleter& operator=(const InsertCompleter&) = delete;
~InsertCompleter();
void select(int offset); void select(int offset);
void update(); void update();

View File

@ -221,31 +221,6 @@ private:
std::vector<std::unique_ptr<OptionDesc>> m_descs; std::vector<std::unique_ptr<OptionDesc>> m_descs;
}; };
struct OptionManagerRegisterFuncs
{
static void insert(OptionManager& options, OptionManagerWatcher& watcher)
{
options.register_watcher(watcher);
}
static void remove(OptionManager& options, OptionManagerWatcher& watcher)
{
options.unregister_watcher(watcher);
}
};
class OptionManagerWatcher_AutoRegister
: public OptionManagerWatcher,
public AutoRegister<OptionManagerWatcher_AutoRegister,
OptionManagerRegisterFuncs, OptionManager>
{
public:
OptionManagerWatcher_AutoRegister(OptionManager& options)
: AutoRegister(options) {}
OptionManager& options() { return registry(); }
};
} }
#endif // option_manager_hh_INCLUDED #endif // option_manager_hh_INCLUDED

View File

@ -176,63 +176,6 @@ bool is_in_range(const T& val, const T& min, const T& max)
return min <= val and val <= max; return min <= val and val <= max;
} }
// *** AutoRegister: RAII handling of value semantics registering classes ***
template<typename EffectiveType, typename RegisterFuncs, typename Registry>
class AutoRegister
{
public:
AutoRegister(Registry& registry)
: m_registry(&registry)
{
RegisterFuncs::insert(*m_registry, effective_this());
}
AutoRegister(const AutoRegister& other)
: m_registry(other.m_registry)
{
RegisterFuncs::insert(*m_registry, effective_this());
}
AutoRegister(AutoRegister&& other)
: m_registry(other.m_registry)
{
RegisterFuncs::insert(*m_registry, effective_this());
}
~AutoRegister()
{
RegisterFuncs::remove(*m_registry, effective_this());
}
AutoRegister& operator=(const AutoRegister& other)
{
if (m_registry != other.m_registry)
{
RegisterFuncs::remove(*m_registry, effective_this());
m_registry = other.m_registry;
RegisterFuncs::insert(*m_registry, effective_this());
}
return *this;
}
AutoRegister& operator=(AutoRegister&& other)
{
if (m_registry != other.m_registry)
{
RegisterFuncs::remove(*m_registry, effective_this());
m_registry = other.m_registry;
RegisterFuncs::insert(*m_registry, effective_this());
}
return *this;
}
Registry& registry() const { return *m_registry; }
private:
EffectiveType& effective_this() { return static_cast<EffectiveType&>(*this); }
Registry* m_registry;
};
} }
// std::pair hashing // std::pair hashing