-
Notifications
You must be signed in to change notification settings - Fork 4
/
yateclass.h
8193 lines (7118 loc) · 247 KB
/
yateclass.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
/**
* yateclass.h
* This file is part of the YATE Project http://YATE.null.ro
*
* Base classes and types, not related to the engine or telephony
*
* Yet Another Telephony Engine - a fully featured software PBX and IVR
* Copyright (C) 2004-2014 Null Team
*
* This software is distributed under multiple licenses;
* see the COPYING file in the main directory for licensing
* information for this specific distribution.
*
* This use of this software may be subject to additional restrictions.
* See the LEGAL file in the main directory for details.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef __YATECLASS_H
#define __YATECLASS_H
#ifndef __cplusplus
#error C++ is required
#endif
#include <limits.h>
#include <sys/types.h>
#include <stddef.h>
#include <unistd.h>
#include <errno.h>
#include <stdarg.h>
#ifndef _WORDSIZE
#if defined(__arch64__) || defined(__x86_64__) \
|| defined(__amd64__) || defined(__ia64__) \
|| defined(__alpha__) || defined(__sparcv9) || defined(__mips64)
#define _WORDSIZE 64
#else
#define _WORDSIZE 32
#endif
#endif
#ifndef _WINDOWS
#if defined(WIN32) || defined(_WIN32)
#define _WINDOWS
#endif
#endif
#ifdef _WINDOWS
#include <windows.h>
#include <io.h>
#include <direct.h>
/**
* Windows definitions for commonly used types
*/
typedef signed __int8 int8_t;
typedef unsigned __int8 u_int8_t;
typedef unsigned __int8 uint8_t;
typedef signed __int16 int16_t;
typedef unsigned __int16 u_int16_t;
typedef unsigned __int16 uint16_t;
typedef signed __int32 int32_t;
typedef unsigned __int32 u_int32_t;
typedef unsigned __int32 uint32_t;
typedef signed __int64 int64_t;
typedef unsigned __int64 u_int64_t;
typedef unsigned __int64 uint64_t;
typedef int pid_t;
typedef int socklen_t;
typedef unsigned long in_addr_t;
#ifndef strcasecmp
#define strcasecmp _stricmp
#endif
#ifndef strncasecmp
#define strncasecmp _strnicmp
#endif
#define vsnprintf _vsnprintf
#define snprintf _snprintf
#define strdup _strdup
#define strtoll _strtoi64
#define open _open
#define dup2 _dup2
#define read _read
#define write _write
#define close _close
#define getpid _getpid
#define chdir _chdir
#define mkdir(p,m) _mkdir(p)
#define unlink _unlink
#define O_RDWR _O_RDWR
#define O_RDONLY _O_RDONLY
#define O_WRONLY _O_WRONLY
#define O_APPEND _O_APPEND
#define O_BINARY _O_BINARY
#define O_EXCL _O_EXCL
#define O_CREAT _O_CREAT
#define O_TRUNC _O_TRUNC
#define O_NOCTTY 0
#define S_IRUSR _S_IREAD
#define S_IWUSR _S_IWRITE
#define S_IXUSR 0
#define S_IRWXU (_S_IREAD|_S_IWRITE)
#ifdef LIBYATE_EXPORTS
#define YATE_API __declspec(dllexport)
#else
#ifndef LIBYATE_STATIC
#define YATE_API __declspec(dllimport)
#endif
#endif
#define FMT64 "%I64d"
#define FMT64U "%I64u"
#else /* _WINDOWS */
#include <sys/time.h>
#include <sys/socket.h>
#if defined(__FreeBSD__)
#include <netinet/in_systm.h>
#endif
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
/**
* Non-Windows definitions for commonly used types
*/
#ifndef SOCKET
typedef int SOCKET;
#endif
#ifndef HANDLE
typedef int HANDLE;
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
#if _WORDSIZE == 64 && !defined(__APPLE__)
#define FMT64 "%ld"
#define FMT64U "%lu"
#else
#define FMT64 "%lld"
#define FMT64U "%llu"
#endif
#endif /* ! _WINDOWS */
#ifndef LLONG_MAX
#ifdef _I64_MAX
#define LLONG_MAX _I64_MAX
#else
#define LLONG_MAX 9223372036854775807LL
#endif
#endif
#ifndef LLONG_MIN
#ifdef _I64_MIN
#define LLONG_MIN _I64_MIN
#else
#define LLONG_MIN (-LLONG_MAX - 1LL)
#endif
#endif
#ifndef ULLONG_MAX
#ifdef _UI64_MAX
#define ULLONG_MAX _UI64_MAX
#else
#define ULLONG_MAX 18446744073709551615ULL
#endif
#endif
#ifndef O_LARGEFILE
#define O_LARGEFILE 0
#endif
#ifndef IPTOS_LOWDELAY
#define IPTOS_LOWDELAY 0x10
#define IPTOS_THROUGHPUT 0x08
#define IPTOS_RELIABILITY 0x04
#endif
#ifndef IPTOS_MINCOST
#define IPTOS_MINCOST 0x02
#endif
#ifndef IPPROTO_SCTP
#define IPPROTO_SCTP 132
#endif
#ifndef YATE_API
#define YATE_API
#endif
#ifdef _WINDOWS
#undef RAND_MAX
#define RAND_MAX 2147483647
#endif
/**
* Holds all Telephony Engine related classes.
*/
namespace TelEngine {
#ifdef HAVE_GCC_FORMAT_CHECK
#define FORMAT_CHECK(f) __attribute__((format(printf,(f),(f)+1)))
#else
#define FORMAT_CHECK(f)
#endif
#define YIGNORE(v) while (v) { break; }
#ifdef HAVE_BLOCK_RETURN
#define YSTRING(s) (*({static const String str("" s);&str;}))
#define YATOM(s) (*({static const String* str(0);str ? str : String::atom(str,"" s);}))
#else
#define YSTRING(s) ("" s)
#define YATOM(s) ("" s)
#endif
#define YSTRING_INIT_HASH ((unsigned) -1)
/**
* Abort execution (and coredump if allowed) if the abort flag is set.
* This function may not return.
*/
YATE_API void abortOnBug();
/**
* Set the abort on bug flag. The default flag state is false.
* @return The old state of the flag.
*/
YATE_API bool abortOnBug(bool doAbort);
/**
* Standard debugging levels.
* The DebugFail level is special - it is always displayed and may abort
* the program if @ref abortOnBug() is set.
*/
enum DebugLevel {
DebugFail = 0,
DebugTest = 1,
DebugGoOn = 2,
DebugConf = 3,
DebugStub = 4,
DebugWarn = 5,
DebugMild = 6,
DebugCall = 7,
DebugNote = 8,
DebugInfo = 9,
DebugAll = 10
};
/**
* Retrieve the current global debug level
* @return The current global debug level
*/
YATE_API int debugLevel();
/**
* Set the current global debug level.
* @param level The desired debug level
* @return The new global debug level (may be different)
*/
YATE_API int debugLevel(int level);
/**
* Check if debugging output should be generated
* @param level The global debug level we are testing
* @return True if messages should be output, false otherwise
*/
YATE_API bool debugAt(int level);
/**
* Get an ANSI string to colorize debugging output
* @param level The debug level who's color is requested.
* Negative or out of range will reset to the default color
* @return ANSI string that sets color corresponding to level
*/
YATE_API const char* debugColor(int level);
/**
* Get the name of a debugging or alarm level
* @param level The debug level
* @return Short C string describing the level
*/
YATE_API const char* debugLevelName(int level);
/**
* Holds a local debugging level that can be modified separately from the
* global debugging
* @short A holder for a debug level
*/
class YATE_API DebugEnabler
{
public:
/**
* Constructor
* @param level The initial local debug level
* @param enabled Enable debugging on this object
*/
inline DebugEnabler(int level = TelEngine::debugLevel(), bool enabled = true)
: m_level(DebugFail), m_enabled(enabled), m_chain(0), m_name(0)
{ debugLevel(level); }
inline ~DebugEnabler()
{ m_name = 0; m_chain = 0; }
/**
* Retrieve the current local debug level
* @return The current local debug level
*/
inline int debugLevel() const
{ return m_chain ? m_chain->debugLevel() : m_level; }
/**
* Set the current local debug level.
* @param level The desired debug level
* @return The new debug level (may be different)
*/
int debugLevel(int level);
/**
* Retrieve the current debug activation status
* @return True if local debugging is enabled
*/
inline bool debugEnabled() const
{ return m_chain ? m_chain->debugEnabled() : m_enabled; }
/**
* Set the current debug activation status
* @param enable The new debug activation status, true to enable
*/
inline void debugEnabled(bool enable)
{ m_enabled = enable; m_chain = 0; }
/**
* Get the current debug name
* @return Name of the debug activation if set or NULL
*/
inline const char* debugName() const
{ return m_name; }
/**
* Check if debugging output should be generated
* @param level The debug level we are testing
* @return True if messages should be output, false otherwise
*/
bool debugAt(int level) const;
/**
* Check if this enabler is chained to another one
* @return True if local debugging is chained to other enabler
*/
inline bool debugChained() const
{ return m_chain != 0; }
/**
* Chain this debug holder to a parent or detach from existing one
* @param chain Pointer to parent debug level, NULL to detach
*/
inline void debugChain(const DebugEnabler* chain = 0)
{ m_chain = (chain != this) ? chain : 0; }
/**
* Copy debug settings from another object or from engine globals
* @param original Pointer to a DebugEnabler to copy settings from
*/
void debugCopy(const DebugEnabler* original = 0);
protected:
/**
* Set the current debug name
* @param name Static debug name or NULL
*/
inline void debugName(const char* name)
{ m_name = name; }
private:
int m_level;
bool m_enabled;
const DebugEnabler* m_chain;
const char* m_name;
};
#if 0 /* for documentation generator */
/**
* Convenience macro.
* Does the same as @ref Debug if DEBUG is \#defined (compiling for debugging)
* else it does not get compiled at all.
*/
void DDebug(int level, const char* format, ...);
/**
* Convenience macro.
* Does the same as @ref Debug if DEBUG is \#defined (compiling for debugging)
* else it does not get compiled at all.
*/
void DDebug(const char* facility, int level, const char* format, ...);
/**
* Convenience macro.
* Does the same as @ref Debug if DEBUG is \#defined (compiling for debugging)
* else it does not get compiled at all.
*/
void DDebug(const DebugEnabler* local, int level, const char* format, ...);
/**
* Convenience macro.
* Does the same as @ref Debug if XDEBUG is \#defined (compiling for extra
* debugging) else it does not get compiled at all.
*/
void XDebug(int level, const char* format, ...);
/**
* Convenience macro.
* Does the same as @ref Debug if XDEBUG is \#defined (compiling for extra
* debugging) else it does not get compiled at all.
*/
void XDebug(const char* facility, int level, const char* format, ...);
/**
* Convenience macro.
* Does the same as @ref Debug if XDEBUG is \#defined (compiling for extra
* debugging) else it does not get compiled at all.
*/
void XDebug(const DebugEnabler* local, int level, const char* format, ...);
/**
* Convenience macro.
* Does the same as @ref Debug if NDEBUG is not \#defined
* else it does not get compiled at all (compiling for mature release).
*/
void NDebug(int level, const char* format, ...);
/**
* Convenience macro.
* Does the same as @ref Debug if NDEBUG is not \#defined
* else it does not get compiled at all (compiling for mature release).
*/
void NDebug(const char* facility, int level, const char* format, ...);
/**
* Convenience macro.
* Does the same as @ref Debug if NDEBUG is not \#defined
* else it does not get compiled at all (compiling for mature release).
*/
void NDebug(const DebugEnabler* local, int level, const char* format, ...);
#endif
#if defined(_DEBUG) || defined(DEBUG) || defined(XDEBUG)
#undef DEBUG
#define DEBUG 1
#endif
#ifdef DEBUG
#define DDebug Debug
#else
#ifdef _WINDOWS
#define DDebug do { break; } while
#else
#define DDebug(arg...)
#endif
#endif
#ifdef XDEBUG
#define XDebug Debug
#else
#ifdef _WINDOWS
#define XDebug do { break; } while
#else
#define XDebug(arg...)
#endif
#endif
#ifndef NDEBUG
#define NDebug Debug
#else
#ifdef _WINDOWS
#define NDebug do { break; } while
#else
#define NDebug(arg...)
#endif
#endif
/**
* Outputs a debug string.
* @param level The level of the message
* @param format A printf() style format string
*/
YATE_API void Debug(int level, const char* format, ...) FORMAT_CHECK(2);
/**
* Outputs a debug string for a specific facility.
* @param facility Facility that outputs the message
* @param level The level of the message
* @param format A printf() style format string
*/
YATE_API void Debug(const char* facility, int level, const char* format, ...) FORMAT_CHECK(3);
/**
* Outputs a debug string for a specific facility.
* @param local Pointer to a DebugEnabler holding current debugging settings
* @param level The level of the message
* @param format A printf() style format string
*/
YATE_API void Debug(const DebugEnabler* local, int level, const char* format, ...) FORMAT_CHECK(3);
/**
* Outputs a debug string and emits an alarm if a callback is installed
* @param component Component that emits the alarm
* @param level The level of the alarm
* @param format A printf() style format string
*/
YATE_API void Alarm(const char* component, int level, const char* format, ...) FORMAT_CHECK(3);
/**
* Outputs a debug string and emits an alarm if a callback is installed
* @param component Pointer to a DebugEnabler holding component name and debugging settings
* @param level The level of the alarm
* @param format A printf() style format string
*/
YATE_API void Alarm(const DebugEnabler* component, int level, const char* format, ...) FORMAT_CHECK(3);
/**
* Outputs a debug string and emits an alarm if a callback is installed
* @param component Component that emits the alarm
* @param info Extra alarm information
* @param level The level of the alarm
* @param format A printf() style format string
*/
YATE_API void Alarm(const char* component, const char* info, int level, const char* format, ...) FORMAT_CHECK(4);
/**
* Outputs a debug string and emits an alarm if a callback is installed
* @param component Pointer to a DebugEnabler holding component name and debugging settings
* @param info Extra alarm information
* @param level The level of the alarm
* @param format A printf() style format string
*/
YATE_API void Alarm(const DebugEnabler* component, const char* info, int level, const char* format, ...) FORMAT_CHECK(4);
/**
* Outputs a string to the debug console with formatting
* @param format A printf() style format string
*/
YATE_API void Output(const char* format, ...) FORMAT_CHECK(1);
/**
* This class is used as an automatic variable that logs messages on creation
* and destruction (when the instruction block is left or function returns).
* IMPORTANT: the name is not copied so it should best be static.
* @short An object that logs messages on creation and destruction
*/
class YATE_API Debugger
{
public:
/**
* Timestamp formatting
*/
enum Formatting {
None = 0,
Relative, // from program start
Absolute, // from EPOCH (1-1-1970)
Textual, // absolute GMT in YYYYMMDDhhmmss.uuuuuu format
TextLocal, // local time in YYYYMMDDhhmmss.uuuuuu format
TextSep, // absolute GMT in YYYY-MM-DD_hh:mm:ss.uuuuuu format
TextLSep, // local time in YYYY-MM-DD_hh:mm:ss.uuuuuu format
};
/**
* The constructor prints the method entry message and indents.
* @param name Name of the function or block entered, must be static
* @param format printf() style format string
*/
explicit Debugger(const char* name, const char* format = 0, ...);
/**
* The constructor prints the method entry message and indents.
* @param level The level of the message
* @param name Name of the function or block entered, must be static
* @param format printf() style format string
*/
Debugger(int level, const char* name, const char* format = 0, ...);
/**
* The destructor prints the method leave message and deindents.
*/
~Debugger();
/**
* Set the output callback
* @param outFunc Pointer to the output function, NULL to use stderr
*/
static void setOutput(void (*outFunc)(const char*,int) = 0);
/**
* Set the interactive output callback
* @param outFunc Pointer to the output function, NULL to disable
*/
static void setIntOut(void (*outFunc)(const char*,int) = 0);
/**
* Set the alarm hook callback
* @param alarmFunc Pointer to the alarm callback function, NULL to disable
*/
static void setAlarmHook(void (*alarmFunc)(const char*,int,const char*,const char*) = 0);
/**
* Set the relay hook callback that will process all Output, Debug and Alarm
* @param relayFunc Pointer to the relay callback function, NULL to disable
*/
static void setRelayHook(void (*relayFunc)(int,const char*,const char*,const char*) = 0);
/**
* Enable or disable the debug output
* @param enable Set to true to globally enable output
* @param colorize Enable ANSI colorization of output
*/
static void enableOutput(bool enable = true, bool colorize = false);
/**
* Retrieve the start timestamp
* @return Start timestamp value in seconds
*/
static uint32_t getStartTimeSec();
/**
* Retrieve the format of timestamps
* @return The current formatting type for timestamps
*/
static Formatting getFormatting();
/**
* Set the format of timestamps on output messages and set the time start reference
* @param format Desired timestamp formatting
* @param startTimeSec Optional start timestamp (in seconds)
*/
static void setFormatting(Formatting format, uint32_t startTimeSec = 0);
/**
* Fill a buffer with a current timestamp prefix
* @param buf Buffer to fill, must be at least 24 characters long
* @param format Desired timestamp formatting
* @return Length of the prefix written in buffer excluding final NUL
*/
static unsigned int formatTime(char* buf, Formatting format = getFormatting());
/**
* Processes a preformatted string as Output, Debug or Alarm.
* This method is intended to relay messages from other processes, DO NOT USE!
* @param level The level of the debug or alarm, negative for an output
* @param buffer Preformatted text buffer, MUST HAVE SPACE for at least strlen + 2
* @param component Component that emits the alarm if applicable
* @param info Extra alarm information if applicable
*/
static void relayOutput(int level, char* buffer, const char* component = 0, const char* info = 0);
private:
const char* m_name;
int m_level;
};
/**
* A structure to build (mainly static) Token-to-ID translation tables.
* A table of such structures must end with an entry with a null token
*/
struct TokenDict {
/**
* Token to match
*/
const char* token;
/**
* Value the token translates to
*/
int value;
};
class String;
class Mutex;
class ObjList;
class NamedCounter;
#if 0 /* for documentation generator */
/**
* Macro to ignore the result of a function
* @param value Returned value to be ignored, must be interpretable as boolean
*/
void YIGNORE(primitive value);
/**
* Macro to create a local static String if supported by compiler, use with caution
* @param string Literal constant string
* @return A const String& if supported, literal string if not supported
*/
constant YSTRING(const char* string);
/**
* Macro to create a shared static String if supported by compiler, use with caution
* @param string Literal constant string
* @return A const String& if supported, literal string if not supported
*/
constant YATOM(const char* string);
/**
* Macro to create a GenObject class from a base class and implement @ref GenObject::getObject
* @param type Class that is declared
* @param base Base class that is inherited
*/
void YCLASS(class type,class base);
/**
* Macro to create a GenObject class from two base classes and implement @ref GenObject::getObject
* @param type Class that is declared
* @param base1 First base class that is inherited
* @param base2 Second base class that is inherited
*/
void YCLASS2(class type,class base1,class base2);
/**
* Macro to create a GenObject class from three base classes and implement @ref GenObject::getObject
* @param type Class that is declared
* @param base1 First base class that is inherited
* @param base2 Second base class that is inherited
* @param base3 Third base class that is inherited
*/
void YCLASS3(class type,class base1,class base2,class base3);
/**
* Macro to implement @ref GenObject::getObject in a derived class
* @param type Class that is declared
* @param base Base class that is inherited
*/
void YCLASSIMP(class type,class base);
/**
* Macro to implement @ref GenObject::getObject in a derived class
* @param type Class that is declared
* @param base1 First base class that is inherited
* @param base2 Second base class that is inherited
*/
void YCLASSIMP2(class type,class base1,class base2);
/**
* Macro to implement @ref GenObject::getObject in a derived class
* @param type Class that is declared
* @param base1 First base class that is inherited
* @param base2 Second base class that is inherited
* @param base3 Third base class that is inherited
*/
void YCLASSIMP3(class type,class base1,class base2,class base3);
/**
* Macro to retrieve a typed pointer to an interface from an object
* @param type Class we want to return
* @param pntr Pointer to the object we want to get the interface from
* @return Pointer to the class we want or NULL
*/
class* YOBJECT(class type,GenObject* pntr);
/**
* Macro to disable automatic copy and assignment operators
* @param type Class that is declared
*/
void YNOCOPY(class type);
#endif
#define YCLASS(type,base) \
public: virtual void* getObject(const String& name) const \
{ return (name == YATOM(#type)) ? const_cast<type*>(this) : base::getObject(name); }
#define YCLASS2(type,base1,base2) \
public: virtual void* getObject(const String& name) const \
{ if (name == YATOM(#type)) return const_cast<type*>(this); \
void* tmp = base1::getObject(name); \
return tmp ? tmp : base2::getObject(name); }
#define YCLASS3(type,base1,base2,base3) \
public: virtual void* getObject(const String& name) const \
{ if (name == YATOM(#type)) return const_cast<type*>(this); \
void* tmp = base1::getObject(name); \
if (tmp) return tmp; \
tmp = base2::getObject(name); \
return tmp ? tmp : base3::getObject(name); }
#define YCLASSIMP(type,base) \
void* type::getObject(const String& name) const \
{ return (name == YATOM(#type)) ? const_cast<type*>(this) : base::getObject(name); }
#define YCLASSIMP2(type,base1,base2) \
void* type::getObject(const String& name) const \
{ if (name == YATOM(#type)) return const_cast<type*>(this); \
void* tmp = base1::getObject(name); \
return tmp ? tmp : base2::getObject(name); }
#define YCLASSIMP3(type,base1,base2,base3) \
void* type::getObject(const String& name) const \
{ if (name == YATOM(#type)) return const_cast<type*>(this); \
void* tmp = base1::getObject(name); \
if (tmp) return tmp; \
tmp = base2::getObject(name); \
return tmp ? tmp : base3::getObject(name); }
#define YOBJECT(type,pntr) (static_cast<type*>(GenObject::getObject(YATOM(#type),pntr)))
#define YNOCOPY(type) private: \
type(const type&); \
void operator=(const type&)
/**
* An object with just a public virtual destructor
*/
class YATE_API GenObject
{
YNOCOPY(GenObject); // no automatic copies please
public:
/**
* Default constructor
*/
GenObject();
/**
* Destructor.
*/
virtual ~GenObject() { setObjCounter(0); }
/**
* Check if the object is still valid and safe to access.
* Note that you should not trust this result unless the object is locked
* by other means.
* @return True if the object is still useable
*/
virtual bool alive() const;
/**
* Destroys the object, disposes the memory.
*/
virtual void destruct();
/**
* Get a string representation of this object
* @return A reference to a String representing this object
* which is either null, the object itself (for objects derived from
* String) or some form of identification
*/
virtual const String& toString() const;
/**
* Get a pointer to a derived class given that class name
* @param name Name of the class we are asking for
* @return Pointer to the requested class or NULL if this object doesn't implement it
*/
virtual void* getObject(const String& name) const;
/**
* Helper method to get the pointer to a derived class
* @param name Name of the class we are asking for
* @param obj Pointer to the object to get derived class from
* @return Pointer to the requested class or NULL if this object doesn't implement it
*/
static inline void* getObject(const String& name, const GenObject* obj)
{ return obj ? obj->getObject(name) : 0; }
/**
* Get the global state of object counting
* @return True if object counting is enabled
*/
static inline bool getObjCounting()
{ return s_counting; }
/**
* Set the global state of object counting
* @param enable True to enable object counting, false to disable
*/
static inline void setObjCounting(bool enable)
{ s_counting = enable; }
/**
* Get the counter of this object
* @return Pointer to current counter object
*/
inline NamedCounter* getObjCounter() const
{ return m_counter; }
/**
* Set the counter of this object
* @param counter New counter object or NULL
* @return Pointer to old counter object
*/
NamedCounter* setObjCounter(NamedCounter* counter);
/**
* Retrieve or allocate an object counter
* @param name Name of the counter
* @param create True to create a new counter if needed
* @return Pointer to existing or new counter object
*/
static NamedCounter* getObjCounter(const String& name, bool create = true);
/**
* Access the object counters list
* @return Reference to the global object counters list
*/
static ObjList& getObjCounters();
private:
NamedCounter* m_counter;
static bool s_counting;
};
/**
* Helper function that destroys a GenObject only if the pointer is non-NULL.
* Use it instead of the delete operator.
* @param obj Pointer (rvalue) to the object to destroy
*/
inline void destruct(GenObject* obj)
{ if (obj) obj->destruct(); }
/**
* Helper template function that destroys a GenObject descendant if the pointer
* is non-NULL and also zeros out the pointer.
* Use it instead of the delete operator.
* @param obj Reference to pointer (lvalue) to the object to destroy
*/
template <class Obj> void destruct(Obj*& obj)
{ if (obj) { obj->destruct(); obj = 0; } }
/**
* A reference counted object.
* Whenever using multiple inheritance you should inherit this class virtually.
*/
class YATE_API RefObject : public GenObject
{
YNOCOPY(RefObject); // no automatic copies please
public:
/**
* The constructor initializes the reference counter to 1!
* Use deref() to destruct the object when safe
*/
RefObject();
/**
* Destructor.
*/
virtual ~RefObject();
/**
* Get a pointer to a derived class given that class name
* @param name Name of the class we are asking for
* @return Pointer to the requested class or NULL if this object doesn't implement it
*/
virtual void* getObject(const String& name) const;
/**
* Check if the object is still referenced and safe to access.
* Note that you should not trust this result unless the object is locked
* by other means.
* @return True if the object is referenced and safe to access
*/
virtual bool alive() const;
/**
* Increments the reference counter if not already zero
* @return True if the object was successfully referenced and is safe to access
*/
bool ref();
/**
* Decrements the reference counter, destroys the object if it reaches zero
* <pre>
* // Deref this object, return quickly if the object was deleted
* if (deref()) return;
* </pre>
* @return True if the object may have been deleted, false if it still exists and is safe to access
*/
bool deref();
/**
* Get the current value of the reference counter
* @return The value of the reference counter
*/
inline int refcount() const
{ return m_refcount; }