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:
00039     virtual ~Converter() {}
00040 
00044     virtual OUT operator()(const IN& item) const = 0;
00045 
00049     virtual OpSet<OUT> operator()(const OpSet<IN>& items) const
00050     {
00051         OpSet<OUT> res;
00052 
00053         for (typename OpSet<IN>::const_iterator i = items.begin();
00054                 i != items.end(); i++)
00055         {
00056             OUT t = (*this)(*i);
00057             if (t != OUT())
00058                 res += t;
00059         }
00060 
00061         return res;
00062     }
00063 };
00064 
00065 template<typename IN, typename OUT>
00066 class TrivialConverter : public Converter<IN, OUT>
00067 {
00068 public:
00069     virtual ~TrivialConverter() {}
00070 
00074     virtual OUT operator()(const IN& item) const { return item; }
00075 };
00076 
00077 
00081 template<typename IN_ITEM, typename IN_TAG, typename OUT_ITEM, typename OUT_TAG>
00082 class ConversionFilter : public Consumer<IN_ITEM, IN_TAG>
00083 {
00084     /*
00085      * Implementation detail: we cannot derive from Filter because the type of
00086      * the output consumer is different from the type of the input consumer.
00087      * So we have to reimplement filter methods.
00088      */
00089 
00090 protected:
00091     Converter<IN_ITEM, OUT_ITEM>& citem;
00092     Converter<IN_TAG, OUT_TAG>& ctag;
00093     Consumer<OUT_ITEM, OUT_TAG>* consumer;
00094 
00095     virtual void consumeItemUntagged(const IN_ITEM& item)
00096     {
00097         consumer->consume(citem(item));
00098     }
00099     virtual void consumeItem(const IN_ITEM& item, const OpSet<IN_TAG>& tags)
00100     {
00101         consumer->consume(citem(item), ctag(tags));
00102     }
00103     virtual void consumeItemsUntagged(const OpSet<IN_ITEM>& items)
00104     {
00105         consumer->consume(citem(items));
00106     }
00107     virtual void consumeItems(const OpSet<IN_ITEM>& items, const OpSet<IN_TAG>& tags)
00108     {
00109         consumer->consume(citem(items), ctag(tags));
00110     }
00111 
00112 public:
00113     ConversionFilter(
00114             Converter<IN_ITEM, OUT_ITEM>& citem,
00115             Converter<IN_TAG, OUT_TAG>& ctag) : citem(citem), ctag(ctag), consumer(0) {}
00116     ConversionFilter(
00117             Converter<IN_ITEM, OUT_ITEM>& citem,
00118             Converter<IN_TAG, OUT_TAG>& ctag,
00119             Consumer<OUT_ITEM, OUT_TAG>& consumer) : citem(citem), ctag(ctag), consumer(&consumer) {}
00120     virtual ~ConversionFilter() throw () {}
00121 
00122     Consumer<OUT_ITEM, OUT_TAG>& getConsumer() const { return *consumer; }
00123     void setConsumer(Consumer<OUT_ITEM, OUT_TAG>& consumer) { this->consumer = &consumer; }
00124 };
00125 
00126 }
00127 
00128 // vim:set ts=4 sw=4:
00129 #endif

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