Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
AHDispatcher< Key, Operation > Class Template Reference

Tree-based command dispatcher. More...

#include <ah-dispatcher.H>

Collaboration diagram for AHDispatcher< Key, Operation >:
[legend]

Public Member Functions

 AHDispatcher ()=default
 Default constructor.
 
template<typename ... Pairs>
 AHDispatcher (const Key &key, Operation op, Pairs ... pairs)
 Variadic constructor for initializing with key-operation pairs.
 
void insert (const Key &key, Operation op)
 Register a key-operation pair.
 
template<typename ... Args>
auto run (const Key &key, Args &&... args) const
 Execute the operation associated with a key.
 
void remove (const Key &key)
 Remove a key-operation pair.
 
bool valid_key (const Key &key)
 Check if a key is registered.
 
DynList< Key > keys () const
 Get all registered keys.
 
size_t size () const
 Get the number of registered operations.
 
bool is_empty () const
 Check if the dispatcher is empty.
 

Private Attributes

DynMapTree< Key, Operationtbl
 

Detailed Description

template<typename Key, class Operation>
class AHDispatcher< Key, Operation >

Tree-based command dispatcher.

Maps keys to operations using a balanced tree (DynMapTree). Provides O(log n) lookup, insertion, and removal.

Template Parameters
KeyThe key type (must be comparable with <)
OperationThe callable type (function pointer, std::function, etc.)

Example

// Calculator dispatcher
ops.insert('+', [](double a, double b) { return a + b; });
ops.insert('-', [](double a, double b) { return a - b; });
ops.insert('*', [](double a, double b) { return a * b; });
ops.insert('/', [](double a, double b) { return a / b; });
double result = ops.run('+', 10.0, 5.0); // 15.0
Tree-based command dispatcher.
T & insert(const T &item)
Insert a new item by copy.
Definition htlist.H:1502
DynList< T > maps(const C &c, Op op)
Classic map operation.
Author
Leandro Rabindranath León

Definition at line 137 of file ah-dispatcher.H.

Constructor & Destructor Documentation

◆ AHDispatcher() [1/2]

template<typename Key , class Operation >
AHDispatcher< Key, Operation >::AHDispatcher ( )
default

Default constructor.

Creates an empty dispatcher.

◆ AHDispatcher() [2/2]

template<typename Key , class Operation >
template<typename ... Pairs>
AHDispatcher< Key, Operation >::AHDispatcher ( const Key &  key,
Operation  op,
Pairs ...  pairs 
)
inline

Variadic constructor for initializing with key-operation pairs.

Parameters
keyFirst key to register.
opOperation associated with the key.
pairsAdditional key-operation pairs.

Example

AHDispatcher<std::string, void(*)()> cmds(
"init", init_func,
"run", run_func,
"stop", stop_func
);

Definition at line 164 of file ah-dispatcher.H.

References Aleph::DynMapTree< Key, Data, Tree, Compare >::insert(), and AHDispatcher< Key, Operation >::tbl.

Member Function Documentation

◆ insert()

template<typename Key , class Operation >
void AHDispatcher< Key, Operation >::insert ( const Key &  key,
Operation  op 
)
inline

Register a key-operation pair.

If the key already exists, its operation is replaced.

Parameters
keyThe key to register.
opThe operation to associate with the key.

Definition at line 178 of file ah-dispatcher.H.

References Aleph::DynMapTree< Key, Data, Tree, Compare >::insert(), and AHDispatcher< Key, Operation >::tbl.

◆ is_empty()

template<typename Key , class Operation >
bool AHDispatcher< Key, Operation >::is_empty ( ) const
inline

Check if the dispatcher is empty.

Returns
true if no operations are registered.

Definition at line 245 of file ah-dispatcher.H.

References Aleph::DynSetTree< Key, Tree, Compare >::is_empty(), and AHDispatcher< Key, Operation >::tbl.

◆ keys()

template<typename Key , class Operation >
DynList< Key > AHDispatcher< Key, Operation >::keys ( ) const
inline

Get all registered keys.

Returns
A DynList containing all keys.

Definition at line 231 of file ah-dispatcher.H.

References Aleph::DynMapTree< Key, Data, Tree, Compare >::keys(), and AHDispatcher< Key, Operation >::tbl.

◆ remove()

template<typename Key , class Operation >
void AHDispatcher< Key, Operation >::remove ( const Key &  key)
inline

Remove a key-operation pair.

Parameters
keyThe key to remove.

Definition at line 213 of file ah-dispatcher.H.

References Aleph::DynMapTree< Key, Data, Tree, Compare >::remove(), and AHDispatcher< Key, Operation >::tbl.

◆ run()

template<typename Key , class Operation >
template<typename ... Args>
auto AHDispatcher< Key, Operation >::run ( const Key &  key,
Args &&...  args 
) const
inline

Execute the operation associated with a key.

Template Parameters
ArgsTypes of arguments to pass to the operation.
Parameters
keyThe key identifying the operation.
argsArguments to forward to the operation.
Returns
The return value of the operation.
Exceptions
Abortsif the key is not registered.
Note
Use valid_key() to check before calling if unsure.

Definition at line 196 of file ah-dispatcher.H.

References Aleph::maps(), Aleph::DynMapTree< Key, Data, Tree, Compare >::search(), and AHDispatcher< Key, Operation >::tbl.

◆ size()

template<typename Key , class Operation >
size_t AHDispatcher< Key, Operation >::size ( ) const
inline

Get the number of registered operations.

Returns
The number of key-operation pairs.

Definition at line 238 of file ah-dispatcher.H.

References Aleph::DynSetTree< Key, Tree, Compare >::size(), and AHDispatcher< Key, Operation >::tbl.

◆ valid_key()

template<typename Key , class Operation >
bool AHDispatcher< Key, Operation >::valid_key ( const Key &  key)
inline

Check if a key is registered.

Parameters
keyThe key to check.
Returns
true if the key exists, false otherwise.

Definition at line 224 of file ah-dispatcher.H.

References Aleph::DynMapTree< Key, Data, Tree, Compare >::has(), and AHDispatcher< Key, Operation >::tbl.

Member Data Documentation

◆ tbl


The documentation for this class was generated from the following file: