From 48f4e384860829966eacf4881fbf1ccbed38be6a Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Wed, 4 Jan 2006 14:15:45 +0000 Subject: [PATCH] Further work on filter registration in RouterFleXML --- src/ex_router_flexml.cpp | 7 ++++--- src/factory_static.cpp | 6 +++--- src/factory_static.hpp | 6 +++--- src/router_flexml.cpp | 33 +++++++++++++++++++-------------- src/router_flexml.hpp | 6 ++++-- src/test_router_flexml.cpp | 43 +++++++++++++++++++++++++++++++------------ 6 files changed, 64 insertions(+), 37 deletions(-) diff --git a/src/ex_router_flexml.cpp b/src/ex_router_flexml.cpp index 013aa32..c2e58f6 100644 --- a/src/ex_router_flexml.cpp +++ b/src/ex_router_flexml.cpp @@ -1,4 +1,4 @@ -/* $Id: ex_router_flexml.cpp,v 1.3 2006-01-04 11:19:04 adam Exp $ +/* $Id: ex_router_flexml.cpp,v 1.4 2006-01-04 14:15:45 adam Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -34,8 +34,9 @@ int main(int argc, char **argv) "\n" "\n" "\n"; - - yp2::RouterFleXML rflexml(xmlconf); + + yp2::FilterFactory factory; + yp2::RouterFleXML rflexml(xmlconf, factory); diff --git a/src/factory_static.cpp b/src/factory_static.cpp index 1f684f6..f988789 100644 --- a/src/factory_static.cpp +++ b/src/factory_static.cpp @@ -1,4 +1,4 @@ -/* $Id: factory_static.cpp,v 1.2 2006-01-04 11:55:31 adam Exp $ +/* $Id: factory_static.cpp,v 1.3 2006-01-04 14:15:45 adam Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -24,7 +24,7 @@ #include "filter_virt_db.hpp" #include "filter_z3950_client.hpp" -yp2::FactoryStatic::FactoryStatic(yp2::FilterFactory &factory) +yp2::FactoryStatic::FactoryStatic() { struct yp2_filter_struct *buildins[] = { &yp2_filter_backend_test, @@ -39,7 +39,7 @@ yp2::FactoryStatic::FactoryStatic(yp2::FilterFactory &factory) int i; for (i = 0; buildins[i]; i++) - factory.add_creator(buildins[i]->type, buildins[i]->creator); + add_creator(buildins[i]->type, buildins[i]->creator); } diff --git a/src/factory_static.hpp b/src/factory_static.hpp index 9433979..d01d13c 100644 --- a/src/factory_static.hpp +++ b/src/factory_static.hpp @@ -1,4 +1,4 @@ -/* $Id: factory_static.hpp,v 1.1 2006-01-04 11:19:04 adam Exp $ +/* $Id: factory_static.hpp,v 1.2 2006-01-04 14:15:45 adam Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -10,9 +10,9 @@ #include "filter_factory.hpp" namespace yp2 { - class FactoryStatic { + class FactoryStatic : public FilterFactory { public: - FactoryStatic(yp2::FilterFactory &factory); + FactoryStatic(); }; } diff --git a/src/router_flexml.cpp b/src/router_flexml.cpp index fc3c6f0..483990e 100644 --- a/src/router_flexml.cpp +++ b/src/router_flexml.cpp @@ -1,4 +1,4 @@ -/* $Id: router_flexml.cpp,v 1.7 2006-01-04 11:19:04 adam Exp $ +/* $Id: router_flexml.cpp,v 1.8 2006-01-04 14:15:45 adam Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -25,15 +25,11 @@ namespace yp2 { friend class RouterFleXML; Rep(); - typedef std::map > - IdFilterMap ; - typedef std::list FilterIdList; - typedef std::map IdRouteMap ; + typedef std::map > + IdFilterMap ; - bool m_xinclude; IdFilterMap m_id_filter_map; - FilterIdList m_filter_id_list; - IdRouteMap m_id_route_map; void create_filter(std::string type, const xmlDoc * xmldoc, @@ -56,7 +52,9 @@ namespace yp2 { const xmlNode* jump_to_next(const xmlNode* node, int xml_node_type); const xmlNode* jump_to_children(const xmlNode* node, int xml_node_type); - FilterFactory m_factory; + bool m_xinclude; + private: + FilterFactory *m_factory; // TODO shared_ptr }; } @@ -166,7 +164,15 @@ void yp2::RouterFleXML::Rep::parse_xml_config_dom(xmlDocPtr doc) //const xmlNode *val; } - yp2::filter::Base* filter_base = m_factory.create(type_value); + yp2::filter::Base* filter_base = m_factory->create(type_value); + + filter_base->configure(node2); + + if (m_id_filter_map.find(id_value) != m_id_filter_map.end()) + throw XMLError("Filter " + id_value + " already defined"); + + m_id_filter_map[id_value] = + boost::shared_ptr(filter_base); node2 = jump_to_next(node2, XML_ELEMENT_NODE); } @@ -219,12 +225,11 @@ yp2::RouterFleXML::Rep::Rep() : { } -yp2::RouterFleXML::RouterFleXML(std::string xmlconf) +yp2::RouterFleXML::RouterFleXML(std::string xmlconf, yp2::FilterFactory &factory) : m_p(new Rep) { - { - yp2::FactoryStatic fs(m_p->m_factory); - } + + m_p->m_factory = &factory; LIBXML_TEST_VERSION; diff --git a/src/router_flexml.hpp b/src/router_flexml.hpp index 14e368c..9cce474 100644 --- a/src/router_flexml.hpp +++ b/src/router_flexml.hpp @@ -1,4 +1,4 @@ -/* $Id: router_flexml.hpp,v 1.7 2005-12-08 15:34:08 adam Exp $ +/* $Id: router_flexml.hpp,v 1.8 2006-01-04 14:15:45 adam Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -6,6 +6,8 @@ #include "router.hpp" +#include "filter_factory.hpp" + #include #include @@ -16,7 +18,7 @@ namespace yp2 { class Rep; public: - RouterFleXML(std::string xmlconf); + RouterFleXML(std::string xmlconf, yp2::FilterFactory &factory); ~RouterFleXML(); diff --git a/src/test_router_flexml.cpp b/src/test_router_flexml.cpp index 8cfbf87..05d7b84 100644 --- a/src/test_router_flexml.cpp +++ b/src/test_router_flexml.cpp @@ -1,4 +1,4 @@ -/* $Id: test_router_flexml.cpp,v 1.9 2006-01-04 11:55:32 adam Exp $ +/* $Id: test_router_flexml.cpp,v 1.10 2006-01-04 14:15:45 adam Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -10,18 +10,24 @@ #include "filter.hpp" #include "router_flexml.hpp" -#include "filter_factory.hpp" +#include "factory_static.hpp" #define BOOST_AUTO_TEST_MAIN #include using namespace boost::unit_test; +static bool tfilter_destroyed = false; class TFilter: public yp2::filter::Base { public: void process(yp2::Package & package) const {}; + ~TFilter() { tfilter_destroyed = true; }; }; - + +static yp2::filter::Base* filter_creator() +{ + return new TFilter; +} BOOST_AUTO_UNIT_TEST( test_router_flexml_1 ) { @@ -37,6 +43,9 @@ BOOST_AUTO_UNIT_TEST( test_router_flexml_1 ) " \n" " mylog1.log\n" " \n" + " \n" + " \n" + " \n" " \n" " mylog2.log\n" " \n" @@ -48,19 +57,19 @@ BOOST_AUTO_UNIT_TEST( test_router_flexml_1 ) " \n" " \n" "\n"; - yp2::RouterFleXML rflexml(xmlconf); - } - catch ( yp2::RouterFleXML::XMLError &e) { - std::cout << "XMLError: " << e.what() << "\n"; - BOOST_CHECK (false); + + yp2::FactoryStatic factory; + factory.add_creator("tfilter", filter_creator); + yp2::RouterFleXML rflexml(xmlconf, factory); } - catch ( yp2::FilterFactoryException &e) { - std::cout << "FilterFactoryException: " << e.what() << "\n"; + catch ( std::runtime_error &e) { + std::cout << "std::runtime error: " << e.what() << "\n"; BOOST_CHECK (false); } catch ( ... ) { BOOST_CHECK (false); } + BOOST_CHECK(tfilter_destroyed == true); } BOOST_AUTO_UNIT_TEST( test_router_flexml_2 ) @@ -75,11 +84,16 @@ BOOST_AUTO_UNIT_TEST( test_router_flexml_2 ) " \n" " 210\n"; - yp2::RouterFleXML rflexml(xmlconf_invalid); + yp2::FilterFactory factory; + yp2::RouterFleXML rflexml(xmlconf_invalid, factory); } catch ( yp2::RouterFleXML::XMLError &e) { got_xml_error = true; } + catch ( std::runtime_error &e) { + std::cout << "std::runtime error: " << e.what() << "\n"; + BOOST_CHECK (false); + } catch ( ... ) { ; } @@ -109,7 +123,12 @@ BOOST_AUTO_UNIT_TEST( test_router_flexml_3 ) " \n" "\n"; - yp2::RouterFleXML rflexml(xmlconf); + yp2::FactoryStatic factory; + yp2::RouterFleXML rflexml(xmlconf, factory); + } + catch ( std::runtime_error &e) { + std::cout << "std::runtime error: " << e.what() << "\n"; + BOOST_CHECK (false); } catch ( ... ) { BOOST_CHECK (false); -- 1.7.10.4