Skip to content

Instantly share code, notes, and snippets.

@mntone
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mntone/706816930b6e55734671 to your computer and use it in GitHub Desktop.
Save mntone/706816930b6e55734671 to your computer and use it in GitHub Desktop.
C++/CX でイベントをうまく自動で開放するラッパー
// 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