2012-06-29 18:37:17 +02:00
|
|
|
#include "register_manager.hh"
|
|
|
|
|
|
|
|
#include "assert.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "utils.hh"
|
2012-06-29 18:37:17 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
// static value register, which can be modified
|
|
|
|
// using operator=, so should be user modifiable
|
|
|
|
class StaticRegister : public Register
|
|
|
|
{
|
|
|
|
public:
|
2012-09-04 00:17:41 +02:00
|
|
|
Register& operator=(const memoryview<String>& values) override
|
2012-06-29 18:37:17 +02:00
|
|
|
{
|
|
|
|
m_content = std::vector<String>(values.begin(), values.end());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-09-04 00:17:41 +02:00
|
|
|
memoryview<String> values(const Context&) override
|
2012-06-29 18:37:17 +02:00
|
|
|
{
|
2012-08-05 20:13:41 +02:00
|
|
|
if (m_content.empty())
|
|
|
|
return memoryview<String>(ms_empty);
|
2012-06-29 18:37:17 +02:00
|
|
|
else
|
2012-08-05 20:13:41 +02:00
|
|
|
return memoryview<String>(m_content);
|
2012-06-29 18:37:17 +02:00
|
|
|
}
|
|
|
|
protected:
|
|
|
|
std::vector<String> 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)) {}
|
|
|
|
|
2012-09-04 00:17:41 +02:00
|
|
|
Register& operator=(const memoryview<String>& values) override
|
2012-06-29 18:37:17 +02:00
|
|
|
{
|
|
|
|
throw runtime_error("this register is not assignable");
|
|
|
|
}
|
|
|
|
|
2012-09-04 00:17:41 +02:00
|
|
|
memoryview<String> values(const Context& context) override
|
2012-06-29 18:37:17 +02:00
|
|
|
{
|
2012-08-05 20:13:41 +02:00
|
|
|
m_content = m_function(context);
|
|
|
|
return StaticRegister::values(context);
|
2012-06-29 18:37:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RegisterRetriever m_function;
|
|
|
|
};
|
|
|
|
|
|
|
|
Register& RegisterManager::operator[](char reg)
|
|
|
|
{
|
|
|
|
auto& reg_ptr = m_registers[reg];
|
|
|
|
if (not reg_ptr)
|
|
|
|
reg_ptr.reset(new StaticRegister());
|
|
|
|
return *reg_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegisterManager::register_dynamic_register(char reg, RegisterRetriever function)
|
|
|
|
{
|
|
|
|
auto& reg_ptr = m_registers[reg];
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(not reg_ptr);
|
2012-06-29 18:37:17 +02:00
|
|
|
reg_ptr.reset(new DynamicRegister(std::move(function)));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|