2011-09-23 16:31:57 +02:00
|
|
|
#ifndef register_manager_hh_INCLUDED
|
|
|
|
#define register_manager_hh_INCLUDED
|
|
|
|
|
2015-04-23 22:51:48 +02:00
|
|
|
#include "array_view.hh"
|
2015-11-25 22:08:33 +01:00
|
|
|
#include "exception.hh"
|
2011-09-23 16:31:57 +02:00
|
|
|
#include "utils.hh"
|
2014-12-16 19:57:19 +01:00
|
|
|
#include "unordered_map.hh"
|
2015-04-23 22:51:48 +02:00
|
|
|
#include "string.hh"
|
2015-01-12 14:58:41 +01:00
|
|
|
#include "vector.hh"
|
2011-09-23 16:31:57 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-04-23 22:51:48 +02:00
|
|
|
class Context;
|
|
|
|
|
|
|
|
class Register
|
|
|
|
{
|
|
|
|
public:
|
2017-01-08 23:30:15 +01:00
|
|
|
virtual ~Register() = default;
|
2015-04-23 22:51:48 +02:00
|
|
|
|
2017-02-14 01:02:01 +01:00
|
|
|
virtual void set(Context& context, ConstArrayView<String> values) = 0;
|
|
|
|
virtual ConstArrayView<String> get(const Context& context) = 0;
|
2015-04-23 22:51:48 +02:00
|
|
|
};
|
|
|
|
|
2015-11-25 22:08:33 +01:00
|
|
|
// static value register, which can be modified
|
|
|
|
// using operator=, so should be user modifiable
|
|
|
|
class StaticRegister : public Register
|
|
|
|
{
|
|
|
|
public:
|
2017-02-14 01:02:01 +01:00
|
|
|
void set(Context&, ConstArrayView<String> values) override
|
2015-11-25 22:08:33 +01:00
|
|
|
{
|
|
|
|
m_content = Vector<String, MemoryDomain::Registers>(values.begin(), values.end());
|
|
|
|
}
|
|
|
|
|
2017-02-14 01:02:01 +01:00
|
|
|
ConstArrayView<String> get(const Context&) override
|
2015-11-25 22:08:33 +01:00
|
|
|
{
|
|
|
|
if (m_content.empty())
|
|
|
|
return ConstArrayView<String>(String::ms_empty);
|
|
|
|
else
|
|
|
|
return ConstArrayView<String>(m_content);
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
Vector<String, MemoryDomain::Registers> m_content;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Dynamic value register, use it's RegisterRetriever
|
|
|
|
// to get it's value when needed.
|
2017-02-14 01:02:01 +01:00
|
|
|
template<typename Getter, typename Setter>
|
2015-11-25 22:08:33 +01:00
|
|
|
class DynamicRegister : public StaticRegister
|
|
|
|
{
|
|
|
|
public:
|
2017-02-14 01:02:01 +01:00
|
|
|
DynamicRegister(Getter getter, Setter setter)
|
|
|
|
: m_getter{std::move(getter)}, m_setter{std::move(setter)} {}
|
2015-11-25 22:08:33 +01:00
|
|
|
|
2017-02-14 01:02:01 +01:00
|
|
|
void set(Context& context, ConstArrayView<String> values) override
|
2015-11-25 22:08:33 +01:00
|
|
|
{
|
2017-02-14 01:02:01 +01:00
|
|
|
m_setter(context, values);
|
2015-11-25 22:08:33 +01:00
|
|
|
}
|
|
|
|
|
2017-02-14 01:02:01 +01:00
|
|
|
ConstArrayView<String> get(const Context& context) override
|
2015-11-25 22:08:33 +01:00
|
|
|
{
|
2017-02-14 01:02:01 +01:00
|
|
|
m_content = m_getter(context);
|
|
|
|
return StaticRegister::get(context);
|
2015-11-25 22:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-02-14 01:02:01 +01:00
|
|
|
Getter m_getter;
|
|
|
|
Setter m_setter;
|
2015-11-25 22:08:33 +01:00
|
|
|
};
|
|
|
|
|
2016-03-31 10:17:02 +02:00
|
|
|
template<typename Func>
|
|
|
|
std::unique_ptr<Register> make_dyn_reg(Func func)
|
|
|
|
{
|
2017-02-14 01:02:01 +01:00
|
|
|
auto setter = [](Context&, ConstArrayView<String>)
|
|
|
|
{
|
|
|
|
throw runtime_error("this register is not assignable");
|
|
|
|
};
|
|
|
|
return make_unique<DynamicRegister<Func, decltype(setter)>>(std::move(func), setter);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Getter, typename Setter>
|
|
|
|
std::unique_ptr<Register> make_dyn_reg(Getter getter, Setter setter)
|
|
|
|
{
|
|
|
|
return make_unique<DynamicRegister<Getter, Setter>>(std::move(getter), std::move(setter));
|
2016-03-31 10:17:02 +02:00
|
|
|
}
|
|
|
|
|
2015-11-25 22:08:33 +01:00
|
|
|
class NullRegister : public Register
|
|
|
|
{
|
|
|
|
public:
|
2017-02-14 01:02:01 +01:00
|
|
|
void set(Context&, ConstArrayView<String>) override {}
|
2015-11-25 22:08:33 +01:00
|
|
|
|
2017-02-14 01:02:01 +01:00
|
|
|
ConstArrayView<String> get(const Context&) override
|
2015-11-25 22:08:33 +01:00
|
|
|
{
|
|
|
|
return ConstArrayView<String>(String::ms_empty);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-09-23 16:31:57 +02:00
|
|
|
class RegisterManager : public Singleton<RegisterManager>
|
|
|
|
{
|
|
|
|
public:
|
2015-11-25 22:08:33 +01:00
|
|
|
Register& operator[](StringView reg) const;
|
|
|
|
Register& operator[](Codepoint c) const;
|
|
|
|
void add_register(char c, std::unique_ptr<Register> reg);
|
2011-09-23 16:31:57 +02:00
|
|
|
|
|
|
|
protected:
|
2015-01-14 20:16:32 +01:00
|
|
|
UnorderedMap<char, std::unique_ptr<Register>, MemoryDomain::Registers> m_registers;
|
2011-09-23 16:31:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // register_manager_hh_INCLUDED
|