summaryrefslogtreecommitdiff
path: root/AudioManagerUtilities/include/CAmSerializer.h
blob: 8abdf9077d87a4455d1fac0a338864c335bb1195 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
/**
 * SPDX license identifier: MPL-2.0
 *
 * Copyright (C) 2012-2017, BMW AG
 *
 * \author Christian Linke, christian.linke@bmw.de BMW 2011,2012
 * \author Alesksandar Donchev, aleksander.donchev@partner.bmw.de BMW 2015, 2017
 *
 * \copyright
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed with
 * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * \file CAmSerializer.h
 * For further information see http://www.genivi.org/.
 */

#ifndef CAMSERIALIZER_H_
#define CAMSERIALIZER_H_

#include <deque>
#include <cassert>
#include <memory>
#include <stdexcept>
#include <unistd.h>
#include "CAmDltWrapper.h"
#include "CAmSocketHandler.h"

/*!
 * \brief Helper structures used within std::bind for automatically identification of all placeholders.
 */
template<std::size_t ... Is>
struct indices
{
};

template<std::size_t N, std::size_t ... Is>
struct build_indices: build_indices<N - 1, N - 1, Is...>
{
};

template<std::size_t ... Is>
struct build_indices<0, Is...> : indices<Is...>
{
};

template<int I> struct placeholder
{
};

namespace std
{
    template<int I>
    struct is_placeholder<::placeholder<I>> : std::integral_constant<int, I>
    {
    };
}

#if defined(__GNUC__)
# define DEPRECATED(MSG) __attribute__ ((__deprecated__((#MSG))))
#else
# define DEPRECATED(MSG)
#endif

namespace am
{
    /**
     * magic class that does the serialization of functions calls
     * The constructor must be called within the main threadcontext, after that using the
     * overloaded template function call will serialize all calls and call them within the
     * main thread context.\n
     * More details can be found here: \ref util
     * \warning asynchronous calls may be used in the mainthread context, but if you want to use synchronous calls make sure that you use one
     * instance of this class per thread otherwise you could be lost in never returning calls.\n
     * Examples of the usage can be found in IAmCommandReceiverShadow of the ControlPlugin or IAmRoutingReceiverShadow of the
     * PluginRoutingInterfaceAsync.
     *
     */

    /**
     * \defgroup Deprecated Obsolete class!
     * @{
     */

    namespace V1
    {
        class CAmSerializer
        {
        private:

            /**
             * Prototype for a delegate
             */
            class CAmDelegate
            {
            public:

                typedef enum
                    :bool
                    {
                        SyncCallType = false, AsyncCallType = true
                } CallType;

                virtual ~CAmDelegate()
                {
                }
                ;
                virtual CallType call(int* pipe)=0;

            };

            /**
             * Prototype for a delegate with variadic template arguments in conjunction with the following class.
             */
            template<class Class, typename Method, typename Tuple, bool Done, int Total, int ... N>
            class CAmDelegateAsyncImpl: public CAmDelegate
            {
                Class mInstance;
                Method mMethod;
                Tuple mArguments;
            public:
                friend class CAmSerializer;
                static void call(Class instance, Method method, Tuple && arguments)
                {
                    CAmDelegateAsyncImpl<Class, Method, Tuple, Total == 1 + sizeof...(N), Total, N..., sizeof...(N)>::call(instance, method, std::forward<Tuple>(arguments));
                }

                CAmDelegateAsyncImpl(Class instance, Method method, Tuple && arguments)
                {
                    mInstance = instance;
                    mMethod = method;
                    mArguments = std::move(arguments);
                }

                CallType call(int* pipe)
                {
                    (void) pipe;
                    call(mInstance, mMethod, std::forward<Tuple>(mArguments));
                    return (AsyncCallType);
                }
                ;
            };

            /**
             * Prototype for a delegate with variadic template arguments.
             */
            template<class Class, typename Method, typename Tuple, int Total, int ... N>
            class CAmDelegateAsyncImpl<Class, Method, Tuple, true, Total, N...> : public CAmDelegate
            {
                Class mInstance;
                Method mMethod;
                Tuple mArguments;
            public:
                friend class CAmSerializer;
                static void call(Class instance, Method method, Tuple && t)
                {
                    (*instance.*method)(std::get<N>(std::forward<Tuple>(t))...);
                }

                CAmDelegateAsyncImpl(Class instance, Method method, Tuple && arguments)
                {
                    mInstance = instance;
                    mMethod = method;
                    mArguments = std::move(arguments);
                }

                CallType call(int* pipe)
                {
                    (void) pipe;
                    call(mInstance, mMethod, std::forward<Tuple>(mArguments));
                    return (AsyncCallType);
                }
                ;
            };

            /**
             * Prototype for a delegate with variadic template arguments in conjunction with the following class.
             */
            template<class Class, typename Method, typename Return, typename Tuple, bool Done, int Total, int ... N>
            class CAmDelegateSyncImpl: public CAmDelegate
            {
                Class mInstance;
                Method mMethod;
                Tuple mArguments;
                Return mReturn;
            public:
                friend class CAmSerializer;
                static void call(Class instance, Method method, Return & result, Tuple && arguments)
                {
                    CAmDelegateSyncImpl<Class, Method, Return, Tuple, Total == 1 + sizeof...(N), Total, N..., sizeof...(N)>::call(instance, method, result, std::forward<Tuple>(arguments));
                }

