xtd - Reference Guide  0.1.1
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
Public Member Functions | List of all members
xtd::event< object_t, handler_t > Class Template Reference

#include <event.h>

Definition

template<typename object_t, typename handler_t>
class xtd::event< object_t, handler_t >

Represents an event.

Namespace
xtd
Library
xtd.core

@paramt object_t the owner object type of the event. @paramt handler_t Typically a xtd::event_handler or inherited type.

Remarks
Only an object of type object_t can be create and invoke event.
Examples
The following example cshow hot to use event.
#include <xtd/xtd>
#include <iostream>
#include <string>
class control : public xtd::object {
public:
control() = default;
const xtd::ustring& text() const {return text_;}
void text(const xtd::ustring& text) {
if (text_ != text) {
text_ = text;
on_text_changed(xtd::event_args::empty);
}
}
protected:
void on_text_changed(const xtd::event_args& e) {text_changed(*this, e);}
private:
xtd::ustring text_;
};
class button : public control {
public:
button() = default;
void perform_click() {on_click(xtd::event_args::empty);}
protected:
virtual void on_click(const xtd::event_args& e) {click(*this, e);}
};
int main() {
button1.text_changed += [](xtd::object& sender, const xtd::event_args& e) {
std::cout << "text_changed [text=" << as<control>(sender).text() << "]" << std::endl;
};
button1.click += [] {
std::cout << "click on button1" << std::endl;
};
button1.text("button1");
// click simulation
button1.perform_click();
}
// This code produces the following output:
//
// text_changed [text=button1]
// click on button1
Represents the base class for classes that contain event data, and provides a value to use for events...
Definition: event_args.h:18
static const event_args empty
Provides a value to use with events that do not have event data.
Definition: event_args.h:31
Represents an event.
Definition: event.h:21
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition: object.h:26
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:48
@ control
The left or right CTRL modifier key.
@ e
The E key.
@ button1
The first button on the message box is the default button.
@ button
The appearance of a button.

Inherits handler_t.

Public Member Functions

bool is_empty () const noexcept
 Gets a value indicate if the event is empty. More...
 
handler_t & operator+= (const handler_t &handler) noexcept
 Adds an handler to the event. More...
 
handler_t & operator+= (const typename handler_t::function_t &function) noexcept
 Adds a function to the event. More...
 
template<typename fn_t >
handler_t & operator+= (fn_t function) noexcept
 Adds a function to the event. More...
 
handler_t & operator-= (const handler_t &handler) noexcept
 Removes an handler to the event. More...
 
handler_t & operator-= (const typename handler_t::function_t &function) noexcept
 Removes a function to the event. More...
 
template<typename fn_t >
handler_t & operator-= (fn_t function) noexcept
 Removes a function to the event. More...
 

Member Function Documentation

◆ is_empty()

template<typename object_t , typename handler_t >
bool xtd::event< object_t, handler_t >::is_empty ( ) const
inlinenoexcept

Gets a value indicate if the event is empty.

Returns
true if evcent does not contains functions; otherwise false.

◆ operator+=() [1/3]

template<typename object_t , typename handler_t >
handler_t& xtd::event< object_t, handler_t >::operator+= ( const handler_t &  handler)
inlinenoexcept

Adds an handler to the event.

Parameters
handlerHandler to add.
Returns
The current event instance.

◆ operator+=() [2/3]

template<typename object_t , typename handler_t >
handler_t& xtd::event< object_t, handler_t >::operator+= ( const typename handler_t::function_t &  function)
inlinenoexcept

Adds a function to the event.

Parameters
handlerFunction to add.
Returns
The current event instance.

◆ operator+=() [3/3]

template<typename object_t , typename handler_t >
template<typename fn_t >
handler_t& xtd::event< object_t, handler_t >::operator+= ( fn_t  function)
inlinenoexcept

Adds a function to the event.

Parameters
functionFunction to add.
Returns
The current event instance.

◆ operator-=() [1/3]

template<typename object_t , typename handler_t >
handler_t& xtd::event< object_t, handler_t >::operator-= ( const handler_t &  handler)
inlinenoexcept

Removes an handler to the event.

Parameters
handlerHandler to remove.
Returns
The current event instance.

◆ operator-=() [2/3]

template<typename object_t , typename handler_t >
handler_t& xtd::event< object_t, handler_t >::operator-= ( const typename handler_t::function_t &  function)
inlinenoexcept

Removes a function to the event.

Parameters
functionFunction to remove.
Returns
The current event instance.

◆ operator-=() [3/3]

template<typename object_t , typename handler_t >
template<typename fn_t >
handler_t& xtd::event< object_t, handler_t >::operator-= ( fn_t  function)
inlinenoexcept

Removes a function to the event.

Parameters
functionFunction to remove.
Returns
The current event instance.

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