lr_parser_methods.H

00001 /*
00002  * This file is part of the "Archon" framework.
00003  * (http://files3d.sourceforge.net)
00004  *
00005  * Copyright © 2002 by Kristian Spangsege and Brian Kristiansen.
00006  *
00007  * Permission to use, copy, modify, and distribute this software and
00008  * its documentation under the terms of the GNU General Public License is
00009  * hereby granted. No representations are made about the suitability of
00010  * this software for any purpose. It is provided "as is" without express
00011  * or implied warranty. See the GNU General Public License
00012  * (http://www.gnu.org/copyleft/gpl.html) for more details.
00013  *
00014  * The characters in this file are ISO8859-1 encoded.
00015  *
00016  * The documentation in this file is in "Doxygen" style
00017  * (http://www.doxygen.org).
00018  */
00019 
00020 #ifndef ARCHON_UTILITIES_LR_PARSER_METHODS_H
00021 #define ARCHON_UTILITIES_LR_PARSER_METHODS_H
00022 
00023 #include <archon/util/lr_parser_base.H>
00024 
00025 namespace Archon
00026 {
00027   namespace Utilities
00028   {
00029     using namespace std;
00030 
00031 
00032     template<class C>
00033     class LrParserBase::Actor<C>::MethodV0: public MethodBase
00034     {
00035       string name;
00036       void (C::*method)();
00037     public:
00038       MethodV0(string name, void (C::*method)()):
00039         name(name), method(method) {}
00040 
00041       string getName() const { return name; }
00042       int getArity() const { return 0; }
00043 
00044       RefAnyConst call(C &context, const vector<RefAnyConst> &) const
00045       {
00046         (context.*method)();
00047         return 0;
00048       }
00049     };
00050 
00051     template<class C>
00052     template<class R>
00053     class LrParserBase::Actor<C>::Method0: public MethodBase
00054     {
00055       string name;
00056       Ref<const R> (C::*method)();
00057     public:
00058       Method0(string name, Ref<const R> (C::*method)()):
00059         name(name), method(method) {}
00060 
00061       string getName() const { return name; }
00062       int getArity() const { return 0; }
00063 
00064       RefAnyConst call(C &context, const vector<RefAnyConst> &) const
00065       {
00066         return (context.*method)();
00067       }
00068     };
00069 
00070 
00071 
00072     template<class C>
00073     template<class A1>
00074     class LrParserBase::Actor<C>::MethodV1: public MethodBase
00075     {
00076       string name;
00077       void (C::*method)(const Ref<const A1> &);
00078     public:
00079       MethodV1(string name,
00080                void (C::*method)(const Ref<const A1> &)):
00081         name(name), method(method) {}
00082 
00083       string getName() const { return name; }
00084       int getArity() const { return 1; }
00085 
00086       RefAnyConst call(C &context, const vector<RefAnyConst> &args) const
00087       {
00088         const A1 *a1 = dynamic_cast<const A1 *>(args[0].get());
00089         if(!a1 && args[0]) ARCHON_THROW2(CallException, 0, typeid(A1));
00090         (context.*method)(a1);
00091         return 0;
00092       }
00093     };
00094 
00095     template<class C>
00096     template<class R, class A1>
00097     class LrParserBase::Actor<C>::Method1: public MethodBase
00098     {
00099       string name;
00100       Ref<const R> (C::*method)(const Ref<const A1> &);
00101     public:
00102       Method1(string name,
00103               Ref<const R> (C::*method)(const Ref<const A1> &)):
00104         name(name), method(method) {}
00105 
00106       string getName() const { return name; }
00107       int getArity() const { return 1; }
00108 
00109       RefAnyConst call(C &context, const vector<RefAnyConst> &args) const
00110       {
00111         const A1 *a1 = dynamic_cast<const A1 *>(args[0].get());
00112         if(!a1 && args[0]) ARCHON_THROW2(CallException, 0, typeid(A1));
00113         return (context.*method)(a1);
00114       }
00115     };
00116 
00117 
00118 
00119     template<class C>
00120     template<class A1, class A2>
00121     class LrParserBase::Actor<C>::MethodV2: public MethodBase
00122     {
00123       string name;
00124       void (C::*method)(const Ref<const A1> &,
00125                         const Ref<const A2> &);
00126     public:
00127       MethodV2(string name,
00128                void (C::*method)(const Ref<const A1> &,
00129                                  const Ref<const A2> &)):
00130         name(name), method(method) {}
00131 
00132       string getName() const { return name; }
00133       int getArity() const { return 2; }
00134 
00135       RefAnyConst call(C &context, const vector<RefAnyConst> &args) const
00136       {
00137         const A1 *a1 = dynamic_cast<const A1 *>(args[0].get());
00138         const A2 *a2 = dynamic_cast<const A2 *>(args[1].get());
00139         if(!a1 && args[0]) ARCHON_THROW2(CallException, 0, typeid(A1));
00140         if(!a2 && args[1]) ARCHON_THROW2(CallException, 1, typeid(A2));
00141         (context.*method)(a1, a2);
00142         return 0;
00143       }
00144     };
00145 
00146     template<class C>
00147     template<class R, class A1, class A2>
00148     class LrParserBase::Actor<C>::Method2: public MethodBase
00149     {
00150       string name;
00151       Ref<const R> (C::*method)(const Ref<const A1> &,
00152                                 const Ref<const A2> &);
00153     public:
00154       Method2(string name,
00155               Ref<const R> (C::*method)(const Ref<const A1> &,
00156                                         const Ref<const A2> &)):
00157         name(name), method(method) {}
00158 
00159       string getName() const { return name; }
00160       int getArity() const { return 2; }
00161 
00162       RefAnyConst call(C &context, const vector<RefAnyConst> &args) const
00163       {
00164         const A1 *a1 = dynamic_cast<const A1 *>(args[0].get());
00165         const A2 *a2 = dynamic_cast<const A2 *>(args[1].get());
00166         if(!a1 && args[0]) ARCHON_THROW2(CallException, 0, typeid(A1));
00167         if(!a2 && args[1]) ARCHON_THROW2(CallException, 1, typeid(A2));
00168         return (context.*method)(a1, a2);
00169       }
00170     };
00171   }
00172 }
00173 
00174 #endif // ARCHON_UTILITIES_LR_PARSER_METHODS_H

Generated on Sun Jul 30 22:55:44 2006 for Archon by  doxygen 1.4.4