                CAmDelegateSyncImpl(Class instance, Method method, Tuple && arguments)
                {
                    mInstance = instance;
                    mMethod = method;
                    mArguments = std::move(arguments);
                }

                CallType call(int* pipe)
                {
                    call(mInstance, mMethod, mReturn, std::forward<Tuple>(mArguments));
                    ssize_t result(-1);
                    result = write(pipe[1], this, sizeof(this));
                    if (result == -1)
                        logError("CAmSerializer: Problem writing into pipe! Error No:", errno);
                    return (SyncCallType);
                }
                ;
            };

            /**
             * Prototype for a delegate with variadic template arguments.
             */
            template<class Class, typename Method, typename Return, typename Tuple, int Total, int ... N>
            class CAmDelegateSyncImpl<Class, Method, Return, Tuple, true, Total, N...> : public CAmDelegate
            {
                Class mInstance;
                Method mMethod;
                Tuple mArguments;
                Return mReturn;
            public:
                friend class CAmSerializer;
                static void call(Class instance, Method method, Return & result, Tuple && t)
                {
                    result = (*instance.*method)(std::get<N>(t)...);
                }

                CAmDelegateSyncImpl(Class instance, Method method, Tuple && arguments)
                {
                    mInstance = instance;
                    mMethod = method;
                    mArguments = std::move(arguments);
                }

                CallType call(int* pipe)
                {
                    call(mInstance, mMethod, mReturn, std::forward<Tuple>(mArguments));
                    ssize_t result(-1);
                    result = write(pipe[1], this, sizeof(this));
                    if (result == -1)
                        logError("CAmSerializer: Problem writing into pipe! Error No:", errno);
                    return (SyncCallType);
                }
                ;
            };

            typedef CAmDelegate* CAmDelegagePtr; //!< pointer to a delegate

        public:
            /**
             * instantiates a async delegate with given arguments and sends the delegate pointer over the pipe
             */
            template<typename Class, typename Method, typename Tuple>
            void doAsyncCall(Class intsance, Method method, Tuple & arguments)
            {
                typedef typename std::decay<Tuple>::type ttype;
                typedef CAmDelegateAsyncImpl<Class, Method, Tuple, 0 == std::tuple_size<ttype>::value, std::tuple_size<ttype>::value> AsyncDelegate;
                AsyncDelegate *pImp = new AsyncDelegate(intsance, method, std::forward<Tuple>(arguments));
                send(pImp);
                //Do not delete the pointer. It will be deleted automatically later.
            }

            /**
             * instantiates a sync delegate with given arguments and sends the delegate pointer over the pipe
             */
            template<typename Class, typename Method, typename Return, typename Tuple>
            void doSyncCall(Class intsance, Method method, Return & result, Tuple & arguments)
            {
                typedef typename std::decay<Tuple>::type ttype;
                typedef CAmDelegateSyncImpl<Class, Method, Return, Tuple, 0 == std::tuple_size<ttype>::value, std::tuple_size<ttype>::value> SyncDelegate;
                SyncDelegate *pImp = new SyncDelegate(intsance, method, std::forward<Tuple>(arguments));
                send(pImp);
                int numReads;
                SyncDelegate *p = NULL;
                if ((numReads = read(mReturnPipe[0], &p, sizeof(p))) == -1)
                {
                    logError("CAmSerializer::doSyncCall could not read pipe!");
                    throw std::runtime_error("CAmSerializer Could not read pipe!");
                }
                result = std::move(pImp->mReturn);
                arguments = std::move(pImp->mArguments);
                //Delete the pointer.
                delete pImp;
            }
        private:

            /**
             * rings the line of the pipe and adds the delegate pointer to the queue
             * @param p delegate pointer
             */
            inline void send(CAmDelegagePtr p)
            {
                if (write(mPipe[1], &p, sizeof(p)) == -1)
                {
                    throw std::runtime_error("could not write to pipe !");
                }
            }

            int mPipe[2]; //!< the pipe
            int mReturnPipe[2]; //!< pipe handling returns
            sh_pollHandle_t mHandle;
            CAmSocketHandler* mpSocketHandler;
            std::deque<CAmDelegagePtr> mListDelegatePoiters; //!< intermediate queue to store the pipe results

        public:

            /**
             * get the size of delegate pointers
             */
            int getListDelegatePoiters()
            {
                return mListDelegatePoiters.size();
            }

            /**
             * calls a function with variadic arguments threadsafe
             * @param instance the instance of the class that shall be called
             * @param function the function that shall be called as member function pointer.
             * @param output variable.
             * @tparam TClass the type of the Class to be called
             * @tparam TRet the type of the result
             * @tparam TArgs argument list
             * \section ex Example:
             * @code
             * class MyGreatClass
             * {
             * public:
             *      int AGreatMethod(int x);
             * }
             * CAmSerializer serial(&Sockethandler);
             * MyGreatClass anInstance;
             * int result;
             * serial.syncCall<MyGreatClass, int, int>(&anInstance,&MyGreatClass::AGreatMethod, result, 100);
             * @endcode
             */
            template<class TClass, class TRet, class ... TArgs>
            void syncCall(TClass* instance, TRet (TClass::*method)(TArgs ...), TRet & result, TArgs & ... arguments)
            {
                auto t = std::make_tuple(arguments...);
                doSyncCall(instance, method, result, t);
                std::tie(arguments...) = t;
            }

