diff --git a/src/filter.hh b/src/filter.hh new file mode 100644 index 00000000..3efa664e --- /dev/null +++ b/src/filter.hh @@ -0,0 +1,16 @@ +#ifndef filter_hh_INCLUDED +#define filter_hh_INCLUDED + +#include + +namespace Kakoune +{ + +class DisplayBuffer; + +typedef std::function FilterFunc; +typedef std::pair FilterAndId; + +} + +#endif // filter_hh_INCLUDED diff --git a/src/filter_registry.cc b/src/filter_registry.cc new file mode 100644 index 00000000..208adaa3 --- /dev/null +++ b/src/filter_registry.cc @@ -0,0 +1,30 @@ +#include "filter_registry.hh" + +#include "exception.hh" + +namespace Kakoune +{ + +struct factory_not_found : public runtime_error +{ + factory_not_found() : runtime_error("factory not found") {} +}; + +void FilterRegistry::register_factory(const std::string& name, + const FilterFactory& factory) +{ + assert(m_factories.find(name) == m_factories.end()); + m_factories[name] = factory; +} + +FilterAndId FilterRegistry::get_filter(const std::string& name, + const FilterParameters& parameters) +{ + auto it = m_factories.find(name); + if (it == m_factories.end()) + throw factory_not_found(); + + return it->second(parameters); +} + +} diff --git a/src/filter_registry.hh b/src/filter_registry.hh new file mode 100644 index 00000000..09b1fec2 --- /dev/null +++ b/src/filter_registry.hh @@ -0,0 +1,32 @@ +#ifndef filter_registry_h_INCLUDED +#define filter_registry_h_INCLUDED + +#include +#include + +#include "filter.hh" +#include "utils.hh" + +namespace Kakoune +{ + +typedef std::vector FilterParameters; + +typedef std::function FilterFactory; + +class FilterRegistry : public Singleton +{ +public: + void register_factory(const std::string& name, + const FilterFactory& factory); + + FilterAndId get_filter(const std::string& factory_name, + const FilterParameters& parameters); + +private: + std::unordered_map m_factories; +}; + +} + +#endif // filter_registry_h_INCLUDED