RWTFunctorMap2 RWTFunctor2
add() clear() contains() |
entries() find() getDefault() |
operator()() operator=() remove() |
resize() setDefault() |
Functor Map, subpackage of Functor
#include <rw/functor/map/RWTFunctorMap2.h>
The RWTFunctorMap2 class represents the functor maps that take two arguments at invocation time. 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 RWTFunctor1<S1> 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.
#include <rw/cstring.h> #include <rw/functor/functor1.h> #include <rw/functor/map/RWTFunctorMap2.h> // Functions that we want to wrap in functors: void okButton(RWCString user_data) { cout << "okButton was pushed, user data is: " << user_data << endl; } void cancelButton(RWCString user_data) { cout << "cancelButton was pushed, user data is: " << user_data << endl; } int main () { // Create new 'RWTFunctor1<RWCString>'s to be the values // in the map. RWTFunctor1<RWCString> okFunctor= rwtMakeFunctor1((void(*)(RWCString))0,okButton); RWTFunctor1<RWCString> cancelFunctor= rwtMakeFunctor1((void(*)(RWCString))0,cancelButton); // Create keys with which to associate the values. RWCString okKey = "ok"; RWCString cancelKey = "cancel"; // Declare a new map. A complete, but empty, instance // now exists. RWTFunctorMap2<RWCString,RWCString> fmap(RWCString::hash); // Add something to the map to make it useful. fmap.add(okKey, okFunctor); fmap.add(cancelKey, cancelFunctor); // Invoking the map with the key "ok" calls the okButton // functor, and passes it the caller data "Fred". fmap("ok", "Fred"); // Finally, invoking the map with the key "cancel" calls the // cancelButton functor, and passes it the caller data "Barney". fmap("cancel", "Barney"); return 0; } OUTPUT: okButton was pushed, user data is: Fred cancelButton was pushed, user data is: Barney
typedef Key key_type;
The type of the key.
typedef unsigned (*hash_function)(const Key&);
The signature of the hash function.
RWTFunctorMap2(hash_function hf, size_t size = RW_FUNCTOR_MAP_CAPACITY);
Constructs an empty map instance. This creates a complete RWTFunctorMap2 instance, but the map contains no entries. The default size parameter is set to RW_FUNCTOR_MAP_CAPACITY, which is set in rw/functor/map/pkgdefs.h.
RWTFunctorMap2(const RWTFunctorMap2<key_type,S1>& second);
Copy constructor. Constructs a new functor map instance which then shares its representation with the original functor map.
RWTFunctorMap2<key_type,S1>& operator=(const RWTFunctorMap2<key_type,S1>& second);
Assignment operator. Binds this functor map instance to the representation of the second map instance.
void operator()(key_type key, S1 s1) 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, it calls the default. If there is no default, and no matching key, this throws an RWTHRInternalError exception.
RWBoolean add(key_type key, RWTFunctor1<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.
void clear();
Clears the functor map of all entries.
RWBoolean contains(key_type key) const;
Returns TRUE if the functor map contains an entry with an equivalent key.
size_t entries() const;
Returns the number of entries in the functor map.
RWBoolean find(key_type key, RWTFunctor1<S1>& functor) const;
Finds the entry in the functor map with the equivalent key, and returns it via the RWTFunctor1<S1> 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: the reference parameter 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.
RWTFunctor1<S1> getDefault() const;
Returns the map's default functor. This functor is used whenever an invalid key accesses the functor map.
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.
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(RWTFunctor1<S1> 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.