Make DynamicRegister statically dispatch to its function

This commit is contained in:
Maxime Coste 2016-03-31 09:17:02 +01:00
parent 9b4bd611ef
commit 8badcdc0d5
2 changed files with 22 additions and 20 deletions

View File

@ -145,29 +145,28 @@ void register_registers()
register_manager.add_register(c, make_unique<StaticRegister>()); register_manager.add_register(c, make_unique<StaticRegister>());
using StringList = Vector<String, MemoryDomain::Registers>; using StringList = Vector<String, MemoryDomain::Registers>;
static const struct {
char name; register_manager.add_register('%', make_dyn_reg(
StringList (*func)(const Context&); [](const Context& context)
} dyn_regs[] = { { return StringList{{context.buffer().display_name()}}; }));
{ '%', [](const Context& context) { return StringList{{context.buffer().display_name()}}; } },
{ '.', [](const Context& context) { register_manager.add_register('.', make_dyn_reg(
[](const Context& context) {
auto content = context.selections_content(); auto content = context.selections_content();
return StringList{content.begin(), content.end()}; return StringList{content.begin(), content.end()};
} }, }));
{ '#', [](const Context& context) {
register_manager.add_register('#', make_dyn_reg(
[](const Context& context) {
StringList res; StringList res;
for (size_t i = 1; i < context.selections().size()+1; ++i) for (size_t i = 1; i < context.selections().size()+1; ++i)
res.push_back(to_string((int)i)); res.push_back(to_string((int)i));
return res; return res;
} } }));
};
for (auto& dyn_reg : dyn_regs)
register_manager.add_register(dyn_reg.name, make_unique<DynamicRegister>(dyn_reg.func));
for (size_t i = 0; i < 10; ++i) for (size_t i = 0; i < 10; ++i)
{ {
register_manager.add_register('0'+i, make_unique<DynamicRegister>( register_manager.add_register('0'+i, make_dyn_reg(
[i](const Context& context) { [i](const Context& context) {
StringList result; StringList result;
for (auto& sel : context.selections()) for (auto& sel : context.selections())

View File

@ -8,8 +8,6 @@
#include "string.hh" #include "string.hh"
#include "vector.hh" #include "vector.hh"
#include <functional>
namespace Kakoune namespace Kakoune
{ {
@ -46,14 +44,13 @@ protected:
Vector<String, MemoryDomain::Registers> m_content; Vector<String, MemoryDomain::Registers> m_content;
}; };
using RegisterRetriever = std::function<Vector<String, MemoryDomain::Registers> (const Context&)>;
// Dynamic value register, use it's RegisterRetriever // Dynamic value register, use it's RegisterRetriever
// to get it's value when needed. // to get it's value when needed.
template<typename Func>
class DynamicRegister : public StaticRegister class DynamicRegister : public StaticRegister
{ {
public: public:
DynamicRegister(RegisterRetriever function) DynamicRegister(Func function)
: m_function(std::move(function)) {} : m_function(std::move(function)) {}
Register& operator=(ConstArrayView<String> values) override Register& operator=(ConstArrayView<String> values) override
@ -68,9 +65,15 @@ public:
} }
private: private:
RegisterRetriever m_function; Func m_function;
}; };
template<typename Func>
std::unique_ptr<Register> make_dyn_reg(Func func)
{
return make_unique<DynamicRegister<Func>>(std::move(func));
}
class NullRegister : public Register class NullRegister : public Register
{ {
public: public: