00001 /** @file rutz/assocarray.h generic associative arrays, implemented as 00002 a thin wrapper around std::map<rutz::fstring, void*> */ 00003 00004 /////////////////////////////////////////////////////////////////////// 00005 // 00006 // Copyright (c) 2004-2007 University of Southern California 00007 // Rob Peters <rjpeters at usc dot edu> 00008 // 00009 // created: Thu Oct 14 18:40:34 2004 00010 // commit: $Id: assocarray.h 8274 2007-04-19 17:44:48Z rjpeters $ 00011 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/assocarray.h $ 00012 // 00013 // -------------------------------------------------------------------- 00014 // 00015 // This file is part of GroovX. 00016 // [http://ilab.usc.edu/rjpeters/groovx/] 00017 // 00018 // GroovX is free software; you can redistribute it and/or modify it 00019 // under the terms of the GNU General Public License as published by 00020 // the Free Software Foundation; either version 2 of the License, or 00021 // (at your option) any later version. 00022 // 00023 // GroovX is distributed in the hope that it will be useful, but 00024 // WITHOUT ANY WARRANTY; without even the implied warranty of 00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00026 // General Public License for more details. 00027 // 00028 // You should have received a copy of the GNU General Public License 00029 // along with GroovX; if not, write to the Free Software Foundation, 00030 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00031 // 00032 /////////////////////////////////////////////////////////////////////// 00033 00034 #ifndef GROOVX_RUTZ_ASSOCARRAY_H_UTC20050626084020_DEFINED 00035 #define GROOVX_RUTZ_ASSOCARRAY_H_UTC20050626084020_DEFINED 00036 00037 #include "rutz/fstring.h" 00038 00039 namespace rutz 00040 { 00041 class file_pos; 00042 // class fstring; 00043 00044 /// A non-typesafe wrapper around std::map<string, void*>. 00045 /** The use must provide a pointer to a function that knows how to 00046 properly destroy the actual contained objects according to their 00047 true type. */ 00048 class assoc_array_base 00049 { 00050 public: 00051 /// Function type for destroying elements. 00052 typedef void (kill_func_t) (void*); 00053 00054 /// Default constructor. 00055 /** @param descr a human-readable description of what this array's 00056 keys represent; this is used in error messages, e.g. if descr 00057 is "frobnicator", then error messages would include "unknown 00058 frobnicator" 00059 00060 @param nocase true if the array should use case-insensitive 00061 string comparisons (default is false, giving normal 00062 case-sensitive string comparisons) 00063 */ 00064 assoc_array_base(kill_func_t* f, const char* descr, bool nocase); 00065 00066 /// Virtual destructor. 00067 ~assoc_array_base(); 00068 00069 /// Get a list of known keys, separated by sep. 00070 rutz::fstring get_known_keys(const char* sep) const; 00071 00072 /// Raise an exception reporting an unknown key. 00073 void throw_for_key(const char* key, 00074 const rutz::file_pos& pos) const; 00075 00076 /// Raise an exception reporting an unknown key. 00077 void throw_for_key(const rutz::fstring& key, 00078 const rutz::file_pos& pos) const; 00079 00080 /// Retrieve the object associated with the tag \a name. 00081 void* get_value_for_key(const rutz::fstring& name) const; 00082 00083 /// Retrieve the object associated with the tag \a name. 00084 void* get_value_for_key(const char* name) const; 00085 00086 /// Associate the object at \a ptr with the tag \a name. 00087 void set_value_for_key(const char* name, void* ptr); 00088 00089 /// Clear all entries, calling the kill function for each. 00090 void clear(); 00091 00092 private: 00093 assoc_array_base(const assoc_array_base&); 00094 assoc_array_base& operator=(const assoc_array_base&); 00095 00096 struct impl; 00097 impl* const rep; 00098 }; 00099 00100 00101 /// rutz::assoc_array is a typesafe wrapper of rutz::assoc_array_base. 00102 00103 template<class value_t> 00104 class assoc_array 00105 { 00106 public: 00107 /// Default constructor 00108 /** @param descr a human-readable description of what this array's 00109 keys represent; this is used in error messages, e.g. if descr 00110 is "frobnicator", then error messages would include "unknown 00111 frobnicator" 00112 00113 @param nocase true if the array should use case-insensitive 00114 string comparisons (default is false, giving normal 00115 case-sensitive string comparisons) 00116 */ 00117 assoc_array(const char* descr, bool nocase = false) 00118 : base(&delete_ptr, descr, nocase) {} 00119 00120 /// Get a string listing of known keys, separated by the string sep. 00121 rutz::fstring get_known_keys(const char* sep) const 00122 { return base.get_known_keys(sep); } 00123 00124 /// Raise an exception reporting an unknown key. 00125 void throw_for_key(const char* key, 00126 const rutz::file_pos& pos) const 00127 { base.throw_for_key(key, pos); } 00128 00129 /// Raise an exception reporting an unknown key. 00130 void throw_for_key(const rutz::fstring& key, 00131 const rutz::file_pos& pos) const 00132 { base.throw_for_key(key, pos); } 00133 00134 /// Get the object associated with the given key. 00135 value_t* get_ptr_for_key(const rutz::fstring& key) const 00136 { return static_cast<value_t*>(base.get_value_for_key(key)); } 00137 00138 /// Get the object associated with the given key. 00139 value_t* get_ptr_for_key(const char* key) const 00140 { return static_cast<value_t*>(base.get_value_for_key(key)); } 00141 00142 /// Associate the object at \a ptr with the given key. 00143 void set_ptr_for_key(const char* key, value_t* ptr) 00144 { base.set_value_for_key(key, static_cast<void*>(ptr)); } 00145 00146 private: 00147 rutz::assoc_array_base base; 00148 00149 static void delete_ptr(void* ptr) 00150 { delete static_cast<value_t*>(ptr); } 00151 }; 00152 } 00153 00154 static const char __attribute__((used)) vcid_groovx_rutz_assocarray_h_utc20050626084020[] = "$Id: assocarray.h 8274 2007-04-19 17:44:48Z rjpeters $ $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/assocarray.h $"; 00155 #endif // !GROOVX_RUTZ_ASSOCARRAY_H_UTC20050626084020_DEFINED