00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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 }