index_helper Struct Reference

When using a non-const hash_list, and invoking the array index operator, [], you get back an index_helper object rather than a reference to the Value object you would expect to be gettting. This is because the behaviour of the array index operation requires that if the object isn't there already that it be inserted. You could insert a 'default' Value object into the container, only to have it deleted if you are writing code like this: More...

#include <hash_list.h>

Collaboration diagram for index_helper:
Collaboration graph
[legend]

List of all members.

Public Types

typedef hash_list< Key, Value,
Hash > 
HL
 short name for this kind of hash list

Public Member Functions

 index_helper (Key const &k, HL *l)
 operator Value const & ()
 When doing this:
Value const & operator= (Value const &r)
 When you do this:

Public Attributes

Key const & key_
 Key of the object being referenced.
HLlist_
 the hash_list being dereferenced

Detailed Description

template<class Key, class Value, class Hash = Hasher<Key>>
struct cxxtls::hash_list< Key, Value, Hash >::index_helper

When using a non-const hash_list, and invoking the array index operator, [], you get back an index_helper object rather than a reference to the Value object you would expect to be gettting. This is because the behaviour of the array index operation requires that if the object isn't there already that it be inserted. You could insert a 'default' Value object into the container, only to have it deleted if you are writing code like this:

That is, if you are inserting into the list using the array index operator, you don't want to have to insert a default value, then remove it, then insert the newValue. Instead, you want this operation to do just a single insert.

The index_helper class aids in this goal. Instead of having the non-const operator[] automatically insert 'default' Value object it returns immediately an index_helper object. The index_helper object can be converted into Value const & and it has an operator= to handle assignments.

For example, if you write code like this:

You are getting the following:

On the other hand if you do this:

hash_list::operator[] returns index_helper(stuff,this) and then v is assigned the value of index_helper::operator Value().

Unfortunately this approach makes it hard if Value is a pointer because the following code won't really work:

Instead you must write:

      Value v = list[stuff];
      v->member = 9;
     @endcoce
    
     Or
     @code
        ((Value)(list[stuff]))->member = 9;  // YUCK!

Definition at line 377 of file hash_list.h.


Member Typedef Documentation

typedef hash_list<Key, Value, Hash> HL

short name for this kind of hash list

Definition at line 436 of file hash_list.h.


Constructor & Destructor Documentation

index_helper ( Key const &  k,
HL l 
)

plain vanila constructor

Definition at line 440 of file hash_list.h.


Member Function Documentation

operator Value const & (  ) 

When doing this:

            Value t = hash_list[key];

You end up calling index_helper::operator Value const&()

Definition at line 447 of file hash_list.h.

Here is the call graph for this function:

Value const& operator= ( Value const &  r  ) 

When you do this:

             hash_list[key] = newValue;

Any old entry with this key/value pair is deleted and a new one is inserted

Definition at line 467 of file hash_list.h.

Here is the call graph for this function:


Member Data Documentation

Key const& key_

Key of the object being referenced.

Definition at line 437 of file hash_list.h.

the hash_list being dereferenced

Definition at line 438 of file hash_list.h.


The documentation for this struct was generated from the following file:
Generated on Wed Feb 29 22:55:54 2012 for CXXUtilities by  doxygen 1.6.3