first functional filter factory implementation plus test case added
authorMarc Cromme <marc@indexdata.dk>
Sat, 29 Oct 2005 17:58:14 +0000 (17:58 +0000)
committerMarc Cromme <marc@indexdata.dk>
Sat, 29 Oct 2005 17:58:14 +0000 (17:58 +0000)
src/filter_factory.hpp
src/test_filter_factory.cpp

index 0ac8b67..902c84d 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: filter_factory.hpp,v 1.1 2005-10-28 10:35:30 marc Exp $
+/* $Id: filter_factory.hpp,v 1.2 2005-10-29 17:58:14 marc Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
 namespace yp2 {
 
     namespace filter {
+
+        
+
+    
+    class FilterFactoryException : public std::runtime_error {
+    public:
+        FilterFactoryException(const std::string message)
+            : std::runtime_error("FilterException: " + message){
+        };
+    };
+
         class FilterFactory {
 
-#if 0
         public:
             typedef yp2::filter::Base* (*CreateFilterCallback)();
             /// true if registration ok
-            bool register_filter(std::string fi, CreateFilterCallback cfc);
+
+            bool add_creator(std::string fi, CreateFilterCallback cfc);
             /// true if unregistration ok
-            bool unregister_filter(std::string fi);
+
+            bool drop_creator(std::string fi);
+            
             /// factory create method
+
             yp2::filter::Base* create(std::string fi);
             
         private:
             typedef std::map<std::string, CreateFilterCallback> CallbackMap;
-
-#endif      
+            CallbackMap m_fcm;
 
         };
+        
+    }
+    
+    bool yp2::filter::FilterFactory::add_creator(std::string fi,
+                                    CreateFilterCallback cfc)
+    {
+        return m_fcm.insert(CallbackMap::value_type(fi, cfc)).second;
+    }
+    
+    
+    bool yp2::filter::FilterFactory::drop_creator(std::string fi)
+    {
+        return m_fcm.erase(fi) == 1;
+    }
+    
+    yp2::filter::Base* yp2::filter::FilterFactory::create(std::string fi)
+    {
+        CallbackMap::const_iterator i = m_fcm.find(fi);
+        
+        if (i == m_fcm.end()){
+            std::string msg = "filter type '" + fi + "' not found";
+            throw yp2::filter::FilterFactoryException(msg);
+        }
+        // call create function
+        return (i->second());
     }
     
-    class FilterFactoryException : public std::runtime_error {
-    public:
-        FilterFactoryException(const std::string message)
-            : std::runtime_error("FilterException: " + message){
-        };
-    };
 
   
 }
index 37b2a57..2ab42af 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: test_filter_factory.cpp,v 1.1 2005-10-28 10:35:30 marc Exp $
+/* $Id: test_filter_factory.cpp,v 1.2 2005-10-29 17:58:14 marc Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -21,31 +21,62 @@ using namespace boost::unit_test;
 class XFilter: public yp2::filter::Base {
 public:
     void process(yp2::Package & package) const {};
+    std::string name(){
+        return std::string("xfilter");
+        }   
 };
 
+
+yp2::filter::Base* xfilter_creator(){
+    return new XFilter;
+}
+
+
 class YFilter: public yp2::filter::Base {
 public:
     void process(yp2::Package & package) const {};
+    std::string name(){
+        return std::string("yfilter");
+        }   
 };
-    
 
-BOOST_AUTO_TEST_CASE( test_router_flexml_1 )
+yp2::filter::Base* yfilter_creator(){
+    return new YFilter;
+}
+
+
+
+//int main(int argc, char **argv)
+BOOST_AUTO_TEST_CASE( test_filter_factory_1 )
 {
-    try{
+    try {
         
         yp2::filter::FilterFactory  ffactory;
         
-
-        BOOST_CHECK (true);
-
-        //BOOST_CHECK_EQUAL(filter.name(), std::string("filter1"));
+        BOOST_CHECK_EQUAL(ffactory.add_creator("xfilter", xfilter_creator),
+                          true);
+        BOOST_CHECK_EQUAL(ffactory.drop_creator("xfilter"),
+                          true);
+        BOOST_CHECK_EQUAL(ffactory.add_creator("xfilter", xfilter_creator),
+                          true);
+        BOOST_CHECK_EQUAL(ffactory.add_creator("yfilter", yfilter_creator),
+                          true);
         
-    }
+        yp2::filter::Base* xfilter = ffactory.create("xfilter");
+        yp2::filter::Base* yfilter = ffactory.create("yfilter");
+        
+        //BOOST_CHECK_EQUAL(xfilter->name(), std::string("xfilter"));
+        //BOOST_CHECK_EQUAL(yfilter->name(), std::string("yfilter"));
+        
+        }
     catch ( ... ) {
         BOOST_CHECK (false);
     }
+        
+    std::exit(0);
 }
 
+
 /*
  * Local variables:
  * c-basic-offset: 4