2012-04-03 15:39:20 +02:00
|
|
|
#ifndef option_manager_hh_INCLUDED
|
|
|
|
#define option_manager_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "utils.hh"
|
|
|
|
#include "exception.hh"
|
2012-04-03 20:25:27 +02:00
|
|
|
#include "completion.hh"
|
2012-04-03 15:39:20 +02:00
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
struct option_not_found : public runtime_error
|
|
|
|
{
|
2012-04-14 03:17:09 +02:00
|
|
|
option_not_found(const String& name)
|
2012-04-03 15:39:20 +02:00
|
|
|
: runtime_error("option not found: " + name) {}
|
|
|
|
};
|
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
class OptionManager;
|
|
|
|
|
2012-04-03 15:39:20 +02:00
|
|
|
class Option
|
|
|
|
{
|
|
|
|
public:
|
2013-03-03 17:25:40 +01:00
|
|
|
Option(OptionManager& manager, String name);
|
|
|
|
virtual ~Option() {}
|
2012-04-03 15:39:20 +02:00
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
template<typename T> const T& get() const;
|
|
|
|
template<typename T> void set(const T& val);
|
2012-04-03 15:39:20 +02:00
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
virtual String get_as_string() const = 0;
|
|
|
|
virtual void set_from_string(const String& str) = 0;
|
2012-06-14 15:16:44 +02:00
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
String name() const { return m_name; }
|
|
|
|
OptionManager& manager() const { return m_manager; }
|
|
|
|
|
|
|
|
virtual Option* clone(OptionManager& manager) const = 0;
|
|
|
|
protected:
|
|
|
|
OptionManager& m_manager;
|
|
|
|
String m_name;
|
2012-04-03 15:39:20 +02:00
|
|
|
};
|
|
|
|
|
2012-06-14 15:16:44 +02:00
|
|
|
class OptionManagerWatcher
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~OptionManagerWatcher() {}
|
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
virtual void on_option_changed(const Option& option) = 0;
|
2012-06-14 15:16:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class OptionManager : private OptionManagerWatcher
|
2012-04-03 15:39:20 +02:00
|
|
|
{
|
|
|
|
public:
|
2012-06-14 15:16:44 +02:00
|
|
|
OptionManager(OptionManager& parent);
|
|
|
|
~OptionManager();
|
2012-04-03 15:39:20 +02:00
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
const Option& operator[] (const String& name) const;
|
2013-03-03 17:25:40 +01:00
|
|
|
Option& get_local_option(const String& name);
|
2012-06-14 15:16:44 +02:00
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
CandidateList complete_option_name(const String& prefix,
|
2012-10-11 00:41:48 +02:00
|
|
|
ByteCount cursor_pos);
|
2012-04-03 15:39:20 +02:00
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
using OptionList = std::vector<const Option*>;
|
|
|
|
OptionList flatten_options() const;
|
2012-06-14 15:16:44 +02:00
|
|
|
|
|
|
|
void register_watcher(OptionManagerWatcher& watcher);
|
|
|
|
void unregister_watcher(OptionManagerWatcher& watcher);
|
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
void on_option_changed(const Option& option) override;
|
2012-04-03 15:39:20 +02:00
|
|
|
private:
|
|
|
|
OptionManager()
|
|
|
|
: m_parent(nullptr) {}
|
|
|
|
// the only one allowed to construct a root option manager
|
2012-11-22 13:50:29 +01:00
|
|
|
friend class GlobalOptions;
|
2012-04-03 15:39:20 +02:00
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
std::vector<std::unique_ptr<Option>> m_options;
|
2012-04-03 15:39:20 +02:00
|
|
|
OptionManager* m_parent;
|
2012-06-14 15:16:44 +02:00
|
|
|
|
|
|
|
std::vector<OptionManagerWatcher*> m_watchers;
|
2012-04-03 15:39:20 +02:00
|
|
|
};
|
|
|
|
|
2012-11-22 13:50:29 +01:00
|
|
|
class GlobalOptions : public OptionManager,
|
|
|
|
public Singleton<GlobalOptions>
|
2012-04-03 15:39:20 +02:00
|
|
|
{
|
|
|
|
public:
|
2012-11-22 13:50:29 +01:00
|
|
|
GlobalOptions();
|
2012-04-03 15:39:20 +02:00
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
template<typename T>
|
|
|
|
Option& declare_option(const String& name, const T& inital_value);
|
|
|
|
};
|
2012-04-03 15:39:20 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // option_manager_hh_INCLUDED
|