            /**
             * calls a function with variadic arguments threadsafe
             * @param instance the instance of the class that shall be called
             * @param function the function that shall be called as member function pointer.
             * @tparam TClass the type of the Class to be called
             * @tparam TRet the type of the result
             * @tparam TArgs argument list
             * \section ex Example:
             * @code
             * class MyGreatClass
             * {
             * public:
             *      int AGreatMethod(int x);
             * }
             * CAmSerializer serial(&Sockethandler);
             * MyGreatClass anInstance;
             * serial.asyncCall<MyGreatClass, void, int>(&anInstance,&MyGreatClass::AGreatMethod, 100);
             * @endcode
             */
            template<class TClass, class TRet, class ... TArgs>
            void asyncCall(TClass* instance, TRet (TClass::*method)(TArgs ...), TArgs & ... arguments)
            {
                auto t = std::make_tuple(arguments...);
                doAsyncCall(instance, method, t);
            }

            /**
             * calls a function with no arguments threadsafe
             * @param instance the instance of the class that shall be called
             * @param function the function that shall be called as memberfunction pointer.
             * @tparam TClass1 the type of the Class to be called
             * \section ex Example:
             * @code
             * class myClass
             * {
             * public:
             *      void myfunction();
             * }
             * CAmSerializer serial(&Sockethandler);
             * myClass instanceMyClass;
             * serial<CommandSender>(&instanceMyClass,&myClass::myfunction);
             * @endcode
             */
            template<class TClass>
            void asyncCall(TClass* instance, void (TClass::*function)())
            {
                auto t = std::make_tuple();
                doAsyncCall(instance, function, t);
            }

            /**
             * calls a function with one arguments asynchronously threadsafe
             * @param instance the instance of the class that shall be called
             * @param function the function that shall be called as memberfunction pointer.
             * @param argument the argument
             * @tparam TClass1 the type of the Class to be called
             * @tparam Targ the type of the argument to be called
             * \section ex Example:
             * @code
             * class myClass
             * {
             * public:
             *      void myfunction(int k);
             * }
             * CAmSerializer serial(&Sockethandler);
             * myClass instanceMyClass;
             * serial<CommandSender,int>(&instanceMyClass,&myClass::myfunction,k);
             * @endcode
             *
             */
            template<class TClass1, class Targ>
            void asyncCall(TClass1* instance, void (TClass1::*function)(Targ), Targ argument)
            {
                auto t = std::make_tuple(argument);
                doAsyncCall(instance, function, t);
            }

            /**
             * calls a function with one argument called by reference asynchronously threadsafe
             * @param instance the instance of the class that shall be called
             * @param function the function that shall be called as memberfunction pointer.
             * @param argument the argument
             * @tparam TClass1 the type of the Class to be called
             * @tparam Targ the type of the argument to be called
             * \section ex Example:
             * @code
             * class myClass
             * {
             * public:
             *      void myfunction(int k);
             * }
             * CAmSerializer serial(&Sockethandler);
             * myClass instanceMyClass;
             * serial<CommandSender,int>(&instanceMyClass,&myClass::myfunction,k);
             * @endcode
             *
             */
            template<class TClass1, class Targ>
            void asyncCall(TClass1* instance, void (TClass1::*function)(Targ&), Targ& argument)
            {
                auto t = std::make_tuple(argument);
                doAsyncCall(instance, function, t);
            }

            /**
             * calls a function with two arguments asynchronously threadsafe. for more see asyncCall with one argument
             * @param instance pointer to the instance of the class
             * @param function memberfunction poitner
             * @param argument the first argument
             * @param argument1 the second argument
             * @tparam TClass1 the type of the Class to be called
             * @tparam Targ the type of the argument to be called
             * @tparam Targ1 the type of the first argument to be called
             */
            template<class TClass1, class Targ, class Targ1>
            void asyncCall(TClass1* instance, void (TClass1::*function)(Targ argument, Targ1 argument1), Targ argument, Targ1 argument1)
            {
                auto t = std::make_tuple(argument, argument1);
                doAsyncCall(instance, function, t);
            }

            /**
             * calls a function with two arguments asynchronously threadsafe, first argument is a reference. for more see asyncCall with one argument
             * @param instance pointer to the instance of the class
             * @param function memberfunction poitner
             * @param argument the first argument
             * @param argument1 the second argument
             * @tparam TClass1 the type of the Class to be called
             * @tparam Targ the type of the argument to be called
             * @tparam Targ1 the type of the first argument to be called
             */
            template<class TClass1, class Targ, class Targ1>
            void asyncCall(TClass1* instance, void (TClass1::*function)(Targ& argument, Targ1 argument1), Targ& argument, Targ1 argument1)
            {
                auto t = std::make_tuple(argument, argument1);
                doAsyncCall(instance, function, t);
            }

            /**
             * calls a function with two arguments asynchronously threadsafe, second argument is a reference. for more see asyncCall with one argument
             * @param instance pointer to the instance of the class
             * @param function memberfunction poitner
             * @param argument the first argument
             * @param argument1 the second argument
             * @tparam TClass1 the type of the Class to be called
             * @tparam Targ the type of the argument to be called
             * @tparam Targ1 the type of the first argument to be called
             */
            template<class TClass1, class Targ, class Targ1>
            void asyncCall(TClass1* instance, void (TClass1::*function)(Targ argument, Targ1& argument1), Targ argument, Targ1& argument1)
            {
                auto t = std::make_tuple(argument, argument1);
                doAsyncCall(instance, function, t);
            }

            /**
             * calls a function with two arguments asynchronously threadsafe, both arguments are references. for more see asyncCall with one argument
             * @param instance pointer to the instance of the class
             * @param function memberfunction poitner
             * @param argument the first argument
             * @param argument1 the second argument
             * @tparam TClass1 the type of the Class to be called
             * @tparam Targ the type of the argument to be called
             * @tparam Targ1 the type of the first argument to be called
             */
            template<class TClass1, class Targ, class Targ1>
            void asyncCall(TClass1* instance, void (TClass1::*function)(Targ& argument, Targ1& argument1), Targ& argument, Targ1& argument1)
            {
                auto t = std::make_tuple(argument, argument1);
                doAsyncCall(instance, function, t);
            }

            /**
             * calls a function with three arguments asynchronously threadsafe. for more see other asycCall
             */
            template<class TClass1, class Targ, class Targ1, class Targ2>
            void asyncCall(TClass1* instance, void (TClass1::*function)(Targ argument, Targ1 argument1, Targ2 argument2), Targ argument, Targ1 argument1, Targ2 argument2)
            {
                auto t = std::make_tuple(argument, argument1, argument2);
                doAsyncCall(instance, function, t);
            }

            /**
             * calls a function with three arguments asynchronously threadsafe. for more see other asycCall
             */
            template<class TClass1, class Targ, class Targ1, class Targ2>
            void asyncCall(TClass1* instance, void (TClass1::*function)(Targ& argument, Targ1 argument1, Targ2 argument2), Targ& argument, Targ1 argument1, Targ2 argument2)
            {
                auto t = std::make_tuple(argument, argument1, argument2);
                doAsyncCall(instance, function, t);
            }

            /**
             * calls a function with three arguments asynchronously threadsafe. for more see other asycCall
             */
            template<class TClass1, class Targ, class Targ1, class Targ2>
            void asyncCall(TClass1* instance, void (TClass1::*function)(Targ argument, Targ1& argument1, Targ2 argument2), Targ argument, Targ1& argument1, Targ2 argument2)
            {
                auto t = std::make_tuple(argument, argument1, argument2);
                doAsyncCall(instance, function, t);
            }

            /**
             * calls a function with three arguments asynchronously threadsafe. for more see other asycCall
             */
            template<class TClass1, class Targ, class Targ1, class Targ2>
            void asyncCall(TClass1* instance, void (TClass1::*function)(Targ argument, Targ1 argument1, Targ2& argument2), Targ argument, Targ1 argument1, Targ2& argument2)
            {
                auto t = std::make_tuple(argument, argument1, argument2);
                doAsyncCall(instance, function, t);
            }

            /**
             * calls a function with three arguments asynchronously threadsafe. for more see other asycCall
             */
            template<class TClass1, class Targ, class Targ1, class Targ2>
            void asyncCall(TClass1* instance, void (TClass1::*function)(Targ argument, Targ1& argument1, Targ2& argument2), Targ argument, Targ1& argument1, Targ2& argument2)
            {
                auto t = std::make_tuple(argument, argument1, argument2);
                doAsyncCall(instance, function, t);
            }

            /**
             * calls a function with three arguments asynchronously threadsafe. for more see other asycCall
             */
            template<class TClass1, class Targ, class Targ1, class Targ2>
            void asyncCall(TClass1* instance, void (TClass1::*function)(Targ& argument, Targ1& argument1, Targ2& argument2), Targ& argument, Targ1& argument1, Targ2& argument2)
            {
                auto t = std::make_tuple(argument, argument1, argument2);
                doAsyncCall(instance, function, t);
            }

            /**
             * calls a function with three arguments asynchronously threadsafe. for more see other asycCall
             */
            template<class TClass1, class Targ, class Targ1, class Targ2>
            void asyncCall(TClass1* instance, void (TClass1::*function)(Targ& argument, Targ1& argument1, Targ2 argument2), Targ& argument, Targ1& argument1, Targ2 argument2)
            {
                auto t = std::make_tuple(argument, argument1, argument2);
                doAsyncCall(instance, function, t);
            }

            /**
             * calls a function with three arguments asynchronously threadsafe. for more see other asycCall
             */
            template<class TClass1, class Targ, class Targ1, class Targ2>
            void asyncCall(TClass1* instance, void (TClass1::*function)(Targ& argument, Targ1 argument1, Targ2& argument2), Targ& argument, Targ1 argument1, Targ2& argument2)
            {
                auto t = std::make_tuple(argument, argument1, argument2);
                doAsyncCall(instance, function, t);
            }

            /**
             * calls a function with four arguments asynchronously threadsafe. for more see other asycCall
             */
            template<class TClass1, class Targ, class Targ1, class Targ2, class Targ3>
            void asyncCall(TClass1* instance, void (TClass1::*function)(Targ argument, Targ1 argument1, Targ2 argument2, Targ3 argument3), Targ argument, Targ1 argument1, Targ2 argument2, Targ3 argument3)
            {
                auto t = std::make_tuple(argument, argument1, argument2, argument3);
                doAsyncCall(instance, function, t);
            }

