• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

RawHashTable.h

Go to the documentation of this file.
00001 /** @file
00002 
00003   C++ wrapper around libts hash tables
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022 
00023   @section details Details
00024 
00025   These C++ RawHashTables are a C++ wrapper around libts hash tables.
00026   They expose an interface very analogous to ink_hash_table, for better
00027   or for worse. See HashTable for a more C++-oriented hash table.
00028 
00029 */
00030 
00031 #ifndef _RawHashTable_h_
00032 #define _RawHashTable_h_
00033 
00034 #include "libts.h"
00035 
00036 #include "ink_apidefs.h"
00037 
00038 //////////////////////////////////////////////////////////////////////////////
00039 //
00040 //      Constants and Type Definitions
00041 //
00042 //////////////////////////////////////////////////////////////////////////////
00043 
00044 typedef enum
00045 {
00046   RawHashTable_KeyType_String = InkHashTableKeyType_String,
00047   RawHashTable_KeyType_Word = InkHashTableKeyType_Word
00048 } RawHashTable_KeyType;
00049 
00050 typedef InkHashTableKey RawHashTable_Key;
00051 typedef InkHashTableValue RawHashTable_Value;
00052 typedef InkHashTableEntry RawHashTable_Binding;
00053 typedef InkHashTableIteratorState RawHashTable_IteratorState;
00054 
00055 //////////////////////////////////////////////////////////////////////////////
00056 //
00057 //      The RawHashTable Class
00058 //
00059 //////////////////////////////////////////////////////////////////////////////
00060 
00061 class RawHashTable
00062 {
00063 private:
00064   InkHashTable * ht;
00065   RawHashTable_KeyType key_type;
00066   bool deallocate_values_on_destruct;
00067 
00068 public:
00069     inkcoreapi RawHashTable(RawHashTable_KeyType key_type, bool deallocate_values_on_destruct = false);
00070     virtual ~ RawHashTable();
00071 
00072   //
00073   // these are the simplest accessor functions
00074   //
00075 
00076   bool getValue(RawHashTable_Key key, RawHashTable_Value * value_ptr);
00077   void setValue(RawHashTable_Key key, RawHashTable_Value value_ptr);
00078   bool isBound(RawHashTable_Key key);
00079   bool unbindKey(RawHashTable_Key key);
00080   void replaceString(char *key, char *string);
00081 
00082   //
00083   // these functions allow you to manipulate the (key,value) bindings directly
00084   //
00085 
00086   RawHashTable_Binding *getCurrentBinding(RawHashTable_Key key);
00087   RawHashTable_Binding *getOrCreateBinding(RawHashTable_Key key, bool * was_new = NULL);
00088 
00089   void setBindingValue(RawHashTable_Binding * binding, RawHashTable_Value value);
00090   RawHashTable_Key getKeyFromBinding(RawHashTable_Binding * binding);
00091   RawHashTable_Value getValueFromBinding(RawHashTable_Binding * binding);
00092 
00093   //
00094   // these functions allow you to iterate through RawHashTable bindings
00095   //
00096 
00097   RawHashTable_Binding *firstBinding(RawHashTable_IteratorState * state_ptr);
00098   RawHashTable_Binding *nextBinding(RawHashTable_IteratorState * state_ptr);
00099 };
00100 
00101 //////////////////////////////////////////////////////////////////////////////
00102 //
00103 //      Inline Methods
00104 //
00105 //////////////////////////////////////////////////////////////////////////////
00106 
00107 /**
00108   This routine gets the value associated with key. If the key has a
00109   binding, the value is stored through value_ptr and true is returned. If
00110   the key DOES NOT have a binding, false is returned.
00111 
00112 */
00113 inline bool
00114 RawHashTable::getValue(RawHashTable_Key key, RawHashTable_Value * value_ptr)
00115 {
00116   int is_bound;
00117 
00118   is_bound = ink_hash_table_lookup(ht, (InkHashTableKey) key, (InkHashTableValue *) value_ptr);
00119   return (is_bound ? true : false);
00120 }
00121 
00122 /**
00123   This routine sets the value associated with key to the value. If
00124   a value is previously bound to the key, the previous value is left
00125   dangling. The caller is responsible to freeing any previous binding
00126   values needing freeing before calling setValue.
00127 
00128   If the key has a binding, the value is stored through value_ptr and
00129   true is returned. If the key DOES NOT have a binding, false is returned.
00130 
00131 */
00132 inline void
00133 RawHashTable::setValue(RawHashTable_Key key, RawHashTable_Value value)
00134 {
00135   ink_hash_table_insert(ht, (InkHashTableKey) key, (InkHashTableValue) value);
00136 }
00137 
00138 /**
00139   This routine sets the value associated with key to the value pointed to
00140   by value_ptr. If a value is previously bound to the key, the previous
00141   value is left dangling. The caller is responsible to freeing any
00142   previous value before setValue.
00143 
00144   If the key has a binding, the value is stored through value_ptr and
00145   true is returned. If the key DOES NOT have a binding, false is returned.
00146 
00147 */
00148 inline bool
00149 RawHashTable::isBound(RawHashTable_Key key)
00150 {
00151   int status = ink_hash_table_isbound(ht, (InkHashTableKey) key);
00152   return (status ? true : false);
00153 }
00154 
00155 /**
00156   This routine removes any association for key from the hash table. If
00157   data was bound to key, the binding will be deleted, but the value will
00158   not be deallocated. The caller is responsible to freeing any previous
00159   value before unbindKey.
00160 
00161   @return true if the key was previously bound, false otherwise.
00162 
00163 */
00164 inline bool
00165 RawHashTable::unbindKey(RawHashTable_Key key)
00166 {
00167   int status;
00168 
00169   status = ink_hash_table_delete(ht, (InkHashTableKey) key);
00170   return (status ? true : false);
00171 }
00172 
00173 /**
00174   This rather specialized routine binds a malloc-allocated string value
00175   to the key, freeing any previous value. The key must be a string,
00176   and the hash table must have been constructed as having key_type
00177   RawHashTable_KeyType_String.
00178 
00179 */
00180 inline void
00181 RawHashTable::replaceString(char *key, char *string)
00182 {
00183 //    if (key_type != RawHashTable_KeyType_String)
00184 //    {
00185 //      throw BadKeyType();
00186 //    }
00187 
00188   ink_hash_table_replace_string(ht, key, string);
00189 }
00190 
00191 /**
00192   This function looks up a binding for key in the hash table, and returns
00193   a pointer to the binding data structure directly inside the hash table,
00194   or NULL if there is no binding.
00195 
00196 */
00197 inline RawHashTable_Binding *
00198 RawHashTable::getCurrentBinding(RawHashTable_Key key)
00199 {
00200   InkHashTableEntry *he_ptr;
00201 
00202   he_ptr = ink_hash_table_lookup_entry(ht, (InkHashTableKey) key);
00203   return ((RawHashTable_Binding *) he_ptr);
00204 }
00205 
00206 /**
00207   This function looks up a binding for key in the hash table, creates
00208   a binding if one doesn't exist, and returns a pointer to the binding
00209   data structure directly inside the hash table.
00210 
00211   If was_new is not NULL, true is stored through was_new. If no binding
00212   previously existed, false is stored through was_new if a binding
00213   previously existed.
00214 
00215 */
00216 inline RawHashTable_Binding *
00217 RawHashTable::getOrCreateBinding(RawHashTable_Key key, bool * was_new)
00218 {
00219   int _was_new;
00220   InkHashTableEntry *he_ptr;
00221 
00222   he_ptr = ink_hash_table_get_entry(ht, (InkHashTableKey) key, &_was_new);
00223   *was_new = (_was_new ? true : false);
00224   return ((RawHashTable_Binding *) he_ptr);
00225 }
00226 
00227 /**
00228   This function looks up a binding for key in the hash table, creates
00229   a binding if one doesn't exist, and returns a pointer to the binding
00230   data structure directly inside the hash table.
00231 
00232   If was_new is not NULL, true is stored through was_new. If no binding
00233   previously existed, false is stored through was_new if a binding
00234   previously existed.
00235 
00236 */
00237 inline void
00238 RawHashTable::setBindingValue(RawHashTable_Binding * binding, RawHashTable_Value value)
00239 {
00240   ink_hash_table_set_entry(ht, (InkHashTableEntry *) binding, (InkHashTableValue) value);
00241 }
00242 
00243 /**
00244   This function takes a binding and extracts the key.
00245 
00246 */
00247 inline RawHashTable_Key
00248 RawHashTable::getKeyFromBinding(RawHashTable_Binding * binding)
00249 {
00250   InkHashTableKey ht_key;
00251 
00252   ht_key = ink_hash_table_entry_key(ht, (InkHashTableEntry *) binding);
00253   return ((RawHashTable_Key) ht_key);
00254 }
00255 
00256 /**
00257   This function takes a binding and extracts the value.
00258 
00259 */
00260 inline RawHashTable_Value
00261 RawHashTable::getValueFromBinding(RawHashTable_Binding * binding)
00262 {
00263   InkHashTableValue ht_value;
00264 
00265   ht_value = ink_hash_table_entry_value(ht, (InkHashTableEntry *) binding);
00266   return ((RawHashTable_Value) ht_value);
00267 }
00268 
00269 /**
00270   This function takes a hash table, initializes an interator data
00271   structure to point to the first binding in the hash table, and returns
00272   the first binding, or NULL if there are none.
00273 
00274 */
00275 inline RawHashTable_Binding *
00276 RawHashTable::firstBinding(RawHashTable_IteratorState * state_ptr)
00277 {
00278   InkHashTableEntry *he_ptr;
00279 
00280   he_ptr = ink_hash_table_iterator_first(ht, (InkHashTableIteratorState *) state_ptr);
00281   return ((RawHashTable_Binding *) he_ptr);
00282 }
00283 
00284 inline
00285 RawHashTable::RawHashTable(RawHashTable_KeyType akey_type, bool adeallocate_values_on_destruct)
00286 {
00287   RawHashTable::key_type = akey_type;
00288   RawHashTable::deallocate_values_on_destruct = adeallocate_values_on_destruct;
00289   ht = ink_hash_table_create((InkHashTableKeyType) key_type);
00290 }
00291 
00292 inline
00293 RawHashTable::~
00294 RawHashTable()
00295 {
00296   if (deallocate_values_on_destruct)
00297     ink_hash_table_destroy_and_free_values(ht);
00298   else
00299     ink_hash_table_destroy(ht);
00300 }
00301 
00302 /**
00303   This function takes a hash table and a pointer to iterator state,
00304   and advances to the next binding in the hash table, if any. If there
00305   in a next binding, a pointer to the binding is returned, else NULL.
00306 
00307 */
00308 inline RawHashTable_Binding *
00309 RawHashTable::nextBinding(RawHashTable_IteratorState * state_ptr)
00310 {
00311   InkHashTableEntry *he_ptr;
00312 
00313   he_ptr = ink_hash_table_iterator_next(ht, (InkHashTableIteratorState *) state_ptr);
00314   return ((RawHashTable_Binding *) he_ptr);
00315 }
00316 
00317 //////////////////////////////////////////////////////////////////////////////
00318 //
00319 //      The RawHashTableIter Class
00320 //
00321 //////////////////////////////////////////////////////////////////////////////
00322 
00323 class RawHashTableIter
00324 {
00325 public:
00326   RawHashTableIter(RawHashTable & ht);
00327   ~RawHashTableIter();
00328 
00329   RawHashTable_Value & operator ++();   // get next
00330   RawHashTable_Value & operator () () const;    // get current
00331   operator  const void *() const;       // is valid
00332 
00333     RawHashTable_Value & value() const; // get current value
00334   const char *key() const;      // get current key
00335 
00336 
00337 private:
00338     RawHashTable & m_ht;
00339   RawHashTable_Binding *m_currentBinding;
00340   RawHashTable_IteratorState m_hashIterState;
00341 };
00342 
00343 //////////////////////////////////////////////////////////////////////////////
00344 //
00345 //      Inline Methods
00346 //
00347 //////////////////////////////////////////////////////////////////////////////
00348 
00349 inline RawHashTable_Value &
00350 RawHashTableIter::operator () ()
00351 const
00352 {
00353   return (m_currentBinding->clientData);
00354 }
00355 
00356 inline RawHashTable_Value &
00357 RawHashTableIter::operator ++()
00358 {
00359   m_currentBinding = m_ht.nextBinding(&m_hashIterState);
00360   return (m_currentBinding->clientData);
00361 }
00362 
00363 inline
00364 RawHashTableIter::operator  const void *()
00365 const
00366 {
00367   return ((m_currentBinding != 0) ? this : 0);
00368 }
00369 
00370 inline
00371   RawHashTable_Value &
00372 RawHashTableIter::value() const
00373 {
00374   return (m_currentBinding->clientData);
00375 }
00376 
00377 inline const char *
00378 RawHashTableIter::key() const
00379 {
00380   return (m_currentBinding->key.string);
00381 }
00382 
00383 inline
00384 RawHashTableIter::RawHashTableIter(RawHashTable & ht)
00385   :
00386 m_ht(ht),
00387 m_currentBinding(0)
00388 {
00389   m_currentBinding = m_ht.firstBinding(&m_hashIterState);
00390   return;
00391 }
00392 
00393 inline
00394 RawHashTableIter::~
00395 RawHashTableIter()
00396 {
00397   return;
00398 }
00399 
00400 #endif /*_RawHashTable_h_*/

Generated by  doxygen 1.7.1