Skip to content

Commit

Permalink
Better handling of self-assignment.
Browse files Browse the repository at this point in the history
  • Loading branch information
Konard committed Apr 7, 2020
1 parent efee664 commit 76e233f
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions cpp/Platform.Delegates/Platform.Delegates.MulticastDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,10 @@ namespace Platform::Delegates
std::vector<DelegateType> callbacks;
std::mutex mutex;

void CopyCallbacks(MulticastDelegate &multicastDelegate)
void CopyCallbacks(MulticastDelegate &other)
{
if (&mutex == &multicastDelegate.mutex)
{
std::lock_guard lock(mutex);
callbacks = std::vector<DelegateType>(multicastDelegate.callbacks);
}
else
{
std::scoped_lock lock(mutex, multicastDelegate.mutex);
callbacks = std::vector<DelegateType>(multicastDelegate.callbacks);
}
std::scoped_lock lock(mutex, other.mutex);
callbacks = std::vector<DelegateType>(other.callbacks);
}

public:
Expand All @@ -64,9 +56,13 @@ namespace Platform::Delegates
CopyCallbacks(multicastDelegate);
}

void operator=(MulticastDelegate &&multicastDelegate) noexcept
void operator=(MulticastDelegate &&other) noexcept
{
CopyCallbacks(multicastDelegate);
if (this == &other)
{
return;
}
CopyCallbacks(other);
}

MulticastDelegate<ReturnType(Args...)> &operator+=(const DelegateType &callback)
Expand Down

0 comments on commit 76e233f

Please sign in to comment.