xorp

Dispatcher Class Reference

Link between elements and operations. More...

#include <dispatcher.hh>

List of all members.

Classes

class  OpNotFound
 Exception thrown if no operation is found for given arguments. More...
union  Value

Public Types

typedef vector< const Element * > ArgList

Public Member Functions

template<class L , class R , Element *(*)(const L &, const R &) funct>
void add (const BinOper &op)
 Method to register a binary operation callback with dispatcher.
template<class T , Element *(*)(const T &) funct>
void add (const UnOper &op)
 Method to register a unary operation callback with dispatcher.
Elementrun (const Oper &op, unsigned argc, const Element **argv) const
 Execute an n-ary operation.
Elementrun (const UnOper &op, const Element &arg) const
 Execute an unary operation.
Elementrun (const BinOper &op, const Element &left, const Element &right) const
 Execute a binary operation.

Private Types

typedef Element *(* CB_bin )(const Element &, const Element &)
typedef Element *(* CB_un )(const Element &)
typedef unsigned Key
typedef map< Key, ValueMap

Private Member Functions

Key makeKey (const Oper &op, unsigned argc, const Element **argv) const
 Create a key for the callback table based on operation and arguments.
Value lookup (const Oper &op, unsigned argc, const Element **argv) const
 Lookup a callback for the requested operation and elements.

Static Private Attributes

static Value _map [32768]

Detailed Description

Link between elements and operations.

Executes operations on elments.

Implementation of multimethods. Insipred/copied from Alexandrescu [Modern C++ Design].

By taking base element arguments and an operation, it will execute the correct operation based on the concrete type of the arguments.

Similar to an ElementFactory.


Member Function Documentation

template<class L , class R , Element *(*)(const L &, const R &) funct>
void Dispatcher::add ( const BinOper op) [inline]

Method to register a binary operation callback with dispatcher.

Parameters:
Lconcrete class of first argument
Rconcrete class of second argument
functfunction to be called to perform operation.
opbinary operation to be registered.
template<class T , Element *(*)(const T &) funct>
void Dispatcher::add ( const UnOper op) [inline]

Method to register a unary operation callback with dispatcher.

Parameters:
Tconcrete class of argument
functfunction to be called to perform operation.
opunary operation to be registered.
Value Dispatcher::lookup ( const Oper op,
unsigned  argc,
const Element **  argv 
) const [inline, private]

Lookup a callback for the requested operation and elements.

Throws exception if none is found.

Returns:
callback which will perform requested operation.
Parameters:
opoperation to perform.
argsthe arguments of the operation.
Key Dispatcher::makeKey ( const Oper op,
unsigned  argc,
const Element **  argv 
) const [inline, private]

Create a key for the callback table based on operation and arguments.

Returns:
key used for callback lookup.
Parameters:
oprequested operation.
argsthe arguments for the operation.
Element * Dispatcher::run ( const UnOper op,
const Element arg 
) const

Execute an unary operation.

Returns:
Result of operation. Caller is responsible for delete.
Parameters:
opOperation to perform.
argArgument of operation.
Element * Dispatcher::run ( const Oper op,
unsigned  argc,
const Element **  argv 
) const

Execute an n-ary operation.

Throws an exception on failure.

Returns:
result of operation.
Parameters:
opoperation to dispatch.
argsarguments of operation.
Element * Dispatcher::run ( const BinOper op,
const Element left,
const Element right 
) const

Execute a binary operation.

Returns:
result of operation. Caller is responsible for delete.
Parameters:
opOperation to perform.
leftfirst argument.
rightsecond argument.

The documentation for this class was generated from the following files:
 All Classes Namespaces Functions Variables Typedefs Enumerations