rwlogo
SourcePro 11.1

SourcePro® C++ API Reference Guide



   SourcePro C++
Documentation Home

RWTFunctorMapR2< SR, Key, S1 > Class Template Reference
[Functor Map]

A functor map that takes two arguments at invocation time, and returns a value. More...

#include <rw/functor/map/RWTFunctorMapR2.h>

Inheritance diagram for RWTFunctorMapR2< SR, Key, S1 >:
RWTFunctorR2< SR, Key, S1 > RWHandleBase

List of all members.

Public Types

typedef Key key_type
typedef unsigned(* hash_function )(const Key &)

Public Member Functions

 RWTFunctorMapR2 (hash_function hf, size_t size=RW_FUNCTOR_MAP_CAPACITY)
 RWTFunctorMapR2 (const RWTFunctorMapR2< SR, Key, S1 > &second)
 ~RWTFunctorMapR2 (void)
RWTFunctorMapR2< SR, Key, S1 > & operator= (const RWTFunctorMapR2< SR, Key, S1 > &second)
SR operator() (Key key, S1 s1) const
RWTFunctorMapR2Imp< SR, Key, S1 > & body (void) const
bool add (Key key, RWTFunctorR1< SR, S1 > functor)
bool remove (Key key)
bool contains (Key key) const
bool find (Key key, RWTFunctorR1< SR, S1 > &functor) const
void resize (size_t size)
void clear (void)
size_t entries (void) const
void setDefault (RWTFunctorR1< SR, S1 > functor)
RWTFunctorR1< SR, S1 > getDefault (void) const

Detailed Description

template<class SR, class Key, class S1>
class RWTFunctorMapR2< SR, Key, S1 >

The RWTFunctorMapR2 class represents the functor maps that take two arguments at invocation time, and return a value. Since one of these arguments is the key into the map, the functors that are held in the map take only one argument; they are RWTFunctorR1 functors.

Functor maps allow functors to be grouped together in a key/value table structure. They add functors to the map with a corresponding key of type key_type, later using it to invoke the desired functor.

Examples

 #include <rw/cstring.h>
 #include <rw/rwdate.h>
 #include <rw/functor/functorR1.h>
 #include <rw/functor/map/RWTFunctorMapR2.h>
 
 // Functions that we want to wrap in functors:
 class BankAccount{
    float getBalance(RWDate date){ return 1234.56; }
    float getInterest(RWDate date){ return 61.51; }
 };
 
 int main () {
 
   BankAccount currentAcct;
 
   // Create new RWTFunctorR1s to be the values in the map.
   RWTFunctorR1<float,RWDate> balanceFunctor = rwtMakeFunctorR1(
      (float(*)(RWDate))0, currentAcct, &BankAccount::getBalance);
   RWTFunctorR1<float,RWDate> interestFunctor = rwtMakeFunctorR1(
      (float(*)(RWDate))0, currentAcct, &BankAccount::getInterest);
 
   // Create keys with which to associate the values.
   RWCString balanceKey = "balance";
   RWCString interestKey = "interest";
 
   // Declare a new map. A complete, but empty instance now exists.
   RWTFunctorMapR2<float,RWCString,RWDate> fmap(RWCString::hash);
 
   // Add something to the map to make it useful.
   fmap.add(balanceKey, balanceFunctor);
   fmap.add(interestKey, interestFunctor);
 
   // Create an RWDate to represent the date for which we want info.
   RWDate today;
 
   // Invoking the map with the key "balance" results in the
   // balanceFunctor being called.
   float balance = fmap("balance", today);
 
   // Invoking the map with the key "interest" results in the
   // interestFunctor being called.
   float interest = fmap("interest", today);
 
   cout << "Account balance as of " << today << " is "
        << balance << endl;
   cout << "Interest earned as of " << today << " is "
        << interest << endl;
 
   return 0;
 }

Program output:

 Account balance as of 07/14/99 is 1234.56
 Interest earned as of 07/14/99 is 61.51

Member Typedef Documentation

template<class SR, class Key, class S1>
typedef unsigned(* RWTFunctorMapR2< SR, Key, S1 >::hash_function)(const Key &)

The signature of the hash function.

template<class SR, class Key, class S1>
typedef Key RWTFunctorMapR2< SR, Key, S1 >::key_type

The type of the key.


Constructor & Destructor Documentation

template<class SR, class Key, class S1>
RWTFunctorMapR2< SR, Key, S1 >::RWTFunctorMapR2 ( hash_function  hf,
size_t  size = RW_FUNCTOR_MAP_CAPACITY 
)

Constructs an empty map instance. This creates a complete RWTFunctorMapR2 instance, but the map will contain no entries. The size parameter has a default value of RW_FUNCTOR_MAP_CAPACITY, which is defined in rw/functor/map/pkgdefs.h.

template<class SR, class Key, class S1>
RWTFunctorMapR2< SR, Key, S1 >::RWTFunctorMapR2 ( const RWTFunctorMapR2< SR, Key, S1 > &  second  ) 

Copy constructor. Constructs a new functor map instance which then shares its representation with the functor map second.

template<class SR, class Key, class S1>
RWTFunctorMapR2< SR, Key, S1 >::~RWTFunctorMapR2 ( void   ) 

Destructor.


Member Function Documentation

template<class SR, class Key, class S1>
bool RWTFunctorMapR2< SR, Key, S1 >::add ( Key  key,
RWTFunctorR1< SR, S1 >  functor 
)

Adds the specified functor to the functor map with the corresponding key. If an entry already exists with an equivalent key, the addition fails and false is returned.

template<class SR, class Key, class S1>
RWTFunctorMapR2Imp<SR,Key,S1>& RWTFunctorMapR2< SR, Key, S1 >::body ( void   )  const

Gets a reference for the body instance, if any; otherwise, throws an RWTHRInvalidPointer exception.

Reimplemented from RWTFunctorR2< SR, Key, S1 >.

template<class SR, class Key, class S1>
void RWTFunctorMapR2< SR, Key, S1 >::clear ( void   ) 

Clears the functor map of all entries.

template<class SR, class Key, class S1>
bool RWTFunctorMapR2< SR, Key, S1 >::contains ( Key  key  )  const

Returns true if the functor map contains an entry with key equivalent to the given key.

template<class SR, class Key, class S1>
size_t RWTFunctorMapR2< SR, Key, S1 >::entries ( void   )  const

Returns the number of entries in the functor map.

template<class SR, class Key, class S1>
bool RWTFunctorMapR2< SR, Key, S1 >::find ( Key  key,
RWTFunctorR1< SR, S1 > &  functor 
) const

Finds the entry in the functor map with key equivalent to the given key, and returns it via the reference parameter functor. Returns true if the key is found, and otherwise returns false. In the case where a matching key is not found, but a default functor exists, the reference parameter functor is set to the default functor, but returns false. If no match is found, and there is no default functor, an RWTHRInternalError exception is thrown.

template<class SR, class Key, class S1>
RWTFunctorR1<SR,S1> RWTFunctorMapR2< SR, Key, S1 >::getDefault ( void   )  const

Returns the map's default functor. This functor is used whenever an invalid key accesses the functor map.

template<class SR, class Key, class S1>
SR RWTFunctorMapR2< SR, Key, S1 >::operator() ( Key  key,
S1  s1 
) const

Invokes the functor in the map that is associated with a key which is equivalent to the given key. If there is no matching key, and a default functor has been set, calls the default. If there is no default, and no matching key, throws an RWTHRInternalError exception.

Reimplemented from RWTFunctorR2< SR, Key, S1 >.

template<class SR, class Key, class S1>
RWTFunctorMapR2<SR,Key,S1>& RWTFunctorMapR2< SR, Key, S1 >::operator= ( const RWTFunctorMapR2< SR, Key, S1 > &  second  ) 

Assignment operator. Binds this functor map instance to the representation of the second map instance.

template<class SR, class Key, class S1>
bool RWTFunctorMapR2< SR, Key, S1 >::remove ( Key  key  ) 

Removes the entry, if it exists, with key equivalent to the given key, and returns true. If no such entry exists, returns false.

template<class SR, class Key, class S1>
void RWTFunctorMapR2< SR, Key, S1 >::resize ( size_t  size  ) 

Changes the size of the map to size. This is an expensive operation, since the entire map must be re-created. Size can be set at construction time via the size parameter of the constructor.

template<class SR, class Key, class S1>
void RWTFunctorMapR2< SR, Key, S1 >::setDefault ( RWTFunctorR1< SR, S1 >  functor  ) 

Sets functor as the default functor. This functor is used whenever an invalid key is used to access the functor map.


© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.