18 #ifndef CAMSERIALIZER_H_
19 #define CAMSERIALIZER_H_
65 virtual ~CAmDelegate()
67 virtual CallType call(
int* pipe)=0;
74 template <
class Class,
typename Method,
typename Tuple,
bool Done,
int Total,
int... N>
75 class CAmDelegateAsyncImpl :
public CAmDelegate
82 static void call(Class instance, Method method, Tuple && arguments)
84 CAmDelegateAsyncImpl<Class, Method, Tuple, Total == 1 +
sizeof...(N), Total, N...,
sizeof...(N)>::call(instance, method, std::forward<Tuple>(arguments));
87 CAmDelegateAsyncImpl(Class instance, Method method, Tuple && arguments)
91 mArguments = std::move(arguments);
94 CallType call(
int* pipe)
97 call(mInstance, mMethod, std::forward<Tuple>(mArguments));
98 return (AsyncCallType);
105 template <
class Class,
typename Method,
typename Tuple,
int Total,
int... N>
106 class CAmDelegateAsyncImpl<Class, Method, Tuple, true, Total, N...> :
public CAmDelegate
113 static void call(Class instance, Method method, Tuple && t)
115 (*instance.*method)(std::get<N>(std::forward<Tuple>(t))...);
118 CAmDelegateAsyncImpl(Class instance, Method method, Tuple && arguments)
120 mInstance = instance;
122 mArguments = std::move(arguments);
125 CallType call(
int* pipe)
128 call(mInstance, mMethod, std::forward<Tuple>(mArguments));
129 return (AsyncCallType);
136 template <
class Class,
typename Method,
typename Return,
typename Tuple,
bool Done,
int Total,
int... N>
137 class CAmDelegateSyncImpl :
public CAmDelegate
145 static void call(Class instance,Method method, Return & result, Tuple && arguments)
147 CAmDelegateSyncImpl<Class, Method, Return, Tuple, Total == 1 +
sizeof...(N), Total, N...,
sizeof...(N)>::call(instance, method, result, std::forward<Tuple>(arguments));
150 CAmDelegateSyncImpl(Class instance, Method method, Tuple && arguments)
152 mInstance = instance;
154 mArguments = std::move(arguments);
157 CallType call(
int* pipe)
159 call(mInstance, mMethod, mReturn, std::forward<Tuple>(mArguments));
161 result = write(pipe[1],
this,
sizeof(
this));
163 logError(
"CAmSerializer: Problem writing into pipe! Error No:",errno);
164 return (SyncCallType);
171 template <
class Class,
typename Method,
typename Return,
typename Tuple,
int Total,
int... N>
172 class CAmDelegateSyncImpl<Class, Method, Return, Tuple, true, Total, N...> :
public CAmDelegate
180 static void call(Class instance, Method method, Return & result, Tuple && t)
182 result = (*instance.*method)(std::get<N>(t)...);
185 CAmDelegateSyncImpl(Class instance, Method method, Tuple && arguments)
187 mInstance = instance;
189 mArguments = std::move(arguments);
192 CallType call(
int* pipe)
194 call(mInstance, mMethod, mReturn, std::forward<Tuple>(mArguments));
196 result = write(pipe[1],
this,
sizeof(
this));
198 logError(
"CAmSerializer: Problem writing into pipe! Error No:",errno);
199 return (SyncCallType);
203 typedef CAmDelegate* CAmDelegagePtr;
209 template <
typename Class,
typename Method,
typename Tuple>
210 void doAsyncCall(Class intsance, Method method, Tuple & arguments)
212 typedef typename std::decay<Tuple>::type ttype;
213 typedef CAmDelegateAsyncImpl<Class, Method, Tuple, 0 == std::tuple_size<ttype>::value, std::tuple_size<ttype>::value> AsyncDelegate;
214 AsyncDelegate *pImp =
new AsyncDelegate(intsance, method, std::forward<Tuple>(arguments));
222 template <
typename Class,
typename Method,
typename Return,
typename Tuple>
223 void doSyncCall(Class intsance, Method method, Return & result, Tuple & arguments)
225 typedef typename std::decay<Tuple>::type ttype;
226 typedef CAmDelegateSyncImpl<Class, Method, Return, Tuple, 0 == std::tuple_size<ttype>::value, std::tuple_size<ttype>::value> SyncDelegate;
227 SyncDelegate *pImp =
new SyncDelegate(intsance, method, std::forward<Tuple>(arguments));
230 SyncDelegate *p = NULL;
231 if ((numReads = read(mReturnPipe[0], &p,
sizeof(p))) == -1)
233 logError(
"CAmSerializer::doSyncCall could not read pipe!");
234 throw std::runtime_error(
"CAmSerializer Could not read pipe!");
236 result = std::move(pImp->mReturn);
237 arguments = std::move(pImp->mArguments);
247 inline void send(CAmDelegagePtr p)
249 if (write(mPipe[1], &p,
sizeof(p)) == -1)
251 throw std::runtime_error(
"could not write to pipe !");
257 std::deque<CAmDelegagePtr> mListDelegatePoiters;
266 return mListDelegatePoiters.size();
290 template<
class TClass,
class TRet,
class ... TArgs>
291 void syncCall(TClass* instance, TRet (TClass::*method)(TArgs ...), TRet & result, TArgs & ... arguments)
293 auto t = std::make_tuple(arguments...);
295 std::tie(arguments...) = t;
317 template<
class TClass,
class TRet,
class ... TArgs>
318 void asyncCall(TClass* instance, TRet (TClass::*method)(TArgs ...), TArgs & ... arguments)
320 auto t = std::make_tuple(arguments...);
341 template<
class TClass>
342 void asyncCall(TClass* instance,
void (TClass::*
function)())
344 auto t = std::make_tuple();
368 template<
class TClass1,
class Targ>
369 void asyncCall(TClass1* instance,
void (TClass1::*
function)(Targ), Targ argument)
371 auto t = std::make_tuple(argument);
395 template<
class TClass1,
class Targ>
396 void asyncCall(TClass1* instance,
void (TClass1::*
function)(Targ&), Targ& argument)
398 auto t = std::make_tuple(argument);
412 template<
class TClass1,
class Targ,
class Targ1>
413 void asyncCall(TClass1* instance,
void (TClass1::*
function)(Targ argument, Targ1 argument1), Targ argument, Targ1 argument1)
415 auto t = std::make_tuple(argument,argument1);
429 template<
class TClass1,
class Targ,
class Targ1>
430 void asyncCall(TClass1* instance,
void (TClass1::*
function)(Targ& argument, Targ1 argument1), Targ& argument, Targ1 argument1)
432 auto t = std::make_tuple(argument,argument1);
446 template<
class TClass1,
class Targ,
class Targ1>
447 void asyncCall(TClass1* instance,
void (TClass1::*
function)(Targ argument, Targ1& argument1), Targ argument, Targ1& argument1)
449 auto t = std::make_tuple(argument,argument1);
463 template<
class TClass1,
class Targ,
class Targ1>
464 void asyncCall(TClass1* instance,
void (TClass1::*
function)(Targ& argument, Targ1& argument1), Targ& argument, Targ1& argument1)
466 auto t = std::make_tuple(argument,argument1);
473 template<
class TClass1,
class Targ,
class Targ1,
class Targ2>
474 void asyncCall(TClass1* instance,
void (TClass1::*
function)(Targ argument, Targ1 argument1, Targ2 argument2), Targ argument, Targ1 argument1, Targ2 argument2)
476 auto t = std::make_tuple(argument,argument1, argument2);
483 template<
class TClass1,
class Targ,
class Targ1,
class Targ2>
484 void asyncCall(TClass1* instance,
void (TClass1::*
function)(Targ& argument, Targ1 argument1, Targ2 argument2), Targ& argument, Targ1 argument1, Targ2 argument2)
486 auto t = std::make_tuple(argument,argument1, argument2);
494 template<
class TClass1,
class Targ,
class Targ1,
class Targ2>
495 void asyncCall(TClass1* instance,
void (TClass1::*
function)(Targ argument, Targ1& argument1, Targ2 argument2), Targ argument, Targ1& argument1, Targ2 argument2)
497 auto t = std::make_tuple(argument,argument1, argument2);
504 template<
class TClass1,
class Targ,
class Targ1,
class Targ2>
505 void asyncCall(TClass1* instance,
void (TClass1::*
function)(Targ argument, Targ1 argument1, Targ2& argument2), Targ argument, Targ1 argument1, Targ2& argument2)
507 auto t = std::make_tuple(argument,argument1, argument2);
514 template<
class TClass1,
class Targ,
class Targ1,
class Targ2>
515 void asyncCall(TClass1* instance,
void (TClass1::*
function)(Targ argument, Targ1& argument1, Targ2& argument2), Targ argument, Targ1& argument1, Targ2& argument2)
517 auto t = std::make_tuple(argument,argument1, argument2);
524 template<
class TClass1,
class Targ,
class Targ1,
class Targ2>
525 void asyncCall(TClass1* instance,
void (TClass1::*
function)(Targ& argument, Targ1& argument1, Targ2& argument2), Targ& argument, Targ1& argument1, Targ2& argument2)
527 auto t = std::make_tuple(argument,argument1, argument2);
534 template<
class TClass1,
class Targ,
class Targ1,
class Targ2>
535 void asyncCall(TClass1* instance,
void (TClass1::*
function)(Targ& argument, Targ1& argument1, Targ2 argument2), Targ& argument, Targ1& argument1, Targ2 argument2)
537 auto t = std::make_tuple(argument,argument1, argument2);
544 template<
class TClass1,
class Targ,
class Targ1,
class Targ2>
545 void asyncCall(TClass1* instance,
void (TClass1::*
function)(Targ& argument, Targ1 argument1, Targ2& argument2), Targ& argument, Targ1 argument1, Targ2& argument2)
547 auto t = std::make_tuple(argument,argument1, argument2);
554 template<
class TClass1,
class Targ,
class Targ1,
class Targ2,
class Targ3>
555 void asyncCall(TClass1* instance,
void (TClass1::*
function)(Targ argument, Targ1 argument1, Targ2 argument2, Targ3 argument3), Targ argument, Targ1 argument1, Targ2 argument2, Targ3 argument3)
557 auto t = std::make_tuple(argument,argument1, argument2,argument3);
583 template<
class TClass1,
class TretVal>
584 void syncCall(TClass1* instance, TretVal (TClass1::*
function)(), TretVal& retVal)
586 auto t = std::make_tuple();
616 template<
class TClass1,
class TretVal,
class TargCall,
class Targ>
617 void syncCall(TClass1* instance, TretVal (TClass1::*
function)(TargCall), TretVal& retVal, Targ& argument)
619 auto t = std::make_tuple(argument);
621 std::tie(argument) = t;
627 template<
class TClass1,
class TretVal,
class TargCall,
class Targ>
628 void syncCall(TClass1* instance, TretVal (TClass1::*
function)(TargCall)
const, TretVal& retVal, Targ& argument)
630 auto t = std::make_tuple(argument);
632 std::tie(argument) = t;
638 template<
class TClass1,
class TretVal,
class TargCall,
class Targ1Call,
class Targ,
class Targ1>
639 void syncCall(TClass1* instance, TretVal (TClass1::*
function)(TargCall, Targ1Call), TretVal& retVal, Targ& argument, Targ1& argument1)
641 auto t = std::make_tuple(argument, argument1);
643 std::tie(argument, argument1) = t;
648 template<
class TClass1,
class TretVal,
class TargCall,
class Targ1Call,
class Targ,
class Targ1>
649 void syncCall(TClass1* instance, TretVal (TClass1::*
function)(TargCall, Targ1Call)
const, TretVal& retVal, Targ& argument, Targ1& argument1)
651 auto t = std::make_tuple(argument, argument1);
653 std::tie(argument, argument1) = t;
659 template<
class TClass1,
class TretVal,
class TargCall,
class TargCall1,
class TargCall2,
class Targ,
class Targ1,
class Targ2>
660 void syncCall(TClass1* instance, TretVal (TClass1::*
function)(TargCall, TargCall1, TargCall2), TretVal& retVal, Targ& argument, Targ1& argument1, Targ2& argument2)
662 auto t = std::make_tuple(argument, argument1, argument2);
664 std::tie(argument, argument1,argument2) = t;
670 template<
class TClass1,
class TretVal,
class TargCall,
class TargCall1,
class TargCall2,
class Targ,
class Targ1,
class Targ2>
671 void syncCall(TClass1* instance, TretVal (TClass1::*
function)(TargCall, TargCall1, TargCall2)
const, TretVal& retVal, Targ& argument, Targ1& argument1, Targ2& argument2)
673 auto t = std::make_tuple(argument, argument1, argument2);
675 std::tie(argument, argument1,argument2) = t;
681 template<
class TClass1,
class TretVal,
class TargCall,
class TargCall1,
class TargCall2,
class TargCall3,
class Targ,
class Targ1,
class Targ2,
class Targ3>
682 void syncCall(TClass1* instance, TretVal (TClass1::*
function)(TargCall, TargCall1, TargCall2, TargCall3), TretVal& retVal, Targ& argument, Targ1& argument1, Targ2& argument2, Targ3& argument3)
684 auto t = std::make_tuple(argument, argument1, argument2, argument3);
686 std::tie(argument, argument1,argument2, argument3) = t;
692 template<
class TClass1,
class TretVal,
class TargCall,
class TargCall1,
class TargCall2,
class TargCall3,
class TargCall4,
class Targ,
class Targ1,
class Targ2,
class Targ3,
class Targ4>
693 void syncCall(TClass1* instance, TretVal (TClass1::*
function)(TargCall, TargCall1, TargCall2, TargCall3, TargCall4), TretVal& retVal, Targ& argument, Targ1& argument1, Targ2& argument2, Targ3& argument3, Targ4& argument4)
695 auto t = std::make_tuple(argument, argument1, argument2, argument3, argument4);
697 std::tie(argument, argument1,argument2, argument3, argument4) = t;
703 template<
class TClass1,
class TretVal,
class TargCall,
class TargCall1,
class TargCall2,
class TargCall3,
class TargCall4,
class TargCall5,
class Targ,
class Targ1,
class Targ2,
class Targ3,
class Targ4,
class Targ5>
704 void syncCall(TClass1* instance, TretVal (TClass1::*
function)(TargCall, TargCall1, TargCall2, TargCall3, TargCall4, TargCall5), TretVal& retVal, Targ& argument, Targ1& argument1, Targ2& argument2, Targ3& argument3, Targ4& argument4, Targ5& argument5)
706 auto t = std::make_tuple(argument, argument1, argument2, argument3, argument4, argument5);
708 std::tie(argument, argument1,argument2, argument3, argument4, argument5) = t;
719 CAmDelegagePtr listPointers[3];
720 if ((numReads = read(pollfd.fd, &listPointers,
sizeof(listPointers))) == -1)
722 logError(
"CAmSerializer::receiverCallback could not read pipe!");
723 throw std::runtime_error(
"CAmSerializer Could not read pipe!");
725 mListDelegatePoiters.assign(listPointers, listPointers + (numReads /
sizeof(CAmDelegagePtr)));
735 if (mListDelegatePoiters.empty())
747 CAmDelegagePtr delegatePoiter = mListDelegatePoiters.front();
748 mListDelegatePoiters.pop_front();
749 if (delegatePoiter->call(mReturnPipe))
750 delete delegatePoiter;
751 if (mListDelegatePoiters.empty())
767 mListDelegatePoiters(),
772 assert(NULL!=iSocketHandler);
774 if (pipe(mPipe) == -1)
776 logError(
"CAmSerializer could not create pipe!");
777 throw std::runtime_error(
"CAmSerializer Could not open pipe!");
780 if (pipe(mReturnPipe) == -1)
782 logError(
"CAmSerializer could not create mReturnPipe!");
783 throw std::runtime_error(
"CAmSerializer Could not open mReturnPipe!");
796 close(mReturnPipe[0]);
797 close(mReturnPipe[1]);
void asyncCall(TClass1 *instance, void(TClass1::*function)(Targ argument, Targ1 argument1, Targ2 &argument2), Targ argument, Targ1 argument1, Targ2 &argument2)
calls a function with three arguments asynchronously threadsafe.
magic class that does the serialization of functions calls The constructor must be called within the ...
void asyncCall(TClass1 *instance, void(TClass1::*function)(Targ &argument, Targ1 argument1, Targ2 &argument2), Targ &argument, Targ1 argument1, Targ2 &argument2)
calls a function with three arguments asynchronously threadsafe.
void syncCall(TClass1 *instance, TretVal(TClass1::*function)(TargCall, TargCall1, TargCall2, TargCall3), TretVal &retVal, Targ &argument, Targ1 &argument1, Targ2 &argument2, Targ3 &argument3)
calls a function with four arguments synchronously threadsafe.
void asyncCall(TClass1 *instance, void(TClass1::*function)(Targ &argument, Targ1 &argument1, Targ2 &argument2), Targ &argument, Targ1 &argument1, Targ2 &argument2)
calls a function with three arguments asynchronously threadsafe.
void syncCall(TClass *instance, TRet(TClass::*method)(TArgs...), TRet &result, TArgs &...arguments)
calls a function with variadic arguments threadsafe
int getListDelegatePoiters()
get the size of delegate pointers
void asyncCall(TClass1 *instance, void(TClass1::*function)(Targ argument, Targ1 argument1, Targ2 argument2), Targ argument, Targ1 argument1, Targ2 argument2)
calls a function with three arguments asynchronously threadsafe.
void asyncCall(TClass1 *instance, void(TClass1::*function)(Targ &argument, Targ1 &argument1, Targ2 argument2), Targ &argument, Targ1 &argument1, Targ2 argument2)
calls a function with three arguments asynchronously threadsafe.
make private, not public template for a callback
void syncCall(TClass1 *instance, TretVal(TClass1::*function)(TargCall, TargCall1, TargCall2, TargCall3, TargCall4, TargCall5), TretVal &retVal, Targ &argument, Targ1 &argument1, Targ2 &argument2, Targ3 &argument3, Targ4 &argument4, Targ5 &argument5)
calls a function with six arguments synchronously threadsafe.
TAmShPollCheck< CAmSerializer > checkerCallbackT
The am::CAmSocketHandler implements a mainloop for the AudioManager.
void asyncCall(TClass1 *instance, void(TClass1::*function)(Targ &argument, Targ1 &argument1), Targ &argument, Targ1 &argument1)
calls a function with two arguments asynchronously threadsafe, both arguments are references...
uint16_t sh_pollHandle_t
this is a handle for a filedescriptor to be used with the SocketHandler
void asyncCall(TClass1 *instance, void(TClass1::*function)(Targ &argument, Targ1 argument1, Targ2 argument2), Targ &argument, Targ1 argument1, Targ2 argument2)
calls a function with three arguments asynchronously threadsafe.
void doAsyncCall(Class intsance, Method method, Tuple &arguments)
instantiates a async delegate with given arguments and sends the delegate pointer over the pipe ...
SPDX license identifier: MPL-2.0.
void syncCall(TClass1 *instance, TretVal(TClass1::*function)(TargCall, TargCall1, TargCall2, TargCall3, TargCall4), TretVal &retVal, Targ &argument, Targ1 &argument1, Targ2 &argument2, Targ3 &argument3, Targ4 &argument4)
calls a function with five arguments synchronously threadsafe.
void asyncCall(TClass1 *instance, void(TClass1::*function)(Targ &), Targ &argument)
calls a function with one argument called by reference asynchronously threadsafe
void syncCall(TClass1 *instance, TretVal(TClass1::*function)(), TretVal &retVal)
calls a synchronous function with no arguments threadsafe
void syncCall(TClass1 *instance, TretVal(TClass1::*function)(TargCall) const, TretVal &retVal, Targ &argument)
calls a function with one argument synchronous threadsafe for const functions.
am_Error_e addFDPoll(const int fd, const short event, IAmShPollPrepare *prepare, IAmShPollFired *fired, IAmShPollCheck *check, IAmShPollDispatch *dispatch, void *userData, sh_pollHandle_t &handle)
Adds a filedescriptor to the polling loop.
void syncCall(TClass1 *instance, TretVal(TClass1::*function)(TargCall), TretVal &retVal, Targ &argument)
calls a function with one argument synchronous threadsafe
CAmSerializer(CAmSocketHandler *iSocketHandler)
The constructor must be called in the mainthread context !
void syncCall(TClass1 *instance, TretVal(TClass1::*function)(TargCall, TargCall1, TargCall2), TretVal &retVal, Targ &argument, Targ1 &argument1, Targ2 &argument2)
calls a function with three arguments synchronously threadsafe.
void doSyncCall(Class intsance, Method method, Return &result, Tuple &arguments)
instantiates a sync delegate with given arguments and sends the delegate pointer over the pipe ...
void asyncCall(TClass *instance, void(TClass::*function)())
calls a function with no arguments threadsafe
TAmShPollDispatch< CAmSerializer > dispatcherCallbackT
void asyncCall(TClass1 *instance, void(TClass1::*function)(Targ argument, Targ1 &argument1, Targ2 argument2), Targ argument, Targ1 &argument1, Targ2 argument2)
calls a function with three arguments asynchronously threadsafe.
void asyncCall(TClass1 *instance, void(TClass1::*function)(Targ argument, Targ1 argument1, Targ2 argument2, Targ3 argument3), Targ argument, Targ1 argument1, Targ2 argument2, Targ3 argument3)
calls a function with four arguments asynchronously threadsafe.
void asyncCall(TClass *instance, TRet(TClass::*method)(TArgs...), TArgs &...arguments)
calls a function with variadic arguments threadsafe
void syncCall(TClass1 *instance, TretVal(TClass1::*function)(TargCall, Targ1Call) const, TretVal &retVal, Targ &argument, Targ1 &argument1)
calls a function with two arguments synchronously threadsafe const.
SPDX license identifier: MPL-2.0.
void asyncCall(TClass1 *instance, void(TClass1::*function)(Targ &argument, Targ1 argument1), Targ &argument, Targ1 argument1)
calls a function with two arguments asynchronously threadsafe, first argument is a reference...
void logError(T value, TArgs...args)
logs given values with errorlevel with the default context
void asyncCall(TClass1 *instance, void(TClass1::*function)(Targ argument, Targ1 argument1), Targ argument, Targ1 argument1)
calls a function with two arguments asynchronously threadsafe.
void receiverCallback(const pollfd pollfd, const sh_pollHandle_t handle, void *userData)
receiver callback for sockethandling, for more, see CAmSocketHandler
bool checkerCallback(const sh_pollHandle_t handle, void *userData)
checker callback for sockethandling, for more, see CAmSocketHandler
TAmShPollFired< CAmSerializer > receiverCallbackT
bool dispatcherCallback(const sh_pollHandle_t handle, void *userData)
dispatcher callback for sockethandling, for more, see CAmSocketHandler
void asyncCall(TClass1 *instance, void(TClass1::*function)(Targ argument, Targ1 &argument1, Targ2 &argument2), Targ argument, Targ1 &argument1, Targ2 &argument2)
calls a function with three arguments asynchronously threadsafe.
void syncCall(TClass1 *instance, TretVal(TClass1::*function)(TargCall, TargCall1, TargCall2) const, TretVal &retVal, Targ &argument, Targ1 &argument1, Targ2 &argument2)
calls a const function with three arguments synchronously threadsafe.
void asyncCall(TClass1 *instance, void(TClass1::*function)(Targ argument, Targ1 &argument1), Targ argument, Targ1 &argument1)
calls a function with two arguments asynchronously threadsafe, second argument is a reference...
void syncCall(TClass1 *instance, TretVal(TClass1::*function)(TargCall, Targ1Call), TretVal &retVal, Targ &argument, Targ1 &argument1)
calls a function with two arguments synchronously threadsafe.
void asyncCall(TClass1 *instance, void(TClass1::*function)(Targ), Targ argument)
calls a function with one arguments asynchronously threadsafe