Tanl Linguistic Pipeline |
00001 /* 00002 ** IXE 00003 ** include/Visitor.h: visitor pattern. 00004 ** ---------------------------------------------------------------------- 00005 ** Copyright (c) 2010 Giuseppe Attardi (attardi@di.unipi.it). 00006 ** ---------------------------------------------------------------------- 00007 ** 00008 ** This file is part of IXE. 00009 ** 00010 ** IXE is free software; you can redistribute it and/or modify it 00011 ** under the terms of the GNU General Public License, version 3, 00012 ** as published by the Free Software Foundation. 00013 ** 00014 ** IXE 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 00017 ** GNU General Public License for more details. 00018 ** 00019 ** You should have received a copy of the GNU General Public License 00020 ** along with this program. If not, see <http://www.gnu.org/licenses/>. 00021 ** ---------------------------------------------------------------------- 00022 */ 00023 00024 #ifndef IXE_Visitor_H 00025 #define IXE_Visitor_H 00026 00027 namespace IXE { 00028 00075 struct IVisitor { 00076 virtual ~IVisitor() {} 00077 }; 00078 00079 template <class T, typename R = void> 00080 struct Visitor 00081 { 00082 virtual ~Visitor() {} 00083 virtual R visit(T&) = 0; 00084 }; 00085 00086 template <class R = void> 00087 struct Visitable 00088 { 00089 typedef R ReturnType; // used in macro VISITABLE_ACCEPT 00090 00091 virtual R accept(IVisitor& v) = 0; 00092 00093 protected: 00094 template <class T> 00095 static R acceptImpl(T& self, IVisitor& v) { 00096 return dynamic_cast<Visitor<T, R>*>(&v)->visit(self); 00097 } 00098 }; 00099 00100 #define VISITABLE_ACCEPT() \ 00101 virtual ReturnType accept(IVisitor& guest) { return acceptImpl(*this, guest); } 00102 00103 } // namespace IXE 00104 00105 #endif // IXE_Visitor_H