7ad4e191523f7fbe94ee99385dfc57c0afc87338
[metaproxy-moved-to-github.git] / src / filter_factory.hpp
1 /* $Id: filter_factory.hpp,v 1.4 2005-10-31 09:40:18 marc Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6
7 #ifndef FILTER_FACTORY_HPP
8 #define FILTER_FACTORY_HPP
9
10 #include <stdexcept>
11 #include <iostream>
12 #include <string>
13 #include <map>
14
15 #include "config.hpp"
16 #include "filter.hpp"
17
18
19 namespace yp2 {
20
21     namespace filter {
22     
23     class FilterFactoryException : public std::runtime_error {
24     public:
25         FilterFactoryException(const std::string message)
26             : std::runtime_error("FilterException: " + message){
27         };
28     };
29
30         class FilterFactory {
31
32         public:
33             typedef yp2::filter::Base* (*CreateFilterCallback)();
34             /// true if registration ok
35
36             FilterFactory(){};
37
38             bool add_creator(std::string fi, CreateFilterCallback cfc);
39             /// true if unregistration ok
40
41             bool drop_creator(std::string fi);
42             
43             /// factory create method
44
45             yp2::filter::Base* create(std::string fi);
46             
47         private:
48             typedef std::map<std::string, CreateFilterCallback> CallbackMap;
49             CallbackMap m_fcm;
50
51         private:
52             /// disabled because class is singleton
53             FilterFactory(const FilterFactory &);
54             
55             /// disabled because class is singleton
56             FilterFactory& operator=(const FilterFactory &);
57         };
58         
59     }
60     
61     bool yp2::filter::FilterFactory::add_creator(std::string fi,
62                                     CreateFilterCallback cfc)
63     {
64         return m_fcm.insert(CallbackMap::value_type(fi, cfc)).second;
65     }
66     
67     
68     bool yp2::filter::FilterFactory::drop_creator(std::string fi)
69     {
70         return m_fcm.erase(fi) == 1;
71     }
72     
73     yp2::filter::Base* yp2::filter::FilterFactory::create(std::string fi)
74     {
75         CallbackMap::const_iterator i = m_fcm.find(fi);
76         
77         if (i == m_fcm.end()){
78             std::string msg = "filter type '" + fi + "' not found";
79             throw yp2::filter::FilterFactoryException(msg);
80         }
81         // call create function
82         return (i->second());
83     }
84     
85
86   
87 }
88
89 #endif
90 /*
91  * Local variables:
92  * c-basic-offset: 4
93  * indent-tabs-mode: nil
94  * c-file-style: "stroustrup"
95  * End:
96  * vim: shiftwidth=4 tabstop=8 expandtab
97  */