Refactor registers to initialize all of them at startup and add null register

Fixes #497
This commit is contained in:
Maxime Coste 2015-11-25 21:08:33 +00:00
parent ec91ea17fe
commit 3644f2a056
3 changed files with 82 additions and 68 deletions

View File

@ -147,6 +147,11 @@ void register_env_vars()
void register_registers() void register_registers()
{ {
RegisterManager& register_manager = RegisterManager::instance();
for (auto c : "abcdefghijklmnopqrstuvwxyz/\"|^@")
register_manager.add_register(c, make_unique<StaticRegister>());
using StringList = Vector<String, MemoryDomain::Registers>; using StringList = Vector<String, MemoryDomain::Registers>;
static const struct { static const struct {
char name; char name;
@ -165,20 +170,21 @@ void register_registers()
} } } }
}; };
RegisterManager& register_manager = RegisterManager::instance();
for (auto& dyn_reg : dyn_regs) for (auto& dyn_reg : dyn_regs)
register_manager.register_dynamic_register(dyn_reg.name, dyn_reg.func); 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.register_dynamic_register('0'+i, register_manager.add_register('0'+i, make_unique<DynamicRegister>(
[i](const Context& context) { [i](const Context& context) {
StringList result; StringList result;
for (auto& sel : context.selections()) for (auto& sel : context.selections())
result.emplace_back(i < sel.captures().size() ? sel.captures()[i] : ""); result.emplace_back(i < sel.captures().size() ? sel.captures()[i] : "");
return result; return result;
}); }));
} }
register_manager.add_register('_', make_unique<NullRegister>());
} }
void register_options() void register_options()

View File

@ -1,62 +1,12 @@
#include "register_manager.hh" #include "register_manager.hh"
#include "assert.hh" #include "assert.hh"
#include "exception.hh"
#include "id_map.hh" #include "id_map.hh"
namespace Kakoune namespace Kakoune
{ {
// static value register, which can be modified Register& RegisterManager::operator[](StringView reg) const
// using operator=, so should be user modifiable
class StaticRegister : public Register
{
public:
Register& operator=(ConstArrayView<String> values) override
{
m_content = Vector<String, MemoryDomain::Registers>(values.begin(), values.end());
return *this;
}
ConstArrayView<String> values(const Context&) override
{
if (m_content.empty())
return ConstArrayView<String>(ms_empty);
else
return ConstArrayView<String>(m_content);
}
protected:
Vector<String, MemoryDomain::Registers> m_content;
static const String ms_empty;
};
const String StaticRegister::ms_empty;
// Dynamic value register, use it's RegisterRetriever
// to get it's value when needed.
class DynamicRegister : public StaticRegister
{
public:
DynamicRegister(RegisterRetriever function)
: m_function(std::move(function)) {}
Register& operator=(ConstArrayView<String> values) override
{
throw runtime_error("this register is not assignable");
}
ConstArrayView<String> values(const Context& context) override
{
m_content = m_function(context);
return StaticRegister::values(context);
}
private:
RegisterRetriever m_function;
};
Register& RegisterManager::operator[](StringView reg)
{ {
if (reg.length() == 1) if (reg.length() == 1)
return (*this)[reg[0_byte]]; return (*this)[reg[0_byte]];
@ -74,23 +24,21 @@ Register& RegisterManager::operator[](StringView reg)
return (*this)[it->value]; return (*this)[it->value];
} }
Register& RegisterManager::operator[](Codepoint c) Register& RegisterManager::operator[](Codepoint c) const
{ {
c = to_lower(c); c = to_lower(c);
if (c < 32 or c > 127) auto it = m_registers.find(c);
throw runtime_error(format("invalid register name: '{}'", c)); if (it == m_registers.end())
throw runtime_error(format("no such register: '{}'", c));
auto& reg_ptr = m_registers[c]; return *(it->second);
if (not reg_ptr)
reg_ptr.reset(new StaticRegister());
return *reg_ptr;
} }
void RegisterManager::register_dynamic_register(char reg, RegisterRetriever function) void RegisterManager::add_register(char c, std::unique_ptr<Register> reg)
{ {
auto& reg_ptr = m_registers[reg]; auto& reg_ptr = m_registers[c];
kak_assert(not reg_ptr); kak_assert(not reg_ptr);
reg_ptr.reset(new DynamicRegister(std::move(function))); reg_ptr = std::move(reg);
} }
} }

View File

@ -2,6 +2,7 @@
#define register_manager_hh_INCLUDED #define register_manager_hh_INCLUDED
#include "array_view.hh" #include "array_view.hh"
#include "exception.hh"
#include "utils.hh" #include "utils.hh"
#include "unordered_map.hh" #include "unordered_map.hh"
#include "string.hh" #include "string.hh"
@ -23,14 +24,73 @@ public:
virtual ConstArrayView<String> values(const Context& context) = 0; virtual ConstArrayView<String> values(const Context& context) = 0;
}; };
// static value register, which can be modified
// using operator=, so should be user modifiable
class StaticRegister : public Register
{
public:
Register& operator=(ConstArrayView<String> values) override
{
m_content = Vector<String, MemoryDomain::Registers>(values.begin(), values.end());
return *this;
}
ConstArrayView<String> values(const Context&) override
{
if (m_content.empty())
return ConstArrayView<String>(String::ms_empty);
else
return ConstArrayView<String>(m_content);
}
protected:
Vector<String, MemoryDomain::Registers> m_content;
};
using RegisterRetriever = std::function<Vector<String, MemoryDomain::Registers> (const Context&)>; using RegisterRetriever = std::function<Vector<String, MemoryDomain::Registers> (const Context&)>;
// Dynamic value register, use it's RegisterRetriever
// to get it's value when needed.
class DynamicRegister : public StaticRegister
{
public:
DynamicRegister(RegisterRetriever function)
: m_function(std::move(function)) {}
Register& operator=(ConstArrayView<String> values) override
{
throw runtime_error("this register is not assignable");
}
ConstArrayView<String> values(const Context& context) override
{
m_content = m_function(context);
return StaticRegister::values(context);
}
private:
RegisterRetriever m_function;
};
class NullRegister : public Register
{
public:
Register& operator=(ConstArrayView<String> values) override
{
return *this;
}
ConstArrayView<String> values(const Context& context) override
{
return ConstArrayView<String>(String::ms_empty);
}
};
class RegisterManager : public Singleton<RegisterManager> class RegisterManager : public Singleton<RegisterManager>
{ {
public: public:
Register& operator[](StringView reg); Register& operator[](StringView reg) const;
Register& operator[](Codepoint c); Register& operator[](Codepoint c) const;
void register_dynamic_register(char reg, RegisterRetriever function); void add_register(char c, std::unique_ptr<Register> reg);
protected: protected:
UnorderedMap<char, std::unique_ptr<Register>, MemoryDomain::Registers> m_registers; UnorderedMap<char, std::unique_ptr<Register>, MemoryDomain::Registers> m_registers;