pattern.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 __PATTERN_HH
00018 # define __PATTERN_HH
00019 
00020 # include <cassert>
00021 # include "../strategies/failure.hh"
00022 # include "../type_traits/has_model.hh"
00023 
00024 namespace aurelia {
00025 
00028   template <typename Model>
00029   struct pattern_concept {
00030     typedef typename Model::type type;
00031     typedef typename Model::build_type build_type;
00032     typedef decltype((std::declval<type&>() =
00033                       std::declval<build_type>()))
00034       match_type;
00035 
00036     typedef decltype(*(std::declval<type&>()))
00037       builder_type;
00038 
00039 #ifdef DOC_GEN
00040     match_type operator=(type&, build_type) throw (failure);
00041     builder_type operator*(type) throw (failure);
00042 #else
00043     static_assert(std::is_convertible<builder_type, build_type>::value,
00044                   "builder_type is not convertible to build_type.");
00045 
00046     static_assert(std::is_convertible<match_type, build_type>::value,
00047                   "builder_type is not convertible to build_type.");
00048 #endif
00049 
00051     void match(type pattern) {
00052       try {
00053         pattern = *pattern;
00054       }
00055       catch (failure) {
00056         assert(false);
00057       }
00058     }
00059 
00060     typedef void check;
00061   };
00062 
00066   template <typename T>
00067   struct pattern_model {
00068     typedef void no_model;
00069   };
00070 
00071   template <typename Pool, typename T>
00072   struct is_pattern_traits:
00073     public type_traits::has_model<pattern_model<T> > {
00074   };
00075 
00076 }
00077 
00078 #endif