xtd - Reference Guide  0.1.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
Public Types | Public Member Functions | Static Public Member Functions | List of all members
xtd::delegate< result_t(arguments_t...)> Class Template Reference

#include <delegate.h>

Definition

template<typename result_t, typename... arguments_t>
class xtd::delegate< result_t(arguments_t...)>

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

Namespace
xtd
Library
xtd.core
Examples
The following example shows how to define a delegate named myMethoddelegate. Instances of this delegate are created for an instance method && 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>
using namespace std;
using namespace xtd;
void goodbye(const ustring& s) {
console::write_line("Goodbye {}", s);
}
class object {
public:
void hello(const ustring& s) {
console::write_line("Hello {}", s);
}
};
int main() {
using example_function = delegate<void(const ustring&)>;
::object instance;
ustring str("World");
//example_function f = {instance, &::object::hello};
example_function f = {std::bind(&::object::hello, &instance, std::placeholders::_1)};
// 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

Public Types

using function_t = std::function< result_t(arguments_t...)>
 function_t pointer type More...
 
using no_arguments_function_t = std::function< result_t()>
 no_arguments_function_t pointer type More...
 

Public Member Functions

 delegate ()=default
 Initializes an empty delegate. More...
 
 delegate (const delegate &delegate) noexcept
 Initializes a delegate that invokes the specified delegate instance. More...
 
 delegate (const function_t &function) noexcept
 Initializes a delegate that invokes the specified instance method. More...
 
template<typename 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. More...
 
template<typename 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. More...
 
void clear ()
 Clear delegates array. More...
 
const std::vector< function_t > & functions () const
 Gets the delegates array. More...
 
result_t invoke (arguments_t... arguments) const
 invokes the method represented by the current delegate. More...
 
bool is_empty () const noexcept
 Return if the delegate is empty. More...
 
const std::vector< no_arguments_function_t > & no_arguments_functions () const
 Gets the no arguuments delegates array. More...
 
bool operator!= (const delegate &delegate) const
 Determines whether this instance and another specified delegateType object have the same value. More...
 
result_t operator() (arguments_t... arguments) const
 invokes the method represented by the current delegate. More...
 
bool operator== (const delegate &delegate) const noexcept
 Determines whether this instance and another specified delegateType object have the same value. More...
 
- Public Member Functions inherited from xtd::object
 object ()=default
 Create a new instance of the ultimate base class object. More...
 
virtual bool equals (const object &obj) const noexcept
 Determines whether the specified object is equal to the current object. More...
 
virtual size_t get_hash_code () const noexcept
 Serves as a hash function for a particular type. More...
 
template<typename object_t >
std::unique_ptr< object_t > memberwise_clone () const
 Gets the type of the current instance. More...
 
virtual xtd::ustring to_string () const noexcept
 Returns a std::string that represents the current object. More...
 

Static Public Member Functions

static delegate combine (const std::vector< delegate > &delegates) noexcept
 Concatenates the invocation lists of an array of delegates. More...
 
static delegate combine (const delegate &a, const delegate &b) noexcept
 Concatenates the invocation lists of two delegates. More...
 
static delegate remove (const delegate &source, const delegate &value) noexcept
 removes the last occurrence of the invocation list of a delegate from the invocation list of another delegate. More...
 
static delegate remove_all (const delegate &source, const delegate &value) noexcept
 removes all occurrences of the invocation list of a delegate from the invocation list of another delegate. More...
 
- Static Public Member Functions inherited from xtd::object
static bool equals (const object &object_a, const object &object_b) noexcept
 Determines whether the specified object instances are considered equal. More...
 
static bool reference_equals (const object &object_a, const object &object_b) noexcept
 Determines whether the specified object instances are the same instance. More...
 

Member Typedef Documentation

◆ function_t

template<typename result_t , typename... arguments_t>
using xtd::delegate< result_t(arguments_t...)>::function_t = std::function <result_t(arguments_t...)>

function_t pointer type

◆ no_arguments_function_t

template<typename result_t , typename... arguments_t>
using xtd::delegate< result_t(arguments_t...)>::no_arguments_function_t = std::function <result_t()>

no_arguments_function_t pointer type

Constructor & Destructor Documentation

◆ delegate() [1/5]

template<typename result_t , typename... arguments_t>
xtd::delegate< result_t(arguments_t...)>::delegate ( )
default

Initializes an empty delegate.

◆ delegate() [2/5]

template<typename result_t , typename... arguments_t>
xtd::delegate< result_t(arguments_t...)>::delegate ( const delegate< result_t(arguments_t...)> &  delegate)
inlinenoexcept

Initializes a delegate that invokes the specified delegate instance.

Parameters
delegateThe delegate instance.

◆ delegate() [3/5]

template<typename result_t , typename... arguments_t>
xtd::delegate< result_t(arguments_t...)>::delegate ( const function_t function)
inlinenoexcept

Initializes a delegate that invokes the specified instance method.

Parameters
functionthe method instance.

◆ delegate() [4/5]

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

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

Parameters
objectthe class instance.
functionthe method instance.

◆ delegate() [5/5]

template<typename result_t , typename... arguments_t>
template<typename object1_t , typename object2_t >
xtd::delegate< result_t(arguments_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

◆ clear()

template<typename result_t , typename... arguments_t>
void xtd::delegate< result_t(arguments_t...)>::clear ( )
inline

Clear delegates array.

◆ combine() [1/2]

template<typename result_t , typename... arguments_t>
static delegate xtd::delegate< result_t(arguments_t...)>::combine ( const std::vector< delegate< result_t(arguments_t...)> > &  delegates)
inlinestaticnoexcept

Concatenates the invocation lists of an array of delegates.

Parameters
delagatesThe array of delegates to combine.
Returns
Delegte 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, || 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 , typename... arguments_t>
static delegate xtd::delegate< result_t(arguments_t...)>::combine ( const delegate< result_t(arguments_t...)> &  a,
const delegate< result_t(arguments_t...)> &  b 
)
inlinestaticnoexcept

Concatenates the invocation lists of two delegates.

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

◆ functions()

template<typename result_t , typename... arguments_t>
const std::vector<function_t>& xtd::delegate< result_t(arguments_t...)>::functions ( ) const
inline

Gets the delegates array.

Returns
The delagetes array.

◆ invoke()

template<typename result_t , typename... arguments_t>
result_t xtd::delegate< result_t(arguments_t...)>::invoke ( arguments_t...  arguments) const
inline

invokes the method represented by the current delegate.

Parameters
argumentsThe paramter list.
Returns
result_t The return value.

◆ is_empty()

template<typename result_t , typename... arguments_t>
bool xtd::delegate< result_t(arguments_t...)>::is_empty ( ) const
inlinenoexcept

Return if the delegate is empty.

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

◆ no_arguments_functions()

template<typename result_t , typename... arguments_t>
const std::vector<no_arguments_function_t>& xtd::delegate< result_t(arguments_t...)>::no_arguments_functions ( ) const
inline

Gets the no arguuments delegates array.

Returns
The delagetes array.

◆ operator!=()

template<typename result_t , typename... arguments_t>
bool xtd::delegate< result_t(arguments_t...)>::operator!= ( const delegate< result_t(arguments_t...)> &  delegate) const
inline

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

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

◆ operator()()

template<typename result_t , typename... arguments_t>
result_t xtd::delegate< result_t(arguments_t...)>::operator() ( arguments_t...  arguments) const
inline

invokes the method represented by the current delegate.

Parameters
argumentsThe paramter list.
Returns
result_t The return value.

◆ operator==()

template<typename result_t , typename... arguments_t>
bool xtd::delegate< result_t(arguments_t...)>::operator== ( const delegate< result_t(arguments_t...)> &  delegate) const
inlinenoexcept

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

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

◆ remove()

template<typename result_t , typename... arguments_t>
static delegate xtd::delegate< result_t(arguments_t...)>::remove ( const delegate< result_t(arguments_t...)> &  source,
const delegate< result_t(arguments_t...)> &  value 
)
inlinestaticnoexcept

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
delegate 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 || if the invocation list of value is ! 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 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 , typename... arguments_t>
static delegate xtd::delegate< result_t(arguments_t...)>::remove_all ( const delegate< result_t(arguments_t...)> &  source,
const delegate< result_t(arguments_t...)> &  value 
)
inlinestaticnoexcept

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
delegate A new delegate with an invocation list formed by taking the invocation list of source && 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 || if the invocation list of value is ! 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, || 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.

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