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"
|
2014-12-23 14:34:21 +01:00
|
|
|
#include "containers.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "exception.hh"
|
2014-10-23 19:55:45 +02:00
|
|
|
#include "flags.hh"
|
2013-03-26 13:47:14 +01:00
|
|
|
#include "option_types.hh"
|
2014-10-13 14:12:33 +02:00
|
|
|
#include "regex.hh"
|
2015-01-12 14:24:30 +01:00
|
|
|
#include "vector.hh"
|
2012-04-03 15:39:20 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
class OptionManager;
|
|
|
|
|
2014-04-12 21:03:26 +02:00
|
|
|
enum class OptionFlags
|
|
|
|
{
|
|
|
|
None = 0,
|
|
|
|
Hidden = 1,
|
|
|
|
};
|
|
|
|
|
2014-10-23 19:55:45 +02:00
|
|
|
template<> struct WithBitOps<OptionFlags> : std::true_type {};
|
2014-04-12 21:03:26 +02:00
|
|
|
|
|
|
|
class OptionDesc
|
2012-04-03 15:39:20 +02:00
|
|
|
{
|
|
|
|
public:
|
2014-04-12 21:03:26 +02:00
|
|
|
OptionDesc(String name, String docstring, OptionFlags flags);
|
|
|
|
|
|
|
|
const String& name() const { return m_name; }
|
|
|
|
const String& docstring() const { return m_docstring; }
|
2013-11-12 20:21:07 +01:00
|
|
|
|
2014-04-12 21:03:26 +02:00
|
|
|
OptionFlags flags() const { return m_flags; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
String m_name;
|
|
|
|
String m_docstring;
|
|
|
|
OptionFlags m_flags;
|
|
|
|
};
|
2012-04-03 15:39:20 +02:00
|
|
|
|
2014-04-12 21:03:26 +02:00
|
|
|
class Option
|
|
|
|
{
|
|
|
|
public:
|
2014-04-30 20:40:23 +02:00
|
|
|
virtual ~Option() = default;
|
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
template<typename T> const T& get() const;
|
2015-12-12 12:45:45 +01:00
|
|
|
template<typename T> T& get_mutable();
|
2015-07-24 00:42:48 +02:00
|
|
|
template<typename T> void set(const T& val, bool notify=true);
|
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;
|
2014-11-16 21:55:36 +01:00
|
|
|
virtual void set_from_string(StringView str) = 0;
|
|
|
|
virtual void add_from_string(StringView str) = 0;
|
2012-06-14 15:16:44 +02:00
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
virtual Option* clone(OptionManager& manager) const = 0;
|
2014-04-12 21:03:26 +02:00
|
|
|
OptionManager& manager() const { return m_manager; }
|
2013-11-12 20:21:07 +01:00
|
|
|
|
2014-04-12 21:03:26 +02:00
|
|
|
const String& name() const { return m_desc.name(); }
|
|
|
|
const String& docstring() const { return m_desc.docstring(); }
|
|
|
|
OptionFlags flags() const { return m_desc.flags(); }
|
2013-11-12 20:21:07 +01:00
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
protected:
|
2014-04-12 21:03:26 +02:00
|
|
|
Option(const OptionDesc& desc, OptionManager& manager);
|
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
OptionManager& m_manager;
|
2014-04-12 21:03:26 +02:00
|
|
|
const OptionDesc& m_desc;
|
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
|
|
|
|
2014-11-16 21:55:36 +01:00
|
|
|
Option& operator[] (StringView name);
|
|
|
|
const Option& operator[] (StringView name) const;
|
|
|
|
Option& get_local_option(StringView name);
|
2012-06-14 15:16:44 +02:00
|
|
|
|
2015-08-10 14:54:52 +02:00
|
|
|
void unset_option(StringView name);
|
2012-04-03 15:39:20 +02:00
|
|
|
|
2015-01-12 14:24:30 +01:00
|
|
|
using OptionList = Vector<const Option*>;
|
2013-03-03 17:25:40 +01:00
|
|
|
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
|
2014-10-30 15:00:42 +01:00
|
|
|
friend class Scope;
|
|
|
|
friend class OptionsRegistry;
|
2012-04-03 15:39:20 +02:00
|
|
|
|
2015-01-12 14:24:30 +01:00
|
|
|
Vector<std::unique_ptr<Option>, MemoryDomain::Options> m_options;
|
2012-04-03 15:39:20 +02:00
|
|
|
OptionManager* m_parent;
|
2012-06-14 15:16:44 +02:00
|
|
|
|
2015-01-12 14:24:30 +01:00
|
|
|
Vector<OptionManagerWatcher*, MemoryDomain::Options> m_watchers;
|
2012-04-03 15:39:20 +02:00
|
|
|
};
|
|
|
|
|
2013-03-26 13:47:14 +01:00
|
|
|
template<typename T>
|
|
|
|
class TypedOption : public Option
|
|
|
|
{
|
|
|
|
public:
|
2014-08-19 19:56:11 +02:00
|
|
|
TypedOption(OptionManager& manager, const OptionDesc& desc, const T& value)
|
|
|
|
: Option(desc, manager), m_value(value) {}
|
2013-03-26 13:47:14 +01:00
|
|
|
|
2015-07-24 00:42:48 +02:00
|
|
|
void set(T value, bool notify = true)
|
2013-03-26 13:47:14 +01:00
|
|
|
{
|
|
|
|
if (m_value != value)
|
|
|
|
{
|
2013-05-06 13:52:41 +02:00
|
|
|
m_value = std::move(value);
|
2015-07-24 00:42:48 +02:00
|
|
|
if (notify)
|
|
|
|
manager().on_option_changed(*this);
|
2013-03-26 13:47:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const T& get() const { return m_value; }
|
2015-12-12 12:45:45 +01:00
|
|
|
T& get_mutable() { return m_value; }
|
2013-03-26 13:47:14 +01:00
|
|
|
|
|
|
|
String get_as_string() const override
|
|
|
|
{
|
|
|
|
return option_to_string(m_value);
|
|
|
|
}
|
2014-11-16 21:55:36 +01:00
|
|
|
void set_from_string(StringView str) override
|
2013-03-26 13:47:14 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2014-11-16 21:55:36 +01:00
|
|
|
void add_from_string(StringView str) override
|
2013-03-29 19:34:57 +01:00
|
|
|
{
|
2015-12-12 12:26:34 +01:00
|
|
|
if (option_add(m_value, str))
|
2013-03-29 19:34:57 +01:00
|
|
|
m_manager.on_option_changed(*this);
|
|
|
|
}
|
2013-03-26 13:47:14 +01:00
|
|
|
|
|
|
|
Option* clone(OptionManager& manager) const override
|
|
|
|
{
|
2014-08-19 19:56:11 +02:00
|
|
|
return new TypedOption{manager, m_desc, m_value};
|
2013-03-26 13:47:14 +01:00
|
|
|
}
|
2015-01-12 14:24:30 +01:00
|
|
|
|
|
|
|
using Alloc = Allocator<TypedOption, MemoryDomain::Options>;
|
|
|
|
static void* operator new (std::size_t sz)
|
|
|
|
{
|
|
|
|
kak_assert(sz == sizeof(TypedOption));
|
|
|
|
return Alloc{}.allocate(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void operator delete (void* ptr)
|
|
|
|
{
|
|
|
|
return Alloc{}.deallocate(reinterpret_cast<TypedOption*>(ptr), 1);
|
|
|
|
}
|
2013-03-26 13:47:14 +01:00
|
|
|
private:
|
|
|
|
T m_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T> const T& Option::get() const
|
|
|
|
{
|
|
|
|
auto* typed_opt = dynamic_cast<const TypedOption<T>*>(this);
|
|
|
|
if (not typed_opt)
|
2015-06-01 22:15:59 +02:00
|
|
|
throw runtime_error(format("option '{}' is not of type '{}'", name(), typeid(T).name()));
|
2013-03-26 13:47:14 +01:00
|
|
|
return typed_opt->get();
|
|
|
|
}
|
|
|
|
|
2015-12-12 12:45:45 +01:00
|
|
|
template<typename T> T& Option::get_mutable()
|
|
|
|
{
|
|
|
|
return const_cast<T&>(get<T>());
|
|
|
|
}
|
|
|
|
|
2015-07-24 00:42:48 +02:00
|
|
|
template<typename T> void Option::set(const T& val, bool notify)
|
2013-03-26 13:47:14 +01:00
|
|
|
{
|
|
|
|
auto* typed_opt = dynamic_cast<TypedOption<T>*>(this);
|
|
|
|
if (not typed_opt)
|
2015-06-01 22:15:59 +02:00
|
|
|
throw runtime_error(format("option '{}' is not of type '{}'", name(), typeid(T).name()));
|
2015-07-24 00:42:48 +02:00
|
|
|
return typed_opt->set(val, notify);
|
2013-03-26 13:47:14 +01:00
|
|
|
}
|
|
|
|
|
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>
|
2014-11-16 21:55:36 +01:00
|
|
|
auto find_option(T& container, StringView name) -> decltype(container.begin())
|
2013-03-26 13:47:14 +01:00
|
|
|
{
|
|
|
|
using ptr_type = decltype(*container.begin());
|
|
|
|
return find_if(container, [&name](const ptr_type& opt) { return opt->name() == name; });
|
|
|
|
}
|
|
|
|
|
2014-10-30 15:00:42 +01:00
|
|
|
class OptionsRegistry
|
2012-04-03 15:39:20 +02:00
|
|
|
{
|
|
|
|
public:
|
2014-10-30 15:00:42 +01:00
|
|
|
OptionsRegistry(OptionManager& global_manager) : m_global_manager(global_manager) {}
|
2012-04-03 15:39:20 +02:00
|
|
|
|
2013-03-03 17:25:40 +01:00
|
|
|
template<typename T>
|
2014-04-09 21:14:04 +02:00
|
|
|
Option& declare_option(const String& name, const String& docstring,
|
|
|
|
const T& value,
|
2014-08-19 19:56:11 +02:00
|
|
|
OptionFlags flags = OptionFlags::None)
|
2013-03-26 13:47:14 +01:00
|
|
|
{
|
2014-10-30 15:00:42 +01:00
|
|
|
auto& opts = m_global_manager.m_options;
|
|
|
|
auto it = find_option(opts, name);
|
|
|
|
if (it != opts.end())
|
2013-04-17 19:10:51 +02:00
|
|
|
{
|
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;
|
2015-06-01 22:15:59 +02:00
|
|
|
throw runtime_error(format("option '{}' already declared with different type or flags", name));
|
2013-04-17 19:10:51 +02:00
|
|
|
}
|
2014-04-12 21:03:26 +02:00
|
|
|
m_descs.emplace_back(new OptionDesc{name, docstring, flags});
|
2014-10-30 15:00:42 +01:00
|
|
|
opts.emplace_back(new TypedOption<T>{m_global_manager, *m_descs.back(), value});
|
|
|
|
return *opts.back();
|
2013-03-26 13:47:14 +01:00
|
|
|
}
|
2014-05-05 13:55:04 +02:00
|
|
|
|
2015-08-10 14:53:30 +02:00
|
|
|
const OptionDesc* option_desc(StringView name) const
|
2014-05-05 13:55:04 +02:00
|
|
|
{
|
2015-08-10 14:53:30 +02:00
|
|
|
auto it = find_if(m_descs,
|
|
|
|
[&name](const std::unique_ptr<OptionDesc>& opt)
|
|
|
|
{ return opt->name() == name; });
|
|
|
|
return it != m_descs.end() ? it->get() : nullptr;
|
2014-05-05 13:55:04 +02:00
|
|
|
}
|
2015-08-10 14:38:06 +02:00
|
|
|
|
2015-08-10 14:53:30 +02:00
|
|
|
bool option_exists(StringView name) const { return option_desc(name) != nullptr; }
|
|
|
|
|
2015-08-10 14:38:06 +02:00
|
|
|
CandidateList complete_option_name(StringView prefix, ByteCount cursor_pos) const;
|
2014-04-12 21:03:26 +02:00
|
|
|
private:
|
2014-10-30 15:00:42 +01:00
|
|
|
OptionManager& m_global_manager;
|
2015-01-12 14:24:30 +01:00
|
|
|
Vector<std::unique_ptr<OptionDesc>, MemoryDomain::Options> m_descs;
|
2013-03-03 17:25:40 +01:00
|
|
|
};
|
2012-04-03 15:39:20 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // option_manager_hh_INCLUDED
|