From de505bd96848d853b1808790953cb2c9d5c9a7fe Mon Sep 17 00:00:00 2001 From: JW Wang Date: Tue, 18 Aug 2015 11:11:29 +0800 Subject: [PATCH] Bug 1194112. Part 2 - small code refactoring to reduce typing. r=kinetik. --- dom/media/MediaEventSource.h | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/dom/media/MediaEventSource.h b/dom/media/MediaEventSource.h index c991691f8419..e915116ab913 100644 --- a/dom/media/MediaEventSource.h +++ b/dom/media/MediaEventSource.h @@ -76,7 +76,7 @@ struct EventTypeTraits { * Test if a method function or lambda accepts one or more arguments. */ template -class TakeArgs { +class TakeArgsHelper { template static FalseType test(void(C::*)(), int); template static FalseType test(void(C::*)() const, int); template static FalseType test(void(C::*)() volatile, int); @@ -87,6 +87,9 @@ public: typedef decltype(test(DeclVal(), 0)) Type; }; +template +struct TakeArgs : public TakeArgsHelper::Type {}; + template struct EventTarget; template <> @@ -156,7 +159,7 @@ public: // |F| takes one argument. template - typename EnableIf::Type::value, void>::Type + typename EnableIf::value, void>::Type Dispatch(const F& aFunc, T&& aEvent) { nsCOMPtr r = new R(mToken, aFunc, Forward(aEvent)); EventTarget::Dispatch(mTarget.get(), r.forget()); @@ -164,7 +167,7 @@ public: // |F| takes no arguments. Don't bother passing aEvent. template - typename EnableIf::Type::value, void>::Type + typename EnableIf::value, void>::Type Dispatch(const F& aFunc, T&&) { const nsRefPtr& token = mToken; nsCOMPtr r = NS_NewRunnableFunction([=] () { @@ -237,6 +240,9 @@ class MediaEventSource { static_assert(!IsReference::value, "Ref-type not supported!"); typedef typename detail::EventTypeTraits::ArgType ArgType; + template + using TakeArgs = detail::TakeArgs; + /** * Stored by MediaEventSource to send notifications to the listener. */ @@ -286,8 +292,8 @@ class MediaEventSource { // |Method| takes one argument. template - MediaEventListener - ConnectInternal(Target* aTarget, This* aThis, Method aMethod, TrueType) { + typename EnableIf::value, MediaEventListener>::Type + ConnectInternal(Target* aTarget, This* aThis, Method aMethod) { detail::RawPtr thiz(aThis); auto f = [=] (ArgType&& aEvent) { (thiz.get()->*aMethod)(Move(aEvent)); @@ -297,8 +303,8 @@ class MediaEventSource { // |Method| takes no arguments. Don't bother passing the event data. template - MediaEventListener - ConnectInternal(Target* aTarget, This* aThis, Method aMethod, FalseType) { + typename EnableIf::value, MediaEventListener>::Type + ConnectInternal(Target* aTarget, This* aThis, Method aMethod) { detail::RawPtr thiz(aThis); auto f = [=] () { (thiz.get()->*aMethod)(); @@ -341,15 +347,13 @@ public: template MediaEventListener Connect(AbstractThread* aTarget, This* aThis, Method aMethod) { - return ConnectInternal(aTarget, aThis, aMethod, - typename detail::TakeArgs::Type()); + return ConnectInternal(aTarget, aThis, aMethod); } template MediaEventListener Connect(nsIEventTarget* aTarget, This* aThis, Method aMethod) { - return ConnectInternal(aTarget, aThis, aMethod, - typename detail::TakeArgs::Type()); + return ConnectInternal(aTarget, aThis, aMethod); } protected: