Skip to content

Instantly share code, notes, and snippets.

@mook
Created October 10, 2012 04:25
Show Gist options
  • Save mook/3863152 to your computer and use it in GitHub Desktop.
Save mook/3863152 to your computer and use it in GitHub Desktop.
temp
nsRefPtr<sbRunnable_<nsresult>> job =
new sbRunnableXPCOMMethod3_<nsresult,nsIObserverService,
nsISupports*,const char*,const PRUnichar*>(
mObserverService,&nsIObserverService::NotifyObservers,
this,"service-ready",serviceContractID.get());
NS_DispatchToMainThread(job);
rv = job->Wait();
NS_ENSURE_SUCCESS(rv, rv);
sbThreadUtilsXPCOM.h:
In instantiation of
sbRunnableXPCOMMethod3_<
ResultType = unsigned int,
TargetType = nsIObserverService,
Param1Type = nsISupports*,
Param2Type = const char*,
Param3Type = const short unsigned int*,
Arg1Type = nsISupports*,
Arg2Type = const char*,
Arg3Type = const short unsigned int*,
MethodType = unsigned int (nsIObserverService::*)(nsISupports*, const char*, const short unsigned int*)
>::sbRunnableXPCOMMethod3_(
TargetType*,
MethodType,
Param1Type,
Param2Type,
Param3Type,
const char*
):
sbServiceManager.cpp:159:55: required from here
sbThreadUtilsXPCOM.h:1052:16:
error: no matching function for call to ‘
sbRunnableXPCOMMethod_<
unsigned int,
nsIObserverService,
unsigned int (nsIObserverService::*)(nsISupports*, const char*, const short unsigned int*)
>::sbRunnableXPCOMMethod_(
nsIObserverService*&,
unsigned int (nsIObserverService::*&)(nsISupports*, const char*, const short unsigned int*),
const char*&
)
sbThreadUtilsXPCOM.h:1052:16:
note: candidate is:
sbThreadUtilsXPCOM.h:841:3:
note: sbRunnableXPCOMMethod_<
ResultType = unsigned int,
TargetType = nsIObserverService,
MethodType = unsigned int (nsIObserverService::*)(nsISupports*, const char*, const short unsigned int*)
>::sbRunnableXPCOMMethod_(
TargetType&,
MethodType,
const char*
)
sbThreadUtilsXPCOM.h:841:3:
note: no known conversion for argument 1 from ‘nsIObserverService*’ to ‘nsIObserverService&’
/**
* A subclass template of sbRunnable that can return a result.
*/
template <typename ResultType>
class sbRunnable_ : public sbRunnable
{
public:
using sbRunnable::Wait;
public:
sbRunnable_(
const char * aName) :
sbRunnable(aName)
{}
/**
* nsIRunnable run method. Delegates to OnRun(). Subclasses
* must override OnRun(), not this function.
*/
NS_IMETHOD Run()
{
// Invoke method.
mResult = OnRun();
return sbRunnable::Run();
}
/**
* Waits indefinitely for OnRun() to complete and returns its result.
*/
ResultType
Wait()
{
Wait(PR_INTERVAL_NO_TIMEOUT);
return mResult;
}
/**
* Returns true if OnRun() completes before the timeout lapses,
* or false otherwise. If true, returns the result of OnRun()
* in aResult.
*/
bool
Wait(PRIntervalTime aTimeout, ResultType & aResult)
{
if (!Wait(aTimeout)) {
return PR_FALSE;
}
aResult = mResult;
return PR_TRUE;
}
protected:
/**
* The operation to perform when invoked, as defined by a subclass.
*/
virtual ResultType OnRun() = 0;
private:
ResultType mResult;
};
template <typename ResultType,
typename TargetType,
typename Param1Type,
typename Param2Type,
typename Param3Type,
typename Arg1Type = Param1Type,
typename Arg2Type = Param2Type,
typename Arg3Type = Param3Type,
typename MethodType = ResultType (TargetType::*) (Param1Type,
Param2Type,
Param3Type)>
class sbRunnableXPCOMMethod3_ :
public sbRunnableXPCOMMethod_<ResultType, TargetType, MethodType>
{
public:
typedef sbRunnableXPCOMMethod_<ResultType, TargetType, MethodType> BaseType;
/**
* Capture the object and method to call and the arguments to pass
*/
sbRunnableXPCOMMethod3_(
TargetType* aTarget,
MethodType aMethod,
Param1Type aArg1,
Param2Type aArg2,
Param3Type aArg3,
const char * aName = NULL) :
BaseType(aTarget, aMethod, aName),
mArg1(aArg1),
mArg2(aArg2),
mArg3(aArg3)
{}
protected:
/**
* Invokes the captured method on the captured object with the
* captured arguments and returns its result. A subclass that
* overrides this function should eventually delegate to it.
*/
virtual ResultType OnRun()
{
return ((*BaseType::mTarget).*BaseType::mMethod)(mArg1, mArg2, mArg3);
}
private:
Arg1Type mArg1;
Arg2Type mArg2;
Arg3Type mArg3;
};
/**
* A subclass template of sbRunnable_<> that captures an object and
* a method to call when Run() is invoked. A subclass must invoke the
* method, because this class doesn't know what arguments to pass,
* if any.
*/
template <typename ResultType,
typename TargetType,
typename MethodType>
class sbRunnableXPCOMMethod_ : public sbRunnable_<ResultType>
{
public:
sbRunnableXPCOMMethod_(
TargetType * aTarget,
MethodType aMethod,
const char * aName) :
sbRunnable_<ResultType>(aName),
mTarget(aTarget),
mMethod(aMethod)
{}
protected:
/**
* The operation to perform when invoked. Subclasses should call
* mMethod on mTarget with the proper arguments and return the result.
*/
virtual ResultType OnRun() = 0;
nsRefPtr<TargetType> mTarget;
MethodType mMethod;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment