Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

Tagexpr.h

Go to the documentation of this file.
00001 #ifndef TAGCOLL_TAGEXPR_SYNTREE_H
00002 #define TAGCOLL_TAGEXPR_SYNTREE_H
00003 
00004 /*
00005  * Tag expressions
00006  *
00007  * Copyright (C) 2003  Enrico Zini <enrico@debian.org>
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  * This library is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with this library; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00022  */
00023 
00024 #include <tagcoll/OpSet.h>
00025 #include <string>
00026 #include <map>
00027 
00028 namespace Tagcoll
00029 {
00030 
00031 class Tagexpr;
00032 
00033 class TagexprContext
00034 {
00035 protected:
00036     const OpSet<std::string>& tags;
00037     const std::map<std::string, Tagexpr*>& derivedTags;
00038     // Tags "visited" during tag evaluation: used to break circular loops
00039     mutable OpSet<std::string> seen;
00040 
00041 public:
00042     TagexprContext(const OpSet<std::string>& tags, const std::map<std::string, Tagexpr*>& derivedTags)
00043         throw () : tags(tags), derivedTags(derivedTags) {}
00044 
00045     bool eval(const std::string& tag) const throw ();
00046 };
00047 
00048 class Tagexpr
00049 {
00050 public:
00051     virtual ~Tagexpr() throw () {}
00052 
00053     virtual std::string format() const throw () = 0;
00054 
00055     virtual bool eval(const TagexprContext& context) const throw () = 0;
00056     virtual bool eval(const OpSet<std::string>& tags) const throw () = 0;
00057 
00058     virtual Tagexpr* clone() const throw () = 0;
00059 };
00060 
00061 class TagexprTag : public Tagexpr
00062 {
00063 protected:
00064     std::string _tag;
00065     
00066 public:
00067     TagexprTag(const std::string& tag) throw () : _tag(tag) {}
00068     virtual ~TagexprTag() throw () {}
00069 
00070     virtual std::string format() const throw () { return _tag; }
00071 
00072     virtual bool eval(const TagexprContext& context) const throw ()
00073     {
00074         return context.eval(_tag);
00075     }
00076     virtual bool eval(const OpSet<std::string>& tags) const throw ();
00077 
00078     virtual Tagexpr* clone() const throw () { return new TagexprTag(_tag); }
00079 };
00080 
00081 class TagexprNot : public Tagexpr
00082 {
00083 protected:
00084     Tagexpr* _operand;
00085 
00086 public:
00087     TagexprNot(Tagexpr* operand) throw () : _operand(operand) {}
00088     ~TagexprNot() throw () { delete _operand; }
00089 
00090     virtual std::string format() const throw () { return "!" + _operand->format(); }
00091 
00092     virtual bool eval(const TagexprContext& context) const throw ()
00093     {
00094         return ! _operand->eval(context);
00095     }
00096     virtual bool eval(const OpSet<std::string>& tags) const throw ()
00097     {
00098         return ! _operand->eval(tags);
00099     }
00100 
00101     virtual Tagexpr* clone() const throw () { return new TagexprNot(_operand->clone()); }
00102 };
00103 
00104 class TagexprAnd : public Tagexpr
00105 {
00106 protected:
00107     Tagexpr* _operand1;
00108     Tagexpr* _operand2;
00109 
00110 public:
00111     TagexprAnd(Tagexpr* operand1, Tagexpr* operand2) throw ()
00112         : _operand1(operand1), _operand2(operand2) {}
00113     ~TagexprAnd() throw () { delete _operand1; delete _operand2; }
00114 
00115     virtual std::string format() const throw ()
00116     {
00117         return "( " + _operand1->format() + " && " + _operand2->format() + " )";
00118     }
00119 
00120     virtual bool eval(const TagexprContext& context) const throw ()
00121     {
00122         return _operand1->eval(context) && _operand2->eval(context);
00123     }
00124     virtual bool eval(const OpSet<std::string>& tags) const throw ()
00125     {
00126         return _operand1->eval(tags) && _operand2->eval(tags);
00127     }
00128 
00129     virtual Tagexpr* clone() const throw ()
00130     {
00131         return new TagexprAnd(_operand1->clone(), _operand2->clone());
00132     }
00133 };
00134 
00135 class TagexprOr : public Tagexpr
00136 {
00137 protected:
00138     Tagexpr* _operand1;
00139     Tagexpr* _operand2;
00140 
00141 public:
00142     TagexprOr(Tagexpr* operand1, Tagexpr* operand2) throw ()
00143         : _operand1(operand1), _operand2(operand2) {}
00144     ~TagexprOr() throw () { delete _operand1; delete _operand2; }
00145 
00146     virtual std::string format() const throw ()
00147     {
00148         return "( " + _operand1->format() + " || " + _operand2->format() + " )";
00149     }
00150 
00151     virtual bool eval(const TagexprContext& context) const throw ()
00152     {
00153         return _operand1->eval(context) || _operand2->eval(context);
00154     }
00155     virtual bool eval(const OpSet<std::string>& tags) const throw ()
00156     {
00157         return _operand1->eval(tags) || _operand2->eval(tags);
00158     }
00159 
00160     virtual Tagexpr* clone() const throw ()
00161     {
00162         return new TagexprOr(_operand1->clone(), _operand2->clone());
00163     }
00164 };
00165 
00166 };
00167 
00168 // vim:set ts=4 sw=4:
00169 #endif

Generated on Sun Jan 23 02:09:10 2005 for libtagcoll by  doxygen 1.4.0