xtd 0.2.0
xtd::event< object_t, handler_t > Class Template Reference

Definition

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

Represents an event.

Namespace
xtd
Library
xtd.core
Template Parameters
object_tthe owner object type of the event.
handler_tTypically a xtd::event_handler or inherited type.
Remarks
Only an object of type object_t can be create and invoke event.
For more info about events see Events guide.
Examples
The following example cshow hot to use event.
#include <xtd/as>
#include <xtd/console>
#include <xtd/environment>
#include <xtd/event>
#include <xtd/event_handler>
#include <xtd/ustring>
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);}
};
auto main()->int {
auto button1 = button {};
button1.text_changed += [](xtd::object & sender, const xtd::event_args & e) {
xtd::console::out << "text_changed [text=" << xtd::as<control>(sender).text() << "]" << xtd::environment::new_line;
};
button1.click += [] {
};
button1.text("button1");
// click simulation
button1.perform_click();
}
// This code produces the following output:
//
// text_changed [text=button1]
// click on button1
Examples:
event.cpp, numeric_text_box.cpp, and send_message_to_form.cpp.

Methods

bool is_empty () const noexcept
 Gets a value indicate if the event is empty. More...
 

Operators

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...
 
void clear () noexcept
 Clear event. More...
 

Member Function Documentation

◆ clear()

template<typename object_t, typename handler_t>
void xtd::event< object_t, handler_t >::clear ( )
inlinenoexcept

Clear event.

◆ 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: