Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dispatcher_example.C File Reference

Dispatcher pattern in Aleph-w (ah-dispatcher.H) with tree and hash backends. More...

#include <iostream>
#include <iomanip>
#include <string>
#include <functional>
#include <sstream>
#include <ah-dispatcher.H>
#include <tpl_dynArray.H>
Include dependency graph for dispatcher_example.C:

Go to the source code of this file.

Namespaces

namespace  Calculator
 Basic arithmetic operations for the calculator.
 

Functions

void print_header (const string &title)
 
void print_subheader (const string &subtitle)
 
double Calculator::add (double a, double b)
 
double Calculator::sub (double a, double b)
 
double Calculator::mul (double a, double b)
 
double Calculator::div (double a, double b)
 
double Calculator::mod (double a, double b)
 
double Calculator::pow (double a, double b)
 
void demo_calculator ()
 
void demo_text_processor ()
 
void demo_regions_menu ()
 
void demo_state_machine ()
 
void demo_hash_dispatcher ()
 
void demo_variadic_dispatcher ()
 
int main ()
 

Detailed Description

Dispatcher pattern in Aleph-w (ah-dispatcher.H) with tree and hash backends.

Overview

This example demonstrates Aleph-w's dispatcher containers: maps from keys (typically std::string) to callables (functions, lambdas, functors) enabling command-style dispatch without long if/else chains.

It includes multiple demos (calculator, text transforms, menu, state machine) and compares tree-backed vs hash-backed dispatch.

Data model used by this example

  • Keys: std::string
  • Values: callables such as std::function<...> or function pointers

Algorithms and Aleph-w API

Containers shown:

Typical operations:

  • registration: disp[key] = callable;
  • lookup/execute: if (disp.has(key)) disp[key](...);

Complexity

Let n be the number of registered commands.

  • Tree-backed dispatcher: lookup/insert O(log n)
  • Hash-backed dispatcher: lookup/insert O(1) average

Usage

./dispatcher_example

This example has no command-line options; it runs all demos.

Pitfalls and edge cases

  • If you use operator[] to execute, ensure the key exists or use has() first.
  • Hash dispatch has no ordering; tree dispatch preserves key order.

References / see also

Author
Leandro Rabindranath León

Definition in file dispatcher_example.C.

Function Documentation

◆ demo_calculator()

◆ demo_hash_dispatcher()

void demo_hash_dispatcher ( )

◆ demo_regions_menu()

void demo_regions_menu ( )

◆ demo_state_machine()

void demo_state_machine ( )

Definition at line 356 of file dispatcher_example.C.

References Aleph::DynList< T >::insert(), log(), Aleph::maps(), and print_header().

Referenced by main().

◆ demo_text_processor()

◆ demo_variadic_dispatcher()

void demo_variadic_dispatcher ( )

◆ main()

◆ print_header()

void print_header ( const string &  title)

Definition at line 108 of file dispatcher_example.C.

References Aleph::maps().

◆ print_subheader()

void print_subheader ( const string &  subtitle)