            /**
             * calls a synchronous function with no arguments threadsafe
             * @param instance the instance of the class that shall be called
             * @param function the function that shall be called as memberfunction pointer.
             * @param retVal the return parameter, no const allowed !
             * @tparam TClass1 the type of the class to be called
             * @tparam TretVal the type of the return parameter
             * \section ex Example:
             * @code
             * class myClass
             * {
             * public:
             *      am_Error_e myfunction();
             * }
             * CAmSerializer serial(&Sockethandler);
             * myClass instanceMyClass;
             * am_Error_e error;
             * serial<CommandSender,am_Error_e>(&instanceMyClass,&myClass::myfunction, error);
             * @endcode
             * All arguments given to synchronous functions must be non-const since the results of the operations will be written back to the arguments.
             *
             */
            template<class TClass1, class TretVal>
            void syncCall(TClass1* instance, TretVal (TClass1::*function)(), TretVal& retVal)
            {
                auto t = std::make_tuple();
                doSyncCall(instance, function, retVal, t);
            }

            /**
             * calls a function with one argument synchronous threadsafe
             * @param instance the instance of the class that shall be called
             * @param function the function that shall be called as memberfunction pointer.
             * @param retVal the return parameter, no const allowed !
             * @param argument the argument, no const allowed !
             * @tparam TClass1 the type of the class to be called
             * @tparam TretVal the type of the return parameter
             * @tparam TargCall the type of the argument like in the function to be called. here all references and const must be
             * respected!
             * @tparam Targ the type of the argument, here no const and no references allowed !
             * \section ex Example:
             * @code
             * class myClass
             * {
             * public:
             *      am_Error_e myfunction(int k);
             * }
             * CAmSerializer serial(&Sockethandler);
             * myClass instanceMyClass;
             * am_Error_e error;
             * int l;
             * serial<CommandSender,am_Error_e,int>(&instanceMyClass,&myClass::myfunction,error,l);
             * @endcode
             * All arguments given to synchronous functions must be non-const since the results of the operations will be written back to the arguments.
             */
            template<class TClass1, class TretVal, class TargCall, class Targ>
            void syncCall(TClass1* instance, TretVal (TClass1::*function)(TargCall), TretVal& retVal, Targ& argument)
            {
                auto t = std::make_tuple(argument);
                doSyncCall(instance, function, retVal, t);
                std::tie(argument) = t;
            }

            /**
             * calls a function with one argument synchronous threadsafe for const functions. For more see syncCall with one argument
             */
            template<class TClass1, class TretVal, class TargCall, class Targ>
            void syncCall(TClass1* instance, TretVal (TClass1::*function)(TargCall) const, TretVal& retVal, Targ& argument)
            {
                auto t = std::make_tuple(argument);
                doSyncCall(instance, function, retVal, t);
                std::tie(argument) = t;
            }

            /**
             * calls a function with two arguments synchronously threadsafe. For more see syncCall with one argument
             */
            template<class TClass1, class TretVal, class TargCall, class Targ1Call, class Targ, class Targ1>
            void syncCall(TClass1* instance, TretVal (TClass1::*function)(TargCall, Targ1Call), TretVal& retVal, Targ& argument, Targ1& argument1)
            {
                auto t = std::make_tuple(argument, argument1);
                doSyncCall(instance, function, retVal, t);
                std::tie(argument, argument1) = t;
            }
            /**
             * calls a function with two arguments synchronously threadsafe const. For more see syncCall with one argument
             */
            template<class TClass1, class TretVal, class TargCall, class Targ1Call, class Targ, class Targ1>
            void syncCall(TClass1* instance, TretVal (TClass1::*function)(TargCall, Targ1Call) const, TretVal& retVal, Targ& argument, Targ1& argument1)
            {
                auto t = std::make_tuple(argument, argument1);
                doSyncCall(instance, function, retVal, t);
                std::tie(argument, argument1) = t;
            }

            /**
             * calls a function with three arguments synchronously threadsafe. for more see syncCall with one argument
             */
            template<class TClass1, class TretVal, class TargCall, class TargCall1, class TargCall2, class Targ, class Targ1, class Targ2>
            void syncCall(TClass1* instance, TretVal (TClass1::*function)(TargCall, TargCall1, TargCall2), TretVal& retVal, Targ& argument, Targ1& argument1, Targ2& argument2)
            {
                auto t = std::make_tuple(argument, argument1, argument2);
                doSyncCall(instance, function, retVal, t);
                std::tie(argument, argument1, argument2) = t;
            }

            /**
             * calls a const function with three arguments synchronously threadsafe. for more see syncCall with one argument
             */
            template<class TClass1, class TretVal, class TargCall, class TargCall1, class TargCall2, class Targ, class Targ1, class Targ2>
            void syncCall(TClass1* instance, TretVal (TClass1::*function)(TargCall, TargCall1, TargCall2) const, TretVal& retVal, Targ& argument, Targ1& argument1, Targ2& argument2)
            {
                auto t = std::make_tuple(argument, argument1, argument2);
                doSyncCall(instance, function, retVal, t);
                std::tie(argument, argument1, argument2) = t;
            }

