00001 // 00002 // aegis - project change supervisor 00003 // Copyright (C) 2004-2008 Peter Miller 00004 // 00005 // This program is free software; you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation; either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // This program is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with this program. If not, see 00017 // <http://www.gnu.org/licenses/>. 00018 // 00019 00020 #ifndef COMMON_SYMTAB_TEMPLATE_H 00021 #define COMMON_SYMTAB_TEMPLATE_H 00022 00023 #include <common/nstring.h> 00024 #include <common/nstring/list.h> 00025 #include <common/symtab.h> 00026 00042 template <class value_type_t> 00043 class symtab 00044 { 00045 public: 00050 ~symtab() 00051 { 00052 if (stp) 00053 { 00054 delete stp; 00055 stp = 0; 00056 } 00057 } 00058 00062 symtab() : 00063 stp(0) 00064 { 00065 } 00066 00070 symtab(const symtab &arg) : 00071 stp(0) 00072 { 00073 copy(arg); 00074 } 00075 00079 symtab &operator=(const symtab &arg) 00080 { 00081 if (this != &arg) 00082 { 00083 clear(); 00084 copy(arg); 00085 } 00086 return *this; 00087 } 00088 00092 void 00093 clear() 00094 { 00095 if (stp) 00096 stp->clear(); 00097 } 00098 00107 value_type_t 00108 get(const nstring &key) 00109 const 00110 { 00111 value_type_t *vp = query(key); 00112 return (vp ? *vp : value_type_t()); 00113 } 00114 00129 nstring 00130 query_fuzzy(const nstring &key) 00131 const 00132 { 00133 return (stp ? stp->query_fuzzy(key) : nstring()); 00134 } 00135 00144 value_type_t * 00145 query(string_ty *key) 00146 const 00147 { 00148 if (!stp) 00149 return 0; 00150 return (value_type_t *)stp->query(key); 00151 } 00152 00159 value_type_t * 00160 query(const nstring &key) 00161 const 00162 { 00163 if (!stp) 00164 return 0; 00165 return (value_type_t *)stp->query(key); 00166 } 00167 00175 value_type_t * 00176 query(const nstring_list &key_list) 00177 const 00178 { 00179 if (!stp) 00180 return 0; 00181 return (value_type_t *)stp->query(key_list); 00182 } 00183 00197 void 00198 assign(string_ty *key, value_type_t *value) 00199 { 00200 if (!stp) 00201 stp = new symtab_ty(5); 00202 stp->assign(key, (void *)value); 00203 } 00204 00216 void 00217 assign(const nstring &key, value_type_t *value) 00218 { 00219 if (!stp) 00220 stp = new symtab_ty(5); 00221 stp->assign(key, (void *)value); 00222 } 00223 00234 void 00235 assign(const nstring &key, const value_type_t &value) 00236 { 00237 // I hope they called set_reaper already. 00238 assign(key, new value_type_t(value)); 00239 } 00240 00251 void 00252 remove(const nstring &key) 00253 { 00254 if (stp) 00255 stp->remove(key); 00256 } 00257 00265 void 00266 dump(const char *caption) 00267 const 00268 { 00269 if (stp) 00270 stp->dump(caption); 00271 } 00272 00278 void 00279 set_reaper() 00280 { 00281 if (!stp) 00282 stp = new symtab_ty(5); 00283 stp->set_reap(reaper); 00284 } 00285 00290 bool 00291 empty() 00292 const 00293 { 00294 return (!stp || stp->empty()); 00295 } 00296 00301 size_t 00302 size() 00303 const 00304 { 00305 return (stp ? stp->size() : 0); 00306 } 00307 00322 void 00323 keys(nstring_list &result) 00324 const 00325 { 00326 if (stp) 00327 stp->keys(result); 00328 else 00329 result.clear(); 00330 } 00331 00332 private: 00337 symtab_ty *stp; 00338 00343 static void reaper(void *p) { delete (value_type_t *)p; } 00344 00349 void 00350 copy(const symtab &arg) 00351 { 00352 nstring_list names; 00353 arg.keys(names); 00354 for (size_t j = 0; j < names.size(); ++j) 00355 { 00356 value_type_t *p = arg.query(names[j]); 00357 if (p) 00358 assign(names[j], *p); 00359 } 00360 } 00361 }; 00362 00365 #endif // COMMON_SYMTAB_TEMPLATE_H