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
|
|
|
|
2012-06-29 18:37:17 +02:00
|
|
|
#include <functional>
|
2012-02-10 00:47:55 +01:00
|
|
|
|
2011-09-23 16:31:57 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-04-23 22:51:48 +02:00
|
|
|
class Context;
|
|
|
|
|
|
|
|
class Register
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~Register() {}
|
|
|
|
virtual Register& operator=(ConstArrayView<String> values) = 0;
|
|
|
|
|
|
|
|
virtual ConstArrayView<String> values(const Context& context) = 0;
|
|
|
|
};
|
|
|
|
|
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:
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2015-01-14 20:16:32 +01:00
|
|
|
using RegisterRetriever = std::function<Vector<String, MemoryDomain::Registers> (const Context&)>;
|
2012-06-29 18:37:17 +02:00
|
|
|
|
2015-11-25 22:08:33 +01:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|