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