Difference between revisions of "neoGFX Event System"

From neoGFX
Jump to: navigation, search
Line 1: Line 1:
 
The ''neoGFX'' event system is both modern and simple and is an improvement over traditional signals and slots.
 
The ''neoGFX'' event system is both modern and simple and is an improvement over traditional signals and slots.
  
To create an event handler can simply use a lambda expression thus:
+
To create an event handler simply use a lambda expression thus:
  
 
  <nowiki>
 
  <nowiki>
Line 7: Line 7:
 
</nowiki>
 
</nowiki>
  
If you want automatic event de-registration (traditional role of a "slot"):
+
If automatic event handler de-registration (traditional role of a "slot") is wanted:
  
 
  <nowiki>
 
  <nowiki>
Line 24: Line 24:
 
</nowiki>
 
</nowiki>
  
To define your own events and trigger them:
+
For defining events and triggering them:
  
 
  <nowiki>
 
  <nowiki>

Revision as of 18:24, 3 May 2018

The neoGFX event system is both modern and simple and is an improvement over traditional signals and slots.

To create an event handler simply use a lambda expression thus:

button1.clicked([](){ /* ... code ... */ });

If automatic event handler de-registration (traditional role of a "slot") is wanted:

neogfx::sink s;
s += button1.clicked([](){ /* ... code ... */ });

When 's' is destroyed any associated event registrations are de-registered automatically. Sink objects can be on the stack, member variables or the more traditional slot-like base class sub-object.

The event system is fully multi-threaded. If you want to handle the event in the same thread that is emitting the event rather than the thread that created the event handler then one simply uses a "thread snake", ~~~~, to make it obvious that the lambda is being executed in a thread that may be different to that which is running the surrounding code:

/* ... code ... */
~~~~machine.started([](){ /* ... code ... */ });
/* ... code ... */

For defining events and triggering them:

class foo
{
public:
    neogfx::event<int> wibble;
public:
    void something()
    {
        wibble.trigger(42); // by default a synchronous trigger
        wibble.sync_trigger(42); // synchronous trigger
        wibble.async_trigger(42); // asynchronous trigger
    }
};