2012-04-03 15:39:20 +02:00
|
|
|
#ifndef option_manager_hh_INCLUDED
|
|
|
|
#define option_manager_hh_INCLUDED
|
|
|
|
|
2012-04-03 20:25:27 +02:00
|
|
|
#include "completion.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "exception.hh"
|
2013-03-26 13:47:14 +01:00
|
|
|
#include "option_types.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "utils.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;
|
|
|
|
|
2013-04-29 14:20:42 +02:00
|
|
|
class Option : public SafeCountable
|
2012-04-03 15:39:20 +02:00
|
|
|
{
|
|
|
|
public:
|
2013-11-12 20:21:07 +01:00
|
|
|
enum class Flags
|
|
|
|
{
|
|
|
|
None = 0,
|
|
|
|
Hidden = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
Option(OptionManager& manager, String name, Flags flags);
|
2013-03-03 17:25:40 +01:00
|
|
|
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);
|
2013-04-17 19:10:51 +02:00
|
|
|
template<typename T> bool is_of_type() const;
|
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;
|
2013-03-29 19:34:57 +01:00
|
|
|
virtual void add_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;
|
2013-11-12 20:21:07 +01:00
|
|
|
|
|
|
|
Flags flags() const { return m_flags; }
|
|
|
|
|
|
|
|
friend constexpr Flags operator|(Flags lhs, Flags rhs)
|
|
|
|
{ return (Flags)((int)lhs | (int)rhs); }
|
|
|
|
|
|
|
|
friend constexpr bool operator&(Flags lhs, Flags rhs)
|
|
|
|
{ return (bool)((int)lhs & (int)rhs); }
|
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
protected:
|
|
|
|
OptionManager& m_manager;
|
|
|
|
String m_name;
|
2013-11-12 20:21:07 +01:00
|
|
|
Flags m_flags;
|
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
|
|
|
};
|
|
|
|
|
2013-05-06 13:52:41 +02:00
|
|
|
template<typename T> using OptionChecker = std::function<void (const T&)>;
|
|
|
|
|
2013-03-26 13:47:14 +01:00
|
|
|
template<typename T>
|
|
|
|
class TypedOption : public Option
|
|
|
|
{
|
|
|
|
public:
|
2013-11-12 20:21:07 +01:00
|
|
|
TypedOption(OptionManager& manager, String name, Option::Flags flags,
|
|
|
|
const T& value, OptionChecker<T> checker)
|
|
|
|
: Option(manager, std::move(name), flags), m_value(value),
|
2013-05-06 13:52:41 +02:00
|
|
|
m_checker(std::move(checker)) {}
|
2013-03-26 13:47:14 +01:00
|
|
|
|
2013-05-06 13:52:41 +02:00
|
|
|
void set(T value)
|
2013-03-26 13:47:14 +01:00
|
|
|
{
|
|
|
|
if (m_value != value)
|
|
|
|
{
|
2013-05-06 13:52:41 +02:00
|
|
|
if (m_checker)
|
|
|
|
m_checker(value);
|
|
|
|
m_value = std::move(value);
|
2013-03-26 13:47:14 +01:00
|
|
|
m_manager.on_option_changed(*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const T& get() const { return m_value; }
|
|
|
|
|
|
|
|
String get_as_string() const override
|
|
|
|
{
|
|
|
|
return option_to_string(m_value);
|
|
|
|
}
|
|
|
|
void set_from_string(const String& str) override
|
|
|
|
{
|
|
|
|
T val;
|
|
|
|
option_from_string(str, val);
|
2013-05-06 13:52:41 +02:00
|
|
|
set(std::move(val));
|
2013-03-26 13:47:14 +01:00
|
|
|
}
|
2013-03-29 19:34:57 +01:00
|
|
|
void add_from_string(const String& str) override
|
|
|
|
{
|
|
|
|
T val;
|
|
|
|
option_from_string(str, val);
|
2013-05-06 13:52:41 +02:00
|
|
|
if (m_checker)
|
|
|
|
m_checker(val);
|
2013-03-29 19:34:57 +01:00
|
|
|
if (option_add(m_value, val))
|
|
|
|
m_manager.on_option_changed(*this);
|
|
|
|
}
|
2013-03-26 13:47:14 +01:00
|
|
|
|
|
|
|
Option* clone(OptionManager& manager) const override
|
|
|
|
{
|
2013-11-12 20:21:07 +01:00
|
|
|
return new TypedOption{manager, name(), flags(), m_value, m_checker};
|
2013-03-26 13:47:14 +01:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
T m_value;
|
2013-05-06 13:52:41 +02:00
|
|
|
OptionChecker<T> m_checker;
|
2013-03-26 13:47:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T> const T& Option::get() const
|
|
|
|
{
|
|
|
|
auto* typed_opt = dynamic_cast<const TypedOption<T>*>(this);
|
|
|
|
if (not typed_opt)
|
|
|
|
throw runtime_error("option " + name() + " is not of type " + typeid(T).name());
|
|
|
|
return typed_opt->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> void Option::set(const T& val)
|
|
|
|
{
|
|
|
|
auto* typed_opt = dynamic_cast<TypedOption<T>*>(this);
|
|
|
|
if (not typed_opt)
|
|
|
|
throw runtime_error("option " + name() + " is not of type " + typeid(T).name());
|
|
|
|
return typed_opt->set(val);
|
|
|
|
}
|
|
|
|
|
2013-04-17 19:10:51 +02:00
|
|
|
template<typename T> bool Option::is_of_type() const
|
|
|
|
{
|
|
|
|
return dynamic_cast<const TypedOption<T>*>(this) != nullptr;
|
|
|
|
}
|
|
|
|
|
2013-03-26 13:47:14 +01:00
|
|
|
template<typename T>
|
|
|
|
auto find_option(T& container, const String& name) -> decltype(container.begin())
|
|
|
|
{
|
|
|
|
using ptr_type = decltype(*container.begin());
|
|
|
|
return find_if(container, [&name](const ptr_type& opt) { return opt->name() == name; });
|
|
|
|
}
|
|
|
|
|
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>
|
2013-05-06 13:52:41 +02:00
|
|
|
Option& declare_option(const String& name, const T& value,
|
2013-11-12 20:21:07 +01:00
|
|
|
Option::Flags flags = Option::Flags::None,
|
2013-05-06 13:52:41 +02:00
|
|
|
OptionChecker<T> checker = OptionChecker<T>{})
|
2013-03-26 13:47:14 +01:00
|
|
|
{
|
2013-04-17 19:10:51 +02:00
|
|
|
auto it = find_option(m_options, name);
|
|
|
|
if (it != m_options.end())
|
|
|
|
{
|
2013-11-12 20:21:07 +01:00
|
|
|
if ((*it)->is_of_type<T>() and (*it)->flags() == flags)
|
2013-04-17 19:10:51 +02:00
|
|
|
return **it;
|
2013-11-12 20:21:07 +01:00
|
|
|
throw runtime_error("option " + name + " already declared with different type or flags");
|
2013-04-17 19:10:51 +02:00
|
|
|
}
|
2013-11-12 20:21:07 +01:00
|
|
|
m_options.emplace_back(new TypedOption<T>{*this, name, flags, value,
|
|
|
|
std::move(checker)});
|
2013-03-26 13:47:14 +01:00
|
|
|
return *m_options.back();
|
|
|
|
}
|
2013-03-03 17:25:40 +01:00
|
|
|
};
|
2012-04-03 15:39:20 +02:00
|
|
|
|
2013-04-02 13:57:04 +02:00
|
|
|
struct OptionManagerRegisterFuncs
|
|
|
|
{
|
|
|
|
static void insert(OptionManager& options, OptionManagerWatcher& watcher)
|
|
|
|
{
|
|
|
|
options.register_watcher(watcher);
|
|
|
|
}
|
|
|
|
static void remove(OptionManager& options, OptionManagerWatcher& watcher)
|
|
|
|
{
|
|
|
|
options.unregister_watcher(watcher);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class OptionManagerWatcher_AutoRegister
|
|
|
|
: public OptionManagerWatcher,
|
|
|
|
public AutoRegister<OptionManagerWatcher_AutoRegister,
|
|
|
|
OptionManagerRegisterFuncs, OptionManager>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
OptionManagerWatcher_AutoRegister(OptionManager& options)
|
|
|
|
: AutoRegister(options) {}
|
2013-05-03 18:38:12 +02:00
|
|
|
|
|
|
|
OptionManager& options() { return registry(); }
|
2013-04-02 13:57:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-04-03 15:39:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // option_manager_hh_INCLUDED
|