Filter.h

Go to the documentation of this file.
00001 #ifndef TAGCOLL_FILTER_H
00002 #define TAGCOLL_FILTER_H
00003 
00008 /*
00009  * Copyright (C) 2003,2004,2005  Enrico Zini <enrico@debian.org>
00010  *
00011  * This library is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU Lesser General Public
00013  * License as published by the Free Software Foundation; either
00014  * version 2.1 of the License, or (at your option) any later version.
00015  *
00016  * This library is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with this library; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00024  */
00025 
00026 #include <tagcoll/Consumer.h>
00027 
00028 namespace Tagcoll
00029 {
00030 
00038 template< typename ITEM, typename TAG >
00039 class Filter : public Consumer<ITEM, TAG>
00040 {
00041 protected:
00042     Consumer<ITEM, TAG>* consumer;
00043 
00044     virtual void consumeItemUntagged(const ITEM& item) { consumer->consume(item); }
00045     virtual void consumeItem(const ITEM& item, const OpSet<TAG>& tags) { consumer->consume(item, tags); }
00046     //virtual void consumeItemsUntagged(const OpSet<ITEM>& items) { consumer->consume(items); }
00047     //virtual void consumeItems(const OpSet<ITEM>& items, const OpSet<TAG>& tags) { consumer->consume(items, tags); }
00048 
00049 public:
00059     Filter() : consumer(0) {}
00060 
00064     Filter(Consumer<ITEM, TAG>& consumer) : consumer(&consumer) {}
00065     virtual ~Filter() {}
00066 
00068     bool isComplete() { return consumer != 0; }
00069 
00071     virtual Consumer<ITEM, TAG>& getConsumer() const { return *consumer; }
00072     
00074     virtual void setConsumer(Consumer<ITEM, TAG>& consumer) { this->consumer = &consumer; }
00075 };
00076 
00080 template< typename ITEM, typename TAG >
00081 class FilterChain : public Filter<ITEM, TAG>
00082 {
00083 protected:
00084     Filter<ITEM, TAG>* last;
00085 
00086 public:
00087     FilterChain<ITEM, TAG>() throw () : Filter<ITEM, TAG>(), last(0) {}
00088     FilterChain<ITEM, TAG>(Consumer<ITEM, TAG>& consumer) throw ()
00089         : Filter<ITEM, TAG>(consumer), last(0) {}
00090     virtual ~FilterChain<ITEM, TAG>() {}
00091 
00093     void setConsumer(Consumer<ITEM, TAG>& c) throw ()
00094     {
00095         if (last)
00096             last->setConsumer(c);
00097         else
00098             this->consumer = &c;
00099     }
00100 
00102     void appendFilter(Filter<ITEM, TAG>& f) throw ()
00103     {
00104         if (last)
00105         {
00106             if (last->isComplete())
00107                 f.setConsumer(last->getConsumer());
00108             last->setConsumer(f);
00109             last = &f;
00110         } else {
00111             if (this->isComplete())
00112                 f.setConsumer(this->getConsumer());
00113             this->consumer = last = &f;
00114         }
00115     }
00116 };
00117 
00118 }
00119 
00120 // vim:set ts=4 sw=4:
00121 #endif

Generated on Sat Jan 17 03:28:50 2009 for libtagcoll by  doxygen 1.5.1