RWTFunctorMapR1 RWTFunctorR1
add() clear() contains() |
entries() find() getDefault() |
operator()() operator=() remove() |
resize() setDefault() |
Functor Map, subpackage of Functor
#include "rw/functor/map/RWTFunctorMapR1.h";
The RWTFunctorMapR1 class represents the functor maps that take only one argument at invocation time, and return a value. Since this one argument is the key into the map, the functors that are held in the map do not take any arguments at all; they are RWTFunctorR0s.
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.
#include <rw/cstring.h> #include <rw/functor/functorR0.h> #include <rw/functor/map/RWTFunctorMapR1.h> // Functions that we want to wrap in functors: class BankAccount{ float getBalance(){ return 1234.56; } float getInterest(){ return 61.51; } }; int main () { BankAccount myAcct; // Create new RWTFunctorR0s to be the values in the map. RWTFunctorR0<float> balanceFunctor=rwtMakeFunctorR0( (float(*)(void))0,myAcct,&BankAccount::getBalance); RWTFunctorR0<float> interestFunctor=rwtMakeFunctorR0( (float(*)(void))0,myAcct,&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. RWTFunctorMapR1<float,RWCString> fmap(RWCString::hash); // Add something to the map to make it useful. fmap.add(balanceKey, balanceFunctor); fmap.add(interestKey, interestFunctor); // Invoking the map with the key "balance" results in the // balanceFunctor being called. float balance = fmap("balance"); // Invoking the map with the key "interest" results in the // interestFunctor being called. float interest = fmap("interest"); cout << "Account balance is " << balance << endl; cout << "Interest earned this year is " << interest << endl; return 0; } OUTPUT Account balance is 1234.56 Interest earned this year is 61.51
typedef Key key_type;
The type of the key.
typedef unsigned (*hash_function)(const Key&);
The signature of the hash function.
RWTFunctorMapR1(hash_function hf, size_t size = RW_FUNCTOR_MAP_CAPACITY);
Constructs an empty map instance. This creates a complete RWTFunctorMapR1 instance, but the map will contain no entries. The default size parameter is set to RW_FUNCTOR_MAP_CAPACITY, which is set in rw/functor/map/pkgdefs.h.
RWTFunctorMapR1(const RWTFunctorMapR1<SR,key_type>& second);
Copy constructor. Constructs a new functor map instance which then shares its representation with the original functor map.
RWTFunctorMapR1<SR,key_type>& operator=(const RWTFunctorMapR1<SR,key_type>& second);
Assignment operator. Binds this functor map instance to the representation of the second map instance.
SR operator()(key_type key) const;
Invokes the functor in the map that is associated with 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, an RWTHRInternalError exception is thrown.
RWBoolean add(key_type key, RWTFunctorR0<SR> 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.
RWBoolean contains(key_type key) const;
Returns TRUE if the functor map contains an entry with an equivalent key.
RWBoolean find(key_type key, RWTFunctorR0<SR>& functor) const;
Finds the entry in the functor map with the equivalent key, and returns it via the RWTFunctorR0 reference parameter. 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, this sets the reference parameter to the default functor, but returns FALSE. If no match is found, and there is no default functor, this throws an RWTHRInternalError exception.
RWBoolean remove(key_type key);
Removes the entry, if it exists, with the equivalent key, and returns TRUE. If no such entry exists, returns FALSE.
RWTFunctorR0<SR> getDefault() const;
Returns the map's default functor. This functor is used whenever an invalid key accesses the functor map.
size_t entries() const;
Returns the number of entries in the functor map.
void clear();
Clears the functor map of all entries.
void 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.
void setDefault(RWTFunctorR0<SR> functor);
Sets a functor as the default functor. This functor is used whenever an invalid key is used to access the functor map.
©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.