18 template<
typename result_t>
21 template<
typename result_t,
typename ...arguments_t>
22 class delegate<result_t(arguments_t...)>;
55 template<
typename result_t>
58 std::vector<std::function<result_t()>>
functions;
65 [[nodiscard]]
auto async_state()
const noexcept ->
xtd::any_object override;
67 [[nodiscard]]
auto completed_synchronously()
const noexcept ->
bool override;
68 [[nodiscard]]
auto is_completed()
const noexcept ->
bool override;
91 delegate& operator =(
const delegate&
other) {*data_ = *
other.data_;
return *
this;}
101 template<
class object1_t,
typename object2_t>
102 delegate(
const object1_t&
object, result_t(object2_t::*method)()
const) noexcept {data_->functions.push_back(
function_t(std::bind(method,
const_cast<object1_t*
>(&
object))));}
106 template<
class object1_t,
typename object2_t>
107 delegate(
const object1_t&
object, result_t(object2_t::*method)()) noexcept {data_->functions.push_back(
function_t(std::bind(method,
const_cast<object1_t*
>(&
object))));}
115 [[nodiscard]]
auto count() const noexcept ->
xtd::
usize {
return data_->functions.size();}
134 auto clear() ->
void {data_->functions.clear();}
181 if (data_->functions.size() !=
other.data_->functions.size())
return false;
182 for (
auto i =
xtd::usize {0};
i < data_->functions.size();
i++)
183 if (!are_equals(data_->functions[
i],
other.data_->functions[
i]))
return false;
199 for (
auto function :
delegate.data_->functions)
200 result.data_->functions.push_back(function);
217 auto result = source;
218 std::for_each(value.data_->functions.begin(), value.data_->functions.end(), [&](
const auto& function) {
219 auto iterator = std::find_if(result.data_->functions.rbegin(), result.data_->functions.rend(), [&](const auto& item) {return are_equals(item, function);});
220 if (
iterator != result.data_->functions.rend()) result.data_->functions.erase((
iterator + 1).base());
232 for (
const function_t& function : value.data_->functions) {
233 if (find(result.data_->functions.begin(), result.data_->functions.end(), function) != result.data_->functions.end()) {
234 for (
typename function_collection::reverse_iterator
iterator = result.data_->functions.rbegin();
iterator != result.data_->functions.rend(); ++
iterator) {
235 if (are_equals(*
iterator, function))
236 result.data_->functions.erase((
iterator + 1).base());
251 if (data_->functions.size() == 0) {
252 if constexpr(std::is_void_v<result_t>)
return;
253 else if constexpr(std::is_reference_v<result_t>) {
254 using underlying_t = std::remove_reference_t<result_t>;
255 if constexpr(std::is_const_v<underlying_t>) {
256 static const underlying_t empty_value{};
259 static underlying_t empty_value{};
262 }
else return result_t{};
265 for (
auto i =
xtd::usize {0};
i < data_->functions.size() - 1;
i++) {
267 data_->functions[
i]();
270 return data_->functions.back()();
273 auto operator =(
const function_t& function)
noexcept -> delegate& {
274 data_->functions.clear();
275 data_->functions.push_back(function);
281 auto operator +(
const delegate&
other)
const noexcept -> delegate {
282 delegate result = *
this;
287 auto operator +(
const function_t& function)
const noexcept -> delegate {
288 delegate result = *
this;
294 auto operator +(fn_t function)
const noexcept -> delegate {
295 delegate result = *
this;
300 auto operator +=(
const delegate&
other)
noexcept -> delegate& {
301 *
this = delegate::combine(*
this,
other);
305 auto operator +=(
const function_t& function)
noexcept -> delegate& {
306 *
this = delegate::combine(*
this, delegate(function));
311 auto operator +=(fn_t function)
noexcept -> delegate& {
312 *
this = delegate::combine(*
this, delegate(function));
316 auto operator -(
const delegate&
other)
const noexcept -> delegate {
317 delegate result = *
this;
322 auto operator -(
const function_t& function)
const noexcept -> delegate {
323 delegate result = *
this;
329 auto operator -(fn_t function)
const noexcept -> delegate {
330 delegate result = *
this;
335 auto operator -=(
const delegate&
other)
noexcept -> delegate& {
336 *
this = delegate::remove(*
this,
other);
340 auto operator -=(
const function_t& function)
noexcept -> delegate& {
341 *
this = delegate::remove(*
this, delegate(function));
346 auto operator -=(fn_t function)
noexcept -> delegate& {
347 *
this = delegate::remove(*
this, delegate(function));
353 [[nodiscard]]
static auto are_equals(
const std::function<result_t()>& fct1,
const std::function<result_t()>& fct2)
noexcept ->
bool {
return fct1.target_type() == fct2.target_type() && (fct1.template target<result_t(*)()>() == fct2.template target<result_t(*)()>() || *fct1.template target<result_t(*)()>() == *fct2.template target<result_t(*)()>());}
354 [[nodiscard]]
static auto find(
typename function_collection::const_iterator
begin,
typename function_collection::const_iterator
end,
const function_t& function)
noexcept ->
typename function_collection::const_iterator {
355 auto iterator = std::find_if(
begin,
end, [&](
const auto& item) {
return are_equals(item, function);});
377 template<
class result_t,
typename ...arguments_t>
381 std::vector<std::function<result_t(arguments_t...)>>
functions;
388 [[nodiscard]]
auto async_state()
const noexcept ->
xtd::any_object override;
390 [[nodiscard]]
auto completed_synchronously()
const noexcept ->
bool override;
391 [[nodiscard]]
auto is_completed()
const noexcept ->
bool override;
418 delegate& operator =(
const delegate& other) {*data_ = *
other.data_;
return *
this;}
419 delegate(
const delegate<result_t()>& delegate)
noexcept {data_->no_arguments_functions = delegate.functions();}
427 delegate(
const no_arguments_function_t& function)
noexcept {data_->no_arguments_functions.push_back(function);}
433 template<
class object1_t,
typename object2_t>
434 delegate(
const object1_t&
object, result_t(object2_t::*method)()
const) noexcept {data_->functions.push_back(
function_t(std::bind(method,
const_cast<object1_t*
>(&
object))));}
439 template<
class object1_t,
typename object2_t>
440 delegate(
const object1_t&
object, result_t(object2_t::*method)()) noexcept {data_->functions.push_back(
function_t(std::bind(method,
const_cast<object1_t*
>(&
object))));}
444 template<
class object1_t,
typename object2_t,
typename... args_t>
445 delegate(
const object1_t&
object, result_t(object2_t::*method)(args_t...)
const) noexcept {data_->functions.push_back(function_t(std::bind_front(method,
const_cast<object1_t*
>(&
object))));}
447 template<
class object1_t,
typename object2_t,
typename... args_t>
448 delegate(
const object1_t&
object, result_t(object2_t::*method)(args_t...)) noexcept {data_->functions.push_back(function_t(std::bind_front(method,
const_cast<object1_t*
>(&
object))));}
456 [[nodiscard]]
auto count() const noexcept ->
xtd::
usize {
return data_->functions.size() + data_->no_arguments_functions.size();}
516 auto invoke(arguments_t&&... arguments)
const -> result_t {
return operator()(arguments...);}
525 [[nodiscard]]
auto equals(
const delegate& other)
const noexcept ->
bool override {
526 if (data_->functions.size() !=
other.data_->functions.size() || data_->no_arguments_functions.size() !=
other.data_->no_arguments_functions.size())
return false;
527 for (
auto i =
xtd::usize {0};
i < data_->no_arguments_functions.size();
i++)
528 if (!are_equals(data_->no_arguments_functions[
i],
other.data_->no_arguments_functions[
i]))
return false;
529 for (
auto i =
xtd::usize {0};
i < data_->functions.size();
i++)
530 if (!are_equals(data_->functions[
i],
other.data_->functions[
i]))
return false;
546 for (
auto function :
delegate.data_->no_arguments_functions)
547 result.data_->no_arguments_functions.push_back(function);
548 for (
auto function :
delegate.data_->functions)
549 result.data_->functions.push_back(function);
567 auto result = source;
568 std::for_each(value.data_->no_arguments_functions.begin(), value.data_->no_arguments_functions.end(), [&](
const auto& no_arguments_function) {
569 auto iterator = std::find_if(result.data_->no_arguments_functions.rbegin(), result.data_->no_arguments_functions.rend(), [&](const auto& item) {return are_equals(item, no_arguments_function);});
570 if (
iterator != result.data_->no_arguments_functions.rend()) result.data_->no_arguments_functions.erase((
iterator + 1).base());
573 std::for_each(value.data_->functions.begin(), value.data_->functions.end(), [&](
const auto& function) {
574 auto iterator = std::find_if(result.data_->functions.rbegin(), result.data_->functions.rend(), [&](const auto& item) {return are_equals(item, function);});
575 if (
iterator != result.data_->functions.rend()) result.data_->functions.erase((
iterator + 1).base());
586 auto result = source;
587 for (
auto function : value.data_->no_arguments_functions) {
588 if (find(result.data_->no_arguments_functions.begin(), result.data_->no_arguments_functions.end(), function) != result.data_->no_arguments_functions.end()) {
589 for (
typename function_collection::reverse_iterator
iterator = result.data_->no_arguments_functions.rbegin();
iterator != result.data_->no_arguments_functions.rend(); ++
iterator) {
590 if (are_equals(*
iterator, function))
591 result.data_->no_arguments_functions.erase((
iterator + 1).base());
596 for (
auto function : value.data_->functions) {
597 if (find(result.data_->functions.begin(), result.data_->functions.end(), function) != result.data_->functions.end()) {
599 if (are_equals(*
iterator, function))
600 result.data_->functions.erase((
iterator + 1).base());
614 auto operator()(arguments_t... arguments)
const -> result_t {
615 if (data_->no_arguments_functions.size() == 0 && data_->functions.size() == 0) {
616 if constexpr(std::is_void_v<result_t>)
return;
617 else if constexpr(std::is_reference_v<result_t>) {
618 using underlying_t = std::remove_reference_t<result_t>;
619 if constexpr(std::is_const_v<underlying_t>) {
620 static const underlying_t empty_value{};
623 static underlying_t empty_value{};
626 }
else return result_t{};
629 if (data_->no_arguments_functions.size()) {
630 for (
auto i =
xtd::usize {0};
i < data_->no_arguments_functions.size() - (data_->functions.size() == 0 ? 1 : 0);
i++) {
632 data_->no_arguments_functions[
i]();
635 if (data_->functions.size() == 0) {
637 return data_->no_arguments_functions.back()();
641 for (
auto i =
xtd::usize {0};
i < data_->functions.size() - 1;
i++) {
643 data_->functions[
i](arguments...);
646 return data_->functions.back()(arguments...);
651 template<
class type_t>
652 auto operator =(
const type_t& function)
noexcept -> delegate& {
653 data_->no_arguments_functions.clear();
654 data_->functions.clear();
655 data_->functions.push_back(function_t(function));
659 auto operator =(
const function_t& function)
noexcept -> delegate& {
660 data_->no_arguments_functions.clear();
661 data_->functions.clear();
662 data_->functions.push_back(function);
666 auto operator =(
const no_arguments_function_t& function)
noexcept -> delegate& {
667 data_->no_arguments_functions.clear();
668 data_->functions.clear();
669 data_->no_arguments_functions.push_back(function);
673 auto operator +(
const delegate& other)
const noexcept -> delegate {
674 delegate result = *
this;
679 auto operator +(
const no_arguments_function_t& function)
const noexcept -> delegate {
680 delegate result = *
this;
685 auto operator +(
const function_t& function)
const noexcept -> delegate {
686 delegate result = *
this;
692 auto operator +(fn_t function)
const noexcept -> delegate {
693 delegate result = *
this;
698 auto operator +=(
const delegate& other)
noexcept -> delegate& {
699 *
this = delegate::combine(*
this, other);
703 auto operator +=(
const no_arguments_function_t& function)
noexcept -> delegate& {
704 *
this = delegate::combine(*
this, delegate(function));
708 auto operator +=(
const function_t& function)
noexcept -> delegate& {
709 *
this = delegate::combine(*
this, delegate(function));
714 auto operator +=(fn_t function)
noexcept -> delegate& {
715 *
this = delegate::combine(*
this, delegate(function));
719 auto operator -(
const delegate& other)
const noexcept -> delegate {
720 delegate result = *
this;
725 auto operator -(
const no_arguments_function_t& function)
const noexcept -> delegate {
726 delegate result = *
this;
731 auto operator -(
const function_t& function)
const noexcept -> delegate {
732 delegate result = *
this;
738 auto operator -(fn_t function)
const noexcept -> delegate {
739 delegate result = *
this;
744 auto operator -=(
const delegate& other)
noexcept -> delegate& {
745 *
this = delegate::remove(*
this, other);
749 auto operator -=(
const no_arguments_function_t& function)
noexcept -> delegate& {
750 *
this = delegate::remove(*
this, delegate(function));
754 auto operator -=(
const function_t& function)
noexcept -> delegate& {
755 *
this = delegate::remove(*
this, delegate(function));
760 auto operator -=(fn_t function)
noexcept -> delegate& {
761 *
this = delegate::remove(*
this, delegate(function));
767 struct delegate_async_state;
769 [[nodiscard]]
static auto are_equals(
const std::function<result_t(arguments_t...)>& fct1,
const std::function<result_t(arguments_t...)>& fct2)
noexcept ->
bool {
770 return fct1.target_type() == fct2.target_type() && (fct1.template target<result_t(*)(arguments_t...)>() == fct2.template target<result_t(*)(arguments_t...)>() || *fct1.template target<result_t(*)(arguments_t...)>() == *fct2.template target<result_t(*)(arguments_t...)>());
773 [[nodiscard]]
static auto are_equals(
const std::function<result_t()>& fct1,
const std::function<result_t()>& fct2)
noexcept ->
bool {
774 return fct1.target_type() == fct2.target_type() && (fct1.template target<result_t(*)()>() == fct2.template target<result_t(*)()>() || *fct1.template target<result_t(*)()>() == *fct2.template target<result_t(*)()>());
777 [[nodiscard]]
static auto find(
typename no_arguments_function_collection::const_iterator begin,
typename no_arguments_function_collection::const_iterator end,
const no_arguments_function_t& function)
noexcept ->
typename no_arguments_function_collection::const_iterator {
778 auto iterator = std::find_if(begin, end, [&](
const auto& item) {
return are_equals(item, function);});
779 if (iterator != end)
return iterator;
783 [[nodiscard]]
static auto find(
typename function_collection::const_iterator begin,
typename function_collection::const_iterator end,
const function_t& function)
noexcept ->
typename function_collection::const_iterator {
784 auto iterator = std::find_if(begin, end, [&](
const auto& item) {
return are_equals(item, function);});
785 if (iterator != end)
return iterator;
Contains xtd::any type and std::bad_any_cast exception.
Contains xtd::argument_null_exception exception.
Contains xtd::async_result alias.
Represent a polymorphic wrapper capable of holding any type.
Definition any_object.hpp:29
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
auto end_invoke(async_result async) -> result_t
Retrieves the return value of the asynchronous operation represented by the async_result_invoke passe...
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 ...
Definition delegate.hpp:216
static auto combine(const delegate &a, const delegate &b) noexcept -> delegate
Concatenates the invocation lists of two delegates.
Definition delegate.hpp:209
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...
auto operator()() const -> result_t
invokes the method represented by the current delegate.
Definition delegate.hpp:250
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.
Definition delegate.hpp:107
auto begin_invoke() -> async_result
Executes the method represented by the current delegate asynchronously on the thread that the control...
std::vector< function_t > function_collection
Represents the function collection type.
Definition delegate.hpp:80
auto functions() const -> const function_collection &
Gets the delegates array.
Definition delegate.hpp:119
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 dele...
Definition delegate.hpp:230
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.
Definition delegate.hpp:102
auto equals(const object &obj) const noexcept -> bool override
Determines whether this instance and another specified delegateType object have the same value.
Definition delegate.hpp:176
auto equals(const delegate &other) const noexcept -> bool override
Determines whether this instance and another specified delegateType object have the same value.
Definition delegate.hpp:180
delegate()=default
Initializes an empty delegate.
auto clear() -> void
Clear delegates array.
Definition delegate.hpp:134
auto is_empty() const noexcept -> bool
Return if the delegate is empty.
Definition delegate.hpp:123
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...
delegate(const function_t &function) noexcept
Initializes a delegate that invokes the specified instance method.
Definition delegate.hpp:96
auto count() const noexcept -> xtd::usize
Return the size of invocation list.
Definition delegate.hpp:115
static auto combine(const xtd::array< delegate > &delegates) noexcept -> delegate
Concatenates the invocation lists of an array of delegates.
Definition delegate.hpp:196
auto size() const noexcept -> xtd::usize
Return the size of invocation list.
Definition delegate.hpp:127
std::function< result_t()> function_t
Represents function type.
Definition delegate.hpp:78
auto invoke() const -> result_t
invokes the method represented by the current delegate.
Definition delegate.hpp:171
auto size() const noexcept -> xtd::usize
Return the size of invocation list.
Definition delegate.hpp:472
std::vector< function_t > function_collection
function_t Represents the function collection type.
Definition delegate.hpp:407
auto is_empty() const noexcept -> bool
Return if the delegate is empty.
Definition delegate.hpp:468
auto no_arguments_functions() const -> const no_arguments_function_collection &
Gets the no arguments delegates array.
Definition delegate.hpp:460
delegate()=default
Initializes an empty delegate.
auto begin_invoke(xtd::async_callback async_callback, const xtd::any_object &async_state, arguments_t &&... arguments) -> async_result
Executes the method represented by the current delegate asynchronously on the thread that the control...
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 dele...
Definition delegate.hpp:585
auto functions() const -> const function_collection &
Gets the delegates array.
Definition delegate.hpp:464
auto end_invoke(async_result async) -> result_t
Retrieves the return value of the asynchronous operation represented by the async_result_invoke passe...
auto begin_invoke(arguments_t &&... arguments) -> async_result
Executes the method represented by the current delegate asynchronously on the thread that the control...
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.
Definition delegate.hpp:440
auto equals(const delegate &other) const noexcept -> bool override
Determines whether this instance and another specified delegateType object have the same value.
Definition delegate.hpp:525
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 ...
Definition delegate.hpp:566
static auto combine(const delegate &a, const delegate &b) noexcept -> delegate
Concatenates the invocation lists of two delegates.
Definition delegate.hpp:559
auto operator()(arguments_t... arguments) const -> result_t
invokes the method represented by the current delegate.
Definition delegate.hpp:614
auto equals(const object &obj) const noexcept -> bool override
Determines whether this instance and another specified delegateType object have the same value.
Definition delegate.hpp:521
std::vector< no_arguments_function_t > no_arguments_function_collection
function_t Represents the no arguments function collection type.
Definition delegate.hpp:405
std::function< result_t(arguments_t...)> function_t
Represents function type.
Definition delegate.hpp:403
static auto combine(const xtd::array< delegate > &delegates) noexcept -> delegate
Concatenates the invocation lists of an array of delegates.
Definition delegate.hpp:543
auto count() const noexcept -> xtd::usize
Return the size of invocation list.
Definition delegate.hpp:456
delegate(const function_t &function) noexcept
Initializes a delegate that invokes the specified instance method.
Definition delegate.hpp:424
auto invoke(arguments_t &&... arguments) const -> result_t
invokes the method represented by the current delegate.
Definition delegate.hpp:516
auto begin_invoke(xtd::async_callback async_callback, arguments_t &&... arguments) -> async_result
Executes the method represented by the current delegate asynchronously on the thread that the control...
std::function< result_t()> no_arguments_function_t
Represents no arguments function type.
Definition delegate.hpp:401
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.
Definition delegate.hpp:434
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
Represents the status of an asynchronous operation.
Definition iasync_result.hpp:25
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:23
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
object()=default
Create a new instance of the ultimate base class object.
Encapsulates operating system specific objects that wait for exclusive access to shared resources.
Definition wait_handle.hpp:52
xtd::delegate< void(async_result ar)> async_callback
References a method to be called when a corresponding asynchronous operation completes.
Definition delegate.hpp:39
@ argument_null
The argument is null.
Definition exception_case.hpp:33
xtd::shared_ptr_object< type_t > sptr
The xtd::sptr object is a shared pointer.
Definition sptr.hpp:25
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
xtd::sptr< xtd::iasync_result > async_result
Represents the status of an asynchronous operation.
Definition async_result.hpp:19
sptr< type_t > new_sptr(args_t &&... args)
xtd::new_sptr operator creates a xtd::sptr object.
Definition new_sptr.hpp:24
bool is(xtd::any value)
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:484
@ other
The operating system is other.
Definition platform_id.hpp:60
@ a
The A key.
Definition console_key.hpp:88
@ i
The I key.
Definition console_key.hpp:104
@ b
The B key.
Definition console_key.hpp:90
Contains xtd::iequatable interface.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
const xtd::collections::generic::helpers::wrap_pointer_iterator< pointer > iterator
Represents the iterator of read_only_span value type.
Definition read_only_span.hpp:74
const_iterator begin() const
Returns an iterator to the beginning.
Definition read_only_span.hpp:183
const_iterator end() const
Returns an iterator to the end.
Definition read_only_span.hpp:213
Contains xtd::object class.
Contains xtd::object_ref alias.
Contains xtd::threading::thread_pool class.
Contains xtd::usize type.