xtd - Reference Guide  0.1.0
Modern c++17 framework to create console, gui and unit test applications on Windows, macOS and Linux.
xtd::delegate< result_t(arguments_t...)> Class Template Reference

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. 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. More...

#include <delegate.h>

Public Types

using function_t = std::function< result_t(arguments_t...)>
 
using no_arguments_function_t = std::function< result_t()>
 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 ()
 
const std::vector< function_t > & functions () const
 
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
 
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...
 
delegateoperator+= (const delegate &delegate) noexcept
 
delegateoperator+= (const no_arguments_function_t &function) noexcept
 
delegateoperator+= (const function_t &function) noexcept
 
template<typename fn_t >
delegateoperator+= (fn_t function) noexcept
 
delegateoperator-= (const delegate &delegate) noexcept
 
delegateoperator-= (const no_arguments_function_t &function) noexcept
 
delegateoperator-= (const function_t &function) noexcept
 
template<typename fn_t >
delegateoperator-= (fn_t function) noexcept
 
template<typename type_t >
delegateoperator= (const type_t &function) noexcept
 
delegateoperator= (const function_t &function) noexcept
 
delegateoperator= (const no_arguments_function_t &function) noexcept
 
bool operator== (const delegate &delegate) const noexcept
 Determines whether this instance and another specified delegateType object have the same value. 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...
 

Detailed Description

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. 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 string& s) {
console::write_line("Goodbye {}", s);
}
class object {
public:
void hello(const std::string& s) {
console::write_line("Hello {}", s);
}
};
int main() {
using example_function = delegate<void(const string&)>;
::object instance;
string 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

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