            /**
             * calls a function with four arguments synchronously threadsafe. for more see syncCall with one argument
             */
            template<class TClass1, class TretVal, class TargCall, class TargCall1, class TargCall2, class TargCall3, class Targ, class Targ1, class Targ2, class Targ3>
            void syncCall(TClass1* instance, TretVal (TClass1::*function)(TargCall, TargCall1, TargCall2, TargCall3), TretVal& retVal, Targ& argument, Targ1& argument1, Targ2& argument2, Targ3& argument3)
            {
                auto t = std::make_tuple(argument, argument1, argument2, argument3);
                doSyncCall(instance, function, retVal, t);
                std::tie(argument, argument1, argument2, argument3) = t;
            }

            /**
             * calls a function with five arguments synchronously threadsafe. for more see syncCall with one argument
             */
            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>
            void syncCall(TClass1* instance, TretVal (TClass1::*function)(TargCall, TargCall1, TargCall2, TargCall3, TargCall4), TretVal& retVal, Targ& argument, Targ1& argument1, Targ2& argument2, Targ3& argument3, Targ4& argument4)
            {
                auto t = std::make_tuple(argument, argument1, argument2, argument3, argument4);
                doSyncCall(instance, function, retVal, t);
                std::tie(argument, argument1, argument2, argument3, argument4) = t;
            }

            /**
             * calls a function with six arguments synchronously threadsafe. for more see syncCall with one argument
             */
            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>
            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)
            {
                auto t = std::make_tuple(argument, argument1, argument2, argument3, argument4, argument5);
                doSyncCall(instance, function, retVal, t);
                std::tie(argument, argument1, argument2, argument3, argument4, argument5) = t;
            }

            /**
             * receiver callback for sockethandling, for more, see CAmSocketHandler
             */
            void receiverCallback(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)
            {
                (void) handle;
                (void) userData;
                int numReads;
                CAmDelegagePtr listPointers[3];
                if ((numReads = read(pollfd.fd, &listPointers, sizeof(listPointers))) == -1)
                {
                    logError("CAmSerializer::receiverCallback could not read pipe!");
                    throw std::runtime_error("CAmSerializer Could not read pipe!");
                }
                mListDelegatePoiters.assign(listPointers, listPointers + (numReads / sizeof(CAmDelegagePtr)));
            }

            /**
             * checker callback for sockethandling, for more, see CAmSocketHandler
             */
            bool checkerCallback(const sh_pollHandle_t handle, void* userData)
            {
                (void) handle;
                (void) userData;
                if (mListDelegatePoiters.empty())
                    return (false);
                return (true);
            }

            /**
             * dispatcher callback for sockethandling, for more, see CAmSocketHandler
             */
            bool dispatcherCallback(const sh_pollHandle_t handle, void* userData)
            {
                (void) handle;
                (void) userData;
                CAmDelegagePtr delegatePoiter = mListDelegatePoiters.front();
                mListDelegatePoiters.pop_front();
                if (delegatePoiter->call(mReturnPipe))
                    delete delegatePoiter;
                if (mListDelegatePoiters.empty())
                    return (false);
                return (true);
            }

            TAmShPollFired<CAmSerializer> receiverCallbackT;
            TAmShPollDispatch<CAmSerializer> dispatcherCallbackT;
            TAmShPollCheck<CAmSerializer> checkerCallbackT;

            /**
             * The constructor must be called in the mainthread context !
             * @param iSocketHandler pointer to the CAmSocketHandler
             */
            CAmSerializer(CAmSocketHandler *iSocketHandler) :
                    mPipe(), //
                            mReturnPipe(), //
                            mHandle(),
                            mpSocketHandler(iSocketHandler),
                            mListDelegatePoiters(), //
                            receiverCallbackT(this, &CAmSerializer::receiverCallback), //
                            dispatcherCallbackT(this, &CAmSerializer::dispatcherCallback), //
                            checkerCallbackT(this, &CAmSerializer::checkerCallback)
            {
                assert(NULL!=iSocketHandler);

                if (pipe(mPipe) == -1)
                {
                    logError("CAmSerializer could not create pipe!");
                    throw std::runtime_error("CAmSerializer Could not open pipe!");
                }

                if (pipe(mReturnPipe) == -1)
                {
                    logError("CAmSerializer could not create mReturnPipe!");
                    throw std::runtime_error("CAmSerializer Could not open mReturnPipe!");
                }

                short event = 0;
                event |= POLLIN;
                mpSocketHandler->addFDPoll(mPipe[0], event, NULL, &receiverCallbackT, &checkerCallbackT, &dispatcherCallbackT, NULL, mHandle);
            }

