Serializer.h

Go to the documentation of this file.
00001 #ifndef TAGCOLL_SERIALIZER_H
00002 #define TAGCOLL_SERIALIZER_H
00003 
00008 /*
00009  * Copyright (C) 2005  Enrico Zini <enrico@debian.org>
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program 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
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; 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 #include <tagcoll/OpSet.h>
00028 
00029 namespace Tagcoll
00030 {
00031 
00035 template<typename IN, typename OUT>
00036 class Converter
00037 {
00038 public:
00042     OUT operator()(const IN& item) { return OUT(item); }
00043 
00047     OpSet<OUT> operator()(const OpSet<IN>& items)
00048     {
00049         OpSet<OUT> res;
00050 
00051         for (typename OpSet<IN>::const_iterator i = items.begin();
00052                 i != items.end(); i++)
00053         {
00054             OUT t = (*this)(*i);
00055             if (t != OUT())
00056                 res += t;
00057         }
00058 
00059         return res;
00060     }
00061 };
00062 
00066 template<typename IN_ITEM, typename IN_TAG, typename OUT_ITEM, typename OUT_TAG>
00067 class ConversionFilter : public Consumer<IN_ITEM, IN_TAG>
00068 {
00069     /*
00070      * Implementation detail: we cannot derive from Filter because the type of
00071      * the output consumer is different from the type of the input consumer.
00072      * So we have to reimplement filter methods.
00073      */
00074 
00075 protected:
00076     Converter<IN_ITEM, OUT_ITEM> citem;
00077     Converter<IN_TAG, OUT_TAG> ctag;
00078     Consumer<OUT_ITEM, OUT_TAG>* consumer;
00079 
00080     virtual void consumeItemUntagged(const IN_ITEM& item)
00081     {
00082         consumer->consume(citem(item));
00083     }
00084     virtual void consumeItem(const IN_ITEM& item, const OpSet<IN_TAG>& tags)
00085     {
00086         consumer->consume(citem(item), ctag(tags));
00087     }
00088     virtual void consumeItemsUntagged(const OpSet<IN_ITEM>& items)
00089     {
00090         consumer->consume(citem(items));
00091     }
00092     virtual void consumeItems(const OpSet<IN_ITEM>& items, const OpSet<IN_TAG>& tags)
00093     {
00094         consumer->consume(citem(items), ctag(tags));
00095     }
00096 
00097 public:
00098     ConversionFilter(
00099             Converter<IN_ITEM, OUT_ITEM>& citem,
00100             Converter<IN_TAG, OUT_TAG>& ctag) : citem(citem), ctag(ctag), consumer(0) {}
00101     ConversionFilter(
00102             Converter<IN_ITEM, OUT_ITEM>& citem,
00103             Converter<IN_TAG, OUT_TAG>& ctag,
00104             Consumer<OUT_ITEM, OUT_TAG>& consumer) : citem(citem), ctag(ctag), consumer(&consumer) {}
00105     virtual ~ConversionFilter() throw () {}
00106 
00107     Consumer<OUT_ITEM, OUT_TAG>& getConsumer() const { return *consumer; }
00108     void setConsumer(Consumer<OUT_ITEM, OUT_TAG>& consumer) { this->consumer = &consumer; }
00109 };
00110 
00111 }
00112 
00113 // vim:set ts=4 sw=4:
00114 #endif

Generated on Fri Mar 24 22:40:46 2006 for libtagcoll by  doxygen 1.4.6