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 #ifndef _ARRAY_H_
00146 #define _ARRAY_H_
00147
00148 #ifdef P_USE_PRAGMA
00149 #pragma interface
00150 #endif
00151
00152 #include <ptlib/contain.h>
00153
00155
00156
00178 class PAbstractArray : public PContainer
00179 {
00180 PCONTAINERINFO(PAbstractArray, PContainer);
00181
00182 public:
00194 PAbstractArray(
00195 PINDEX elementSizeInBytes,
00196
00197 PINDEX initialSize = 0
00198 );
00199
00217 PAbstractArray(
00218 PINDEX elementSizeInBytes,
00219
00220 const void *buffer,
00221 PINDEX bufferSizeInElements,
00222 BOOL dynamicAllocation
00223 );
00225
00234 virtual void PrintOn(
00235 ostream &strm
00236 ) const;
00237
00244 virtual void ReadFrom(
00245 istream &strm
00246 );
00247
00268 virtual Comparison Compare(
00269 const PObject & obj
00270 ) const;
00272
00283 virtual BOOL SetSize(
00284 PINDEX newSize
00285 );
00287
00298 void Attach(
00299 const void *buffer,
00300 PINDEX bufferSize
00301 );
00302
00316 void * GetPointer(
00317 PINDEX minSize = 1
00318 );
00319
00332 BOOL Concatenate(
00333 const PAbstractArray & array
00334 );
00336
00337 protected:
00338 BOOL InternalSetSize(PINDEX newSize, BOOL force);
00339
00340 virtual void PrintElementOn(
00341 ostream & stream,
00342 PINDEX index
00343 ) const;
00344 virtual void ReadElementFrom(
00345 istream & stream,
00346 PINDEX index
00347 );
00348
00350 PINDEX elementSize;
00351
00353 char * theArray;
00354
00356 BOOL allocatedDynamically;
00357
00358 friend class PArrayObjects;
00359 };
00360
00361
00363
00364
00365 #ifdef PHAS_TEMPLATES
00366
00386 template <class T> class PBaseArray : public PAbstractArray
00387 {
00388 PCLASSINFO(PBaseArray, PAbstractArray);
00389
00390 public:
00398 PBaseArray(
00399 PINDEX initialSize = 0
00400 ) : PAbstractArray(sizeof(T), initialSize) { }
00401
00404 PBaseArray(
00405 T const * buffer,
00406 PINDEX length,
00407 BOOL dynamic = TRUE
00408 ) : PAbstractArray(sizeof(T), buffer, length, dynamic) { }
00410
00415 virtual PObject * Clone() const
00416 {
00417 return PNEW PBaseArray<T>(*this, GetSize());
00418 }
00420
00429 BOOL SetAt(
00430 PINDEX index,
00431 T val
00432 ) {
00433 return SetMinSize(index+1) && val==(((T *)theArray)[index] = val);
00434 }
00435
00442 T GetAt(
00443 PINDEX index
00444 ) const {
00445 PASSERTINDEX(index);
00446 return index < GetSize() ? ((T *)theArray)[index] : (T)0;
00447 }
00448
00457 void Attach(
00458 const T * buffer,
00459 PINDEX bufferSize
00460 ) {
00461 PAbstractArray::Attach(buffer, bufferSize);
00462 }
00463
00477 T * GetPointer(
00478 PINDEX minSize = 0
00479 ) {
00480 return (T *)PAbstractArray::GetPointer(minSize);
00481 }
00483
00495 T operator[](
00496 PINDEX index
00497 ) const {
00498 return GetAt(index);
00499 }
00500
00511 T & operator[](
00512 PINDEX index
00513 ) {
00514 PASSERTINDEX(index);
00515 PAssert(SetMinSize(index+1), POutOfMemory);
00516 return ((T *)theArray)[index];
00517 }
00518
00532 operator T const *() const {
00533 return (T const *)theArray;
00534 }
00535
00547 BOOL Concatenate(
00548 const PBaseArray & array
00549 ) {
00550 return PAbstractArray::Concatenate(array);
00551 }
00553
00554 protected:
00555 virtual void PrintElementOn(
00556 ostream & stream,
00557 PINDEX index
00558 ) const {
00559 stream << GetAt(index);
00560 }
00561 };
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571 #define PBASEARRAY(cls, T) typedef PBaseArray<T> cls
00572
00585 #define PDECLARE_BASEARRAY(cls, T) \
00586 PDECLARE_CLASS(cls, PBaseArray<T>) \
00587 cls(PINDEX initialSize = 0) \
00588 : PBaseArray<T>(initialSize) { } \
00589 cls(T const * buffer, PINDEX length, BOOL dynamic = TRUE) \
00590 : PBaseArray<T>(buffer, length, dynamic) { } \
00591 virtual PObject * Clone() const \
00592 { return PNEW cls(*this, GetSize()); } \
00593
00594
00613 template <class T> class PScalarArray : public PBaseArray<T>
00614 {
00615 public:
00623 PScalarArray(
00624 PINDEX initialSize = 0
00625 ) : PBaseArray<T>(initialSize) { }
00626
00629 PScalarArray(
00630 T const * buffer,
00631 PINDEX length,
00632 BOOL dynamic = TRUE
00633 ) : PBaseArray<T>(buffer, length, dynamic) { }
00635
00636 protected:
00637 virtual void ReadElementFrom(
00638 istream & stream,
00639 PINDEX index
00640 ) {
00641 T t;
00642 stream >> t;
00643 if (!stream.fail())
00644 SetAt(index, t);
00645 }
00646 };
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657 #define PSCALAR_ARRAY(cls, T) typedef PScalarArray<T> cls
00658
00659 #else // PHAS_TEMPLATES
00660
00661 #define PBASEARRAY(cls, T) \
00662 typedef T P_##cls##_Base_Type; \
00663 class cls : public PAbstractArray { \
00664 PCLASSINFO(cls, PAbstractArray) \
00665 public: \
00666 inline cls(PINDEX initialSize = 0) \
00667 : PAbstractArray(sizeof(P_##cls##_Base_Type), initialSize) { } \
00668 inline cls(P_##cls##_Base_Type const * buffer, PINDEX length, BOOL dynamic = TRUE) \
00669 : PAbstractArray(sizeof(P_##cls##_Base_Type), buffer, length, dynamic) { } \
00670 virtual PObject * Clone() const \
00671 { return PNEW cls(*this, GetSize()); } \
00672 inline BOOL SetAt(PINDEX index, P_##cls##_Base_Type val) \
00673 { return SetMinSize(index+1) && \
00674 val==(((P_##cls##_Base_Type *)theArray)[index] = val); } \
00675 inline P_##cls##_Base_Type GetAt(PINDEX index) const \
00676 { PASSERTINDEX(index); return index < GetSize() ? \
00677 ((P_##cls##_Base_Type*)theArray)[index] : (P_##cls##_Base_Type)0; } \
00678 inline P_##cls##_Base_Type operator[](PINDEX index) const \
00679 { PASSERTINDEX(index); return GetAt(index); } \
00680 inline P_##cls##_Base_Type & operator[](PINDEX index) \
00681 { PASSERTINDEX(index); PAssert(SetMinSize(index+1), POutOfMemory); \
00682 return ((P_##cls##_Base_Type *)theArray)[index]; } \
00683 inline void Attach(const P_##cls##_Base_Type * buffer, PINDEX bufferSize) \
00684 { PAbstractArray::Attach(buffer, bufferSize); } \
00685 inline P_##cls##_Base_Type * GetPointer(PINDEX minSize = 0) \
00686 { return (P_##cls##_Base_Type *)PAbstractArray::GetPointer(minSize); } \
00687 inline operator P_##cls##_Base_Type const *() const \
00688 { return (P_##cls##_Base_Type const *)theArray; } \
00689 inline BOOL Concatenate(cls const & array) \
00690 { return PAbstractArray::Concatenate(array); } \
00691 }
00692
00693 #define PDECLARE_BASEARRAY(cls, T) \
00694 PBASEARRAY(cls##_PTemplate, T); \
00695 PDECLARE_CLASS(cls, cls##_PTemplate) \
00696 cls(PINDEX initialSize = 0) \
00697 : cls##_PTemplate(initialSize) { } \
00698 cls(T const * buffer, PINDEX length, BOOL dynamic = TRUE) \
00699 : cls##_PTemplate(buffer, length, dynamic) { } \
00700 virtual PObject * Clone() const \
00701 { return PNEW cls(*this, GetSize()); } \
00702
00703 #define PSCALAR_ARRAY(cls, T) PBASEARRAY(cls, T)
00704
00705 #endif // PHAS_TEMPLATES
00706
00707
00709 #ifdef DOC_PLUS_PLUS
00710 class PCharArray : public PBaseArray {
00711 public:
00717 PCharArray(
00718 PINDEX initialSize = 0
00719 );
00720
00723 PCharArray(
00724 char const * buffer,
00725 PINDEX length,
00726 BOOL dynamic = TRUE
00727 );
00729 #endif
00730 PDECLARE_BASEARRAY(PCharArray, char);
00731 public:
00734
00735 virtual void PrintOn(
00736 ostream & strm
00737 ) const;
00739 virtual void ReadFrom(
00740 istream &strm
00741 );
00743 };
00744
00746 #ifdef DOC_PLUS_PLUS
00747 class PShortArray : public PBaseArray {
00748 public:
00754 PShortArray(
00755 PINDEX initialSize = 0
00756 );
00757
00760 PShortArray(
00761 short const * buffer,
00762 PINDEX length,
00763 BOOL dynamic = TRUE
00764 );
00766 };
00767 #endif
00768 PSCALAR_ARRAY(PShortArray, short);
00769
00770
00772 #ifdef DOC_PLUS_PLUS
00773 class PIntArray : public PBaseArray {
00774 public:
00780 PIntArray(
00781 PINDEX initialSize = 0
00782 );
00783
00786 PIntArray(
00787 int const * buffer,
00788 PINDEX length,
00789 BOOL dynamic = TRUE
00790 );
00792 };
00793 #endif
00794 PSCALAR_ARRAY(PIntArray, int);
00795
00796
00798 #ifdef DOC_PLUS_PLUS
00799 class PLongArray : public PBaseArray {
00800 public:
00806 PLongArray(
00807 PINDEX initialSize = 0
00808 );
00809
00812 PLongArray(
00813 long const * buffer,
00814 PINDEX length,
00815 BOOL dynamic = TRUE
00816 );
00818 };
00819 #endif
00820 PSCALAR_ARRAY(PLongArray, long);
00821
00822
00824 #ifdef DOC_PLUS_PLUS
00825 class PBYTEArray : public PBaseArray {
00826 public:
00832 PBYTEArray(
00833 PINDEX initialSize = 0
00834 );
00835
00838 PBYTEArray(
00839 BYTE const * buffer,
00840 PINDEX length,
00841 BOOL dynamic = TRUE
00842 );
00844 };
00845 #endif
00846 PDECLARE_BASEARRAY(PBYTEArray, BYTE);
00847 public:
00850
00851 virtual void PrintOn(
00852 ostream & strm
00853 ) const;
00855 virtual void ReadFrom(
00856 istream &strm
00857 );
00859 };
00860
00861
00863 #ifdef DOC_PLUS_PLUS
00864 class PWORDArray : public PBaseArray {
00865 public:
00871 PWORDArray(
00872 PINDEX initialSize = 0
00873 );
00874
00877 PWORDArray(
00878 WORD const * buffer,
00879 PINDEX length,
00880 BOOL dynamic = TRUE
00881 );
00883 };
00884 #endif
00885 PSCALAR_ARRAY(PWORDArray, WORD);
00886
00887
00889 #ifdef DOC_PLUS_PLUS
00890 class PUnsignedArray : public PBaseArray {
00891 public:
00897 PUnsignedArray(
00898 PINDEX initialSize = 0
00899 );
00900
00903 PUnsignedArray(
00904 unsigned const * buffer,
00905 PINDEX length,
00906 BOOL dynamic = TRUE
00907 );
00909 };
00910 #endif
00911 PSCALAR_ARRAY(PUnsignedArray, unsigned);
00912
00913
00915 #ifdef DOC_PLUS_PLUS
00916 class PDWORDArray : public PBaseArray {
00917 public:
00923 PDWORDArray(
00924 PINDEX initialSize = 0
00925 );
00926
00929 PDWORDArray(
00930 DWORD const * buffer,
00931 PINDEX length,
00932 BOOL dynamic = TRUE
00933 );
00935 #endif
00936 PSCALAR_ARRAY(PDWORDArray, DWORD);
00937
00938
00940
00941
00963 class PArrayObjects : public PCollection
00964 {
00965 PCONTAINERINFO(PArrayObjects, PCollection);
00966
00967 public:
00976 PINLINE PArrayObjects(
00977 PINDEX initialSize = 0
00978 );
00980
01013 virtual Comparison Compare(
01014 const PObject & obj
01015 ) const;
01017
01020
01021 virtual PINDEX GetSize() const;
01022
01031 virtual BOOL SetSize(
01032 PINDEX newSize
01033 );
01035
01044 virtual PINDEX Append(
01045 PObject * obj
01046 );
01047
01063 virtual PINDEX Insert(
01064 const PObject & before,
01065 PObject * obj
01066 );
01067
01078 virtual PINDEX InsertAt(
01079 PINDEX index,
01080 PObject * obj
01081 );
01082
01091 virtual BOOL Remove(
01092 const PObject * obj
01093 );
01094
01106 virtual PObject * RemoveAt(
01107 PINDEX index
01108 );
01109
01117 virtual BOOL SetAt(
01118 PINDEX index,
01119 PObject * val
01120 );
01121
01128 virtual PObject * GetAt(
01129 PINDEX index
01130 ) const;
01131
01139 virtual PINDEX GetObjectsIndex(
01140 const PObject * obj
01141 ) const;
01142
01152 virtual PINDEX GetValuesIndex(
01153 const PObject & obj
01154 ) const;
01155
01162 virtual void RemoveAll();
01164
01165 protected:
01166 PBASEARRAY(ObjPtrArray, PObject *);
01167 ObjPtrArray * theArray;
01168 };
01169
01170
01171 #ifdef PHAS_TEMPLATES
01172
01179 template <class T> class PArray : public PArrayObjects
01180 {
01181 PCLASSINFO(PArray, PArrayObjects);
01182
01183 public:
01192 PArray(
01193 PINDEX initialSize = 0
01194 ) : PArrayObjects(initialSize) { }
01196
01202 virtual PObject * Clone() const
01203 { return PNEW PArray(0, this); }
01205
01215 T & operator[](
01216 PINDEX index
01217 ) const {
01218 PObject * obj = GetAt(index);
01219 PAssert(obj != NULL, PInvalidArrayElement);
01220 return (T &)*obj;
01221 }
01223
01224 protected:
01225 PArray(int dummy, const PArray * c) : PArrayObjects(dummy, c) { }
01226 };
01227
01228
01240 #define PARRAY(cls, T) typedef PArray<T> cls
01241
01242
01255 #define PDECLARE_ARRAY(cls, T) \
01256 PARRAY(cls##_PTemplate, T); \
01257 PDECLARE_CLASS(cls, cls##_PTemplate) \
01258 protected: \
01259 inline cls(int dummy, const cls * c) \
01260 : cls##_PTemplate(dummy, c) { } \
01261 public: \
01262 inline cls(PINDEX initialSize = 0) \
01263 : cls##_PTemplate(initialSize) { } \
01264 virtual PObject * Clone() const \
01265 { return PNEW cls(0, this); } \
01266
01267 #else // PHAS_TEMPLATES
01268
01269
01270 #define PARRAY(cls, T) \
01271 class cls : public PArrayObjects { \
01272 PCLASSINFO(cls, PArrayObjects); \
01273 protected: \
01274 inline cls(int dummy, const cls * c) \
01275 : PArrayObjects(dummy, c) { } \
01276 public: \
01277 inline cls(PINDEX initialSize = 0) \
01278 : PArrayObjects(initialSize) { } \
01279 virtual PObject * Clone() const \
01280 { return PNEW cls(0, this); } \
01281 inline T & operator[](PINDEX index) const\
01282 { PObject * obj = GetAt(index); \
01283 PAssert(obj != NULL, PInvalidArrayElement); \
01284 \
01285 return (T &)*obj; } \
01286 }
01287
01288 #define PDECLARE_ARRAY(cls, T) \
01289 PARRAY(cls##_PTemplate, T); \
01290 PDECLARE_CLASS(cls, cls##_PTemplate) \
01291 protected: \
01292 inline cls(int dummy, const cls * c) \
01293 : cls##_PTemplate(dummy, c) { } \
01294 public: \
01295 inline cls(PINDEX initialSize = 0) \
01296 : cls##_PTemplate(initialSize) { } \
01297 virtual PObject * Clone() const \
01298 { return PNEW cls(0, this); } \
01299
01300 #endif // PHAS_TEMPLATES
01301
01302
01305 class PBitArray : public PBYTEArray
01306 {
01307 PCLASSINFO(PBitArray, PBYTEArray);
01308
01309 public:
01314 PBitArray(
01315 PINDEX initialSize = 0
01316 );
01317
01320 PBitArray(
01321 const void * buffer,
01322 PINDEX length,
01323 BOOL dynamic = TRUE
01324 );
01326
01331 virtual PObject * Clone() const;
01333
01342 virtual PINDEX GetSize() const;
01343
01352 virtual BOOL SetSize(
01353 PINDEX newSize
01354 );
01355
01362 BOOL SetAt(
01363 PINDEX index,
01364 BOOL val
01365 );
01366
01373 BOOL GetAt(
01374 PINDEX index
01375 ) const;
01376
01385 void Attach(
01386 const void * buffer,
01387 PINDEX bufferSize
01388 );
01389
01403 BYTE * GetPointer(
01404 PINDEX minSize = 0
01405 );
01407
01419 BOOL operator[](
01420 PINDEX index
01421 ) const { return GetAt(index); }
01422
01428 PBitArray & operator+=(
01429 PINDEX index
01430 ) { SetAt(index, TRUE); return *this; }
01431
01437 PBitArray & operator-=(
01438 PINDEX index
01439 ) { SetAt(index, FALSE); return *this; }
01440
01452 BOOL Concatenate(
01453 const PBitArray & array
01454 );
01456 };
01457
01458
01459 #endif
01460