            ~CAmSerializer()
            {
                mpSocketHandler->removeFDPoll(mHandle);
                close(mPipe[0]);
                close(mPipe[1]);
                close(mReturnPipe[0]);
                close(mReturnPipe[1]);
            }
        };

    } /* namespace V1 */

    /**@}*/

    namespace V2
    {
        class CAmSerializer
        {
            /**
             * Prototype for a delegate
             */
            class CAmDelegate
            {
            public:
                typedef enum
                    :bool
                    {
                        SyncCallType = false, AsyncCallType = true
                } CallType;

                virtual ~CAmDelegate()
                {
                }
                ;
                virtual CallType call(int* pipe)=0;
            };

            /**
             * Prototype for a delegate with variadic template arguments.
             */
            template<class TInvocation>
            class CAmDelegateAsyncImpl: public CAmDelegate
            {
                TInvocation mInvocation;
            public:
                friend class CAmSerializer;
                CAmDelegateAsyncImpl(TInvocation && invocation) :
                        mInvocation(std::move(invocation))
                {
                }

                CallType call(int* pipe)
                {
                    (void) pipe;
                    mInvocation();
                    return (AsyncCallType);
                }
                ;
            };

            template<class TInvocation, class TRet>
            class CAmDelegateSyncImpl: public CAmDelegate
            {
                TInvocation mInvocation;
                TRet & mReturn;
            public:
                friend class CAmSerializer;
                CAmDelegateSyncImpl(TInvocation && invocation, TRet && ret) :
                        mInvocation(std::move(invocation)), mReturn(ret)
                {
                }

                CallType call(int* pipe)
                {
                    mReturn = mInvocation();
                    ssize_t result(-1);
                    result = write(pipe[1], this, sizeof(this));
                    if (result == -1)
                        logError("CAmSerializer: Problem writing into pipe! Error No:", errno);
                    return (SyncCallType);
                }
                ;
            };

            template<class TInvocation>
            class CAmDelegateSyncVoidImpl: public CAmDelegate
            {
                TInvocation mInvocation;
            public:
                friend class CAmSerializer;
                CAmDelegateSyncVoidImpl(TInvocation && invocation) :
                        mInvocation(std::move(invocation))
                {
                }

                CallType call(int* pipe)
                {
                    mInvocation();
                    ssize_t result(-1);
                    result = write(pipe[1], this, sizeof(this));
                    if (result == -1)
                        logError("CAmSerializer: Problem writing into pipe! Error No:", errno);
                    return (SyncCallType);
                }
                ;
            };

            typedef CAmDelegate* CAmDelegagePtr; //!< pointer to a delegate

            void sendSync(CAmDelegagePtr pDelegate)
            {
                send(pDelegate);
                int numReads;
                CAmDelegagePtr *p = NULL;
                if ((numReads = read(mReturnPipe[0], &p, sizeof(p))) == -1)
                {
                    logError("CAmSerializer::doSyncCall could not read pipe!");
                    throw std::runtime_error("CAmSerializer Could not read pipe!");
                }
            }

            /**
             * rings the line of the pipe and adds the delegate pointer to the queue
             * @param p delegate pointer
             */
            inline void send(CAmDelegagePtr p)
            {
                if (write(mPipe[1], &p, sizeof(p)) == -1)
                {
                    throw std::runtime_error("could not write to pipe !");
                }
            }

            int mPipe[2]; //!< the pipe
            int mReturnPipe[2]; //!< pipe handling returns
            sh_pollHandle_t mHandle;
            CAmSocketHandler* mpSocketHandler;
            std::deque<CAmDelegagePtr> mListDelegatePointers; //!< intermediate queue to store the pipe results

        public:

            /**
             * get the size of delegate pointers
             */
            size_t getListDelegatePointers()
            {
                return mListDelegatePointers.size();
            }

            /**
             * calls a function with variadic arguments threadsafe
             * @param invocation is a type is produced by std::bind
             * \section ex Example:
             * @code
             * CAmSerializer serial(&Sockethandler);
             * serial.asyncInvocation(std::bind([]()->bool{return true;}));
             * @endcode
             */
            template<class TFunc>
            void asyncInvocation(TFunc invocation)
            {
                static_assert(std::is_bind_expression<TFunc>::value,"The type is not produced by std::bind");
                typedef CAmDelegateAsyncImpl<TFunc> AsyncDelegate;
                AsyncDelegate *pImp = new AsyncDelegate(std::forward<TFunc>(invocation));
                send(pImp);
                //Do not delete the pointer. It will be deleted automatically later.
            }

            /**
             * calls a function with variadic arguments threadsafe
             * @param instance the instance of the class that shall be called
             * @param function the function that shall be called as member function pointer.
             * @tparam TClass the type of the Class to be called
             * @tparam TRet the type of the result
             * @tparam TArgs argument list
             * \section ex Example:
             * @code
             * class AClass
             * {
             * public:
             *      void instanceMethod(int x);
             * }
             * CAmSerializer serial(&Sockethandler);
             * AClass anInstance;
             * serial.asyncCall(&anInstance,&AClass::instanceMethod, 100);
             * @endcode
             */
            template<class TClass, class TMeth, class TRet, class ... TArgs>
            void asyncCall(TClass* instance, TMeth method, TArgs && ... arguments)
            {
                auto invocation = std::bind(method, instance, std::forward<TArgs>(arguments)...);
                asyncInvocation(invocation);
            }

            template<class TClass, class TMeth, class ... TArgs>
            void asyncCall(TClass* instance, TMeth method, TArgs && ... arguments)
            {
                auto invocation = std::bind(method, instance, std::forward<TArgs>(arguments)...);
                asyncInvocation(invocation);
            }

            /**
             * calls a function with variadic arguments threadsafe
             * @param invocation is a type is produced by std::bind
             * @param result from type TRet
             * \section ex Example:
             * @code
             * CAmSerializer serial(&Sockethandler);
             * bool result;
             * serial.syncCall(std::bind([]()->bool{return true;}), result);
             * @endcode
             */
            template<class TFunc, class TRet>
            void syncInvocation(TFunc invocation, TRet && result)
            {
                static_assert(std::is_bind_expression<TFunc>::value,"The type is not produced by std::bind");

                typedef CAmDelegateSyncImpl<TFunc, TRet> SyncDelegate;

                SyncDelegate *pImp = new SyncDelegate(std::forward<TFunc>(invocation), std::forward<TRet>(result));
                sendSync(pImp);
                //Delete the pointer.
                delete pImp;
            }

            /**
             * calls a function with variadic arguments threadsafe
             * @param invocation is a type produced by std::bind
             * \section ex Example:
             * @code
             * CAmSerializer serial(&Sockethandler);
             * serial.syncCall(std::bind([]()->bool{return true;}));
             * @endcode
             */
            template<class TFunc>
            void syncInvocation(TFunc invocation)
            {
                static_assert(std::is_bind_expression<TFunc>::value,"The type is not produced by std::bind");

                typedef CAmDelegateSyncVoidImpl<TFunc> SyncDelegate;

                SyncDelegate *pImp = new SyncDelegate(std::forward<TFunc>(invocation));
                sendSync(pImp);
                //Delete the pointer.
                delete pImp;
            }
            /**
             * calls a function with variadic arguments threadsafe
             * @param instance the instance of the class that shall be called
             * @param function the function that shall be called as member function pointer.
             * @param output variable.
             * @tparam TClass the type of the Class to be called
             * @tparam TRet the type of the result
             * @tparam TArgs argument list
             * \section ex Example:
             * @code
             * class AClass
             * {
             * public:
             *      int instanceMethod(int x);
             * }
             * CAmSerializer serial(&Sockethandler);
             * AClass anInstance;
             * int result;
             * serial.syncCall(&anInstance,&AClass::instanceMethod, result, 100);
             * @endcode
             */
            template<class TClass, class TMeth, class TRet, class ... TArgs>
            void syncCall(TClass* instance, TMeth method, TRet & result, TArgs && ... arguments)
            {
                auto invocation = std::bind(method, instance, std::ref(arguments)...);
                syncInvocation(invocation, result);
            }

            template<class TClass, class TMeth, class ... TArgs>
            void syncCall(TClass* instance, TMeth method, TArgs && ... arguments)
            {
                auto invocation = std::bind(method, instance, std::ref(arguments)...);
                syncInvocation(invocation);
            }

            /**
             * receiver callback for sockethandling, for more, see CAmSocketHandler
             */
            void receiverCallback(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)
            {
                (void) handle;
                (void) userData;
                int numReads;
                CAmDelegagePtr listPointers[3];
                if ((numReads = read(pollfd.fd, &listPointers, sizeof(listPointers))) == -1)
                {
                    logError("CAmSerializer::receiverCallback could not read pipe!");
                    throw std::runtime_error("CAmSerializer Could not read pipe!");
                }
                mListDelegatePointers.assign(listPointers, listPointers + (numReads / sizeof(CAmDelegagePtr)));
            }

            /**
             * checker callback for sockethandling, for more, see CAmSocketHandler
             */
            bool checkerCallback(const sh_pollHandle_t handle, void* userData)
            {
                (void) handle;
                (void) userData;
                if (mListDelegatePointers.empty())
                    return (false);
                return (true);
            }

            /**
             * dispatcher callback for sockethandling, for more, see CAmSocketHandler
             */
            bool dispatcherCallback(const sh_pollHandle_t handle, void* userData)
            {
                (void) handle;
                (void) userData;
                CAmDelegagePtr delegatePoiter = mListDelegatePointers.front();
                mListDelegatePointers.pop_front();
                if (delegatePoiter->call(mReturnPipe))
                    delete delegatePoiter;
                if (mListDelegatePointers.empty())
                    return (false);
                return (true);
            }

            TAmShPollFired<CAmSerializer> receiverCallbackT;
            TAmShPollDispatch<CAmSerializer> dispatcherCallbackT;
            TAmShPollCheck<CAmSerializer> checkerCallbackT;

            /**
             * The constructor must be called in the mainthread context !
             * @param iSocketHandler pointer to the CAmSocketHandler
             */
            CAmSerializer(CAmSocketHandler *iSocketHandler) :
                    mPipe(), //
                            mReturnPipe(), //
                            mHandle(),
                            mpSocketHandler(iSocketHandler),
                            mListDelegatePointers(), //
                            receiverCallbackT(this, &CAmSerializer::receiverCallback), //
                            dispatcherCallbackT(this, &CAmSerializer::dispatcherCallback), //
                            checkerCallbackT(this, &CAmSerializer::checkerCallback)
            {
                assert(NULL!=iSocketHandler);

                if (pipe(mPipe) == -1)
                {
                    logError("CAmSerializer could not create pipe!");
                    throw std::runtime_error("CAmSerializer Could not open pipe!");
                }

                if (pipe(mReturnPipe) == -1)
                {
                    logError("CAmSerializer could not create mReturnPipe!");
                    throw std::runtime_error("CAmSerializer Could not open mReturnPipe!");
                }

                short event = 0;
                event |= POLLIN;
                mpSocketHandler->addFDPoll(mPipe[0], event, NULL, &receiverCallbackT, &checkerCallbackT, &dispatcherCallbackT, NULL, mHandle);
            }

            ~CAmSerializer()
            {
                mpSocketHandler->removeFDPoll(mHandle);
                close(mPipe[0]);
                close(mPipe[1]);
                close(mReturnPipe[0]);
                close(mReturnPipe[1]);
            }
        };
    } /* namespace V2 */

    typedef V1::CAmSerializer CAmSerializer DEPRECATED("You should use V2::CAmSerializer instead!");

} /* namespace am */
#endif /* CAMSERIALIZER_H_ */