rwlogo
SourcePro C++ 12.0

SourcePro® C++ API Reference Guide



   SourcePro C++
Documentation Home

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

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

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

Inheritance diagram for RWTFunctorMap2< Key, S1 >:
RWTFunctor2< Key, S1 > RWHandleBase

List of all members.

Public Types

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

Public Member Functions

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

Detailed Description

template<class Key, class S1>
class RWTFunctorMap2< Key, S1 >

The RWTFunctorMap2 class represents the functor maps that take two arguments at invocation time and return no value. Since one of these arguments is the key into the map, the functors held in the map take only one argument, and 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, used later to invoke the desired functor.

Examples

 #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;
 }

Program output:

 okButton was pushed, user data is: Fred
 cancelButton was pushed, user data is: Barney

Member Typedef Documentation

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

The signature of the hash function.

template<class Key, class S1>
typedef Key RWTFunctorMap2< Key, S1 >::key_type

The type of the key.


Constructor & Destructor Documentation

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

Constructs an empty map instance. This creates a complete RWTFunctorMap2 instance, but with no entries. The size parameter has a default value of RW_FUNCTOR_MAP_CAPACITY, defined in rw/functor/map/pkgdefs.h.

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

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

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

Destructor.


Member Function Documentation

template<class Key, class S1>
bool RWTFunctorMap2< Key, S1 >::add ( Key  key,
RWTFunctor1< S1 >  functor 
)

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

template<class Key, class S1>
RWTFunctorMap2Imp<Key,S1>& RWTFunctorMap2< Key, S1 >::body ( void   )  const

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

Reimplemented from RWTFunctor2< Key, S1 >.

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

Clears the functor map of all entries.

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

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

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

Returns the number of entries in the functor map.

template<class Key, class S1>
bool RWTFunctorMap2< Key, S1 >::find ( Key  key,
RWTFunctor1< S1 > &  functor 
) const

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

See also:
setDefault().
template<class Key, class S1>
RWTFunctor1<S1> RWTFunctorMap2< Key, S1 >::getDefault ( void   )  const

Returns the map's default functor.

See also:
setDefault().
template<class Key, class S1>
void RWTFunctorMap2< Key, S1 >::operator() ( Key  key,
S1  s1 
) const

Invokes the functor in the map that is associated with a key 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 RWTFunctor2< Key, S1 >.

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

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

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

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

template<class Key, class S1>
void RWTFunctorMap2< 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 Key, class S1>
void RWTFunctorMap2< Key, S1 >::setDefault ( RWTFunctor1< S1 >  functor  ) 

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

 All Classes Functions Variables Typedefs Enumerations Enumerator Friends

© 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.