| 1 | /* |
| 2 | This file is part of the GVars3 Library. |
| 3 | |
| 4 | Copyright (C) 2005 The Authors |
| 5 | |
| 6 | This library is free software, see LICENSE file for details |
| 7 | */ |
| 8 | #include "gvars3/instances.h" |
| 9 | #include "gvars3/GStringUtil.h" |
| 10 | #include <vector> |
| 11 | #include <iostream> |
| 12 | #include <set> |
| 13 | #include <pthread.h> |
| 14 | |
| 15 | using namespace std; |
| 16 | |
| 17 | namespace GVars3 |
| 18 | { |
| 19 | |
| 20 | template<class C> class ThreadLocal |
| 21 | { |
| 22 | private: |
| 23 | pthread_key_t key; |
| 24 | |
| 25 | static void deleter(void* v) |
| 26 | { |
| 27 | delete static_cast<C*>(v); |
| 28 | } |
| 29 | |
| 30 | public: |
| 31 | ThreadLocal() |
| 32 | { |
| 33 | pthread_key_create(&key, deleter); |
| 34 | pthread_setspecific(key, new C); |
| 35 | } |
| 36 | |
| 37 | ~ThreadLocal() |
| 38 | { |
| 39 | deleter(pthread_getspecific(key)); |
| 40 | pthread_setspecific(key, 0); |
| 41 | pthread_key_delete(key); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | C& operator()() |
| 46 | { |
| 47 | return *static_cast<C*>(pthread_getspecific(key)); |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | template<class A, class B> class MutexMap |
| 52 | { |
| 53 | private: |
| 54 | map<A, B> _map; |
| 55 | pthread_mutex_t mutex; |
| 56 | |
| 57 | public: |
| 58 | MutexMap() |
| 59 | { |
| 60 | pthread_mutex_init(&mutex, 0); |
| 61 | } |
| 62 | |
| 63 | ~MutexMap() |
| 64 | { |
| 65 | pthread_mutex_destroy(&mutex); |
| 66 | } |
| 67 | |
| 68 | B get(const A& a) |
| 69 | { |
| 70 | B b; |
| 71 | pthread_mutex_lock(&mutex); |
| 72 | b = _map[a]; |
| 73 | pthread_mutex_unlock(&mutex); |
| 74 | return b; |
| 75 | } |
| 76 | |
| 77 | void set(const A&a, const B& b) |
| 78 | { |
| 79 | pthread_mutex_lock(&mutex); |
| 80 | _map[a] = b; |
| 81 | pthread_mutex_unlock(&mutex); |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | |
| 86 | class GUI_language |
| 87 | { |
| 88 | |
| 89 | public: |
| 90 | GUI_language() |
| 91 | { |
| 92 | GUI.RegisterCommand(".", collect_lineCB, this); |
| 93 | GUI.RegisterCommand("function", functionCB, this); |
| 94 | GUI.RegisterCommand("endfunction", endfunctionCB, this); |
| 95 | GUI.RegisterCommand("if_equal", gui_if_equalCB, this); |
| 96 | GUI.RegisterCommand("else", gui_if_elseCB, this); |
| 97 | GUI.RegisterCommand("endif", gui_endifCB, this); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | ~GUI_language() |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | |
| 106 | private: |
| 107 | pthread_mutex_t functionlist_mutex; |
| 108 | |
| 109 | ThreadLocal<string> current_function, if_gvar, if_string; |
| 110 | ThreadLocal<vector<string> > collection, ifbit, elsebit; |
| 111 | MutexMap<string, vector<string> > functions; |
| 112 | |
| 113 | static GUI_language& C(void* v) |
| 114 | { |
| 115 | return *static_cast<GUI_language*>(v); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | #define CallBack(X) static void X##CB(void* t, string a, string b){C(t).X(a, b);} |
| 120 | |
| 121 | CallBack(collect_line); |
| 122 | void collect_line(string, string l) |
| 123 | { |
| 124 | collection().push_back(l); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | |
| 129 | CallBack(function); |
| 130 | void function(string name, string args) |
| 131 | { |
| 132 | |
| 133 | vector<string> vs = ChopAndUnquoteString(args); |
| 134 | if(vs.size() != 1) |
| 135 | { |
| 136 | cerr << "Error: " << name << " takes 1 argument: " << name << " name\n"; |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | current_function()=vs[0]; |
| 141 | collection().clear(); |
| 142 | } |
| 143 | |
| 144 | CallBack(endfunction) |
| 145 | void endfunction(string name, string args) |
| 146 | { |
| 147 | if(current_function() == "") |
| 148 | { |
| 149 | cerr << "Error: " << name << ": no current function.\n"; |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | vector<string> vs = ChopAndUnquoteString(args); |
| 154 | if(vs.size() != 0) |
| 155 | cerr << "Warning: " << name << " takes 0 arguments.\n"; |
| 156 | |
| 157 | functions.set(current_function(), collection()); |
| 158 | |
| 159 | GUI.RegisterCommand(current_function(), runfuncCB, this); |
| 160 | |
| 161 | current_function().clear(); |
| 162 | collection().clear(); |
| 163 | } |
| 164 | |
| 165 | CallBack(runfunc) |
| 166 | void runfunc(string name, string /*args*/) |
| 167 | { |
| 168 | vector<string> v = functions.get(name); |
| 169 | for(unsigned int i=0; i < v.size(); i++) |
| 170 | GUI.ParseLine(v[i]); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | CallBack(gui_if_equal) |
| 175 | void gui_if_equal(string name, string args) |
| 176 | { |
| 177 | vector<string> vs = ChopAndUnquoteString(args); |
| 178 | if(vs.size() != 2) |
| 179 | { |
| 180 | cerr << "Error: " << name << " takes 2 arguments: " << name << " gvar string\n"; |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | collection().clear(); |
| 185 | if_gvar() = vs[0]; |
| 186 | if_string() = vs[1]; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | CallBack(gui_if_else) |
| 191 | void gui_if_else(string /*name*/, string /*args*/) |
| 192 | { |
| 193 | ifbit() = collection(); |
| 194 | if(ifbit().empty()) |
| 195 | ifbit().push_back(""); |
| 196 | collection().clear(); |
| 197 | } |
| 198 | |
| 199 | CallBack(gui_endif) |
| 200 | void gui_endif(string /*name*/, string /*args*/) |
| 201 | { |
| 202 | if(ifbit().empty()) |
| 203 | ifbit() = collection(); |
| 204 | else |
| 205 | elsebit() = collection(); |
| 206 | |
| 207 | collection().clear(); |
| 208 | |
| 209 | //Save a copy, since it canget trashed |
| 210 | vector<string> ib = ifbit(), eb = elsebit(); |
| 211 | string gv = if_gvar(), st = if_string(); |
| 212 | |
| 213 | ifbit().clear(); |
| 214 | elsebit().clear(); |
| 215 | if_gvar().clear(); |
| 216 | if_string().clear(); |
| 217 | |
| 218 | if(GV3::get_var(gv) == st) |
| 219 | for(unsigned int i=0; i < ib.size(); i++) |
| 220 | GUI.ParseLine(ib[i]); |
| 221 | else |
| 222 | for(unsigned int i=0; i < eb.size(); i++) |
| 223 | GUI.ParseLine(eb[i]); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | |
| 228 | }; |
| 229 | |
| 230 | GUI_language* get_new_lang() |
| 231 | { |
| 232 | return new GUI_language; |
| 233 | } |
| 234 | |
| 235 | void remove_lang(GUI_language* l) |
| 236 | { |
| 237 | delete l; |
| 238 | } |
| 239 | //GUI_language GUI_language_instance; |
| 240 | } |