Last active
August 29, 2015 14:08
-
-
Save mntone/706816930b6e55734671 to your computer and use it in GitHub Desktop.
C++/CX でイベントをうまく自動で開放するラッパー
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MIT license | |
// Copyright (C) 2014 mntone. All rights reserved. | |
#pragma once | |
#include <functional> | |
namespace Mntone { namespace Nicola { namespace Utilities { | |
template<typename T> | |
class EventWrapper final | |
{ | |
public: | |
EventWrapper( | |
T^ sender, | |
::Windows::Foundation::EventRegistrationToken token, | |
::std::function<void( T^, ::Windows::Foundation::EventRegistrationToken )> removeHandlerFunc ) | |
: sender_( sender ) | |
, eventToken_( token ) | |
, removeHandlerFunc_( removeHandlerFunc ) | |
{ } | |
~EventWrapper() | |
{ | |
auto sender = sender_.Resolve<T>(); | |
if( sender ) | |
{ | |
removeHandlerFunc_( sender, eventToken_ ); | |
} | |
} | |
private: | |
::Platform::WeakReference sender_; | |
::Windows::Foundation::EventRegistrationToken eventToken_; | |
::std::function<void( T^, ::Windows::Foundation::EventRegistrationToken )> removeHandlerFunc_; | |
}; | |
} } } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment