assocarray.h

Go to the documentation of this file.
00001 
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 10097 2007-08-07 19:08:27Z rjpeters $
00011 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/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 //
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 
00045 
00048   class assoc_array_base
00049   {
00050   public:
00052     typedef void (kill_func_t) (void*);
00053 
00055 
00064     assoc_array_base(kill_func_t* f, const char* descr, bool nocase);
00065 
00067     ~assoc_array_base();
00068 
00070     rutz::fstring get_known_keys(const char* sep) const;
00071 
00073     void throw_for_key(const char* key,
00074                        const rutz::file_pos& pos) const;
00075 
00077     void throw_for_key(const rutz::fstring& key,
00078                        const rutz::file_pos& pos) const;
00079 
00081     void* get_value_for_key(const rutz::fstring& name) const;
00082 
00084     void* get_value_for_key(const char* name) const;
00085 
00087     void set_value_for_key(const char* name, void* ptr);
00088 
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 
00102 
00103   template<class value_t>
00104   class assoc_array
00105   {
00106   public:
00108 
00117     assoc_array(const char* descr, bool nocase = false)
00118       : base(&delete_ptr, descr, nocase) {}
00119 
00121     rutz::fstring get_known_keys(const char* sep) const
00122     { return base.get_known_keys(sep); }
00123 
00125     void throw_for_key(const char* key,
00126                        const rutz::file_pos& pos) const
00127     { base.throw_for_key(key, pos); }
00128 
00130     void throw_for_key(const rutz::fstring& key,
00131                        const rutz::file_pos& pos) const
00132     { base.throw_for_key(key, pos); }
00133 
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 
00139     value_t* get_ptr_for_key(const char* key) const
00140     { return static_cast<value_t*>(base.get_value_for_key(key)); }
00141 
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 10097 2007-08-07 19:08:27Z rjpeters $ $HeadURL: file:
00155 #endif // !GROOVX_RUTZ_ASSOCARRAY_H_UTC20050626084020_DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:40 2008 by Doxygen version 1.5.5.