From 7467b072bb64f5c8575daa9917cce7df62d832f1 Mon Sep 17 00:00:00 2001 From: Marc Cromme Date: Sat, 29 Oct 2005 22:23:36 +0000 Subject: [PATCH] filter factory in working shape, see example in test_filer_factory.cpp added member function const std::string type() const to all filters in both hpp and test code --- src/.cvsignore | 1 + src/Makefile.am | 6 ++-- src/ex_filter_frontend_net.cpp | 5 ++- src/filter.cpp | 24 +++++++++++++ src/filter.hpp | 22 ++---------- src/filter_backend_test.hpp | 5 ++- src/filter_frontend_net.hpp | 5 ++- src/filter_log.hpp | 5 ++- src/filter_virt_db.hpp | 5 ++- src/filter_z3950_client.hpp | 5 ++- src/test_filter1.cpp | 14 ++++---- src/test_filter2.cpp | 10 ++++-- src/test_filter_factory.cpp | 71 ++++++++++++++++++++++++++++---------- src/test_filter_frontend_net.cpp | 5 ++- src/test_filter_log.cpp | 5 ++- src/test_router_flexml.cpp | 5 ++- 16 files changed, 133 insertions(+), 60 deletions(-) create mode 100644 src/filter.cpp diff --git a/src/.cvsignore b/src/.cvsignore index 314f478..b89e0d7 100644 --- a/src/.cvsignore +++ b/src/.cvsignore @@ -21,6 +21,7 @@ test_package1 test_thread_pool_observer test_session1 test_session2 +test_filter_factory test_filter_z3950_client test_filter_backend_test test_filter_virt_db diff --git a/src/Makefile.am b/src/Makefile.am index 527da10..c61fd4d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,4 @@ -## $Id: Makefile.am,v 1.30 2005-10-28 10:35:30 marc Exp $ +## $Id: Makefile.am,v 1.31 2005-10-29 22:23:36 marc Exp $ MAINTAINERCLEANFILES = Makefile.in config.in config.hpp @@ -10,11 +10,11 @@ lib_LTLIBRARIES = libyp2.la libyp2_la_LDFLAGS = -version-info 0:0:0 libyp2_la_SOURCES = \ - session.cpp session.hpp package.hpp filter.hpp\ + session.cpp session.hpp package.hpp \ router.hpp router_chain.hpp router_chain.cpp \ router_flexml.hpp router_flexml.cpp \ thread_pool_observer.cpp thread_pool_observer.hpp \ - filter_factory.hpp \ + filter.hpp filter.cpp filter_factory.hpp \ filter_frontend_net.cpp filter_frontend_net.hpp \ filter_log.cpp filter_log.hpp \ filter_virt_db.cpp filter_virt_db.hpp \ diff --git a/src/ex_filter_frontend_net.cpp b/src/ex_filter_frontend_net.cpp index d7701ad..df2e214 100644 --- a/src/ex_filter_frontend_net.cpp +++ b/src/ex_filter_frontend_net.cpp @@ -1,4 +1,4 @@ -/* $Id: ex_filter_frontend_net.cpp,v 1.14 2005-10-26 18:53:49 adam Exp $ +/* $Id: ex_filter_frontend_net.cpp,v 1.15 2005-10-29 22:23:36 marc Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -49,6 +49,9 @@ public: } return package.move(); }; + const std::string type() const { + return "HTTPFilter"; + }; }; int main(int argc, char **argv) diff --git a/src/filter.cpp b/src/filter.cpp new file mode 100644 index 0000000..14f6e37 --- /dev/null +++ b/src/filter.cpp @@ -0,0 +1,24 @@ +/* $Id: filter.cpp,v 1.1 2005-10-29 22:23:36 marc Exp $ + Copyright (c) 2005, Index Data. + +%LICENSE% + */ + +#include + +#include "filter.hpp" + + +// defining and initializing static members + +// std::string yp2::filter::Base:m_type("Base"); + + +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * c-file-style: "stroustrup" + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ diff --git a/src/filter.hpp b/src/filter.hpp index 8f053b5..b71e85f 100644 --- a/src/filter.hpp +++ b/src/filter.hpp @@ -1,4 +1,4 @@ -/* $Id: filter.hpp,v 1.6 2005-10-24 09:53:06 adam Exp $ +/* $Id: filter.hpp,v 1.7 2005-10-29 22:23:36 marc Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -22,26 +22,10 @@ namespace yp2 { ///sends Package off to next Filter, returns altered Package virtual void process(Package & package) const = 0; - virtual void configure(const xmlNode * ptr = 0) { } ; + virtual void configure(const xmlNode * ptr = 0) { }; - /// get function - right val in assignment - std::string name() const { - return m_name; - } - /// set function - left val in assignment - std::string & name() { - return m_name; - } - - /// set function - can be chained - Base & name(const std::string & name){ - m_name = name; - return *this; - } - - private: - std::string m_name; + virtual const std::string type() const = 0; }; } diff --git a/src/filter_backend_test.hpp b/src/filter_backend_test.hpp index 0b2b757..6442d43 100644 --- a/src/filter_backend_test.hpp +++ b/src/filter_backend_test.hpp @@ -1,4 +1,4 @@ -/* $Id: filter_backend_test.hpp,v 1.2 2005-10-29 15:54:29 adam Exp $ +/* $Id: filter_backend_test.hpp,v 1.3 2005-10-29 22:23:36 marc Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -21,6 +21,9 @@ namespace yp2 { ~Backend_test(); Backend_test(); void process(yp2::Package & package) const; + const std::string type() const { + return "Backend_test"; + }; private: boost::scoped_ptr m_p; }; diff --git a/src/filter_frontend_net.hpp b/src/filter_frontend_net.hpp index 98d2c6f..2a68a91 100644 --- a/src/filter_frontend_net.hpp +++ b/src/filter_frontend_net.hpp @@ -1,4 +1,4 @@ -/* $Id: filter_frontend_net.hpp,v 1.5 2005-10-15 14:09:09 adam Exp $ +/* $Id: filter_frontend_net.hpp,v 1.6 2005-10-29 22:23:36 marc Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -18,6 +18,9 @@ namespace yp2 { public: FrontendNet::FrontendNet(); void process(yp2::Package & package) const; + const std::string type() const { + return "FrontendNet"; + }; private: int m_no_threads; std::vector m_ports; diff --git a/src/filter_log.hpp b/src/filter_log.hpp index e2af7c0..58cb266 100644 --- a/src/filter_log.hpp +++ b/src/filter_log.hpp @@ -1,4 +1,4 @@ -/* $Id: filter_log.hpp,v 1.7 2005-10-25 16:01:36 adam Exp $ +/* $Id: filter_log.hpp,v 1.8 2005-10-29 22:23:36 marc Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -22,6 +22,9 @@ namespace yp2 { Log(const std::string &msg); Log(); void process(yp2::Package & package) const; + const std::string type() const { + return "Log"; + }; private: /// static mutex to lock Ostream during logging operation static boost::mutex m_log_mutex; diff --git a/src/filter_virt_db.hpp b/src/filter_virt_db.hpp index 9c4f41e..e33b74e 100644 --- a/src/filter_virt_db.hpp +++ b/src/filter_virt_db.hpp @@ -1,4 +1,4 @@ -/* $Id: filter_virt_db.hpp,v 1.2 2005-10-29 15:54:29 adam Exp $ +/* $Id: filter_virt_db.hpp,v 1.3 2005-10-29 22:23:36 marc Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -21,6 +21,9 @@ namespace yp2 { ~Virt_db(); Virt_db(); void process(yp2::Package & package) const; + const std::string type() const { + return "Virt_db"; + }; void add_map_db2vhost(std::string db, std::string vhost); private: boost::scoped_ptr m_p; diff --git a/src/filter_z3950_client.hpp b/src/filter_z3950_client.hpp index 2f8fbb5..b55dd37 100644 --- a/src/filter_z3950_client.hpp +++ b/src/filter_z3950_client.hpp @@ -1,4 +1,4 @@ -/* $Id: filter_z3950_client.hpp,v 1.2 2005-10-29 15:54:29 adam Exp $ +/* $Id: filter_z3950_client.hpp,v 1.3 2005-10-29 22:23:36 marc Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -22,6 +22,9 @@ namespace yp2 { ~Z3950Client(); Z3950Client(); void process(yp2::Package & package) const; + const std::string type() const { + return "Z3950Client"; + }; private: boost::scoped_ptr m_p; }; diff --git a/src/test_filter1.cpp b/src/test_filter1.cpp index 98809f4..1c3ff81 100644 --- a/src/test_filter1.cpp +++ b/src/test_filter1.cpp @@ -1,4 +1,4 @@ -/* $Id: test_filter1.cpp,v 1.11 2005-10-15 14:09:09 adam Exp $ +/* $Id: test_filter1.cpp,v 1.12 2005-10-29 22:23:36 marc Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -18,6 +18,9 @@ using namespace boost::unit_test; class TFilter: public yp2::filter::Base { public: void process(yp2::Package & package) const {}; + const std::string type() const { + return "TFilter"; + }; }; @@ -25,14 +28,9 @@ BOOST_AUTO_TEST_CASE( test_filter1 ) { try{ TFilter filter; + - filter.name("filter1"); - - BOOST_CHECK (filter.name() == "filter1"); - - filter.name() = "filter1 rename"; - - BOOST_CHECK(filter.name() == "filter1 rename"); + BOOST_CHECK (filter.type() == "TFilter"); } catch ( ... ) { BOOST_CHECK (false); diff --git a/src/test_filter2.cpp b/src/test_filter2.cpp index 9325f05..14aa299 100644 --- a/src/test_filter2.cpp +++ b/src/test_filter2.cpp @@ -1,4 +1,4 @@ -/* $Id: test_filter2.cpp,v 1.13 2005-10-26 10:55:26 marc Exp $ +/* $Id: test_filter2.cpp,v 1.14 2005-10-29 22:23:36 marc Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -27,6 +27,9 @@ public: package.data() = m_constant; package.move(); }; + const std::string type() const { + return "FilterConstant"; + }; void configure(const xmlNode* ptr = 0); int get_constant() const { return m_constant; }; private: @@ -115,6 +118,9 @@ public: package.data() = package.data() * 2; package.move(); }; + const std::string type() const { + return "FilterConstant"; + }; }; @@ -122,9 +128,7 @@ BOOST_AUTO_TEST_CASE( testfilter2_1 ) { try { FilterConstant fc; - fc.name() = "FilterConstant"; FilterDouble fd; - fd.name() = "FilterDouble"; { yp2::RouterChain router1; diff --git a/src/test_filter_factory.cpp b/src/test_filter_factory.cpp index 2ab42af..3b982f1 100644 --- a/src/test_filter_factory.cpp +++ b/src/test_filter_factory.cpp @@ -1,8 +1,9 @@ -/* $Id: test_filter_factory.cpp,v 1.2 2005-10-29 17:58:14 marc Exp $ +/* $Id: test_filter_factory.cpp,v 1.3 2005-10-29 22:23:36 marc Exp $ Copyright (c) 2005, Index Data. %LICENSE% - */ + +*/ #include @@ -21,9 +22,9 @@ 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"); - } + const std::string type() const{ + return "XFilter"; + }; }; @@ -31,13 +32,12 @@ 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"); - } + const std::string type() const{ + return "YFilter"; + }; }; yp2::filter::Base* yfilter_creator(){ @@ -53,23 +53,37 @@ BOOST_AUTO_TEST_CASE( test_filter_factory_1 ) yp2::filter::FilterFactory ffactory; - BOOST_CHECK_EQUAL(ffactory.add_creator("xfilter", xfilter_creator), + XFilter xf; + YFilter yf; + + const std::string xfid = xf.type(); + const std::string yfid = yf.type(); + + //std::cout << "Xfilter name: " << xfid << std::endl; + //std::cout << "Yfilter name: " << yfid << std::endl; + + BOOST_CHECK_EQUAL(ffactory.add_creator(xfid, xfilter_creator), true); - BOOST_CHECK_EQUAL(ffactory.drop_creator("xfilter"), + BOOST_CHECK_EQUAL(ffactory.drop_creator(xfid), true); - BOOST_CHECK_EQUAL(ffactory.add_creator("xfilter", xfilter_creator), + BOOST_CHECK_EQUAL(ffactory.add_creator(xfid, xfilter_creator), true); - BOOST_CHECK_EQUAL(ffactory.add_creator("yfilter", yfilter_creator), + BOOST_CHECK_EQUAL(ffactory.add_creator(yfid, 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")); + yp2::filter::Base* xfilter = ffactory.create(xfid); + yp2::filter::Base* yfilter = ffactory.create(yfid); + + BOOST_CHECK_EQUAL(xf.type(), xfilter->type()); + BOOST_CHECK_EQUAL(yf.type(), yfilter->type()); + + //std::cout << "Xfilter pointer name: " << xfilter->type() << std::endl; + //std::cout << "Yfilter pointer name: " << yfilter->type() << std::endl; + } catch ( ... ) { + throw; BOOST_CHECK (false); } @@ -77,6 +91,27 @@ BOOST_AUTO_TEST_CASE( test_filter_factory_1 ) } + + + + // get function - right val in assignment + //std::string name() const { + //return m_name; + // return "Base"; + //} + + // set function - left val in assignment + //std::string & name() { + // return m_name; + //} + + // set function - can be chained + //Base & name(const std::string & name){ + // m_name = name; + // return *this; + //} + + /* * Local variables: * c-basic-offset: 4 diff --git a/src/test_filter_frontend_net.cpp b/src/test_filter_frontend_net.cpp index fa6c641..9969b21 100644 --- a/src/test_filter_frontend_net.cpp +++ b/src/test_filter_frontend_net.cpp @@ -1,4 +1,4 @@ -/* $Id: test_filter_frontend_net.cpp,v 1.10 2005-10-26 10:55:26 marc Exp $ +/* $Id: test_filter_frontend_net.cpp,v 1.11 2005-10-29 22:23:36 marc Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -42,6 +42,9 @@ public: } return package.move(); }; + const std::string type() const { + return "FilterInit"; + }; }; diff --git a/src/test_filter_log.cpp b/src/test_filter_log.cpp index 8a7fc66..2e3af23 100644 --- a/src/test_filter_log.cpp +++ b/src/test_filter_log.cpp @@ -1,4 +1,4 @@ -/* $Id: test_filter_log.cpp,v 1.4 2005-10-26 10:55:26 marc Exp $ +/* $Id: test_filter_log.cpp,v 1.5 2005-10-29 22:23:36 marc Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -42,6 +42,9 @@ public: } return package.move(); }; + const std::string type() const { + return "FilterBounceInit"; + }; }; diff --git a/src/test_router_flexml.cpp b/src/test_router_flexml.cpp index e9eec52..2aac9e5 100644 --- a/src/test_router_flexml.cpp +++ b/src/test_router_flexml.cpp @@ -1,4 +1,4 @@ -/* $Id: test_router_flexml.cpp,v 1.1 2005-10-26 14:12:00 marc Exp $ +/* $Id: test_router_flexml.cpp,v 1.2 2005-10-29 22:23:36 marc Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -19,6 +19,9 @@ using namespace boost::unit_test; class TFilter: public yp2::filter::Base { public: void process(yp2::Package & package) const {}; + const std::string type() const { + return "TFilter"; + }; }; -- 1.7.10.4