-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_Model.h
1632 lines (1443 loc) · 59.3 KB
/
model_Model.h
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
//PLEASE DO NOT EDIT THIS CODE
//This code was generated using the UMPLE 1.29.1.4295.41a59b8ce modeling language
#ifndef DEF__
#define DEF__
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || defined(_WIN64)
#define WINDOWS_OS
// NO PREPROCESSOR DEFINITION FOR PRAGMA
#if _MSC_VER
#define PRAGMA
#pragma warning( disable : 4290 )
#endif
#elif defined(hpux) || defined(__hpux) || defined(__hpux__)
#define HPUX_OS
#elif defined(__APPLE__) || defined(macintosh)
#define MAC_OS
#elif defined(bsdi) || defined(__bsdi__)
#define BSD_OS
#endif
#ifdef PRAGMA
#pragma once
#ifdef _MSC_VER
#pragma include_alias("..\\model_Model.h", "model_Model.h")
#pragma include_alias(".\\SysCallReader.h", "/SysCallReader.h")
#pragma include_alias(".\\UsingSystem.h", "/UsingSystem.h")
#pragma include_alias(".\\UsingIO.h", "/UsingIO.h")
#pragma include_alias(".\\AnyActions.h", "/AnyActions.h")
#pragma include_alias(".\\AllRunModules.h", "/AllRunModules.h")
#pragma include_alias(".\\AllRunExecutions.h", "/AllRunExecutions.h")
#pragma include_alias(".\\Registers.h", "/Registers.h")
#pragma include_alias(".\\Function.h", "/Function.h")
#pragma include_alias(".\\Parameters.h", "/Parameters.h")
#pragma include_alias(".\\Clusterizing.h", "/Clusterizing.h")
#pragma include_alias(".\\Classifications.h", "/Classifications.h")
#pragma include_alias(".\\Localizer.h", "/Localizer.h")
#pragma include_alias(".\\AllProcesstoBuffers.h", "/AllProcesstoBuffers.h")
#pragma include_alias(".\\MemoryAccess.h", "/MemoryAccess.h")
#pragma include_alias(".\\HardWareAccess.h", "/HardWareAccess.h")
#pragma include_alias(".\\ExecutedProcess.h", "/ExecutedProcess.h")
#pragma include_alias(".\\InterfaceforAgentExecManager.h", "/InterfaceforAgentExecManager.h")
#pragma include_alias(".\\End.h", "/End.h")
#else
#pragma interface "model_Model.h"
#endif
#endif
//------------------------
// PACKAGE FILES DECLARATION
//------------------------
#include <sstream>
#include <cmath>
#ifdef WINDOWS_OS
#include <windows.h>
#include <process.h>
#else
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include <cstring>
#include <signal.h>
#endif
#ifdef HPUX_OS
#include <sys/pstat.h>
#elif defined MAC_OS
#undef DEBUG
#include <CoreServices/CoreServices.h>
#elif defined BSD_OS
#include <mach/mach_types.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#endif
//------------------------
//USED LIBRARIES
//------------------------
using namespace std;
//------------------------
//USED LIBRARIES
//------------------------
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <iostream>
#include <queue>
#include <iostream>
#include <map>
#include <exception>
#include <stdexcept>
#include <cassert>
#include "stdio.h"
//------------------------------
//NAMESPACES AND PREDEFINITIONS
//------------------------------
#ifdef __cplusplus
#endif
//is_pointer
template <typename T> struct remove_const_type { typedef T type; };
template <typename T> struct remove_const_type<const T> { typedef T type; };
template <typename T> struct remove_volatile_type { typedef T type; };
template <typename T> struct remove_volatile_type<volatile T> { typedef T type; };
template <typename T> struct removeType : remove_const_type<typename remove_volatile_type<T>::type> {};
template <typename T> struct is_ptr_type { enum { value = false }; };
template <typename T> struct is_ptr_type<T*> { enum { value = true }; };
template <typename T> struct is_ptr : is_ptr_type<typename removeType<T>::type> {};
#define PLACE_HOLDER int
#define USECS_PER_MSEC 1000
#define MUSECS_PER_SEC 1000
#define USECS_PER_SEC 1000000
#define INSTANCEOF(object, clazz) !dynamic_cast<clazz*>(object)
#define ARGUMENT_UPPER_LIMIT 10
#define EMPTY()
#define COMMA() ,
#define SEMICOLON() ;
#define TYPENAME_ARGS(i, value) typename ArgumentType##i
#define TYPENAME_VALUE_ARGS(i, value) typename ArgumentType##i=value
#define INIT_VALUE_ARG(i, name) this->_##name##i=name##i
#define SER_ARG(i, name) _##name##i=transport[i - 1]
#define DES_ARG(i, name) transport[i - 1]=_##name##i
#define NAMED_ARG(i, name) name##i
#define MEMBER_ARG(i, name) ArgumentType##i name##i
#define INIT_MEMBER_ARG(i, name) _##name##i(name##i)
#define VOID_ARG(i, value) void
#define CAT(a, ...) a ## __VA_ARGS__
#define REPEAT_DEC(count ,macro, split, ...) CAT(REPEAT_DEC_,count)(macro, split, __VA_ARGS__)
#define REPEAT_DEC_1(macro, split, ...)
#define REPEAT_DEC_2(macro, split, ...) macro(1, __VA_ARGS__)
#define REPEAT_DEC_3(macro, split, ...) macro(2, __VA_ARGS__) split() REPEAT_DEC_2(macro, split, __VA_ARGS__)
#define REPEAT_DEC_4(macro, split, ...) macro(3, __VA_ARGS__) split() REPEAT_DEC_3(macro, split, __VA_ARGS__)
#define REPEAT_DEC_5(macro, split, ...) macro(4, __VA_ARGS__) split() REPEAT_DEC_4(macro, split, __VA_ARGS__)
#define REPEAT_DEC_6(macro, split, ...) macro(5, __VA_ARGS__) split() REPEAT_DEC_5(macro, split, __VA_ARGS__)
#define REPEAT_DEC_7(macro, split, ...) macro(6, __VA_ARGS__) split() REPEAT_DEC_6(macro, split, __VA_ARGS__)
#define REPEAT_DEC_8(macro, split, ...) macro(7, __VA_ARGS__) split() REPEAT_DEC_7(macro, split, __VA_ARGS__)
#define REPEAT_DEC_9(macro, split, ...) macro(8, __VA_ARGS__) split() REPEAT_DEC_8(macro, split, __VA_ARGS__)
#define REPEAT_DEC_10(macro, split, ...) macro(9, __VA_ARGS__) split() REPEAT_DEC_9(macro, split, __VA_ARGS__)
#define REPEAT_DEC_11(macro, split, ...) macro(10, __VA_ARGS__) split() REPEAT_DEC_10(macro, split, __VA_ARGS__)
#define REPEAT_DEC_12(macro, split, ...) macro(11, __VA_ARGS__) split() REPEAT_DEC_11(macro, split, __VA_ARGS__)
#define REPEAT_DEC_13(macro, split, ...) macro(12, __VA_ARGS__) split() REPEAT_DEC_12(macro, split, __VA_ARGS__)
#define REPEAT_DEC_14(macro, split, ...) macro(13, __VA_ARGS__) split() REPEAT_DEC_13(macro, split, __VA_ARGS__)
#define REPEAT_DEC_15(macro, split, ...) macro(14, __VA_ARGS__) split() REPEAT_DEC_14(macro, split, __VA_ARGS__)
#define REPEAT_DEC_16(macro, split, ...) macro(15, __VA_ARGS__) split() REPEAT_DEC_15(macro, split, __VA_ARGS__)
#define REPEAT_INC(count, macro, split, ...) CAT(REPEAT_INC_,count)(macro, split, __VA_ARGS__)
#define REPEAT_INC_1(macro, split, ...) macro(1, __VA_ARGS__)
#define REPEAT_INC_2(macro, split, ...) REPEAT_INC_1(macro, split, __VA_ARGS__) split() macro(2, __VA_ARGS__)
#define REPEAT_INC_3(macro, split, ...) REPEAT_INC_2(macro, split, __VA_ARGS__) split() macro(3, __VA_ARGS__)
#define REPEAT_INC_4(macro, split, ...) REPEAT_INC_3(macro, split, __VA_ARGS__) split() macro(4, __VA_ARGS__)
#define REPEAT_INC_5(macro, split, ...) REPEAT_INC_4(macro, split, __VA_ARGS__) split() macro(5, __VA_ARGS__)
#define REPEAT_INC_6(macro, split, ...) REPEAT_INC_5(macro, split, __VA_ARGS__) split() macro(6, __VA_ARGS__)
#define REPEAT_INC_7(macro, split, ...) REPEAT_INC_6(macro, split, __VA_ARGS__) split() macro(7, __VA_ARGS__)
#define REPEAT_INC_8(macro, split, ...) REPEAT_INC_7(macro, split, __VA_ARGS__) split() macro(8, __VA_ARGS__)
#define REPEAT_INC_9(macro, split, ...) REPEAT_INC_8(macro, split, __VA_ARGS__) split() macro(9, __VA_ARGS__)
#define REPEAT_INC_10(macro, split, ...) REPEAT_INC_9(macro, split, __VA_ARGS__) split() macro(10, __VA_ARGS__)
#define REPEAT_INC_11(macro, split, ...) REPEAT_INC_10(macro, split, __VA_ARGS__) split() macro(11, __VA_ARGS__)
#define REPEAT_INC_12(macro, split, ...) REPEAT_INC_11(macro, split, __VA_ARGS__) split() macro(12, __VA_ARGS__)
#define REPEAT_INC_13(macro, split, ...) REPEAT_INC_12(macro, split, __VA_ARGS__) split() macro(13, __VA_ARGS__)
#define REPEAT_INC_14(macro, split, ...) REPEAT_INC_13(macro, split, __VA_ARGS__) split() macro(14, __VA_ARGS__)
#define REPEAT_INC_15(macro, split, ...) REPEAT_INC_14(macro, split, __VA_ARGS__) split() macro(15, __VA_ARGS__)
#define REPEAT_INC_16(macro, split, ...) REPEAT_INC_15(macro, split, __VA_ARGS__) split() macro(16, __VA_ARGS__)
#define VAR_TYPES(N) REPEAT_INC(N, TYPENAME_ARGS, COMMA)
#define VAR_TYPES_DEFAULT(N,VALUE) REPEAT_INC(N, TYPENAME_VALUE_ARGS, COMMA, VALUE)
#define VAR_ARGS(N) REPEAT_INC(N, NAMED_ARG, COMMA, ArgumentType)
#define VAR_NAMED_ARGS(N, name) REPEAT_INC(N, NAMED_ARG, COMMA, name)
#define VOID_ARGS(N) REPEAT_INC(N, VOID_ARG, COMMA)
#define VAR_ARGS_MEMBERS(N, name, delim) REPEAT_INC(N, MEMBER_ARG, delim, name)
#define INIT_VAR_ARGS_MEMBERS(N, name) REPEAT_INC(N, INIT_MEMBER_ARG, COMMA, name)
#define INIT_VALUE_ARGS(N, name) REPEAT_INC(N, INIT_VALUE_ARG, SEMICOLON, name)
#define SERIALIZE_ARGS(N, name) REPEAT_INC(N, SER_ARG, SEMICOLON, name)
#define DESERIALIZE_ARGS(N, name) REPEAT_INC(N, DES_ARG, SEMICOLON, name)
#define GENERATE_METHOD_CALLBACK_SIGNATURES_ARGUMENTS(N, value) \
template<typename Caller, typename ReturnType, VAR_TYPES(N)> \
struct MethodCallbackSignature<Caller,ReturnType, VAR_ARGS(N)> { \
typedef ReturnType(Caller::*Method)(VAR_ARGS(N)); }; \
template<typename Caller, VAR_TYPES(N)> \
struct MethodCallbackSignature<Caller, void, VAR_ARGS(N)> { \
typedef void (Caller::*Method)(VAR_ARGS(N)); };
#define GENERATE_METHOD_CALLBACK_INVOKE_ARGUMENTS(N, value) \
template <class BASE, class Caller, class FutureResultType, class ReturnType, VAR_TYPES(N)> \
class MethodCallbackInvoke<BASE,Caller,FutureResultType,ReturnType, VAR_ARGS(N)> \
: public BaseMethodCallbackInvoke<BASE, Caller, FutureResultType, ReturnType> {public: \
typedef typename MethodCallbackSignature<Caller, ReturnType, VAR_ARGS(N)>::Method Callback; \
MethodCallbackInvoke(Caller* caller, Callback method, VAR_ARGS_MEMBERS(N, arg, COMMA), const FutureResultType& result) : \
BaseMethodCallbackInvoke(caller, result), _method(method), INIT_VAR_ARGS_MEMBERS(N, arg) {} \
protected: VAR_ARGS_MEMBERS(N, _arg, SEMICOLON); Callback _method; };
#define GENERATE_DELEGATE_INVOKE_ARGUMENTS(N, value) \
template <class BASE, class Caller, class FutureType, class ReturnType, VAR_TYPES(N)> \
class DelegateInvoke<BASE, Caller, FutureType, ReturnType, VAR_ARGS(N)> : \
public MethodCallbackInvoke<BASE, Caller, FutureType, ReturnType, VAR_ARGS(N)> {public: \
DelegateInvoke(Caller* caller, Callback method, VAR_ARGS_MEMBERS(N, arg, COMMA), const FutureType& result) \
: MethodCallbackInvoke(caller, method, VAR_NAMED_ARGS(N, arg), result) {} \
void invokeMethod() { _result.resolveData(new ReturnType((_context->*_method)(VAR_NAMED_ARGS(N, _arg))));}}; \
template <class BASE, class Caller, class FutureType, VAR_TYPES(N)> \
class DelegateInvoke<BASE, Caller, FutureType, void, VAR_ARGS(N)> : \
public MethodCallbackInvoke<BASE, Caller, FutureType, void, VAR_ARGS(N)>{public: \
DelegateInvoke(Caller* caller, Callback method, VAR_ARGS_MEMBERS(N, arg, COMMA), const FutureType& result) \
: MethodCallbackInvoke(caller, method, VAR_NAMED_ARGS(N, arg), result) {} \
void invokeMethod() { (_context->*_method)(VAR_NAMED_ARGS(N, _arg));}};
#define GENERATE_DELEGATE_ARGUMENTS(N, value) \
template <class Caller, class ReturnType, VAR_TYPES(N)> class Delegate<Caller, ReturnType, VAR_ARGS(N)> : \
public DelegateInvoke < DelegateBase, Caller, FutureResult<ReturnType>, ReturnType, VAR_ARGS(N) > { \
public: Delegate(Caller* caller, Callback method, VAR_ARGS_MEMBERS(N, arg, COMMA), const FutureResult<ReturnType>& result) \
: DelegateInvoke(caller, method, VAR_NAMED_ARGS(N, arg), result) {} }; \
template <class Caller, VAR_TYPES(N)> class Delegate<Caller, void, VAR_TYPES(N)> : \
public DelegateInvoke < DelegateBase, Caller, FutureResult<void>, void, VAR_ARGS(N) >{ \
public: Delegate(Caller* caller, Callback method, VAR_ARGS_MEMBERS(N, arg, COMMA), const FutureResult<void>& result) \
: DelegateInvoke(caller, method, VAR_NAMED_ARGS(N, arg), result) {}};
#define GENERATE_MULTICAST_ARGUMENTS(N, value) \
template<class ReturnType, VAR_TYPES(N)> \
class IDelegatePublisher<ReturnType, VAR_ARGS(N)> {public: \
virtual FutureResult<ReturnType> publish(VAR_ARGS_MEMBERS(N, arg, COMMA), int priority = 0, long delay = 0, long timeout = 0) = 0; \
}; \
template <class ReturnType, VAR_TYPES(N)> \
class MulticastDelegate<ReturnType, VAR_ARGS(N)> : public IDelegatePublisher<ReturnType, VAR_ARGS(N)>{private: \
typedef std::vector< IDelegatePublisher<ReturnType, VAR_ARGS(N)>* > SubscribersList; \
SubscribersList subscribers; \
public: \
MulticastDelegate() {} \
MulticastDelegate& operator += (IDelegatePublisher<ReturnType, VAR_ARGS(N)>* method) { \
subscribers.push_back(method); \
return *this;} \
void notify(VAR_ARGS_MEMBERS(N, arg, COMMA), int priority = 0, long delay = 0, long timeout = 0) { \
typename SubscribersList::iterator it = subscribers.begin(); \
for (; it != subscribers.end(); it++) { (*it)->publish(VAR_NAMED_ARGS(N, arg), priority, delay, timeout); } } \
FutureResult<ReturnType> operator () (VAR_ARGS_MEMBERS(N, arg, COMMA), int priority = 0, long delay = 0, long timeout = 0) { \
FutureResult<ReturnType> result = publish(VAR_NAMED_ARGS(N, arg), priority, delay, timeout); \
notify(VAR_NAMED_ARGS(N, arg), priority, delay, timeout); \
return result;}};
#define GENERATE_ACTIVE_ARGUMENTS(N, value) \
template <class Caller, class ReturnType, VAR_TYPES(N)> class Active<Caller, ReturnType, VAR_ARGS(N)> : public ActiveConstraintUID, public MulticastDelegate<ReturnType, VAR_ARGS(N)> { public: \
typedef Delegate<Caller, ReturnType, VAR_ARGS(N)> DelegateType; \
typedef typename MethodCallbackSignature<Caller, ReturnType, VAR_ARGS(N)>::Method Callback; \
Active(Caller* caller, Scheduler<Caller>* sch, Callback method) :_context(caller), _sch(sch), _method(method) {} \
FutureResult<ReturnType> operator () (VAR_ARGS_MEMBERS(N, arg, COMMA), int priority = 0, long delay = 0, long timeout = 0) { \
FutureResult<ReturnType> result = publish(VAR_NAMED_ARGS(N, arg), priority, delay, timeout); return result;}; \
FutureResult<ReturnType> publish(VAR_ARGS_MEMBERS(N, arg, COMMA), int priority = 0, long delay = 0, long timeout = 0) { \
FutureResult<ReturnType> result(new FutureObject<ReturnType>()); \
DelegateBase::Ptr pDelegate(new DelegateType(_context, _method, VAR_NAMED_ARGS(N, arg), result)); \
_sch->schedule(pDelegate,priority,delay, timeout, _guardId, _conditionId); \
this->notify(VAR_NAMED_ARGS(N, arg), priority, delay, timeout); \
return result;} private: Caller* _context; Scheduler<Caller>* _sch; Callback _method; };
#define GENERATE_METHOD_CALLBACK_SIGNATURES(N) REPEAT_DEC(N, GENERATE_METHOD_CALLBACK_SIGNATURES_ARGUMENTS, EMPTY)
#define GENERATE_METHOD_CALLBACK_INVOKE(N) REPEAT_DEC(N, GENERATE_METHOD_CALLBACK_INVOKE_ARGUMENTS, EMPTY)
#define GENERATE_DELEGATE_INVOKE(N) REPEAT_DEC(N, GENERATE_DELEGATE_INVOKE_ARGUMENTS, EMPTY)
#define GENERATE_DELEGATE(N) REPEAT_DEC(N, GENERATE_DELEGATE_ARGUMENTS, EMPTY)
#define GENERATE_MULTICAST_METHOD(N) REPEAT_DEC(N, GENERATE_MULTICAST_ARGUMENTS, EMPTY)
#define GENERATE_ACTIVE_METHOD(N) REPEAT_DEC(N, GENERATE_ACTIVE_ARGUMENTS, EMPTY)
#ifdef WINDOWS_OS
#define isnan(x) _isnan(x)
#define isinf(x) (!_finite(x))
#define SOCKET_TYPE SOCKET
#define CLOSE_SOCKET(arg) \
closesocket(arg)
#define EVENT_TYPE HANDLE
#define CONDITION_TYPE PLACE_HOLDER
#define THREAD_TYPE HANDLE
#define THREAD_RETURN_TYPE unsigned WINAPI
#define THREAD_ERROR_INSTANCE(returnValue) ((returnValue) == NULL)
#define THREAD_ERROR_CODE(value) GetLastError()
#define MUTEX_CRITICAL_SECTION CRITICAL_SECTION
#define START_MUTEX_FUNCTION(arg) \
InitializeCriticalSection((arg))
#define TERMINATE_MUTEX_FUNCTION(arg) \
DeleteCriticalSection((arg))
#define LOCK_MUTEX_FUNCTION(arg) \
EnterCriticalSection((arg))
#define UNLOCK_MUTEX_FUNCTION(arg) \
LeaveCriticalSection((arg))
#define START_EVENT_TYPE_FUNCTION(mutex, cond, reset) \
mutex = CreateEvent(NULL, reset, FALSE, NULL); \
if (!mutex) \
throw ThreadException("mutex signal failed")
#define TERMINATE_EVENT_FUNCTION(mutex, cond) \
CloseHandle(mutex)
#define WAIT_EVENT_FUNCTION(mutex, cond, wakeup) \
switch(WaitForSingleObject(mutex, INFINITE)) { \
case WAIT_OBJECT_0: \
return; \
default: \
throw ThreadException("wait event failed"); \
}
#define WAIT_TIME_EVENT_FUNCTION(mutex, cond, time, wakeup, reset, status) \
switch (WaitForSingleObject(mutex, time + 1)) \
{ \
case WAIT_OBJECT_0: \
status = true; \
break; \
case WAIT_TIMEOUT: \
status = false; \
break; \
default: \
throw ThreadException("wait failed"); \
}
#define WAKEUP_EVENT_FUNCTION(mutex, cond, wakeup) \
SetEvent(mutex)
#define THREAD_JOIN_FUNCTION(hdl) WaitForSingleObject(hdl, INFINITE)
#define THREAD_SLEEP_FUNCTION(ms) Sleep((ms))
#define THREAD_CREATE_FUNCTION(id, funPtr, callPtr) id =(HANDLE)CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)funPtr,callPtr,0L,NULL)
#define THREAD_TERMINATE_FUNCTION(hdl) TerminateThread(hdl, 0)
#define THREAD_CANCEL_FUNCTION(hdl) TerminateThread(hdl, 0)
#define IS_THREAD_ALIVE_FUNCTION(hdl, isRunning) \
DWORD exitCode = 0; \
if(GetExitCodeThread(hdl, &exitCode)) \
isRunning = (exitCode == STILL_ACTIVE)
#define BROADCAST_FUNCTION(arg) 0
#define SET_EVENT_FUNCTION(arg) \
SetEvent((arg))
#define RESET_EVENT_FUNCTION(arg) \
ResetEvent((arg))
#define LOCK_MUTEX_EVENT_FUNCTION(arg) 0
#define UNLOCK_MUTEX_EVENT_FUNCTION(arg) 0
#else
typedef int BOOL;
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#define SOCKET_TYPE int
#define CLOSE_SOCKET(arg) \
close(arg)
#define EVENT_TYPE pthread_mutex_t
#define CONDITION_TYPE pthread_cond_t
#define THREAD_TYPE pthread_t
#define THREAD_RETURN_TYPE void *
#define THREAD_ERROR_INSTANCE(returnValue) ((returnValue) == NULL)
#define THREAD_ERROR_CODE(value) errno
#define MUTEX_CRITICAL_SECTION pthread_mutex_t
#define START_MUTEX_FUNCTION(arg) \
pthread_mutex_init ((arg), NULL)
#define TERMINATE_MUTEX_FUNCTION(arg) \
pthread_mutex_destroy((arg))
#define LOCK_MUTEX_FUNCTION(arg) \
pthread_mutex_lock((arg))
#define UNLOCK_MUTEX_FUNCTION(arg) \
pthread_mutex_unlock((arg))
#define START_EVENT_TYPE_FUNCTION(mutex, cond, reset) \
if (pthread_mutex_init(&mutex, NULL)) \
throw ThreadException("mutex signal failed"); \
pthread_cond_init(&cond, NULL)
#define TERMINATE_EVENT_FUNCTION(mutex, cond) \
pthread_cond_destroy(&cond); \
pthread_mutex_destroy(&mutex)
#define WAIT_EVENT_FUNCTION(mutex, cond, wakeup) \
pthread_mutex_lock(&mutex); \
int err = 0; \
while (!wakeup) { \
err = pthread_cond_wait(&cond, &mutex); \
if (err) { \
pthread_mutex_unlock(&mutex); \
throw ThreadException("wait event failed"); \
} \
} \
wakeup = FALSE; \
pthread_mutex_unlock(&mutex)
#define WAIT_TIME_EVENT_FUNCTION(mutex, cond, ms, wakeup, reset, status) \
struct timeval tv \
struct timespec tdif \
gettimeofday(&tv, NULL) \
tdif.tv_sec = tv.tv_sec + ms / MUSECS_PER_SEC \
tdif.tv_nsec = tv.tv_usec*MUSECS_PER_SEC + (ms % MUSECS_PER_SEC)*USECS_PER_SEC \
if (tdif.tv_nsec >= NSECS_PER_SEC) { \
tdif.tv_nsec -= NSECS_PER_SEC \
tdif.tv_sec++ \
} \
pthread_mutex_lock(&mutex) \
while (!wakeup) \
{ \
status = pthread_cond_timedwait(&cond, &mutex, &tdif) \
if(status) { \
if (status == ETIMEDOUT) break; \
pthread_mutex_unlock(&mutex) \
throw ThreadException(get_error(status)) \
} \
} \
wakeup = status == 0 && reset ? false : wakeup \
pthread_mutex_unlock(&mutex)
#define WAKEUP_EVENT_FUNCTION(mutex, cond, wakeup) \
pthread_mutex_lock(&mutex); \
wakeup = TRUE; \
pthread_cond_signal(&cond); \
pthread_mutex_unlock(&mutex)
#define THREAD_JOIN_FUNCTION(id) pthread_join(id, NULL)
#define THREAD_SLEEP_FUNCTION(ms) \
struct timeval tv; \
tv.tv_usec = (ms % MUSECS_PER_SEC) * USECS_PER_MSEC; \
tv.tv_sec = ms / MUSECS_PER_SEC; \
select(0, NULL, NULL, NULL, &tv)
#define THREAD_CREATE_FUNCTION(id, funPtr, callPtr) \
pthread_attr_t attr; \
pthread_attr_init(&attr); \
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); \
pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); \
pthread_create(&id, &attr, funPtr, callPtr); \
pthread_attr_destroy(&attr)
#define THREAD_TERMINATE_FUNCTION(arg) pthread_exit(arg)
#define THREAD_CANCEL_FUNCTION(Id) \
if (pthread_cancel(Id) == 0) \
pthread_detach(Id);
#define IS_THREAD_ALIVE_FUNCTION(hdl, isRunning) \
isRunning = (pthread_kill(hdl, 0) == 0)
#define BROADCAST_FUNCTION(arg) \
pthread_cond_broadcast((arg))
#define SET_EVENT_FUNCTION(arg) 1
#define RESET_EVENT_FUNCTION(arg) 1
#define LOCK_MUTEX_EVENT_FUNCTION(arg) \
pthread_mutex_lock((arg))
#define UNLOCK_MUTEX_EVENT_FUNCTION(arg) \
pthread_mutex_unlock((arg))
#endif
#define CREATE_THREAD(id, funPtr, callPtr) \
THREAD_CREATE_FUNCTION(id, funPtr, callPtr); \
if(THREAD_ERROR_INSTANCE(id)) \
throw ThreadException(ErrorMessage(THREAD_ERROR_CODE(id)))
static std::string ErrorMessage(int errorCode){
string str = "";
if (errorCode == 0) {
return str;
}
char const* what = "Error Numer";
int whlen = strlen(what);
int ncode = errorCode, dlen = 1;
while (ncode !=0) {dlen++; ncode/=10;}
char *buffer = (char *) malloc(sizeof(char) * (whlen + dlen + 1));
sprintf(buffer, "%s = %d", what, errorCode);
str = buffer;
return str;
}
class Exception : public std::exception {
friend ostream& operator<<(ostream& output, const Exception& ex) {
output << "Exception: " << ex.error;
return output;
}
public:
Exception() throw() : error(std::string("Exception")) {}
Exception(std::string err) throw() : error(err) {}
Exception(const Exception& source) throw() : std::exception(source) { error = source.error; }
virtual ~Exception() throw () {}
Exception& operator=(const Exception& source) throw() {
if (&source != this) {
error = source.error;
}
std::exception::operator= (source);
return *this;
}
void setError(std::string exce) { error = exce; }
virtual const char* what() const throw() { return error.c_str(); }
protected:
std::string error;
};
struct ThreadException : public Exception{
public:
ThreadException() : Exception() {}
ThreadException(char* errorMessage) : Exception(errorMessage) {}
ThreadException(std::string errorMessage) : Exception(errorMessage) {}
};
template <typename T>
void* ConvertToFunctionPointer(T x) {
return *reinterpret_cast<void**>(&x);
}
struct Runnable {
virtual void run() = 0;
};
class AtomicMutex{
public:
AtomicMutex() { START_MUTEX_FUNCTION(§ion); }
~AtomicMutex() { TERMINATE_MUTEX_FUNCTION(§ion); }
void lock() { LOCK_MUTEX_FUNCTION(§ion); }
void unlock() { UNLOCK_MUTEX_FUNCTION(§ion); }
private:
MUTEX_CRITICAL_SECTION section;
};
static AtomicMutex atomicMutex;
class MutexLock{
public:
MutexLock() : m_value(0){
wakeup = FALSE;
START_EVENT_TYPE_FUNCTION(mutex, cond, FALSE);
}
MutexLock(const MutexLock& m) {
this->m_value = m.m_value;
this->wakeup = m.wakeup;
this->mutex = m.mutex;
this->cond = m.cond;
}
~MutexLock(){
TERMINATE_EVENT_FUNCTION(mutex, cond);
}
void wait() {
WAIT_EVENT_FUNCTION(mutex, cond, wakeup);
}
void wakeUp() {
WAKEUP_EVENT_FUNCTION(mutex, cond, wakeup);
}
void lock() {
bool canBeAccessed = this->verifyLock();
if (canBeAccessed){
return;
}
atomicMutex.lock();
canBeAccessed = m_value == 0;
m_value += 1;
atomicMutex.unlock();
if (!canBeAccessed){
wait();
atomicMutex.lock();
m_value -= 1;
atomicMutex.unlock();
}
}
bool isLocked() {
atomicMutex.lock();
bool canBeAccessed = m_value == 0;
atomicMutex.unlock();
return !canBeAccessed;
}
bool verifyLock(int set = 0) {
atomicMutex.lock();
bool canBeAccessed = m_value == 0;
if (m_value == set) {
m_value = !set;
canBeAccessed = true;
}
atomicMutex.unlock();
return canBeAccessed;
}
void unlock() {
if (!verifyLock(1)){
wakeUp();
}
}
private:
volatile int m_value;
volatile bool wakeup;
EVENT_TYPE mutex;
CONDITION_TYPE cond;
};
#define synchronized(L) \
for(L.lock();L.isLocked(); \
L.unlock() )
struct ThreadParameters
{
void* runCall;
void* context;
ThreadParameters(void* ctx, void* callPtr) : context(ctx), runCall(callPtr) {}
};
class Thread: public Runnable {
public:
Thread(string threadName = "") : runnableObject(NULL), name(threadName), thrParams(this,ConvertToFunctionPointer(&Thread::run))
, _threadHdl(0),_isRunning(false),_isTerminated(false) {}
Thread(Runnable *target, string threadName = "") : name(threadName),thrParams(this,ConvertToFunctionPointer(&Thread::run)) , runnableObject(target) {}
Thread(void* funcPtr, void* ctx = 0 ,string threadName = "") : runnableObject(NULL), name(threadName), thrParams(ctx,funcPtr) {}
Thread(void (*funcPtr)(void*), void* ctx = 0, string threadName = "") : runnableObject(NULL),name(threadName), thrParams(ctx,ConvertToFunctionPointer(funcPtr)) {}
Thread(void (*funcPtr)(), string threadName = "") : runnableObject(NULL),name(threadName), thrParams(this,ConvertToFunctionPointer(funcPtr)) {}
template<class T>
Thread(void (T::*RunnableCall)(), string threadName = "") : runnableObject(NULL), name(threadName), thrParams(this,ConvertToFunctionPointer(RunnableCall)) {}
template<class T>
Thread(void (T::*RunnableCall)(void*), void* ctx = 0, string threadName = "") : runnableObject(NULL), name(threadName), thrParams(ctx,ConvertToFunctionPointer(RunnableCall)) {}
virtual ~Thread() {
THREAD_TERMINATE_FUNCTION(0);
}
static void sleep(long ms) throw(ThreadException) {
THREAD_SLEEP_FUNCTION(ms);
}
THREAD_TYPE getId() const {
return this->_threadHdl;
}
static THREAD_RETURN_TYPE threadFunctionPointer(void* ptr) {
ThreadParameters* threadParameters = (ThreadParameters*)ptr;
if(threadParameters->context != NULL) {
Runnable* run = (Thread*) threadParameters->context;
run->run();
//((void (*)(void*)) threadParameters->runCall)(threadParameters->context);
} else {
((void (*)(void)) threadParameters->runCall)();
}
return 0;
}
bool isFinished() {
return !this->isRunning();
}
bool isTerminated() {
synchronized(lock) {
return this->_isTerminated;
}
return 0;
}
bool isRunning() {
synchronized(lock) {
if(this->_isRunning) {
IS_THREAD_ALIVE_FUNCTION(_threadHdl,_isRunning);
}
return this->_isRunning;
}
return 0;
}
virtual void run() {
if(this->runnableObject != NULL) {
runnableObject->run();
}
}
virtual void stop() {
synchronized(lock) {
THREAD_CANCEL_FUNCTION(_threadHdl);
_isTerminated = true;
_isRunning = false;
}
}
string getName() const {
return name;
}
void setName(string name) {
this->name = name;
}
virtual void start(Runnable *target) throw(ThreadException) {
runnableObject = target;
start();
}
virtual void start() throw(ThreadException) {
synchronized(lock) {
reset();
CREATE_THREAD(_threadHdl,Thread::threadFunctionPointer, &thrParams);
_isRunning = true;
}
}
void join() throw(ThreadException) {
THREAD_JOIN_FUNCTION(_threadHdl);
}
void wait() throw(ThreadException) {
lock.wait();
}
void wakeUp() throw(ThreadException) {
lock.wakeUp();
}
private:
mutable MutexLock lock;
THREAD_TYPE _threadHdl;
string name;
ThreadParameters thrParams;
Runnable* runnableObject;
bool _isRunning;
bool _isTerminated;
void reset() {
_threadHdl = 0;
_isRunning = false;
_isTerminated = false;
}
};
template <class PT>
class AutoPtr {
public:
AutoPtr(PT* p = NULL, bool shared = false) : _ptr(NULL) { _ptr=p; if (shared && _ptr) _ptr->reference(); }
AutoPtr(const AutoPtr& ptr) : _ptr(NULL) { _ptr=ptr._ptr; if (_ptr) _ptr->reference(); }
~AutoPtr() { if (_ptr) _ptr->release(); }
AutoPtr& operator = (const AutoPtr& ptr) {
if (&ptr != this) {
if (_ptr) _ptr->release();
_ptr = ptr._ptr;
if (_ptr) _ptr->reference();
}
return *this;
}
PT* operator -> () {
if (_ptr)
return _ptr;
else
throw std::exception("Null Pointer Exception");
}
PT* reference() { if (_ptr) _ptr->reference(); return _ptr; }
PT* value() { return _ptr; }
operator const PT* () const { return _ptr; }
bool operator == (const AutoPtr& ptr) const { return _ptr == ptr._ptr; }
bool operator != (const AutoPtr& ptr) const { return _ptr != ptr._ptr; }
private:
PT* _ptr;
};
class ReferenceObject {
private:
mutable MutexLock mutex;
mutable volatile int referenceNumber;
public:
ReferenceObject() {
synchronized(mutex) {
referenceNumber = 1;
}
}
void reference() const {
synchronized(mutex) {
++referenceNumber;
}
}
void release() const {
synchronized(mutex) {
--referenceNumber;
}
if (referenceNumber == 0) {
delete this;
}
}
size_t size() const {
int val = 0;
synchronized(mutex) {
val = referenceNumber;
}
}
protected:
virtual ~ReferenceObject() {}
};
template <typename Object> struct RefPointer : public ReferenceObject { typedef AutoPtr<Object> Ptr; };
class AutoLock {
public:
explicit AutoLock(MutexLock& mutex) : _mutex(mutex) { _mutex.lock(); }
~AutoLock() { try{ _mutex.unlock(); } catch (...) {} }
private:
MutexLock& _mutex;
};
class Signal {
public:
Signal(bool manualReset = true) {
START_EVENT_TYPE_FUNCTION(_mutex, _cond, manualReset ? FALSE : TRUE);
}
~Signal() {
TERMINATE_EVENT_FUNCTION(_mutex, _cond);
}
void notify() {
if (LOCK_MUTEX_EVENT_FUNCTION(&_mutex))
throw ThreadException("cannot notify lock");
if (BROADCAST_FUNCTION(&_cond))
{
UNLOCK_MUTEX_EVENT_FUNCTION(&_mutex);
throw ThreadException("cannot notify lock");
}
if (!SET_EVENT_FUNCTION(_mutex))
{
throw ThreadException("cannot notify lock");
}
UNLOCK_MUTEX_EVENT_FUNCTION(&_mutex);
}
void wait() {
WAIT_EVENT_FUNCTION(_mutex, _cond, _state);
}
bool wait(long ms, bool timeout = false) {
int status = false;
WAIT_TIME_EVENT_FUNCTION(_mutex, _cond, ms, _state, _auto, status)
if (timeout && !status)
throw ThreadException("Timeout Exception");
return status;
}
void reset()
{
if (LOCK_MUTEX_EVENT_FUNCTION(&_mutex)) {
throw ThreadException("reset signal lock");
}
if (!RESET_EVENT_FUNCTION(_mutex)) {
throw ThreadException("reset signal lock");
}
UNLOCK_MUTEX_EVENT_FUNCTION(&_mutex);
}
private:
EVENT_TYPE _mutex;
CONDITION_TYPE _cond;
};
class UID {
public:
unsigned int _uid;
UID() { _uid = ++uid; }
UID(const UID& uid) { _uid = uid._uid; }
UID& operator=(const UID& uid) { _uid = uid._uid; return(*this); }
operator int() { return _uid; }
bool operator == (const UID& uid) const { return _uid == uid._uid; }
bool operator != (const UID& uid) const { return _uid != uid._uid; }
bool operator == (const unsigned int& uid) const { return _uid == uid; }
bool operator != (const unsigned int& uid) const { return _uid != uid; }
protected:
static unsigned int uid;
};
template <class DataType>
class DataResolver {
public:
DataResolver() : _data(0) { }
DataType& data() {
return *_data;
}
void resolveData(DataType* data) {
delete _data;
_data = data;
}
private:
DataType* _data;
};
class ErrorResolver {
public:
ErrorResolver() :_error(0) {}
std::string getErrorMessage() const {
return (_error) ? _error->what() : std::string();
}
std::exception* getError() const {
return _error;
}
void resolveError(const std::string& msg) {
delete _error;
_error = new std::exception(msg.c_str());
}
bool hasError() const {
return _error != 0;
}
private:
std::exception* _error;
};
class SharedObject : public ReferenceObject {
public:
SharedObject() : _signal(false) {}
void wait() {
_signal.wait();
}
bool wait(long ms, bool timeout = false) {
return _signal.wait(ms, timeout);
}
void notify() {
_signal.notify();
}
private:
Signal _signal;
};
template <typename T>
struct SharedObjectProxy
{
typedef T* DataTypePtr;
public:
SharedObjectProxy(DataTypePtr data) : _data(data) {}
SharedObjectProxy(const SharedObjectProxy& proxy) {
_data = proxy._data;
_data->reference();
}
~SharedObjectProxy() {
_data->release();
}
void snooze() {
_data->wait(0); //for (; data->wait(10); );
}
void wait() {
_data->wait();
}
bool wait(long ms, bool timeout = false) {
return _data->wait(ms, timeout);
}
bool ready() const {
return _data->wait(0);
}
void notify() {
_data->notify();
}
protected:
SharedObjectProxy();
DataTypePtr _data;
};
template <typename BASE, typename T = typename BASE::DataTypePtr>
struct ErrorProxy : public BASE {
public:
ErrorProxy(T data) : BASE(data) {}
ErrorProxy(const ErrorProxy& proxy) : BASE(proxy) {}