match.hh

00001 // This file is a part of Aurelia.
00002 // Copyright (C) 2010  Valentin David
00003 // Copyright (C) 2010  University of Bergen
00004 //
00005 // This program is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 #ifndef __MATCH_HH
00018 # define __MATCH_HH
00019 
00020 # include "../../terms/term.hh"
00021 # include "../../patterns/pattern.hh"
00022 # include "../../patterns/variable.hh"
00023 # include "../strategies.hh"
00024 
00025 namespace aurelia {
00026 
00030   template <typename Pat>
00031   struct match_strategy {
00032     typedef typename pattern_concept<typename pattern_model<Pat>::model>
00033     ::check require;
00034   private:
00035     Pat pat;
00036   public:
00037     match_strategy(const Pat& pat): pat(pat) {
00038     }
00039 
00040     template <typename T>
00041     T operator()(const T& term) const {
00042       Pat p(pat);
00043       p = term;
00044       return term;
00045     }
00046   };
00047 
00048   template <typename Pool>
00049   struct match_strategy<untyped_term<Pool> > {
00050   private:
00051     untyped_term<Pool> pat;
00052   public:
00053     match_strategy(const untyped_term<Pool>& pat): pat(pat) {
00054     }
00055 
00056     untyped_term<Pool> operator()(const untyped_term<Pool>& term) const {
00057       if (term != pat)
00058         throw failure();
00059       return term;
00060     }
00061   };
00062   /* @} */
00063 
00067   template <typename Pool, typename List>
00068   match_strategy<untyped_pattern<Pool, List> >
00069   operator~(const untyped_pattern<Pool, List>& p) {
00070     return match_strategy<untyped_pattern<Pool, List> >(p);
00071   }
00072 
00076   template <typename T>
00077   match_strategy<variable<T> > operator~(const variable<T>& t) {
00078     return match_strategy<variable<T> >(t);
00079   }
00080 
00084   template <typename Pool>
00085   match_strategy<untyped_term<Pool> > operator~(const untyped_term<Pool>& t) {
00086     return match_strategy<untyped_term<Pool> >(t);
00087   }
00088 
00092   template <typename Pool>
00093   match_strategy<untyped_term<Pool> > operator~(const untyped_constructor<Pool>& t) {
00094     return match_strategy<untyped_term<Pool> >(t);
00095   }
00096 
00097 
00098   template <typename Pattern,
00099             typename PattermModel = typename pattern_model<Pattern>::model>
00100   match_strategy<Pattern> operator~(const Pattern& pattern) {
00101     return match_strategy<Pattern>(pattern);
00102   }
00103 }
00104 
00105 #endif