filter factory in working shape, see example in test_filer_factory.cpp
[metaproxy-moved-to-github.git] / src / filter_factory.hpp
1 /* $Id: filter_factory.hpp,v 1.2 2005-10-29 17:58:14 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             bool add_creator(std::string fi, CreateFilterCallback cfc);
40             /// true if unregistration ok
41
42             bool drop_creator(std::string fi);
43             
44             /// factory create method
45
46             yp2::filter::Base* create(std::string fi);
47             
48         private:
49             typedef std::map<std::string, CreateFilterCallback> CallbackMap;
50             CallbackMap m_fcm;
51
52         };
53         
54     }
55     
56     bool yp2::filter::FilterFactory::add_creator(std::string fi,
57                                     CreateFilterCallback cfc)
58     {
59         return m_fcm.insert(CallbackMap::value_type(fi, cfc)).second;
60     }
61     
62     
63     bool yp2::filter::FilterFactory::drop_creator(std::string fi)
64     {
65         return m_fcm.erase(fi) == 1;
66     }
67     
68     yp2::filter::Base* yp2::filter::FilterFactory::create(std::string fi)
69     {
70         CallbackMap::const_iterator i = m_fcm.find(fi);
71         
72         if (i == m_fcm.end()){
73             std::string msg = "filter type '" + fi + "' not found";
74             throw yp2::filter::FilterFactoryException(msg);
75         }
76         // call create function
77         return (i->second());
78     }
79     
80
81   
82 }
83
84 #endif
85 /*
86  * Local variables:
87  * c-basic-offset: 4
88  * indent-tabs-mode: nil
89  * c-file-style: "stroustrup"
90  * End:
91  * vim: shiftwidth=4 tabstop=8 expandtab
92  */