/home/andreas/src/svn/mapnik/include/mapnik/expression.hpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * 
00003  * This file is part of Mapnik (c++ mapping toolkit)
00004  *
00005  * Copyright (C) 2006 Artem Pavlenko
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  *
00021  *****************************************************************************/
00022 
00023 //$Id$
00024 
00025 #ifndef EXPRESSION_HPP
00026 #define EXPRESSION_HPP
00027 
00028 #include <mapnik/value.hpp>
00029 #include <mapnik/filter_visitor.hpp>
00030 
00031 namespace mapnik {
00032     template <typename FeatureT> class filter_visitor;
00033     template <typename FeatureT>
00034     class expression
00035     {
00036     public:
00037         virtual value get_value(FeatureT const& feature) const=0;
00038         virtual void accept(filter_visitor<FeatureT>& v)=0;
00039         virtual expression<FeatureT>* clone() const=0;
00040         virtual std::string to_string() const=0;
00041         virtual ~expression() {}
00042     };
00043 
00044     template <typename FeatureT> 
00045     class literal : public expression<FeatureT>
00046     {
00047     public:
00048         literal(int val)
00049             : expression<FeatureT>(),
00050               value_(val) {}
00051         literal(double val)
00052             : expression<FeatureT>(),
00053               value_(val) {}
00054         literal(std::wstring const& val)
00055             : expression<FeatureT>(),
00056               value_(val) {}
00057         literal(literal const& other)
00058             : expression<FeatureT>(),
00059               value_(other.value_) {}
00060         
00061         value get_value(FeatureT const& /*feature*/) const
00062         {
00063             return value_;
00064         }
00065           
00066         void accept(filter_visitor<FeatureT>& v)
00067         {
00068             v.visit(*this);
00069         }
00070           
00071         expression<FeatureT>* clone() const
00072         {
00073             return new literal(*this); 
00074         }
00075           
00076         std::string to_string() const
00077         {
00078             return value_.to_expression_string();
00079         }
00080         ~literal() {}
00081     private:
00082         value value_;
00083     };
00084   
00085 
00086     template <typename FeatureT> 
00087     class property : public expression<FeatureT>
00088     {
00089     public:
00090         property(std::string const& name)
00091             : expression<FeatureT>(),
00092               name_(name)
00093             {}
00094         
00095         property(property const& other)
00096             : expression<FeatureT>(),
00097               name_(other.name_)
00098             {}
00099 
00100         value get_value(FeatureT const& feature) const
00101         {
00102             return feature[name_];
00103         }
00104         void accept(filter_visitor<FeatureT>& v)
00105         {
00106             v.visit(*this);
00107         }
00108         expression<FeatureT>* clone() const
00109         {
00110             return new property(*this); 
00111         }
00112         std::string const& name() const
00113         {
00114             return name_;
00115         }
00116 
00117         std::string to_string() const
00118         {
00119             return "["+name_+"]";
00120         }
00121 
00122         ~property() {}
00123 
00124     private:
00125             std::string name_;
00126     };
00127 }
00128 
00129 #endif //EXPRESSION_HPP

Generated on Thu Jul 19 17:59:25 2007 for Mapnik by  doxygen 1.4.7