forked from OlehKulykov/FayeCpp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fayecpp.h
4128 lines (3202 loc) · 104 KB
/
fayecpp.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
/*
* Copyright (c) 2014 - 2016 Kulykov Oleh <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __FAYECPP_FAYECPP_H__
#define __FAYECPP_FAYECPP_H__
/*
* Faye C++ client main and one header file.
* All class interfaces added to namespace, preventing include files mess(yes, this is unusual structure, but lightweight).
*
* Changes on version 1.2.0 (current):
* - Update Libwebsockets to version 2.0.0.
* - Update jansson to version 2.8.
* - Update client info message.
*
* Changes on version 1.1.1:
* - Windows mutex blocked lock.
*
* Changes on version 1.1.0:
* - Advice reconnect functionality can be controlled.
*
* Changes on version 1.0.3:
* - Update Libwebsockets to version 1.7.
* - Update jansson to latest stable version.
* - Update wolfssl to latest stable version.
*
* Changes on version 1.0.1:
* - Update Libwebsockets and ssl to latest stable version.
*
* Changes on version 1.0.0:
* - Stable Objective-C and C++ clients.
*
* Changes on version 0.2.1:
* - SSL Mac & mobile support.
*
* Changes on version 0.2.0:
* - Minimum supported client version is 1.0.
* - Public advice information.
* - Use Libwebsockets version 1.6.
* - Automatic reconnect using advice information (Libwebsockets & QWebSocket transport version).
*
* Changes on version 0.1.16:
* - Cocoapod with OpenSSL support(pod 'FayeCpp+OpenSSL'), recommended for all Faye users.
*
* Changes on version 0.1.15:
* - Important json update.
* - Patch for building in Windows with MSC version 19 (Windows SDK 10).
* - Remove redefinition of preprocessor macros.
*
* Changes on version 0.1.14:
* - Minor JSON parsing optimizations.
* - Minor client responce processing optimizations.
* - Refactor websocket connection method and remove unused string data copying.
* - Variant holder structure modifications and remove C casting.
* - iOS Swift example application.
* - Fix REVariantMap comparation.
*
* Changes on version 0.1.13:
* - Objective-C - no need to control delegate pointer during deallocating.
* - Objective-C client nullable & nonnullable methods/properties, need for integration with Swift.
*
* Changes on version 0.1.12:
* - Cocoapod now available also for OSX 10.7 and later.
* - Objective-C client minor update.
*
* Changes on version 0.1.11:
* - Added variant objects compare functionality.
* - Detecting UTF8 strings fix.
*
* Changes on version 0.1.10:
* - Added faye client connect test (connect, handshake and subscribing).
* - Minor WebSocket transport based on libwebsockets refactoring (reduce code size).
* - Rename some internal source/header files, cause of file name duplication with
* other third party libraries (prevent compile issue with Qt 5.4.1 by Qt Creator 3.3.2).
* - MinGW compiler support.
*
* Changes on version 0.1.9:
* - Objective-C client wrapper speed optimization (used only NON-ARC mode and CoreFoundation framework).
* - Apple localization bundle fix.
*
* Changes on version 0.1.8:
* - Added additional error processing with new Error class.
*
* Changes on version 0.1.7:
* - Minor libwebsockets fixes.
* - Added error processing of received messages.
* - Client transport based on Libwebsockets can automatically self destruct on socket error.
*
* Changes on version 0.1.6:
* - Added extra(ext) message field included in any Bayeux message.
* The contents of ext message field may be arbitrary values that allow extensions
* to be negotiated and implemented between server and client implementations.
* http://docs.cometd.org/2/reference/bayeux_message_fields.html
*
* Changes on version 0.1.5:
* - Added secure socket connection support with SSL data source.
* - Client transport become logically detached from the client which adds possibility
* to control client logic from delegate methods and from another thread.
* - Added thread safety to WebSocket transport based on libwebsockets.
* - Added processing of destroying socket context during some inactive time.
* - Optimized code of WebSocket transport based on libwebsockets.
* - Removed unused classes RETime, REThread and REMutex.
* - Redused size of code and library size thanks to previous two punkts.
* - Added client library build information.
* - Build results (binary library/framework & headers) from continuous integration systems now stores
* and available on GitHub release page: https://github.com/OlehKulykov/FayeCpp/releases
*
* Changes on version 0.1.4:
* - Possibility to switch client between IPV4 & IPV6 if possible.
*
* Changes on version 0.1.3:
* - Added processing large received binary and text frames in case using 'libwebsockets'.
*
* Changes on version 0.1.2:
* - Added autoreconnect to the client while disconnect with unknown error (not by user).
* - Added to cocoapods repository.
*
* Version 0.1.1:
* - Implemented basic logic.
*/
#define FAYECPP_VERSION_MAJOR 1
#define FAYECPP_VERSION_MINOR 2
#define FAYECPP_VERSION_PATCH 0
#if !defined(HAVE_SUITABLE_QT_VERSION)
/* Try to check Qt version, should be more or equal than 5.3.0 */
#if defined(QT_VERSION) && defined(QT_VERSION_CHECK)
#if (QT_VERSION >= QT_VERSION_CHECK(5, 3, 0))
#define HAVE_SUITABLE_QT_VERSION 1
#endif
#endif /* End check Qt version */
#endif /* End check exestance of Qt suitable version */
#if !defined(__RE_OS_WINDOWS__) && !defined(__RE_OS_ANDROID__)
/* OS not selected, try detect OS */
#if (defined(WIN32) || defined(_WIN32) || defined(WIN32_LEAN_AND_MEAN) || defined(_WIN64) || defined(WIN64))
#define __RE_OS_WINDOWS__ 1
#ifndef WIN32_LEAN_AND_MEAN
/* Exclude rarely-used stuff from Windows headers */
#define WIN32_LEAN_AND_MEAN
#endif /* WIN32_LEAN_AND_MEAN */
#if !defined(__RE_COMPILER_MINGW__)
#if defined(__MINGW32__) || defined(__MINGW64__) || defined(MINGW)
#define __RE_COMPILER_MINGW__ 1
#endif
#endif
#endif /* END CHECKING WINDOWS PLATFORM */
/***********************************************************************************/
#if defined(ANDROID_NDK) || defined(__ANDROID__) || defined(ANDROID)
#define __RE_OS_ANDROID__ 1
#endif /* END CHECKING ANDROID PLATFORM */
/***********************************************************************************/
#endif /* END DETECT OS */
#if defined(TARGET_OS_MAC) || defined(TARGET_OS_IPHONE) || defined(TARGET_IPHONE_SIMULATOR) || defined(__APPLE__)
#ifndef __APPLE__
#define __APPLE__ 1
#endif
#endif
#if defined(__cplusplus) || defined(_cplusplus)
#define __RE_EXTERN__ extern "C"
#else
#define __RE_EXTERN__ extern
#endif
#if defined(__RE_OS_WINDOWS__) && !defined(HAVE_SUITABLE_QT_VERSION) && !defined(FAYECPP_STATIC)
#include <windows.h>
#if defined(CMAKE_BUILD) || defined(__BUILDING_RECORE_DYNAMIC_LIBRARY__)
# if defined(_MSC_VER) || defined(__RE_COMPILER_MINGW__)
# define __RE_PUBLIC_CLASS_API__ __declspec(dllexport)
# define __RE_EXPORT__ __RE_EXTERN__ __declspec(dllexport)
# elif defined(__GNUC__)
# define __RE_PUBLIC_CLASS_API__ __attribute__((dllexport))
# define __RE_EXPORT__ __RE_EXTERN__ __attribute__((dllexport))
# endif
#else
# if defined(_MSC_VER) || defined(__RE_COMPILER_MINGW__)
# define __RE_PUBLIC_CLASS_API__ __declspec(dllimport)
# define __RE_EXPORT__ __RE_EXTERN__ __declspec(dllimport)
# elif defined(__GNUC__)
# define __RE_PUBLIC_CLASS_API__ __attribute__((dllimport))
# define __RE_EXPORT__ __RE_EXTERN__ __attribute__((dllimport))
# endif
#endif
#endif /* __RE_OS_WINDOWS__ */
#if defined(__GNUC__)
# if __GNUC__ >= 4
# if !defined(__RE_PUBLIC_CLASS_API__)
# define __RE_PUBLIC_CLASS_API__ __attribute__ ((visibility("default")))
# endif
# endif
#endif
#ifndef __RE_EXPORT__
#define __RE_EXPORT__ __RE_EXTERN__
#endif
#ifndef __RE_PUBLIC_CLASS_API__
#define __RE_PUBLIC_CLASS_API__
#endif
/* Standart C Library headers */
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <limits.h>
#include <math.h>
#include <float.h>
#include <string.h>
#include <time.h>
#include <wchar.h>
/* C++ Standard Library header */
#ifndef __RE_OS_ANDROID__
#include <vector>
#endif
#if defined(__RE_OS_ANDROID__)
#include <setjmp.h>
#include <ctype.h>
#include <cerrno>
#include <cstddef>
#include <sys/types.h>
#include <sys/errno.h>
#endif
#if defined(RE_HAVE_ASSERT_H)
#include <assert.h>
#endif
/**
@brief 8 bit unsigned byte type.
@detailed 8 bit unsigned byte with 1 byte size.
*/
typedef uint8_t REUByte;
/**
@brief 16 bit unsigned integer type.
@detailed 16 bit unsigned integer with 2 byte size.
*/
typedef uint16_t REUInt16;
/**
@brief 32 bit unsigned integer type.
@detailed 32 bit unsigned integer with 4 byte size.
*/
typedef uint32_t REUInt32;
/**
@brief 64 bit unsigned integer type.
@detailed 64 bit unsigned integer with 8 byte size.
*/
typedef uint64_t REUInt64;
/**
@brief 8 bit signed byte type.
@detailed 8 bit signed byte with 1 byte size.
*/
typedef int8_t REByte;
/**
@brief 16 bit signed integer type.
@detailed 16 bit signed integer with 2 byte size.
*/
typedef int16_t REInt16;
/**
@brief 32 bit signed integer type.
@detailed 32 bit signed integer with 4 byte size.
*/
typedef int32_t REInt32;
/**
@brief 64 bit signed integer type.
@detailed 64 bit signed integer with 8 byte size.
*/
typedef int64_t REInt64;
/**
@brief Unsigned identifier integer type.
@detailed unsigned identifier integer type size is same as pointer.
*/
typedef uintptr_t REUIdentifier;
/**
@brief Boolean type.
@detailed Boolean type with possible values: 'true' or 'false'
*/
typedef bool REBOOL;
#if defined(SIZEOF_FLOAT_T)
/**
@brief 32 bit float type.
@detailed 32 bit float type with 4 byte size.
*/
typedef float_t REFloat32;
#else
/**
@brief 32 bit float type.
@detailed 32 bit float type with 4 byte size.
*/
typedef float REFloat32;
#endif
#if defined(SIZEOF_DOUBLE_T)
/**
@brief 64 bit float type.
@detailed 64 bit float type with 8 byte size.
*/
typedef double_t REFloat64;
#else
/**
@brief 64 bit float type.
@detailed 64 bit float type with 8 byte size.
*/
typedef double REFloat64;
#endif
/**
@brief Time interval type.
@detailed Time interval type with 8 byte size. Used for holding seconds with fractional part
*/
typedef REFloat64 RETimeInterval;
/**
@brief Define for NULL pointer.
*/
#ifndef NULL
#define NULL 0
#endif
/**
@brief Define for 32 bit unsigned integer maximum value.
*/
#ifndef UINT32_MAX
#define UINT32_MAX (4294967295u)
#endif
/**
@brief Define for 32 bit unsigned integer maximum value.
*/
#ifndef REUInt32Max
#define REUInt32Max UINT32_MAX
#endif
/**
@brief Define for 16 bit unsigned integer maximum value.
*/
#ifndef USHRT_MAX
#define USHRT_MAX (65535u)
#endif
/**
@brief Define for 16 bit unsigned integer maximum value.
*/
#ifndef REUInt16Max
#define REUInt16Max USHRT_MAX
#endif
/**
@brief Define for default signed integer(int) maximum value.
*/
#ifndef INT_MAX
#define INT_MAX (2147483647)
#endif
/**
@brief Define for default signed integer(int) minimum value.
*/
#ifndef INT_MIN
#define INT_MIN (-2147483648)
#endif
/**
@brief Define for indicating that something can't be found or undefined.
*/
#define RENotFound REUInt32Max
/**
@brief Define for indicating that index can't be found.
*/
#define REIndexNotFound REUInt32Max
#ifndef MIN
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#endif
namespace FayeCpp {
#if defined(__APPLE__)
/**
@brief Name of the bundle contained localization for faye client.
*/
__RE_EXPORT__ const char * const kFayeCppBundleName;
/**
@brief Localization table name
*/
__RE_EXPORT__ const char * const kFayeCppBundleLocalizationTableName;
#endif
class Client;
class Responce;
class REVariantList;
class REVariantMap;
class REVariant;
/**
@brief Class template of autopointer.
@detailed Holds created pointer and delete when it's need, usually when no referecnces to pointer.
*/
template <typename PointerType>
class __RE_PUBLIC_CLASS_API__ REPtr {
private:
PointerType* _object;
REInt32* _referenceCount;
void retain() {
if (_referenceCount) {
*_referenceCount = (*_referenceCount) + 1;
}
}
void deleteObject() {
if (_object) {
delete _object;
}
_object = NULL;
}
public:
/**
@brief Check pointer is NULL.
@return True if pointer is NULL, othervice false;
*/
REBOOL isEmpty() const {
return (_object == NULL);
}
/**
@brief Check pointer is not NULL.
@return True if not NULL, othervice false.
*/
REBOOL isNotEmpty() const {
return (_object != NULL);
}
/**
@brief Check this class is only one woner of the pointer.
@return True is only one owner, othervice false.
*/
REBOOL isSingleOwner() const {
return _referenceCount ? ((*_referenceCount) <= 1) : false;
}
/**
@brief Releases holded pointer.
@detailed Decrement reference counter and if there is not references delete's pointer.
*/
void release() {
if (_referenceCount) {
*_referenceCount = (*_referenceCount) - 1;
if ((*_referenceCount) <= 0) {
this->deleteObject();
free(_referenceCount);
_referenceCount = NULL;
}
}
_object = NULL;
_referenceCount = NULL;
}
/**
@brief Assing operator.
@detailed Releases prev. pointer reference, hold another pointer and increments it's reference.
@param anotherPtr Another autopointer object.
@return Address of this autopointer object.
*/
REPtr<PointerType> & operator=(const REPtr<PointerType> & anotherPtr) {
if (this != &anotherPtr) {
this->release();
if (anotherPtr.isNotEmpty()) {
_object = anotherPtr._object;
_referenceCount = anotherPtr._referenceCount;
this->retain();
}
}
return (*this);
}
/**
@brief Structure dereference operator.
@return Address(pointer to pointer) to holded object.
*/
PointerType* operator->() {
return _object;
}
/**
@brief Const variant of the structure dereference operator.
@return Const address(pointer to pointer) to holded object.
*/
const PointerType* operator->() const {
return _object;
}
/**
@brief Indirection operator.
@return Returns object pointer.
*/
operator PointerType* () {
return _object;
}
/**
@brief Const version of indirection operator.
@return Returns object const pointer.
*/
operator const PointerType* () const {
return _object;
}
/**
@brief Default contructor.
@detailed Contructs autopointer without any object pointer and reference.
*/
REPtr() : _object(NULL), _referenceCount(NULL) { }
/**
@brief Contructor with typed pointer to object.
@detailed If pointer is not NULL then holds pointer and create reference to it.
@param object Pointer to typed object.
*/
REPtr(PointerType* object) : _object(object), _referenceCount(NULL) {
if (_object) {
REInt32 * count = (REInt32 *)malloc(sizeof(REInt32));
#if defined(RE_HAVE_ASSERT_H)
assert(count);
#endif
if (count) {
*count = 1;
_referenceCount = count;
} else {
_object = NULL;
}
}
}
/**
@brief Contructs autopointer with with another autopointer object.
@detailed If another autopointer is not empty(holds pointer) than increment reference, othervice created empty autopointer object.
@param anotherPtr Another autopointer object.
*/
REPtr(const REPtr<PointerType> & anotherPtr) : _object(NULL), _referenceCount(NULL) {
if (this != &anotherPtr) {
if (anotherPtr.isNotEmpty()) {
_object = anotherPtr._object;
_referenceCount = anotherPtr._referenceCount;
this->retain();
}
}
}
/**
@brief Default destructor.
@detailed Releases holded pointer if available.
*/
~REPtr() {
this->release();
}
};
/**
@brief Function template for casting objects using "static_cast".
*/
template <typename resultType, typename sourceType>
static resultType* REPtrCast(sourceType* sourcePointer) {
return static_cast<resultType*>( static_cast<void*>(sourcePointer) );
}
/**
@brief Const version of function template for casting objects using "static_cast".
*/
template <typename resultType, typename sourceType>
static const resultType* REPtrCast(const sourceType* sourcePointer) {
return static_cast<const resultType*>( static_cast<const void*>(sourcePointer) );
}
/**
@brief Class template for list.
*/
template <typename T>
class __RE_PUBLIC_CLASS_API__ REList {
public:
/**
@brief Class of list node.
*/
class Node;
private:
class NodeBase {
public:
Node * next;
Node * previous;
NodeBase() : next(NULL), previous(NULL) { }
};
public:
/**
@brief Class of list node.
@detailed Holds value and pointer to prev. & next nodes.
*/
class Node : public NodeBase {
public:
T value;
Node(const T & newValue) : NodeBase(), value(newValue) { }
};
/**
@brief Type of list values comparation results.
*/
typedef enum {
/**
@brief Left operand is lest then right.
*/
Descending = -1,
/**
@brief Left and right operands are the same.
*/
Same = 0,
/**
@brief Left operand is greater then right.
*/
Ascending = 1
}
/**
@brief Type of list values comparation results.
*/
ValueCompareResult;
/**
@brief Define pointer to list node.
*/
typedef Node * NodePtr;
/**
@brief Define node creator callback function with defaul value.
@param newValue Const address to new node value.
*/
typedef NodePtr (*CreateNodeCallback)(const T & newValue);
/**
@brief Define node destructor callback function with node pointer.
@param Pointer to list node.
*/
typedef void (*ReleaseNodeCallback)(NodePtr);
/**
@brief Define node values comparator callback function.
@param left Pointer to left value.
@param right Pointer to right value.
*/
typedef ValueCompareResult (*NodeValuesComparator)(const T * left, const T * right);
/**
@brief Define node values comparator callback function.
@param left Pointer to left value.
@param right Pointer to custom right value.
*/
typedef ValueCompareResult (*CustomNodeValueComparator)(const T * nodeValue, void * customValue);
private:
CreateNodeCallback _createNode;
ReleaseNodeCallback _releaseNode;
union {
NodeBase * _head;
Node * _castHead;
};
public:
/**
@brief Default node creator static method callback.
@detailed Create node with "new" operator.
@param newValue Default node value.
@return Pointer to created node with value.
*/
static NodePtr newNode(const T & newValue) {
return (new Node(newValue));
}
/**
@brief Default node destructor static method callback.
@detailed Release node with "delete" operator.
@param node Pointer to node.
*/
static void deleteNode(NodePtr node) {
delete node;
}
/**
@brief Node creator static method callback.
@detailed Allocate node with "malloc" function.
@param newValue Default node value.
@return Pointer to allocated node with value.
*/
static NodePtr allocateNode(const T & newValue) {
NodePtr node = (NodePtr)malloc(sizeof(Node));
if (node) {
node->value = newValue;
}
return node;
}
/**
@brief Node destructor static method callback.
@detailed Release node with "free" function.
@param node Pointer to node.
*/
static void freeNode(NodePtr node) {
free(node);
}
public:
/**
@brief Iterator class for list values.
*/
class Iterator {
private:
Node * _head;
Node * _node;
public:
/**
* @brief Move to next list node.
* @return True if moved next, othervice false.
*/
bool next() {
_node = _node ? _node->next : this->_head->next;
return _node != this->_head;
}
/**
@brief Getter for node pointer.
@return Pointer to current list node.
*/
Node * node() const {
return _node;
}
/**
@brief Getter for current node value.
@return Const address for current value.
*/
const T & value() const {
return _node->value;
}
/**
@brief Constructs iterator with another iterator.
@param it Another iterator.
*/
Iterator(const Iterator & it) : _head(it._head), _node(NULL) { }
/**
@brief Constructs iterator with pointer to list head node.
@detailed Need for internal list implementation.
@param listHead Pointer to list head node.
*/
Iterator(Node * listHead) : _head(listHead), _node(NULL) { }
};
/**
@brief Creates new list iterator object.
@return New iterator.
*/
Iterator iterator() const {
return Iterator(this->_castHead);
}
/**
@brief Check list containes any values.
@return True if there is no values, othervice false.
*/
bool isEmpty() const {
return (this->_head->next == this->_head);
}
/**
@brief Removes all list values.
*/
void clear() {
Node * node = this->_head->next;
while (node != this->_head) {
node = this->removeNode(node);
}
}
/**
@brief Calculates count of the list by iterating all elements.
@return Count of the elements.
*/
REUInt32 count() const {
REUInt32 c = 0;
Node * next = this->_head->next;
while (next != this->_head) {
c++;
next = next->next;
}
return c;
}
/**
@brief Locate node with same value described as void pointer.
@param customValue Void pointer to searched value.
@param comparator Values comparator callback
@return Pointer to node contained same value or NULL if there is such values in list.
*/
Node * findNode(void * customValue, CustomNodeValueComparator comparator) const {
Node * next = this->_head->next;
while (next != this->_head) {
if (comparator(&next->value, customValue) == Same) {
return next;
}
next = next->next;
}
return NULL;
}
/**
@brief Locate node with same value as parameter.
@param value The value to be located in list.
@param comparator Conparator callback for comparing 2 values.
@return Pointer to node contained same value or NULL if there is such values in list.
*/
Node * findNode(const T & value, NodeValuesComparator comparator) const {
Node * next = this->_head->next;
while (next != this->_head) {
if (comparator(&next->value, &value) == Same) {
return next;
}
next = next->next;
}
return NULL;
}
/**
@brief Locate node with same value as parameter.
@param value The value to be located in list. For comparing values used "==" operator.
@return Pointer to node contained same value or NULL if there is such values in list.
*/
Node * findNode(const T & value) const {
Node * next = this->_head->next;
while (next != this->_head) {
if (next->value == value) {
return next;
}
next = next->next;
}
return NULL;
}
/**
@brief Check is list containes some value.
@param value The value to be finded in list.
@return True if such value containes in list, othervice false.
*/
bool isContaines(const T & value) const {
return this->findNode(value) ? true : false;
}
/**