Logo Search packages:      
Sourcecode: tagcoll version File versions  Download package

Serializer.h

Go to the documentation of this file.
#ifndef TAGCOLL_SERIALIZER_H
#define TAGCOLL_SERIALIZER_H

/** \file
 * Serializer for tags and items
 */

/*
 * Copyright (C) 2005  Enrico Zini <enrico@debian.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <tagcoll/Consumer.h>
#include <tagcoll/OpSet.h>

namespace Tagcoll
{

/**
 * General class converting items from one representation to another
 */
template<typename IN, typename OUT>
00036 class Converter
{
public:
      /**
       * Convert a single item
       */
00042       OUT operator()(const IN& item) { return OUT(item); }

      /**
       * Convert a set of items
       */
00047       OpSet<OUT> operator()(const OpSet<IN>& items)
      {
            OpSet<OUT> res;

            for (typename OpSet<IN>::const_iterator i = items.begin();
                        i != items.end(); i++)
            {
                  OUT t = (*this)(*i);
                  if (t != OUT())
                        res += t;
            }

            return res;
      }
};

/**
 * Filter converting streams of tagged items among two representations
 */
template<typename IN_ITEM, typename IN_TAG, typename OUT_ITEM, typename OUT_TAG>
00067 class ConversionFilter : public Consumer<IN_ITEM, IN_TAG>
{
      /*
       * Implementation detail: we cannot derive from Filter because the type of
       * the output consumer is different from the type of the input consumer.
       * So we have to reimplement filter methods.
       */

protected:
      Converter<IN_ITEM, OUT_ITEM> citem;
      Converter<IN_TAG, OUT_TAG> ctag;
      Consumer<OUT_ITEM, OUT_TAG>* consumer;

00080       virtual void consumeItemUntagged(const IN_ITEM& item)
      {
            consumer->consume(citem(item));
      }
00084       virtual void consumeItem(const IN_ITEM& item, const OpSet<IN_TAG>& tags)
      {
            consumer->consume(citem(item), ctag(tags));
      }
00088       virtual void consumeItemsUntagged(const OpSet<IN_ITEM>& items)
      {
            consumer->consume(citem(items));
      }
00092       virtual void consumeItems(const OpSet<IN_ITEM>& items, const OpSet<IN_TAG>& tags)
      {
            consumer->consume(citem(items), ctag(tags));
      }

public:
      ConversionFilter(
                  Converter<IN_ITEM, OUT_ITEM>& citem,
                  Converter<IN_TAG, OUT_TAG>& ctag) : citem(citem), ctag(ctag), consumer(0) {}
      ConversionFilter(
                  Converter<IN_ITEM, OUT_ITEM>& citem,
                  Converter<IN_TAG, OUT_TAG>& ctag,
                  Consumer<OUT_ITEM, OUT_TAG>& consumer) : citem(citem), ctag(ctag), consumer(&consumer) {}
      virtual ~ConversionFilter() throw () {}

      Consumer<OUT_ITEM, OUT_TAG>& getConsumer() const { return *consumer; }
      void setConsumer(Consumer<OUT_ITEM, OUT_TAG>& consumer) { this->consumer = &consumer; }
};

}

// vim:set ts=4 sw=4:
#endif

Generated by  Doxygen 1.6.0   Back to index