xtd 0.2.0
Loading...
Searching...
No Matches
xtd::delegate< result_t()> Class Template Reference
Inheritance diagram for xtd::delegate< result_t()>:
xtd::object xtd::iequatable< delegate< result_t()> > xtd::interface xtd::extensions::equality_operators< delegate< result_t()>, iequatable< delegate< result_t()> > >

Definition

template<typename result_t>
class xtd::delegate< result_t()>

Represents a delegate, which is a data structure that refers to a static method or to a class instance and an instance method of that class.

Header
#include <xtd/delegate>
Namespace
xtd
Library
xtd.core
Remarks
For more info about delegates see Delegates guide.
Examples
The following example shows how to define a delegate named my Method delegate. Instances of this delegate are created for an instance method and a static method of the nested mySampleClass class. The delegate for the instance method requires an instance of mySampleClass. The mySampleClass instance is saved in a variable named mySC.
#include <xtd/xtd>
auto goodbye(const string& s) -> void {
console::write_line("Goodbye {}", s);
}
class object {
public:
auto hello(const string& s) -> void {
console::write_line("Hello {}", s);
}
};
auto main() -> int {
using example_function = delegate<void(const string&)>;
auto instance = ::object {};
auto str = string {"World"};
//equivanet to : example_function f = {std::bind(&::object::hello, &instance, std::placeholders::_1)};
auto f = example_function {instance, &::object::hello};
// equivalent to : instance.hello(str)
f(str);
f = goodbye;
// equivalent to : goodbye(str)
f(str);
return 0;
}
// This code produces the following output :
//
// Hello, World
// Goodbye, World
static auto write_line() -> void
Writes the current line terminator to the standard output stream using the specified format informati...
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
@ f
The F key.
Definition console_key.hpp:98

Public Aliases

using function_t
 Represents function type.
using function_collection
 Represents the function collection type.

Public Constructors

 delegate ()=default
 Initializes an empty delegate.
 delegate (const function_t &function) noexcept
 Initializes a delegate that invokes the specified instance method.
template<class object1_t, typename object2_t>
 delegate (const object1_t &object, result_t(object2_t::*method)() const) noexcept
 Initializes a delegate that invokes the specified instance method on the specified class instance.
template<class object1_t, typename object2_t>
 delegate (const object1_t &object, result_t(object2_t::*method)()) noexcept
 Initializes a delegate that invokes the specified instance method on the specified class instance.

Public Properties

auto count () const noexcept -> xtd::usize
 Return the size of invocation list.
auto functions () const -> const function_collection &
 Gets the delegates array.
auto is_empty () const noexcept -> bool
 Return if the delegate is empty.
auto size () const noexcept -> xtd::usize
 Return the size of invocation list.

Public Methods

auto clear () -> void
 Clear delegates array.
auto begin_invoke () -> async_result
 Executes the method represented by the current delegate asynchronously on the thread that the control's underlying handle was created on.
auto begin_invoke (xtd::async_callback async_callback) -> async_result
 Executes the method represented by the current delegate asynchronously on the thread that the control's underlying handle was created on.
auto begin_invoke (xtd::async_callback async_callback, const xtd::any_object &async_state) -> async_result
 Executes the method represented by the current delegate asynchronously on the thread that the control's underlying handle was created on.
auto end_invoke (async_result async) -> result_t
 Retrieves the return value of the asynchronous operation represented by the async_result_invoke passed.
auto invoke () const -> result_t
 invokes the method represented by the current delegate.
auto equals (const object &obj) const noexcept -> bool override
 Determines whether this instance and another specified delegateType object have the same value.
auto equals (const delegate &other) const noexcept -> bool override
 Determines whether this instance and another specified delegateType object have the same value.

Operators

auto operator() () const -> result_t
 invokes the method represented by the current delegate.
auto operator= (const function_t &function) noexcept -> delegate &

Public Static Methods

static auto combine (const xtd::array< delegate > &delegates) noexcept -> delegate
 Concatenates the invocation lists of an array of delegates.
static auto combine (const delegate &a, const delegate &b) noexcept -> delegate
 Concatenates the invocation lists of two delegates.
static auto remove (const delegate &source, const delegate &value) noexcept -> delegate
 removes the last occurrence of the invocation list of a delegate from the invocation list of another delegate.
static auto remove_all (const delegate &source, const delegate &value) noexcept -> delegate
 removes all occurrences of the invocation list of a delegate from the invocation list of another delegate.

