2012-04-03 17:39:20 +04:00
|
|
|
#ifndef option_manager_hh_INCLUDED
|
|
|
|
#define option_manager_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "utils.hh"
|
|
|
|
#include "exception.hh"
|
2012-04-03 22:25:27 +04:00
|
|
|
#include "completion.hh"
|
2012-04-03 17:39:20 +04:00
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
struct option_not_found : public runtime_error
|
|
|
|
{
|
2012-04-14 05:17:09 +04:00
|
|
|
option_not_found(const String& name)
|
2012-04-03 17:39:20 +04:00
|
|
|
: runtime_error("option not found: " + name) {}
|
|
|
|
};
|
|
|
|
|
2012-04-14 05:17:09 +04:00
|
|
|
String int_to_str(int value);
|
2012-04-03 17:39:20 +04:00
|
|
|
|
|
|
|
class Option
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Option() {}
|
|
|
|
explicit Option(int value) : m_value(int_to_str(value)) {}
|
2012-04-14 05:17:09 +04:00
|
|
|
explicit Option(const String& value) : m_value(value) {}
|
2012-04-03 17:39:20 +04:00
|
|
|
|
|
|
|
Option& operator=(int value) { m_value = int_to_str(value); return *this; }
|
2012-04-14 05:17:09 +04:00
|
|
|
Option& operator=(const String& value) { m_value = value; return *this; }
|
2012-04-03 17:39:20 +04:00
|
|
|
|
2012-06-14 17:16:44 +04:00
|
|
|
bool operator==(const Option& other) const { return m_value == other.m_value; }
|
|
|
|
bool operator!=(const Option& other) const { return m_value != other.m_value; }
|
|
|
|
|
2012-06-27 16:26:29 +04:00
|
|
|
int as_int() const { return str_to_int(m_value); }
|
2012-06-12 22:26:20 +04:00
|
|
|
String as_string() const { return m_value; }
|
2012-04-03 17:39:20 +04:00
|
|
|
private:
|
2012-04-14 05:17:09 +04:00
|
|
|
String m_value;
|
2012-04-03 17:39:20 +04:00
|
|
|
};
|
|
|
|
|
2012-06-14 17:16:44 +04:00
|
|
|
class OptionManagerWatcher
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~OptionManagerWatcher() {}
|
|
|
|
|
|
|
|
virtual void on_option_changed(const String& name,
|
|
|
|
const Option& option) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class OptionManager : private OptionManagerWatcher
|
2012-04-03 17:39:20 +04:00
|
|
|
{
|
|
|
|
public:
|
2012-06-14 17:16:44 +04:00
|
|
|
OptionManager(OptionManager& parent);
|
|
|
|
~OptionManager();
|
2012-04-03 17:39:20 +04:00
|
|
|
|
2012-04-14 05:17:09 +04:00
|
|
|
const Option& operator[] (const String& name) const;
|
2012-04-03 22:25:27 +04:00
|
|
|
|
2012-06-14 17:16:44 +04:00
|
|
|
void set_option(const String& name, const Option& value);
|
|
|
|
|
2012-04-14 05:17:09 +04:00
|
|
|
CandidateList complete_option_name(const String& prefix,
|
2012-10-11 02:41:48 +04:00
|
|
|
ByteCount cursor_pos);
|
2012-04-03 17:39:20 +04:00
|
|
|
|
2012-06-14 17:16:44 +04:00
|
|
|
typedef std::unordered_map<String, Option> OptionMap;
|
|
|
|
OptionMap flatten_options() const;
|
|
|
|
|
|
|
|
void register_watcher(OptionManagerWatcher& watcher);
|
|
|
|
void unregister_watcher(OptionManagerWatcher& watcher);
|
|
|
|
|
2012-04-03 17:39:20 +04:00
|
|
|
private:
|
|
|
|
OptionManager()
|
|
|
|
: m_parent(nullptr) {}
|
|
|
|
// the only one allowed to construct a root option manager
|
|
|
|
friend class GlobalOptionManager;
|
|
|
|
|
2012-06-14 17:16:44 +04:00
|
|
|
OptionMap m_options;
|
2012-04-03 17:39:20 +04:00
|
|
|
OptionManager* m_parent;
|
2012-06-14 17:16:44 +04:00
|
|
|
|
|
|
|
void on_option_changed(const String& name,
|
|
|
|
const Option& value);
|
|
|
|
|
|
|
|
std::vector<OptionManagerWatcher*> m_watchers;
|
2012-04-03 17:39:20 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class GlobalOptionManager : public OptionManager,
|
|
|
|
public Singleton<GlobalOptionManager>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GlobalOptionManager();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // option_manager_hh_INCLUDED
|