src/tools/values/vRegistry.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023 
00024 ***************************************************************************
00025 
00026 */
00027 
00028 #include "vCore.h"
00029 #include "vRegistry.h"
00030 
00031 namespace vValue {
00032 namespace Registry {
00033 
00034 Registration::Registration(std::vector<tString> flags, tString fname, int argc, fptr ctor):
00035         m_flags(flags),
00036         m_fname(fname),
00037         m_argc(argc),
00038         m_ctor(ctor)
00039 {
00040     Registry::theRegistry().reg(this);
00041 }
00042 
00043 Registration::Registration(const char *flags, const char *fname, int argc, fptr ctor):
00044         m_fname(fname),
00045         m_argc(argc),
00046         m_ctor(ctor)
00047 {
00048     m_flags.clear();
00049     for (
00050         const char *ptr = flags, *ep;
00051         ptr;
00052         ptr = (ep[0] == '\0') ? 0 : (ep + 1)
00053     )
00054     {
00055         ep = strchr(ptr, '\n');
00056         if (!ep)
00057             ep = ptr + strlen(ptr);
00058         std::string s(ptr, ep - ptr);
00059         m_flags.push_back(s);
00060     }
00061     Registry::theRegistry().reg(this);
00062 }
00063 
00064 Expr::
00065 Base *
00066 Registration::use(arglist args) {
00067     switch (args.size()) {
00068     case 0:     return ((ctor::a0*)m_ctor)();
00069     case 1:     return ((ctor::a1*)m_ctor)(args[0]);
00070     case 2:     return ((ctor::a2*)m_ctor)(args[0], args[1]);
00071     case 3:     return ((ctor::a3*)m_ctor)(args[0], args[1], args[2]);
00072     case 4:     return ((ctor::a4*)m_ctor)(args[0], args[1], args[2], args[3]);
00073     case 5:     return ((ctor::a5*)m_ctor)(args[0], args[1], args[2], args[3], args[4]);
00074     }
00075     throw("Unimplemented # of args");
00076 }
00077 
00078 bool
00079 Registration::match(std::vector<tString> flags, tString fname, int argc)
00080 {
00081     if (fname.Compare(m_fname, true))
00082         return false;
00083     for (std::vector<tString>::iterator i = flags.begin(); i != flags.end(); ++i)
00084     {
00085         bool flagmatch = (flags[0] == "!");
00086         tString flag = *i;
00087         if (flagmatch)
00088             flag = flag.SubStr(1);
00089         if (!flag.Compare(*i, true))
00090             flagmatch = !flagmatch;
00091         if (!flagmatch)
00092             return false;
00093     }
00094     if (argc != m_argc)
00095         return false;
00096     return true;
00097 }
00098 
00099 void
00100 Registry::reg(Registration *it)
00101 {
00102     registrations.push_back(it);
00103 }
00104 
00105 Expr::
00106 Base *
00107 Registry::create(std::vector<tString> flags, tString fname, arglist args)
00108 {
00109     for (std::vector<Registration *>::iterator i = registrations.begin(); i != registrations.end(); ++i)
00110         if ((*i)->match(flags, fname, args.size()))
00111             return (*i)->use(args);
00112     return NULL;
00113 }
00114 
00115 Registry & Registry::theRegistry()
00116 {
00117     static Registry theRegistry;
00118     return theRegistry;
00119 }
00120 
00121 }
00122 }

Generated on Sat Mar 15 22:56:04 2008 for Armagetron Advanced by  doxygen 1.5.4