Additional Inherited Members

 object ()=default
 Create a new instance of the ultimate base class object.
virtual xtd::usize get_hash_code () const noexcept
 Serves as a hash function for a particular type.
virtual type_object get_type () const noexcept
 Gets the type of the current instance.
template<typename object_t>
xtd::unique_ptr_object< object_t > memberwise_clone () const
 Creates a shallow copy of the current object.
virtual xtd::string to_string () const
 Returns a xtd::string that represents the current object.
virtual bool equals (const delegate< result_t()> &) const noexcept=0
 Indicates whether the current object is equal to another object of the same type.
template<typename object_a_t, typename object_b_t>
static bool equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are considered equal.
template<typename object_a_t, typename object_b_t>
static bool reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are the same instance.

Member Typedef Documentation

◆ function_t

template<typename result_t>
using xtd::delegate< result_t()>::function_t

Represents function type.

◆ function_collection

template<typename result_t>
using xtd::delegate< result_t()>::function_collection

Represents the function collection type.

Constructor & Destructor Documentation

◆ delegate() [1/4]

template<typename result_t>
xtd::delegate< result_t()>::delegate ( )
default

Initializes an empty delegate.

◆ delegate() [2/4]

template<typename result_t>
xtd::delegate< result_t()>::delegate ( const function_t & function)
inlinenoexcept

Initializes a delegate that invokes the specified instance method.

Parameters
functionthe method instance.

◆ delegate() [3/4]

template<typename result_t>
template<class object1_t, typename object2_t>
xtd::delegate< result_t()>::delegate ( const object1_t & object,
result_t(object2_t::* method )() const )
inlinenoexcept

Initializes a delegate that invokes the specified instance method on the specified class instance.

Parameters
objectthe class instance.
functionthe method instance.

◆ delegate() [4/4]

template<typename result_t>
template<class object1_t, typename object2_t>
xtd::delegate< result_t()>::delegate ( const object1_t & object,
result_t(object2_t::* method )() )
inlinenoexcept

Initializes a delegate that invokes the specified instance method on the specified class instance.

Parameters
objectthe class instance.
functionthe method instance.

Member Function Documentation

◆ count()

template<typename result_t>
auto xtd::delegate< result_t()>::count ( ) const -> xtd::usize
inlinenodiscardnoexcept

Return the size of invocation list.

Returns
Return the size of invocation list.

◆ functions()

template<typename result_t>
auto xtd::delegate< result_t()>::functions ( ) const -> const function_collection &
inlinenodiscard

Gets the delegates array.

Returns
The delegates array.

◆ is_empty()

template<typename result_t>
auto xtd::delegate< result_t()>::is_empty ( ) const -> bool
inlinenodiscardnoexcept

Return if the delegate is empty.

Returns
bool Return true if delegate is empty; otherwise false.

◆ size()

template<typename result_t>
auto xtd::delegate< result_t()>::size ( ) const -> xtd::usize
inlinenodiscardnoexcept

Return the size of invocation list.

Returns
Return the size of invocation list.

◆ clear()

template<typename result_t>
auto xtd::delegate< result_t()>::clear ( ) -> void
inline

Clear delegates array.

◆ begin_invoke() [1/3]

template<typename result_t>
auto xtd::delegate< result_t()>::begin_invoke ( ) -> async_result
nodiscard

Executes the method represented by the current delegate asynchronously on the thread that the control's underlying handle was created on.

Returns
An async_result_invoke that represents the result of the begin_invoke operation.
Examples
The following examples shows hot tu use xtd::delegate::begin_invoke, xtd::delegate::end_invoke, xtd::delegate::invoke methods.
#include <xtd/xtd>
auto main() -> int {
auto d1 = delegate<int(const string&)> {[](auto&& name) {
console::write_line("(Thread {}) Hello {}!", thread::current_thread().managed_thread_id(), name);
return name == "xtd" ? 42 : 24;
}};
auto result = d1.begin_invoke("Gammasoft");
console::write_line("return = {}", d1.invoke("xtd"));
console::write_line("return = {}", d1.end_invoke(result));
}
// This code produces the following output :
//
// (Thread 1) Hello xtd!
// return = 42
// (Thread 4) Hello Gammasoft!
// return = 24
delegate()=default
Initializes an empty delegate.
@ d1
The 1 key.
Definition console_key.hpp:70

◆ begin_invoke() [2/3]

