00001 #ifndef CONSUMER_H 00002 #define CONSUMER_H 00003 00004 /* 00005 * Consumer and filter interfaces 00006 * 00007 * Copyright (C) 2003 Enrico Zini <enrico@debian.org> 00008 * 00009 * This program is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software 00021 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 */ 00023 00024 namespace buffy { 00025 00026 template<class ITEM> 00027 class Consumer 00028 { 00029 public: 00030 virtual ~Consumer() {} 00031 virtual void consume(ITEM&) = 0; 00032 }; 00033 00034 template<class ITEM> 00035 class Matcher 00036 { 00037 public: 00038 virtual ~Matcher() {} 00039 virtual bool match(ITEM& item) const 00040 { 00041 return true; 00042 } 00043 }; 00044 00045 template<class ITEM> 00046 class Filter : public Consumer<ITEM> 00047 { 00048 protected: 00049 Consumer<ITEM>& next; 00050 public: 00051 Filter<ITEM>(Consumer<ITEM>& next) : next(next) {} 00052 00053 virtual void consume(ITEM& item) 00054 { 00055 next.consume(item); 00056 } 00057 }; 00058 00059 template<class ITEM> 00060 class MatcherFilter : public Filter<ITEM> 00061 { 00062 protected: 00063 Matcher<ITEM>& matcher; 00064 00065 public: 00066 MatcherFilter<ITEM>(Matcher<ITEM>& matcher, Consumer<ITEM>& next) throw () 00067 : Filter<ITEM>(next), matcher(matcher) {} 00068 00069 virtual void consume(ITEM& item) 00070 { 00071 if (matcher.match(item)) 00072 this->next.consume(item); 00073 } 00074 }; 00075 00076 } 00077 00078 // vim:set ts=4 sw=4: 00079 #endif