00001 #ifndef TAGCOLL_SERIALIZER_H
00002 #define TAGCOLL_SERIALIZER_H
00003
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00071
00072
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
00114 #endif