template<typename result_t>
auto xtd::delegate< result_t()>::begin_invoke ( xtd::async_callback async_callback) -> async_result
nodiscard

Executes the method represented by the current delegate asynchronously on the thread that the control's underlying handle was created on.

Parameters
async_callbackThe asynchronous callback that will be called at the end of the invocation.
Returns
An async_result_invoke that represents the result of the begin_invoke operation.
Examples
The following examples shows hot tu use xtd::delegate::begin_invoke, xtd::delegate::end_invoke, xtd::delegate::invoke methods.
#include <xtd/xtd>
auto main() -> int {
auto d1 = delegate<int(const string&)> {[](auto&& name) {
console::write_line("(Thread {}) Hello {}!", thread::current_thread().managed_thread_id(), name);
return name == "xtd" ? 42 : 24;
}};
auto result = d1.begin_invoke("Gammasoft");
console::write_line("return = {}", d1.invoke("xtd"));
console::write_line("return = {}", d1.end_invoke(result));
}
// This code produces the following output :
//
// (Thread 1) Hello xtd!
// return = 42
// (Thread 4) Hello Gammasoft!
// return = 24

◆ begin_invoke() [3/3]

template<typename result_t>
auto xtd::delegate< result_t()>::begin_invoke ( xtd::async_callback async_callback,
const xtd::any_object & async_state ) -> async_result
nodiscard

Executes the method represented by the current delegate asynchronously on the thread that the control's underlying handle was created on.

Parameters
async_callbackThe asynchronous callback that will be called at the end of the invocation.
async_stateThe asynchronous state associated with the invocation.
Returns
An async_result_invoke that represents the result of the begin_invoke operation.
Examples
The following examples shows hot tu use xtd::delegate::begin_invoke, xtd::delegate::end_invoke, xtd::delegate::invoke methods.
#include <xtd/xtd>
auto main() -> int {
auto d1 = delegate<int(const string&)> {[](auto&& name) {
console::write_line("(Thread {}) Hello {}!", thread::current_thread().managed_thread_id(), name);
return name == "xtd" ? 42 : 24;
}};
auto result = d1.begin_invoke("Gammasoft");
console::write_line("return = {}", d1.invoke("xtd"));
console::write_line("return = {}", d1.end_invoke(result));
}
// This code produces the following output :
//
// (Thread 1) Hello xtd!
// return = 42
// (Thread 4) Hello Gammasoft!
// return = 24

◆ end_invoke()

template<typename result_t>
auto xtd::delegate< result_t()>::end_invoke ( async_result async) -> result_t

Retrieves the return value of the asynchronous operation represented by the async_result_invoke passed.

Parameters
asyncThe async_result_invoke that represents a specific invoke asynchronous operation, returned when calling begin_invoke.
Examples
The following examples shows hot tu use xtd::delegate::begin_invoke, xtd::delegate::end_invoke, xtd::delegate::invoke methods.
#include <xtd/xtd>
auto main() -> int {
auto d1 = delegate<int(const string&)> {[](auto&& name) {
console::write_line("(Thread {}) Hello {}!", thread::current_thread().managed_thread_id(), name);
return name == "xtd" ? 42 : 24;
}};
auto result = d1.begin_invoke("Gammasoft");
console::write_line("return = {}", d1.invoke("xtd"));
console::write_line("return = {}", d1.end_invoke(result));
}
// This code produces the following output :
//
// (Thread 1) Hello xtd!
// return = 42
// (Thread 4) Hello Gammasoft!
// return = 24

◆ invoke()

template<typename result_t>
auto xtd::delegate< result_t()>::invoke ( ) const -> result_t
inline

invokes the method represented by the current delegate.

Parameters
argumentsThe parameter list.
Returns
result_t The return value.
Examples
The following examples shows hot tu use xtd::delegate::begin_invoke, xtd::delegate::end_invoke, xtd::delegate::invoke methods.
#include <xtd/xtd>
auto main() -> int {
auto d1 = delegate<int(const string&)> {[](auto&& name) {
console::write_line("(Thread {}) Hello {}!", thread::current_thread().managed_thread_id(), name);
return name == "xtd" ? 42 : 24;
}};
auto result = d1.begin_invoke("Gammasoft");
console::write_line("return = {}", d1.invoke("xtd"));
console::write_line("return = {}", d1.end_invoke(result));
}
// This code produces the following output :
//
// (Thread 1) Hello xtd!
// return = 42
// (Thread 4) Hello Gammasoft!
// return = 24

◆ equals() [1/2]

template<typename result_t>
auto xtd::delegate< result_t()>::equals ( const object & obj) const -> bool
inlinenodiscardoverridevirtualnoexcept

Determines whether this instance and another specified delegateType object have the same value.

Parameters
objThe delegateType to compare.
Returns
bool true if the value of this instance is the same as the value of value; otherwise, false.

Reimplemented from xtd::object.

◆ equals() [2/2]

template<typename result_t>
auto xtd::delegate< result_t()>::equals ( const delegate< result_t()> & other) const -> bool
inlinenodiscardoverridenoexcept

Determines whether this instance and another specified delegateType object have the same value.

Parameters
otherThe delegateType to compare.
Returns
bool true if the value of this instance is the same as the value of value; otherwise, false.

◆ combine() [1/2]

template<typename result_t>
auto xtd::delegate< result_t()>::combine ( const xtd::array< delegate< result_t()> > & delegates) -> delegate
inlinestaticnodiscardnoexcept

Concatenates the invocation lists of an array of delegates.

Parameters
delegatesThe array of delegates to combine.
Returns
Delegate A new delegate with an invocation list that concatenates the invocation lists of the delegates in the delegates array. Returns null if delegates is null, if delegates contains zero elements, or if every entry in delegates is null.
Remarks
If the delegates array contains entries that are null, those entries are ignored.
The invocation list can contain duplicate entries; that is, entries that refer to the same method on the same object.

◆ combine() [2/2]

template<typename result_t>
auto xtd::delegate< result_t()>::combine ( const delegate< result_t()> & a,
const delegate< result_t()> & b ) -> delegate
inlinestaticnodiscardnoexcept

Concatenates the invocation lists of two delegates.

Parameters
aThe delegate whose invocation list comes first.
bThe delegate whose invocation list comes second.
Returns
A new delegate with an invocation list that concatenates the invocation lists of a and b in that order. Returns a if b is empty, returns b if a is a empty, and returns empty if both a and b are empty.
Remarks
The invocation list can contain duplicate entries; that is, entries that refer to the same method on the same object.

◆ remove()

template<typename result_t>
auto xtd::delegate< result_t()>::remove ( const delegate< result_t()> & source,
const delegate< result_t()> & value ) -> delegate
inlinestaticnodiscardnoexcept

removes the last occurrence of the invocation list of a delegate from the invocation list of another delegate.

Parameters
sourceThe delegate from which to remove the invocation list of value.
valueThe delegate that supplies the invocation list to remove from the invocation list of source.
Returns
A new delegate with an invocation list formed by taking the invocation list of source and removing the last occurrence of the invocation list of value, if the invocation list of value is found within the invocation list of source. Returns source if value is null or if the invocation list of value is not found within the invocation list of source. Returns a null reference if the invocation list of value is equal to the invocation list of source or if source is a null reference.
Remarks
If the invocation list of value matches a contiguous set of elements in the invocation list of source, then the invocation list of value is said to occur within the invocation list of source. If the invocation list of value occurs more than once in the invocation list of source, the last occurrence is removed.

◆ remove_all()

template<typename result_t>
auto xtd::delegate< result_t()>::remove_all ( const delegate< result_t()> & source,
const delegate< result_t()> & value ) -> delegate
inlinestaticnodiscardnoexcept

removes all occurrences of the invocation list of a delegate from the invocation list of another delegate.

Parameters
sourceThe delegate from which to remove the invocation list of value.
valueThe delegate that supplies the invocation list to remove from the invocation list of source.
Returns
A new delegate with an invocation list formed by taking the invocation list of source and removing all occurrences of the invocation list of value, if the invocation list of value is found within the invocation list of source. Returns source if value is null or if the invocation list of value is not found within the invocation list of source. Returns a null reference if the invocation list of value is equal to the invocation list of source, if source contains only a series of invocation lists that are equal to the invocation list of value, or if source is a null reference.
Remarks
If the invocation list of value matches a contiguous set of elements in the invocation list of source, then the invocation list of value is said to occur within the invocation list of source. If the invocation list of value occurs more than once in the invocation list of source, all occurrences are removed.

◆ operator()()

template<typename result_t>
auto xtd::delegate< result_t()>::operator() ( ) const -> result_t
inline

invokes the method represented by the current delegate.

Parameters
argumentsThe parameter list.
Returns
result_t The return value.

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