Add a filter registry class

This commit is contained in:
Maxime Coste 2011-11-08 14:27:21 +00:00
parent 06fdacf1c8
commit e4ff1d8ad8
3 changed files with 78 additions and 0 deletions

16
src/filter.hh Normal file
View File

@ -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

30
src/filter_registry.cc Normal file
View File

@ -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);
}
}

32
src/filter_registry.hh Normal file
View File

@ -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