DerivedTags.h

Go to the documentation of this file.
00001 #ifndef TAGCOLL_DERIVEDTAGS_H
00002 #define TAGCOLL_DERIVEDTAGS_H
00003 
00009 /*
00010  * Copyright (C) 2003  Enrico Zini <enrico@debian.org>
00011  *
00012  * This library is free software; you can redistribute it and/or
00013  * modify it under the terms of the GNU Lesser General Public
00014  * License as published by the Free Software Foundation; either
00015  * version 2.1 of the License, or (at your option) any later version.
00016  *
00017  * This library is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  * Lesser General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU Lesser General Public
00023  * License along with this library; if not, write to the Free Software
00024  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00025  */
00026 
00027 #include <tagcoll/Filter.h>
00028 #include <tagcoll/Expression.h>
00029 
00030 #include <map>
00031 #include <string>
00032 
00033 #include <stdio.h>
00034 
00035 namespace Tagcoll
00036 {
00037 
00042 class DerivedTags : public std::map<std::string, Expression>
00043 {
00044 public:
00045     virtual ~DerivedTags() {}
00046 
00050     void add(const std::string& tag, const Expression& expr)
00051     {
00052         insert(make_pair(tag, expr));
00053     }
00054 
00058     OpSet<std::string> derivedTags(const OpSet<std::string>& tags) const
00059     {
00060         OpSet<std::string> res;
00061         for (const_iterator i = begin(); i != end(); i++)
00062         {
00063             TagexprContext context(tags, *this);
00064             if (i->second(context))
00065                 res += i->first;
00066         }
00067         return res;
00068 
00069     }
00070 };
00071 
00075 template <class ITEM>
00076 class AddDerived : public Filter<ITEM, std::string>
00077 {
00078 protected:
00079     DerivedTags dtags;
00080 
00081     virtual void consumeItemUntagged(const ITEM& item)
00082     {
00083         this->consumer->consume(item, dtags.derivedTags(OpSet<std::string>()));
00084     }
00085     virtual void consumeItem(const ITEM& item, const OpSet<std::string>& tags)
00086     {
00087         this->consumer->consume(item, tags + dtags.derivedTags(tags));
00088     }
00089     virtual void consumeItemsUntagged(const OpSet<ITEM>& items)
00090     {
00091         this->consumer->consume(items, dtags.derivedTags(OpSet<std::string>()));
00092     }
00093     virtual void consumeItems(const OpSet<ITEM>& items, const OpSet<std::string>& tags)
00094     {
00095         this->consumer->consume(items, tags + dtags.derivedTags(tags));
00096     }
00097     
00098 public:
00099     AddDerived() {}
00100     AddDerived(Consumer<ITEM, std::string>& cons) : Filter<ITEM, std::string>(cons) {}
00101     AddDerived(const DerivedTags& dtags) : dtags(dtags) {}
00102     AddDerived(Consumer<ITEM, std::string>& cons, const DerivedTags& dtags)
00103         : Filter<ITEM, std::string>(cons), dtags(dtags) {}
00104     virtual ~AddDerived() {}
00105 
00109     DerivedTags& derivedTags() { return dtags; }
00110 
00114     const DerivedTags& derivedTags() const { return dtags; }
00115 };
00116 
00120 template <class ITEM>
00121 class RemoveDerived : public Filter<ITEM, std::string>
00122 {
00123 protected:
00124     DerivedTags dtags;
00125 
00126     virtual void consumeItemUntagged(const ITEM& item)
00127     {
00128         this->consumer->consume(item);
00129     }
00130     virtual void consumeItem(const ITEM& item, const OpSet<std::string>& tags)
00131     {
00132         this->consumer->consume(item, tags - dtags.derivedTags(tags));
00133     }
00134     virtual void consumeItemsUntagged(const OpSet<ITEM>& items)
00135     {
00136         this->consumer->consume(items);
00137     }
00138     virtual void consumeItems(const OpSet<ITEM>& items, const OpSet<std::string>& tags)
00139     {
00140         this->consumer->consume(items, tags - dtags.derivedTags(tags));
00141     }
00142 
00143 public:
00144     RemoveDerived() {}
00145     RemoveDerived(Consumer<ITEM, std::string>& cons) : Filter<ITEM, std::string>(cons) {}
00146     RemoveDerived(const DerivedTags& dtags) : dtags(dtags) {}
00147     RemoveDerived(Consumer<ITEM, std::string>& cons, const DerivedTags& dtags)
00148         : Filter<ITEM, std::string>(cons), dtags(dtags) {}
00149     virtual ~RemoveDerived() {}
00150 
00154     DerivedTags& derivedTags() { return dtags; }
00155 
00159     const DerivedTags& derivedTags() const { return dtags; }
00160 };
00161 
00162 };
00163 
00164 // vim:set ts=4 sw=4:
00165 #endif

Generated on Wed Jun 20 19:25:39 2007 for libtagcoll by  doxygen 1.5.2