diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2011-11-08 14:27:21 +0000 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2011-11-08 14:27:21 +0000 |
| commit | e4ff1d8ad8f3daf9ce2931d4a0cc58604e164bcd (patch) | |
| tree | b8b010f030f3d93bbe7b0b755dba8123977a7379 /src | |
| parent | 06fdacf1c898cf0befaca95a148b354236aa4c16 (diff) | |
Add a filter registry class
Diffstat (limited to 'src')
| -rw-r--r-- | src/filter.hh | 16 | ||||
| -rw-r--r-- | src/filter_registry.cc | 30 | ||||
| -rw-r--r-- | src/filter_registry.hh | 32 |
3 files changed, 78 insertions, 0 deletions
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 <functional> + +namespace Kakoune +{ + +class DisplayBuffer; + +typedef std::function<void (DisplayBuffer& display_buffer)> FilterFunc; +typedef std::pair<std::string, FilterFunc> 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 <string> +#include <unordered_map> + +#include "filter.hh" +#include "utils.hh" + +namespace Kakoune +{ + +typedef std::vector<std::string> FilterParameters; + +typedef std::function<FilterAndId (const FilterParameters& params)> FilterFactory; + +class FilterRegistry : public Singleton<FilterRegistry> +{ +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<std::string, FilterFactory> m_factories; +}; + +} + +#endif // filter_registry_h_INCLUDED |
