SourcePro® API Reference Guide

 
List of all members | Public Types | Public Member Functions
RWTFunctorMapR1< SR, Key > Class Template Reference

Deprecated. A functor map that takes only one argument at invocation time, and returns a value. More...

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

Inheritance diagram for RWTFunctorMapR1< SR, Key >:
RWTFunctorR1< SR, Key >

Public Types

typedef unsigned(* hash_function) (const Key &)
 
typedef Key key_type
 
- Public Types inherited from RWTFunctorR1< SR, Key >
typedef SR(* CallerSignature) (Key)
 
typedef Key S1Type
 
typedef SR SRType
 

Public Member Functions

 RWTFunctorMapR1 (hash_function hf, size_t size=RW_FUNCTOR_MAP_CAPACITY)
 
 RWTFunctorMapR1 (const RWTFunctorMapR1< SR, Key > &second)
 
 ~RWTFunctorMapR1 (void)
 
bool add (Key key, RWTFunctorR0< SR > functor)
 
void clear (void)
 
bool contains (Key key) const
 
size_t entries (void) const
 
bool find (Key key, RWTFunctorR0< SR > &functor) const
 
RWTFunctorR0< SR > getDefault (void) const
 
SR operator() (Key key) const
 
RWTFunctorMapR1< SR, Key > & operator= (const RWTFunctorMapR1< SR, Key > &second)
 
bool remove (Key key)
 
void resize (size_t size)
 
void setDefault (RWTFunctorR0< SR > functor)
 
- Public Member Functions inherited from RWTFunctorR1< SR, Key >
 RWTFunctorR1 (void)
 
 RWTFunctorR1 (RWStaticCtor)
 
 RWTFunctorR1 (RWTFunctorR1Imp< SR, Key > *functorImp)
 
 RWTFunctorR1 (const RWTFunctorR1< SR, Key > &second)
 
 RWTFunctorR1 (const RWTFunctor< SR(Key)> &second)
 
 ~RWTFunctorR1 (void)
 
RWTFunctorR1Imp< SR, Key > & body (void) const
 
RWTFunctorR1< SR, Key > & operator= (const RWTFunctorR1< SR, Key > &second)
 
RWTFunctorR1< SR, Key > & operator= (const RWTFunctor< SR(Key)> &second)
 

Detailed Description

template<class SR, class Key>
class RWTFunctorMapR1< SR, Key >

Deprecated:
As of SourcePro 12.5, use RWTValHashMap<Key, RWTFunctor<SR()>> instead.

The RWTFunctorMapR1 class represents the functor maps that take only one argument at invocation time, and return a value. Since this single argument is the key into the map, the functors held in the map take no arguments, and are RWTFunctorR0 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.

Example
#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
{
public:
float getBalance() {
return 1234.56f;
}
float getInterest() {
return 61.51f;
}
};
int main()
{
BankAccount myAcct;
// Create new RWTFunctorR0s to be the values in the map.
(float(*)(void))0, myAcct, &BankAccount::getBalance);
(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.
// 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");
std::cout << "Account balance is " << balance << std::endl;
std::cout << "Interest earned this year is " << interest << std::endl;
return 0;
}

OUTPUT:

Account balance is 1234.56
Interest earned this year is 61.51

Member Typedef Documentation

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

The signature of the hash function.

template<class SR, class Key>
typedef Key RWTFunctorMapR1< SR, Key >::key_type

The type of the key.

Constructor & Destructor Documentation

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

Constructs an empty map instance. This creates a complete RWTFunctorMapR1 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 SR, class Key>
RWTFunctorMapR1< SR, Key >::RWTFunctorMapR1 ( const RWTFunctorMapR1< SR, Key > &  second)

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

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

Destructor.

Member Function Documentation

template<class SR, class Key>
bool RWTFunctorMapR1< SR, Key >::add ( Key  key,
RWTFunctorR0< SR >  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 SR, class Key>
void RWTFunctorMapR1< SR, Key >::clear ( void  )

Clears the functor map of all entries.

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

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

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

Returns the number of entries in the functor map.

template<class SR, class Key>
bool RWTFunctorMapR1< SR, Key >::find ( Key  key,
RWTFunctorR0< SR > &  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 SR, class Key>
RWTFunctorR0<SR> RWTFunctorMapR1< SR, Key >::getDefault ( void  ) const

Returns the map's default functor.

See also
setDefault().
template<class SR, class Key>
SR RWTFunctorMapR1< SR, Key >::operator() ( Key  key) 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.

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

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

template<class SR, class Key>
bool RWTFunctorMapR1< SR, Key >::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 SR, class Key>
void RWTFunctorMapR1< SR, Key >::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>
void RWTFunctorMapR1< SR, Key >::setDefault ( RWTFunctorR0< SR >  functor)

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

Copyright © 2023 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.