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