From e4ff1d8ad8f3daf9ce2931d4a0cc58604e164bcd Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 8 Nov 2011 14:27:21 +0000 Subject: [PATCH] Add a filter registry class --- src/filter.hh | 16 ++++++++++++++++ src/filter_registry.cc | 30 ++++++++++++++++++++++++++++++ src/filter_registry.hh | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 src/filter.hh create mode 100644 src/filter_registry.cc create mode 100644 src/filter_registry.hh diff --git a/src/filter.hh b/src/filter.hh new file mode 100644 index 000000000..3efa664e9 --- /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 000000000..208adaa35 --- /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 000000000..09b1fec2a --- /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