Listing 1: A usage example
1 // A global event. 2 class DestroyEvent : public Signal::Event {}; 3 4 class Employee 5 { ... 6 // Employee-specific events. 7 8 class EnterEvent : public Signal::Event {...}; 9 class LeaveEvent : public Signal::Event {...}; 10 11 void enter() 12 { ... 13 // Report an employee-entering-facility event. 14 Signal::emit(*this, EnterEvent()); 15 } 16 17 void leave() 18 { ... 19 // Report an employee-leaving-facility event. 20 Signal::emit(*this, LeaveEvent()); 21 } 22 23 ~Employee() 24 { ... 25 // Report an event of employee being fired. 26 Signal::emit(*this, DestroyEvent()); 27 } 28 }; 29 30 class PayrollOffice 31 { ... 32 // Callbacks. 33 void employee_enters( 34 const Employee::EnterEvent&, Employee&); 35 void employee_leaves( 36 const Employee::LeaveEvent&, Employee&); 37 }; 38 39 PayrollOffice::employee_enters( 40 const Employee::EnterEvent& ev, Employee* employee) 41 { 42 if (ev.is_late()) // Employee is late in. 43 { 44 remember_the_guy(employee); 45 } 46 else { ... } 47 } 48 49 PayrollOffice::employee_leaves( 50 const Employee::LeaveEvent& ev, Employee* employee) 51 { 52 if (ev.is_early()) // Employee is early out. 53 { 54 remember_the_guy(employee); 55 } 56 else { ... } 57 } 58 59 int main() 60 { 61 PayrollOffice po; 62 Employee john; 63 64 typedef PayrollOffice PO; 65 66 Signal::connect(john, po, &PO::employee_enters); 67 Signal::connect(john, po, &PO::employee_leaves); 68 69 // Registered for Employee::EnterEvent callbacks will be 70 // invoked in response to the employee entering facility. 71 72 john.enter(); 73 74 Signal::disconnect(john, po, &PO::employee_enters); 75 Signal::disconnect(john, po, &PO::employee_leaves); 76 } End of Listing