Files Classes Functions Hierarchy
00001 #ifndef COMMANDLINE_H 00002 #define COMMANDLINE_H 00003 00004 #include <cassert> 00005 #include <iostream> 00006 #include <string> 00007 #include <map> 00008 #include <sstream> 00009 #include <algorithm> 00010 #include <cctype> 00011 #include <vector> 00012 #include <set> 00013 using namespace std; 00014 00015 #include <typedefs.h> 00016 00038 class commandline 00039 { 00041 commandline() { assert(false); } 00042 00044 map<string,string> mp; 00045 public: 00046 00048 vector<string> args; 00049 00050 // 00051 // Input Reading Functions 00052 00054 explicit commandline(int argc, char** argv); 00056 explicit commandline(istream & is); 00058 explicit commandline(stringc & arg); 00059 00062 void read(stringc & arg); 00063 00064 void read(istream & is); 00068 void readfile(stringc var); 00069 00070 // Binding 00071 00073 template< typename T > 00074 boolc mapvar(T& var, stringc & key); 00075 00077 template< typename T > 00078 boolc mapvar(T& var, stringc & key, T const & init); 00079 00082 template< typename T > 00083 boolc mapcallback 00084 ( 00085 T & x, 00086 void ( T::*p )(), 00087 stringc & token 00088 ); 00089 00090 // Miscellaneous 00091 00093 multiset<string> tokens; 00095 boolc hastoken(stringc & token) const 00096 { return (tokens.find(token)!=tokens.end()); } 00097 00099 vector<string> bindvar; 00100 00102 ostream & enablehelp(ostream & os=cout) const; 00103 00104 }; 00105 00106 00107 00108 // --------------------------------------------------------- 00109 // Implementation 00110 00111 // Note: I had difficulty with map<string,string> 00112 // as my compiler just did not produce workable code 00113 // with iterating over mp within the commandline class. 00114 // This was weird. The same code however did work in map_var. 00115 00116 00121 template< typename T > 00122 class map_var 00123 { 00125 T var; 00127 map<string,string>& tbl; 00129 bool found; 00130 public: 00131 00134 map_var(T var_, map<string,string>& tbl_, stringc & key) 00135 : var(var_), tbl(tbl_) 00136 { 00137 map<string,string>::iterator pos = tbl.find(key); 00138 found = (pos!=tbl.end()); 00139 if (found) 00140 { 00141 stringstream ss(tbl[key]); 00142 ss >> var; 00143 } 00144 } 00145 00147 boolc foundGet() const { return found; } 00149 T operator() () { return var; } 00150 }; 00151 00152 00156 template<> 00157 class map_var<string&> 00158 { 00159 string& var; 00160 map<string,string>& tbl; 00161 bool found; 00162 public: 00163 00164 map_var(string& var_, map<string,string>& tbl_, stringc & key) 00165 : var(var_), tbl(tbl_) 00166 { 00167 map<string,string>::iterator pos = tbl.find(key); 00168 found = (pos!=tbl.end()); 00169 if (found) 00170 { 00171 var = tbl[key]; 00172 } 00173 } 00174 00175 boolc foundGet() const { return found; } 00176 string& operator() () { return var; } 00177 }; 00178 00179 00183 template<> 00184 class map_var<bool&> 00185 { 00186 bool& var; 00187 map<string,string>& tbl; 00188 bool found; 00189 public: 00190 00192 map_var(bool& var_, map<string,string>& tbl_, stringc & key) 00193 : var(var_), tbl(tbl_) 00194 { 00195 map<string,string>::iterator pos = tbl.find(key); 00196 found = (pos!=tbl.end()); 00197 if (found) 00198 { 00199 string s = tbl[key]; 00200 transform (s.begin(), s.end(), s.begin(), ::tolower ); 00201 if ((s=="false") || (s=="0")) 00202 var = false; 00203 if ((s=="true") || (s=="1")) 00204 var = true; 00205 } 00206 } 00207 00208 boolc foundGet() const { return found; } 00209 bool& operator() () { return var; } 00210 }; 00211 00212 template< typename T > 00213 boolc commandline::mapvar(T& var, stringc & key) 00214 { 00215 bindvar.push_back(key); 00216 map_var<T&> t(var,mp,key); 00217 return t.foundGet(); 00218 } 00219 00220 template< typename T > 00221 boolc commandline::mapvar 00222 ( 00223 T& var, 00224 stringc & key, 00225 T const & init 00226 ) 00227 { 00228 bindvar.push_back(key); 00229 map_var<T&> t(var,mp,key); 00230 boolc res = t.foundGet(); 00231 if (res==false) 00232 var = init; 00233 00234 return res; 00235 } 00236 00237 template< typename T > 00238 boolc commandline::mapcallback 00239 ( 00240 T & x, 00241 void ( T::*p )(), 00242 stringc & token 00243 ) 00244 { 00245 bool found = (tokens.find(token)!=tokens.end()); 00246 if (found) 00247 (x.*p)(); 00248 00249 return found; 00250 } 00251 00252 00253 #endif 00254 00255 00256
1.5.8