00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468 #ifndef _POBJECT_H
00469 #define _POBJECT_H
00470
00471 #ifdef P_USE_PRAGMA
00472 #pragma interface
00473 #endif
00474
00475 #ifdef _WIN32
00476 #include "msos/ptlib/contain.h"
00477 #else
00478 #include "unix/ptlib/contain.h"
00479 #endif
00480
00481 #if defined(P_VXWORKS)
00482 #include <private/stdiop.h>
00483 #endif
00484
00485 #include <stdio.h>
00486 #include <stdarg.h>
00487 #include <stdlib.h>
00488
00489 #include <string.h>
00490
00491 #ifdef __USE_STL__
00492 #include <string>
00493 #include <iomanip>
00494 #include <iostream>
00495 #if (__GNUC__ >= 3)
00496 #include <sstream>
00497 typedef std::ostringstream ostrstream;
00498 #else
00499 #include <strstream>
00500 #endif
00501
00502 #ifdef _STLP_IOS_BASE_H
00503 typedef std::ios_base::fmtflags _Ios_Fmtflags;
00504 #endif
00505 #else
00506 #if (__GNUC__ >= 3)
00507 #include <iosfwd>
00508 #ifndef __MWERKS__
00509 #include <iomanip>
00510 #endif
00511 #else
00512 #include <iosfwd>
00513 #ifdef __GNUC__
00514 #include <strstream.h>
00515 #else
00516 #include <strstrea.h>
00517 #endif
00518 #ifndef __MWERKS__
00519 #include <iomanip.h>
00520 #endif
00521 #endif
00522 #endif
00523
00524 #if (__GNUC__ < 3) && !defined(_STLP_IOS_BASE_H)
00525 typedef long _Ios_Fmtflags;
00526 #endif
00527
00528 #if _MSC_VER<1300
00529 #define _BADOFF -1
00530 #endif
00531
00533
00534
00535
00536 #ifndef P_USE_INLINES
00537 #ifdef _DEBUG
00538 #define P_USE_INLINES 0
00539 #else
00540 #define P_USE_INLINES 0
00541 #endif
00542 #endif
00543
00544 #if P_USE_INLINES
00545 #define PINLINE inline
00546 #else
00547 #define PINLINE
00548 #endif
00549
00550
00552
00553
00554 #ifndef P_USE_ASSERTS
00555 #define P_USE_ASSERTS 1
00556 #endif
00557
00558 #if !P_USE_ASSERTS
00559
00560 #define PAssert(b, m) (b)
00561 #define PAssert2(b, c, m) (b)
00562 #define PAssertOS(b) (b)
00563 #define PAssertNULL(p) (p)
00564 #define PAssertAlways(m)
00565 #define PAssertAlways2(c, m)
00566
00567 #else // P_USE_ASSERTS
00568
00570 enum PStandardAssertMessage {
00571 PLogicError,
00572 POutOfMemory,
00573 PNullPointerReference,
00574 PInvalidCast,
00575 PInvalidArrayIndex,
00576 PInvalidArrayElement,
00577 PStackEmpty,
00578 PUnimplementedFunction,
00579 PInvalidParameter,
00580 POperatingSystemError,
00581 PChannelNotOpen,
00582 PUnsupportedFeature,
00583 PInvalidWindow,
00584 PMaxStandardAssertMessage
00585 };
00586
00587 #define __CLASS__ NULL
00588
00589 void PAssertFunc(const char * file, int line, const char * className, PStandardAssertMessage msg);
00590 void PAssertFunc(const char * file, int line, const char * className, const char * msg);
00591 void PAssertFunc(const char * full_msg);
00592
00593 inline bool PAssertFuncInline(bool b, const char * file, int line, const char * className, PStandardAssertMessage msg)
00594 {
00595 if (!b)
00596 PAssertFunc(file, line, className, msg);
00597 return b;
00598 }
00599 inline bool PAssertFuncInline(bool b, const char * file, int line, const char * className, const char * msg)
00600 {
00601 if (!b)
00602 PAssertFunc(file, line, className, msg);
00603 return b;
00604 }
00605
00612 #define PAssert(b, m) PAssertFuncInline((b), __FILE__,__LINE__,__CLASS__,(m))
00613
00621 #define PAssert2(b, c, m) PAssertFuncInline((b), __FILE__,__LINE__,(c),(m))
00622
00629 #define PAssertOS(b) PAssertFuncInline((b), __FILE__,__LINE__,__CLASS__,POperatingSystemError)
00630
00640 #define PAssertNULL(p) (((p)!=NULL)?(p): \
00641 (PAssertFunc(__FILE__,__LINE__, __CLASS__, PNullPointerReference),(p)))
00642
00649 #define PAssertAlways(m) PAssertFunc(__FILE__,__LINE__,__CLASS__,(m))
00650
00657 #define PAssertAlways2(c, m) PAssertFunc(__FILE__,__LINE__,(c),(m))
00658
00659 #endif // P_USE_ASSERTS
00660
00661
00666 ostream & PGetErrorStream();
00667
00671 void PSetErrorStream(ostream * strm );
00672
00687 #define PError (PGetErrorStream())
00688
00689
00690
00692
00693
00694 #ifndef PTRACING
00695 #define PTRACING 1
00696 #endif
00697
00698 #if PTRACING
00699
00704 class PTrace
00705 {
00706 public:
00708 enum Options {
00714 Blocks = 1,
00716 DateAndTime = 2,
00718 Timestamp = 4,
00720 Thread = 8,
00722 TraceLevel = 16,
00724 FileAndLine = 32,
00726 ThreadAddress = 64,
00728 AppendToFile = 128,
00731 GMTTime = 256,
00734 RotateDaily = 512,
00738 SystemLogStream = 32768
00739 };
00740
00748 static void Initialise(
00749 unsigned level,
00750 const char * filename = NULL,
00751 unsigned options = Timestamp | Thread | Blocks
00752 );
00753
00764 static void Initialise(
00765 unsigned level,
00766 const char * filename,
00767 const char * rolloverPattern,
00768 unsigned options = Timestamp | Thread | Blocks
00769 );
00770
00777 static void SetOptions(unsigned options );
00778
00786 static void ClearOptions(unsigned options );
00787
00792 static unsigned GetOptions();
00793
00799 static void SetLevel(unsigned level );
00800
00806 static unsigned GetLevel();
00807
00812 static BOOL CanTrace(unsigned level );
00813
00818 static void SetStream(ostream * out );
00819
00835 static ostream & Begin(
00836 unsigned level,
00837 const char * fileName,
00838 int lineNum
00839 );
00840
00857 static ostream & End(ostream & strm );
00858
00859
00865 class Block {
00866 public:
00868 Block(
00869 const char * fileName,
00870 int lineNum,
00871 const char * traceName
00873 );
00874 Block(const Block & obj)
00875 : file(obj.file), line(obj.line), name(obj.name) { }
00877 ~Block();
00878 private:
00879 Block & operator=(const Block &)
00880 { return *this; }
00881 const char * file;
00882 int line;
00883 const char * name;
00884 };
00885 };
00886
00887
00888
00889 #define PTRACE_PARAM(param) param
00890
00897 #define PTRACE_BLOCK(name) PTrace::Block __trace_block_instance(__FILE__, __LINE__, name)
00898
00902 #define PTRACE_LINE() \
00903 if (!PTrace::CanTrace(1)) ; else \
00904 PTrace::Begin(1, __FILE__, __LINE__) << __FILE__ << '(' << __LINE__ << ')' << PTrace::End
00905
00911 #define PTRACE(level, args) \
00912 if (!PTrace::CanTrace(level)) ; else \
00913 PTrace::Begin(level, __FILE__, __LINE__) << args << PTrace::End
00914
00922 #define PTRACE_IF(level, cond, args) \
00923 if (!(PTrace::CanTrace(level) && (cond))) ; else \
00924 PTrace::Begin(level, __FILE__, __LINE__) << args << PTrace::End
00925
00926 #else // PTRACING
00927
00928 #define PTRACE_PARAM(param)
00929 #define PTRACE_BLOCK(n)
00930 #define PTRACE_LINE()
00931 #define PTRACE(level, arg)
00932 #define PTRACE_IF(level, cond, args)
00933
00934 #endif // PTRACING
00935
00936
00937
00938 #if PMEMORY_CHECK || (defined(_MSC_VER) && defined(_DEBUG) && !defined(_WIN32_WCE))
00939
00940 #define PMEMORY_HEAP 1
00941
00948 class PMemoryHeap {
00949 public:
00951 PMemoryHeap();
00952
00953
00954 ~PMemoryHeap();
00955
00962 static void * Allocate(
00963 size_t nSize,
00964 const char * file,
00965 int line,
00966 const char * className
00967 );
00974 static void * Allocate(
00975 size_t count,
00976 size_t iSize,
00977 const char * file,
00978 int line
00979 );
00980
00988 static void * Reallocate(
00989 void * ptr,
00990 size_t nSize,
00991 const char * file,
00992 int line
00993 );
00994
01000 static void Deallocate(
01001 void * ptr,
01002 const char * className
01003 );
01004
01007 enum Validation {
01008 Ok, Bad, Trashed
01009 };
01017 static Validation Validate(
01018 const void * ptr,
01019 const char * className,
01020 ostream * error
01021 );
01022
01027 static BOOL ValidateHeap(
01028 ostream * error = NULL
01029 );
01030
01036 static BOOL SetIgnoreAllocations(
01037 BOOL ignore
01038 );
01039
01043 static void DumpStatistics();
01047 static void DumpStatistics(ostream & strm );
01048
01049 #if PMEMORY_CHECK
01050 struct State {
01051 DWORD allocationNumber;
01052 };
01053 #else
01054 typedef _CrtMemState State;
01055 #endif
01056
01057
01058
01059
01060
01061 static void GetState(
01062 State & state
01063 );
01064
01072 static void DumpObjectsSince(
01073 const State & when
01074 );
01075
01081 static void DumpObjectsSince(
01082 const State & when,
01083 ostream & strm
01084 );
01085
01091 static void SetAllocationBreakpoint(
01092 DWORD point
01093 );
01094
01095 #if PMEMORY_CHECK
01096
01097 protected:
01098 void * InternalAllocate(
01099 size_t nSize,
01100 const char * file,
01101 int line,
01102 const char * className
01103 );
01104 Validation InternalValidate(
01105 const void * ptr,
01106 const char * className,
01107 ostream * error
01108 );
01109 void InternalDumpStatistics(ostream & strm);
01110 void InternalDumpObjectsSince(DWORD objectNumber, ostream & strm);
01111
01112 class Wrapper {
01113 public:
01114 Wrapper();
01115 ~Wrapper();
01116 PMemoryHeap * operator->() const { return instance; }
01117 private:
01118 PMemoryHeap * instance;
01119 };
01120 friend class Wrapper;
01121
01122 enum Flags {
01123 NoLeakPrint = 1
01124 };
01125
01126 #pragma pack(1)
01127 struct Header {
01128 enum {
01129
01130 NumGuardBytes = 16 - (sizeof(Header *) +
01131 sizeof(Header *) +
01132 sizeof(const char *) +
01133 sizeof(const char *) +
01134 sizeof(size_t) +
01135 sizeof(DWORD) +
01136 sizeof(WORD) +
01137 sizeof(BYTE)
01138 #ifdef P_LINUX
01139 + sizeof(pthread_t)
01140 #endif
01141 )%8
01142 };
01143
01144 Header * prev;
01145 Header * next;
01146 const char * className;
01147 const char * fileName;
01148 size_t size;
01149 DWORD request;
01150 WORD line;
01151 BYTE flags;
01152 #ifdef P_LINUX
01153 pthread_t thread;
01154 #endif
01155 char guard[NumGuardBytes];
01156
01157 static char GuardBytes[NumGuardBytes];
01158 };
01159 #pragma pack()
01160
01161 BOOL isDestroyed;
01162
01163 Header * listHead;
01164 Header * listTail;
01165
01166 static DWORD allocationBreakpoint;
01167 DWORD allocationRequest;
01168 DWORD firstRealObject;
01169 BYTE flags;
01170
01171 char allocFillChar;
01172 char freeFillChar;
01173
01174 DWORD currentMemoryUsage;
01175 DWORD peakMemoryUsage;
01176 DWORD currentObjects;
01177 DWORD peakObjects;
01178 DWORD totalObjects;
01179
01180 ostream * leakDumpStream;
01181
01182 #if defined(_WIN32)
01183 CRITICAL_SECTION mutex;
01184 #elif defined(P_PTHREADS)
01185 pthread_mutex_t mutex;
01186 #elif defined(P_VXWORKS)
01187 void * mutex;
01188 #endif
01189
01190 #else
01191
01192 #define P_CLIENT_BLOCK (_CLIENT_BLOCK|(0x61<<16)) // This identifies a PObject derived class
01193 _CrtMemState initialState;
01194
01195 #endif // PMEMORY_CHECK
01196 };
01197
01198
01203 inline void * runtime_malloc(size_t bytes ) { return malloc(bytes); }
01204
01209 inline void runtime_free(void * ptr ) { free(ptr); }
01210
01211
01218 #define malloc(s) PMemoryHeap::Allocate(s, __FILE__, __LINE__, NULL)
01219
01226 #define calloc(n,s) PMemoryHeap::Allocate(n, s, __FILE__, __LINE__)
01227
01234 #define realloc(p,s) PMemoryHeap::Reallocate(p, s, __FILE__, __LINE__)
01235
01236
01243 #define free(p) PMemoryHeap::Deallocate(p, NULL)
01244
01245
01252 #define cfree(p) PMemoryHeap::Deallocate(p, NULL)
01253
01254
01269 #define PNEW new (__FILE__, __LINE__)
01270
01271 #if !defined(_MSC_VER) || _MSC_VER<1200
01272 #define PSPECIAL_DELETE_FUNCTION
01273 #else
01274 #define PSPECIAL_DELETE_FUNCTION \
01275 void operator delete(void * ptr, const char *, int) \
01276 { PMemoryHeap::Deallocate(ptr, Class()); } \
01277 void operator delete[](void * ptr, const char *, int) \
01278 { PMemoryHeap::Deallocate(ptr, Class()); }
01279 #endif
01280
01281 #define PNEW_AND_DELETE_FUNCTIONS \
01282 void * operator new(size_t nSize, const char * file, int line) \
01283 { return PMemoryHeap::Allocate(nSize, file, line, Class()); } \
01284 void * operator new(size_t nSize) \
01285 { return PMemoryHeap::Allocate(nSize, NULL, 0, Class()); } \
01286 void operator delete(void * ptr) \
01287 { PMemoryHeap::Deallocate(ptr, Class()); } \
01288 void * operator new(size_t, void * placement) \
01289 { return placement; } \
01290 void operator delete(void *, void *) \
01291 { } \
01292 void * operator new[](size_t nSize, const char * file, int line) \
01293 { return PMemoryHeap::Allocate(nSize, file, line, Class()); } \
01294 void * operator new[](size_t nSize) \
01295 { return PMemoryHeap::Allocate(nSize, NULL, 0, Class()); } \
01296 void operator delete[](void * ptr) \
01297 { PMemoryHeap::Deallocate(ptr, Class()); } \
01298 PSPECIAL_DELETE_FUNCTION
01299
01300
01301 inline void * operator new(size_t nSize, const char * file, int line)
01302 { return PMemoryHeap::Allocate(nSize, file, line, NULL); }
01303
01304 inline void * operator new[](size_t nSize, const char * file, int line)
01305 { return PMemoryHeap::Allocate(nSize, file, line, NULL); }
01306
01307 #ifndef __GNUC__
01308 void * operator new(size_t nSize);
01309 void * operator new[](size_t nSize);
01310
01311 void operator delete(void * ptr);
01312 void operator delete[](void * ptr);
01313
01314 #if defined(_MSC_VER) && _MSC_VER>=1200
01315 inline void operator delete(void * ptr, const char *, int)
01316 { PMemoryHeap::Deallocate(ptr, NULL); }
01317
01318 inline void operator delete[](void * ptr, const char *, int)
01319 { PMemoryHeap::Deallocate(ptr, NULL); }
01320 #endif
01321 #endif
01322
01323
01324 class PMemoryHeapIgnoreAllocationsForScope {
01325 public:
01326 PMemoryHeapIgnoreAllocationsForScope() : previousIgnoreAllocations(PMemoryHeap::SetIgnoreAllocations(TRUE)) { }
01327 ~PMemoryHeapIgnoreAllocationsForScope() { PMemoryHeap::SetIgnoreAllocations(previousIgnoreAllocations); }
01328 private:
01329 BOOL previousIgnoreAllocations;
01330 };
01331
01332 #define PMEMORY_IGNORE_ALLOCATIONS_FOR_SCOPE PMemoryHeapIgnoreAllocationsForScope instance_PMemoryHeapIgnoreAllocationsForScope
01333
01334 class PMemoryAllocationBreakpoint {
01335 public:
01336 PMemoryAllocationBreakpoint(DWORD point)
01337 {
01338 PMemoryHeap::SetAllocationBreakpoint(point);
01339 }
01340 };
01341
01342 #define PMEMORY_ALLOCATION_BREAKPOINT(point) PMemoryAllocationBreakpoint PMemoryAllocationBreakpointInstance(point)
01343
01344
01345 #else // PMEMORY_CHECK || (defined(_MSC_VER) && defined(_DEBUG))
01346
01347 #define PMEMORY_HEAP 0
01348
01349 #define PNEW new
01350
01351 #define PNEW_AND_DELETE_FUNCTIONS
01352
01353 #define runtime_malloc(s) malloc(s)
01354 #define runtime_free(p) free(p)
01355
01356 #define PMEMORY_IGNORE_ALLOCATIONS_FOR_SCOPE
01357 #define PMEMORY_ALLOCATION_BREAKPOINT(point)
01358
01359 #endif // PMEMORY_CHECK || (defined(_MSC_VER) && defined(_DEBUG))
01360
01361
01372 #define PCLASSINFO(cls, par) \
01373 public: \
01374 static inline const char * Class() \
01375 { return #cls; } \
01376 virtual BOOL InternalIsDescendant(const char * clsName) const \
01377 { return strcmp(clsName, cls::Class()) == 0 || par::InternalIsDescendant(clsName); } \
01378 virtual const char * GetClass(unsigned ancestor = 0) const \
01379 { return ancestor > 0 ? par::GetClass(ancestor-1) : cls::Class(); } \
01380 virtual Comparison CompareObjectMemoryDirect(const PObject & obj) const \
01381 { return (Comparison)memcmp(this, &obj, sizeof(cls)); } \
01382 PNEW_AND_DELETE_FUNCTIONS
01383
01384
01385 #if P_HAS_TYPEINFO
01386
01387 #define PIsDescendant(ptr, cls) (dynamic_cast<const cls *>(ptr) != NULL)
01388 #define PIsDescendantStr(ptr, str) ((ptr)->InternalIsDescendant(str))
01389
01390 #define PRemoveConst(cls, ptr) (const_cast<cls*>(ptr))
01391
01392 #if P_USE_ASSERTS
01393 template<class BaseClass> inline BaseClass * PAssertCast(BaseClass * obj, const char * file, int line)
01394 { if (obj == NULL) PAssertFunc(file, line, BaseClass::Class(), PInvalidCast); return obj; }
01395 #define PDownCast(cls, ptr) PAssertCast<cls>(dynamic_cast<cls*>(ptr),__FILE__,__LINE__)
01396 #else
01397 #define PDownCast(cls, ptr) (dynamic_cast<cls*>(ptr))
01398 #endif
01399
01400 #include <typeinfo>
01401
01402 #else // P_HAS_TYPEINFO
01403
01404 #define PIsDescendant(ptr, cls) ((ptr)->InternalIsDescendant(cls::Class()))
01405 #define PIsDescendantStr(ptr, str) ((ptr)->InternalIsDescendant(str))
01406
01407 #define PRemoveConst(cls, ptr) ((cls*)(ptr))
01408
01409 #if P_USE_ASSERTS
01410 template<class BaseClass> inline BaseClass * PAssertCast(PObject * obj, const char * file, int line)
01411 { if (obj->InternalIsDescendant(BaseClass::Class()) return (BaseClass *)obj; PAssertFunc(file, line, BaseClass::Class(), PInvalidCast); return NULL; }
01412 #define PDownCast(cls, ptr) PAssertCast<cls>((ptr),__FILE__,__LINE__)
01413 #else
01414 #define PDownCast(cls, ptr) ((cls*)(ptr))
01415 #endif
01416
01417 #endif // P_HAS_TYPEINFO
01418
01419
01428 #define PDECLARE_CLASS(cls, par) class cls : public par { PCLASSINFO(cls, par)
01429 #ifdef DOC_PLUS_PLUS
01430 } Match previous opening brace in doc++
01431 #endif
01432
01434
01435
01440 class PObject {
01441
01442 protected:
01446 PObject() { }
01447
01448 public:
01449
01450
01451
01452 virtual ~PObject() { }
01453
01466 static inline const char * Class() { return "PObject"; }
01467
01480 virtual const char * GetClass(unsigned = 0) const { return Class(); }
01481
01482 BOOL IsClass(const char * cls) const
01483 { return strcmp(cls, GetClass()) == 0; }
01484
01494 virtual BOOL InternalIsDescendant(
01495 const char * clsName
01496 ) const
01497 { return IsClass(clsName); }
01498
01500
01506 enum Comparison {
01507 LessThan = -1,
01508 EqualTo = 0,
01509 GreaterThan = 1
01510 };
01511
01523 virtual Comparison Compare(
01524 const PObject & obj
01525 ) const;
01526
01538 virtual Comparison CompareObjectMemoryDirect(
01539 const PObject & obj
01540 ) const;
01541
01547 bool operator==(
01548 const PObject & obj
01549 ) const { return Compare(obj) == EqualTo; }
01550
01556 bool operator!=(
01557 const PObject & obj
01558 ) const { return Compare(obj) != EqualTo; }
01559
01565 bool operator<(
01566 const PObject & obj
01567 ) const { return Compare(obj) == LessThan; }
01568
01574 bool operator>(
01575 const PObject & obj
01576 ) const { return Compare(obj) == GreaterThan; }
01577
01583 bool operator<=(
01584 const PObject & obj
01585 ) const { return Compare(obj) != GreaterThan; }
01586
01592 bool operator>=(
01593 const PObject & obj
01594 ) const { return Compare(obj) != LessThan; }
01596
01605 virtual void PrintOn(
01606 ostream &strm
01607 ) const;
01608
01615 virtual void ReadFrom(
01616 istream &strm
01617 );
01618
01619
01625 inline friend ostream & operator<<(
01626 ostream &strm,
01627 const PObject & obj
01628 ) { obj.PrintOn(strm); return strm; }
01629
01635 inline friend istream & operator>>(
01636 istream &strm,
01637 PObject & obj
01638 ) { obj.ReadFrom(strm); return strm; }
01639
01640
01655 virtual PObject * Clone() const;
01656
01668 virtual PINDEX HashFunction() const;
01670 };
01671
01673
01674
01675
01676
01677
01678
01679
01680 #define PANSI_CHAR 1
01681 #define PLITTLE_ENDIAN 2
01682 #define PBIG_ENDIAN 3
01683
01684
01685 #if 0
01686 class PStandardType
01687
01688
01689
01690
01691
01692
01693 {
01694 public:
01695 PStandardType(
01696 type newVal
01697 ) { data = newVal; }
01698
01699
01700
01701
01702 operator type() { return data; }
01703
01704
01705
01706
01707
01708
01709 friend ostream & operator<<(ostream & strm, const PStandardType & val)
01710 { return strm << (type)val; }
01711
01712
01713
01714
01715
01716
01717 friend istream & operator>>(istream & strm, PStandardType & val)
01718 { type data; strm >> data; val = PStandardType(data); return strm; }
01719
01720
01721
01722
01723
01724
01725
01726 private:
01727 type data;
01728 };
01729 #endif
01730
01731
01732 #define PI_SAME(name, type) \
01733 struct name { \
01734 name() : data(0) { } \
01735 name(type value) : data(value) { } \
01736 name(const name & value) : data(value.data) { } \
01737 name & operator =(type value) { data = value; return *this; } \
01738 name & operator =(const name & value) { data = value.data; return *this; } \
01739 operator type() const { return data; } \
01740 friend ostream & operator<<(ostream & s, const name & v) { return s << v.data; } \
01741 friend istream & operator>>(istream & s, name & v) { return s >> v.data; } \
01742 private: type data; \
01743 }
01744
01745 #define PI_LOOP(src, dst) \
01746 BYTE *s = ((BYTE *)&src)+sizeof(src); BYTE *d = (BYTE *)&dst; \
01747 while (s != (BYTE *)&src) *d++ = *--s;
01748
01749 #define PI_DIFF(name, type) \
01750 struct name { \
01751 name() : data(0) { } \
01752 name(type value) : data(0) { operator=(value); } \
01753 name(const name & value) : data(value.data) { } \
01754 name & operator =(type value) { PI_LOOP(value, data); return *this; } \
01755 name & operator =(const name & value) { data = value.data; return *this; } \
01756 operator type() const { type value; PI_LOOP(data, value); return value; } \
01757 friend ostream & operator<<(ostream & s, const name & value) { return s << (type)value; } \
01758 friend istream & operator>>(istream & s, name & v) { type val; s >> val; v = val; return s; } \
01759 private: type data; \
01760 }
01761
01762 #ifndef PCHAR8
01763 #define PCHAR8 PANSI_CHAR
01764 #endif
01765
01766 #if PCHAR8==PANSI_CHAR
01767 PI_SAME(PChar8, char);
01768 #endif
01769
01770 PI_SAME(PInt8, signed char);
01771
01772 PI_SAME(PUInt8, unsigned char);
01773
01774 #if PBYTE_ORDER==PLITTLE_ENDIAN
01775 PI_SAME(PInt16l, PInt16);
01776 #elif PBYTE_ORDER==PBIG_ENDIAN
01777 PI_DIFF(PInt16l, PInt16);
01778 #endif
01779
01780 #if PBYTE_ORDER==PLITTLE_ENDIAN
01781 PI_DIFF(PInt16b, PInt16);
01782 #elif PBYTE_ORDER==PBIG_ENDIAN
01783 PI_SAME(PInt16b, PInt16);
01784 #endif
01785
01786 #if PBYTE_ORDER==PLITTLE_ENDIAN
01787 PI_SAME(PUInt16l, WORD);
01788 #elif PBYTE_ORDER==PBIG_ENDIAN
01789 PI_DIFF(PUInt16l, WORD);
01790 #endif
01791
01792 #if PBYTE_ORDER==PLITTLE_ENDIAN
01793 PI_DIFF(PUInt16b, WORD);
01794 #elif PBYTE_ORDER==PBIG_ENDIAN
01795 PI_SAME(PUInt16b, WORD);
01796 #endif
01797
01798 #if PBYTE_ORDER==PLITTLE_ENDIAN
01799 PI_SAME(PInt32l, PInt32);
01800 #elif PBYTE_ORDER==PBIG_ENDIAN
01801 PI_DIFF(PInt32l, PInt32);
01802 #endif
01803
01804 #if PBYTE_ORDER==PLITTLE_ENDIAN
01805 PI_DIFF(PInt32b, PInt32);
01806 #elif PBYTE_ORDER==PBIG_ENDIAN
01807 PI_SAME(PInt32b, PInt32);
01808 #endif
01809
01810 #if PBYTE_ORDER==PLITTLE_ENDIAN
01811 PI_SAME(PUInt32l, DWORD);
01812 #elif PBYTE_ORDER==PBIG_ENDIAN
01813 PI_DIFF(PUInt32l, DWORD);
01814 #endif
01815
01816 #if PBYTE_ORDER==PLITTLE_ENDIAN
01817 PI_DIFF(PUInt32b, DWORD);
01818 #elif PBYTE_ORDER==PBIG_ENDIAN
01819 PI_SAME(PUInt32b, DWORD);
01820 #endif
01821
01822 #if PBYTE_ORDER==PLITTLE_ENDIAN
01823 PI_SAME(PInt64l, PInt64);
01824 #elif PBYTE_ORDER==PBIG_ENDIAN
01825 PI_DIFF(PInt64l, PInt64);
01826 #endif
01827
01828 #if PBYTE_ORDER==PLITTLE_ENDIAN
01829 PI_DIFF(PInt64b, PInt64);
01830 #elif PBYTE_ORDER==PBIG_ENDIAN
01831 PI_SAME(PInt64b, PInt64);
01832 #endif
01833
01834 #if PBYTE_ORDER==PLITTLE_ENDIAN
01835 PI_SAME(PUInt64l, PUInt64);
01836 #elif PBYTE_ORDER==PBIG_ENDIAN
01837 PI_DIFF(PUInt64l, PUInt64);
01838 #endif
01839
01840 #if PBYTE_ORDER==PLITTLE_ENDIAN
01841 PI_DIFF(PUInt64b, PUInt64);
01842 #elif PBYTE_ORDER==PBIG_ENDIAN
01843 PI_SAME(PUInt64b, PUInt64);
01844 #endif
01845
01846 #if PBYTE_ORDER==PLITTLE_ENDIAN
01847 PI_SAME(PFloat32l, float);
01848 #elif PBYTE_ORDER==PBIG_ENDIAN
01849 PI_DIFF(PFloat32l, float);
01850 #endif
01851
01852 #if PBYTE_ORDER==PLITTLE_ENDIAN
01853 PI_DIFF(PFloat32b, float);
01854 #elif PBYTE_ORDER==PBIG_ENDIAN
01855 PI_SAME(PFloat32b, float);
01856 #endif
01857
01858 #if PBYTE_ORDER==PLITTLE_ENDIAN
01859 PI_SAME(PFloat64l, double);
01860 #elif PBYTE_ORDER==PBIG_ENDIAN
01861 PI_DIFF(PFloat64l, double);
01862 #endif
01863
01864 #if PBYTE_ORDER==PLITTLE_ENDIAN
01865 PI_DIFF(PFloat64b, double);
01866 #elif PBYTE_ORDER==PBIG_ENDIAN
01867 PI_SAME(PFloat64b, double);
01868 #endif
01869
01870 #ifndef NO_LONG_DOUBLE // stupid OSX compiler
01871 #if PBYTE_ORDER==PLITTLE_ENDIAN
01872 PI_SAME(PFloat80l, long double);
01873 #elif PBYTE_ORDER==PBIG_ENDIAN
01874 PI_DIFF(PFloat80l, long double);
01875 #endif
01876
01877 #if PBYTE_ORDER==PLITTLE_ENDIAN
01878 PI_DIFF(PFloat80b, long double);
01879 #elif PBYTE_ORDER==PBIG_ENDIAN
01880 PI_SAME(PFloat80b, long double);
01881 #endif
01882 #endif
01883
01884 #undef PI_LOOP
01885 #undef PI_SAME
01886 #undef PI_DIFF
01887
01888
01890
01891
01892
01893
01894
01895
01896 #define PARRAYSIZE(array) ((PINDEX)(sizeof(array)/sizeof(array[0])))
01897
01898
01899
01900
01901
01902
01903 #define PMIN(v1, v2) ((v1) < (v2) ? (v1) : (v2))
01904
01905
01906
01907
01908
01909
01910 #define PMAX(v1, v2) ((v1) > (v2) ? (v1) : (v2))
01911
01912
01913
01914
01915
01916
01917 #define PABS(v) ((v) < 0 ? -(v) : (v))
01918
01919 #endif // _POBJECT_H
01920
01921