00001 #ifndef TAGCOLL_DERIVEDTAGS_H
00002 #define TAGCOLL_DERIVEDTAGS_H
00003
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00165 #endif