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 #ifndef _ARRAY_H_
00149 #define _ARRAY_H_
00150
00151 #ifdef P_USE_PRAGMA
00152 #pragma interface
00153 #endif
00154
00155 #include <ptlib/contain.h>
00156
00158
00159
00181 class PAbstractArray : public PContainer
00182 {
00183 PCONTAINERINFO(PAbstractArray, PContainer);
00184
00185 public:
00197 PAbstractArray(
00198 PINDEX elementSizeInBytes,
00199
00200 PINDEX initialSize = 0
00201 );
00202
00220 PAbstractArray(
00221 PINDEX elementSizeInBytes,
00222
00223 const void *buffer,
00224 PINDEX bufferSizeInElements,
00225 BOOL dynamicAllocation
00226 );
00228
00237 virtual void PrintOn(
00238 ostream &strm
00239 ) const;
00240
00247 virtual void ReadFrom(
00248 istream &strm
00249 );
00250
00271 virtual Comparison Compare(
00272 const PObject & obj
00273 ) const;
00275
00286 virtual BOOL SetSize(
00287 PINDEX newSize
00288 );
00290
00301 void Attach(
00302 const void *buffer,
00303 PINDEX bufferSize
00304 );
00305
00319 void * GetPointer(
00320 PINDEX minSize = 1
00321 );
00322
00335 BOOL Concatenate(
00336 const PAbstractArray & array
00337 );
00339
00340 protected:
00341 BOOL InternalSetSize(PINDEX newSize, BOOL force);
00342
00343 virtual void PrintElementOn(
00344 ostream & stream,
00345 PINDEX index
00346 ) const;
00347 virtual void ReadElementFrom(
00348 istream & stream,
00349 PINDEX index
00350 );
00351
00353 PINDEX elementSize;
00354
00356 char * theArray;
00357
00359 BOOL allocatedDynamically;
00360
00361 friend class PArrayObjects;
00362 };
00363
00364
00366
00367
00368 #ifdef PHAS_TEMPLATES
00369
00389 template <class T> class PBaseArray : public PAbstractArray
00390 {
00391 PCLASSINFO(PBaseArray, PAbstractArray);
00392
00393 public:
00401 PBaseArray(
00402 PINDEX initialSize = 0
00403 ) : PAbstractArray(sizeof(T), initialSize) { }
00404
00407 PBaseArray(
00408 T const * buffer,
00409 PINDEX length,
00410 BOOL dynamic = TRUE
00411 ) : PAbstractArray(sizeof(T), buffer, length, dynamic) { }
00413
00418 virtual PObject * Clone() const
00419 {
00420 return PNEW PBaseArray<T>(*this, GetSize());
00421 }
00423
00432 BOOL SetAt(
00433 PINDEX index,
00434 T val
00435 ) {
00436 return SetMinSize(index+1) && val==(((T *)theArray)[index] = val);
00437 }
00438
00445 T GetAt(
00446 PINDEX index
00447 ) const {
00448 PASSERTINDEX(index);
00449 return index < GetSize() ? ((T *)theArray)[index] : (T)0;
00450 }
00451
00460 void Attach(
00461 const T * buffer,
00462 PINDEX bufferSize
00463 ) {
00464 PAbstractArray::Attach(buffer, bufferSize);
00465 }
00466
00480 T * GetPointer(
00481 PINDEX minSize = 0
00482 ) {
00483 return (T *)PAbstractArray::GetPointer(minSize);
00484 }
00486
00498 T operator[](
00499 PINDEX index
00500 ) const {
00501 return GetAt(index);
00502 }
00503
00514 T & operator[](
00515 PINDEX index
00516 ) {
00517 PASSERTINDEX(index);
00518 PAssert(SetMinSize(index+1), POutOfMemory);
00519 return ((T *)theArray)[index];
00520 }
00521
00535 operator T const *() const {
00536 return (T const *)theArray;
00537 }
00538
00550 BOOL Concatenate(
00551 const PBaseArray & array
00552 ) {
00553 return PAbstractArray::Concatenate(array);
00554 }
00556
00557 protected:
00558 virtual void PrintElementOn(
00559 ostream & stream,
00560 PINDEX index
00561 ) const {
00562 stream << GetAt(index);
00563 }
00564 };
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574 #define PBASEARRAY(cls, T) typedef PBaseArray<T> cls
00575
00588 #define PDECLARE_BASEARRAY(cls, T) \
00589 PDECLARE_CLASS(cls, PBaseArray<T>) \
00590 cls(PINDEX initialSize = 0) \
00591 : PBaseArray<T>(initialSize) { } \
00592 cls(T const * buffer, PINDEX length, BOOL dynamic = TRUE) \
00593 : PBaseArray<T>(buffer, length, dynamic) { } \
00594 virtual PObject * Clone() const \
00595 { return PNEW cls(*this, GetSize()); } \
00596
00597
00616 template <class T> class PScalarArray : public PBaseArray<T>
00617 {
00618 public:
00626 PScalarArray(
00627 PINDEX initialSize = 0
00628 ) : PBaseArray<T>(initialSize) { }
00629
00632 PScalarArray(
00633 T const * buffer,
00634 PINDEX length,
00635 BOOL dynamic = TRUE
00636 ) : PBaseArray<T>(buffer, length, dynamic) { }
00638
00639 protected:
00640 virtual void ReadElementFrom(
00641 istream & stream,
00642 PINDEX index
00643 ) {
00644 T t;
00645 stream >> t;
00646 if (!stream.fail())
00647 SetAt(index, t);
00648 }
00649 };
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660 #define PSCALAR_ARRAY(cls, T) typedef PScalarArray<T> cls
00661
00662 #else // PHAS_TEMPLATES
00663
00664 #define PBASEARRAY(cls, T) \
00665 typedef T P_##cls##_Base_Type; \
00666 class cls : public PAbstractArray { \
00667 PCLASSINFO(cls, PAbstractArray) \
00668 public: \
00669 inline cls(PINDEX initialSize = 0) \
00670 : PAbstractArray(sizeof(P_##cls##_Base_Type), initialSize) { } \
00671 inline cls(P_##cls##_Base_Type const * buffer, PINDEX length, BOOL dynamic = TRUE) \
00672 : PAbstractArray(sizeof(P_##cls##_Base_Type), buffer, length, dynamic) { } \
00673 virtual PObject * Clone() const \
00674 { return PNEW cls(*this, GetSize()); } \
00675 inline BOOL SetAt(PINDEX index, P_##cls##_Base_Type val) \
00676 { return SetMinSize(index+1) && \
00677 val==(((P_##cls##_Base_Type *)theArray)[index] = val); } \
00678 inline P_##cls##_Base_Type GetAt(PINDEX index) const \
00679 { PASSERTINDEX(index); return index < GetSize() ? \
00680 ((P_##cls##_Base_Type*)theArray)[index] : (P_##cls##_Base_Type)0; } \
00681 inline P_##cls##_Base_Type operator[](PINDEX index) const \
00682 { PASSERTINDEX(index); return GetAt(index); } \
00683 inline P_##cls##_Base_Type & operator[](PINDEX index) \
00684 { PASSERTINDEX(index); PAssert(SetMinSize(index+1), POutOfMemory); \
00685 return ((P_##cls##_Base_Type *)theArray)[index]; } \
00686 inline void Attach(const P_##cls##_Base_Type * buffer, PINDEX bufferSize) \
00687 { PAbstractArray::Attach(buffer, bufferSize); } \
00688 inline P_##cls##_Base_Type * GetPointer(PINDEX minSize = 0) \
00689 { return (P_##cls##_Base_Type *)PAbstractArray::GetPointer(minSize); } \
00690 inline operator P_##cls##_Base_Type const *() const \
00691 { return (P_##cls##_Base_Type const *)theArray; } \
00692 inline BOOL Concatenate(cls const & array) \
00693 { return PAbstractArray::Concatenate(array); } \
00694 }
00695
00696 #define PDECLARE_BASEARRAY(cls, T) \
00697 PBASEARRAY(cls##_PTemplate, T); \
00698 PDECLARE_CLASS(cls, cls##_PTemplate) \
00699 cls(PINDEX initialSize = 0) \
00700 : cls##_PTemplate(initialSize) { } \
00701 cls(T const * buffer, PINDEX length, BOOL dynamic = TRUE) \
00702 : cls##_PTemplate(buffer, length, dynamic) { } \
00703 virtual PObject * Clone() const \
00704 { return PNEW cls(*this, GetSize()); } \
00705
00706 #define PSCALAR_ARRAY(cls, T) PBASEARRAY(cls, T)
00707
00708 #endif // PHAS_TEMPLATES
00709
00710
00712 #ifdef DOC_PLUS_PLUS
00713 class PCharArray : public PBaseArray {
00714 public:
00720 PCharArray(
00721 PINDEX initialSize = 0
00722 );
00723
00726 PCharArray(
00727 char const * buffer,
00728 PINDEX length,
00729 BOOL dynamic = TRUE
00730 );
00732 #endif
00733 PDECLARE_BASEARRAY(PCharArray, char);
00734 public:
00737
00738 virtual void PrintOn(
00739 ostream & strm
00740 ) const;
00742 virtual void ReadFrom(
00743 istream &strm
00744 );
00746 };
00747
00749 #ifdef DOC_PLUS_PLUS
00750 class PShortArray : public PBaseArray {
00751 public:
00757 PShortArray(
00758 PINDEX initialSize = 0
00759 );
00760
00763 PShortArray(
00764 short const * buffer,
00765 PINDEX length,
00766 BOOL dynamic = TRUE
00767 );
00769 };
00770 #endif
00771 PSCALAR_ARRAY(PShortArray, short);
00772
00773
00775 #ifdef DOC_PLUS_PLUS
00776 class PIntArray : public PBaseArray {
00777 public:
00783 PIntArray(
00784 PINDEX initialSize = 0
00785 );
00786
00789 PIntArray(
00790 int const * buffer,
00791 PINDEX length,
00792 BOOL dynamic = TRUE
00793 );
00795 };
00796 #endif
00797 PSCALAR_ARRAY(PIntArray, int);
00798
00799
00801 #ifdef DOC_PLUS_PLUS
00802 class PLongArray : public PBaseArray {
00803 public:
00809 PLongArray(
00810 PINDEX initialSize = 0
00811 );
00812
00815 PLongArray(
00816 long const * buffer,
00817 PINDEX length,
00818 BOOL dynamic = TRUE
00819 );
00821 };
00822 #endif
00823 PSCALAR_ARRAY(PLongArray, long);
00824
00825
00827 #ifdef DOC_PLUS_PLUS
00828 class PBYTEArray : public PBaseArray {
00829 public:
00835 PBYTEArray(
00836 PINDEX initialSize = 0
00837 );
00838
00841 PBYTEArray(
00842 BYTE const * buffer,
00843 PINDEX length,
00844 BOOL dynamic = TRUE
00845 );
00847 };
00848 #endif
00849 PDECLARE_BASEARRAY(PBYTEArray, BYTE);
00850 public:
00853
00854 virtual void PrintOn(
00855 ostream & strm
00856 ) const;
00858 virtual void ReadFrom(
00859 istream &strm
00860 );
00862 };
00863
00864
00866 #ifdef DOC_PLUS_PLUS
00867 class PWORDArray : public PBaseArray {
00868 public:
00874 PWORDArray(
00875 PINDEX initialSize = 0
00876 );
00877
00880 PWORDArray(
00881 WORD const * buffer,
00882 PINDEX length,
00883 BOOL dynamic = TRUE
00884 );
00886 };
00887 #endif
00888 PSCALAR_ARRAY(PWORDArray, WORD);
00889
00890
00892 #ifdef DOC_PLUS_PLUS
00893 class PUnsignedArray : public PBaseArray {
00894 public:
00900 PUnsignedArray(
00901 PINDEX initialSize = 0
00902 );
00903
00906 PUnsignedArray(
00907 unsigned const * buffer,
00908 PINDEX length,
00909 BOOL dynamic = TRUE
00910 );
00912 };
00913 #endif
00914 PSCALAR_ARRAY(PUnsignedArray, unsigned);
00915
00916
00918 #ifdef DOC_PLUS_PLUS
00919 class PDWORDArray : public PBaseArray {
00920 public:
00926 PDWORDArray(
00927 PINDEX initialSize = 0
00928 );
00929
00932 PDWORDArray(
00933 DWORD const * buffer,
00934 PINDEX length,
00935 BOOL dynamic = TRUE
00936 );
00938 #endif
00939 PSCALAR_ARRAY(PDWORDArray, DWORD);
00940
00941
00943
00944
00966 class PArrayObjects : public PCollection
00967 {
00968 PCONTAINERINFO(PArrayObjects, PCollection);
00969
00970 public:
00979 PINLINE PArrayObjects(
00980 PINDEX initialSize = 0
00981 );
00983
01016 virtual Comparison Compare(
01017 const PObject & obj
01018 ) const;
01020
01023
01024 virtual PINDEX GetSize() const;
01025
01034 virtual BOOL SetSize(
01035 PINDEX newSize
01036 );
01038
01047 virtual PINDEX Append(
01048 PObject * obj
01049 );
01050
01066 virtual PINDEX Insert(
01067 const PObject & before,
01068 PObject * obj
01069 );
01070
01081 virtual PINDEX InsertAt(
01082 PINDEX index,
01083 PObject * obj
01084 );
01085
01094 virtual BOOL Remove(
01095 const PObject * obj
01096 );
01097
01109 virtual PObject * RemoveAt(
01110 PINDEX index
01111 );
01112
01120 virtual BOOL SetAt(
01121 PINDEX index,
01122 PObject * val
01123 );
01124
01131 virtual PObject * GetAt(
01132 PINDEX index
01133 ) const;
01134
01142 virtual PINDEX GetObjectsIndex(
01143 const PObject * obj
01144 ) const;
01145
01155 virtual PINDEX GetValuesIndex(
01156 const PObject & obj
01157 ) const;
01158
01165 virtual void RemoveAll();
01167
01168 protected:
01169
01170 PBaseArray<PObject *> * theArray;
01171 };
01172
01173
01174 #ifdef PHAS_TEMPLATES
01175
01182 template <class T> class PArray : public PArrayObjects
01183 {
01184 PCLASSINFO(PArray, PArrayObjects);
01185
01186 public:
01195 PArray(
01196 PINDEX initialSize = 0
01197 ) : PArrayObjects(initialSize) { }
01199
01205 virtual PObject * Clone() const
01206 { return PNEW PArray(0, this); }
01208
01218 T & operator[](
01219 PINDEX index
01220 ) const {
01221 PObject * obj = GetAt(index);
01222 PAssert(obj != NULL, PInvalidArrayElement);
01223 return (T &)*obj;
01224 }
01226
01227 protected:
01228 PArray(int dummy, const PArray * c) : PArrayObjects(dummy, c) { }
01229 };
01230
01231
01243 #define PARRAY(cls, T) typedef PArray<T> cls
01244
01245
01258 #define PDECLARE_ARRAY(cls, T) \
01259 PARRAY(cls##_PTemplate, T); \
01260 PDECLARE_CLASS(cls, cls##_PTemplate) \
01261 protected: \
01262 inline cls(int dummy, const cls * c) \
01263 : cls##_PTemplate(dummy, c) { } \
01264 public: \
01265 inline cls(PINDEX initialSize = 0) \
01266 : cls##_PTemplate(initialSize) { } \
01267 virtual PObject * Clone() const \
01268 { return PNEW cls(0, this); } \
01269
01270 #else // PHAS_TEMPLATES
01271
01272
01273 #define PARRAY(cls, T) \
01274 class cls : public PArrayObjects { \
01275 PCLASSINFO(cls, PArrayObjects); \
01276 protected: \
01277 inline cls(int dummy, const cls * c) \
01278 : PArrayObjects(dummy, c) { } \
01279 public: \
01280 inline cls(PINDEX initialSize = 0) \
01281 : PArrayObjects(initialSize) { } \
01282 virtual PObject * Clone() const \
01283 { return PNEW cls(0, this); } \
01284 inline T & operator[](PINDEX index) const\
01285 { PObject * obj = GetAt(index); \
01286 PAssert(obj != NULL, PInvalidArrayElement); \
01287 \
01288 return (T &)*obj; } \
01289 }
01290
01291 #define PDECLARE_ARRAY(cls, T) \
01292 PARRAY(cls##_PTemplate, T); \
01293 PDECLARE_CLASS(cls, cls##_PTemplate) \
01294 protected: \
01295 inline cls(int dummy, const cls * c) \
01296 : cls##_PTemplate(dummy, c) { } \
01297 public: \
01298 inline cls(PINDEX initialSize = 0) \
01299 : cls##_PTemplate(initialSize) { } \
01300 virtual PObject * Clone() const \
01301 { return PNEW cls(0, this); } \
01302
01303 #endif // PHAS_TEMPLATES
01304
01305
01308 class PBitArray : public PBYTEArray
01309 {
01310 PCLASSINFO(PBitArray, PBYTEArray);
01311
01312 public:
01317 PBitArray(
01318 PINDEX initialSize = 0
01319 );
01320
01323 PBitArray(
01324 const void * buffer,
01325 PINDEX length,
01326 BOOL dynamic = TRUE
01327 );
01329
01334 virtual PObject * Clone() const;
01336
01345 virtual PINDEX GetSize() const;
01346
01355 virtual BOOL SetSize(
01356 PINDEX newSize
01357 );
01358
01365 BOOL SetAt(
01366 PINDEX index,
01367 BOOL val
01368 );
01369
01376 BOOL GetAt(
01377 PINDEX index
01378 ) const;
01379
01388 void Attach(
01389 const void * buffer,
01390 PINDEX bufferSize
01391 );
01392
01406 BYTE * GetPointer(
01407 PINDEX minSize = 0
01408 );
01410
01422 BOOL operator[](
01423 PINDEX index
01424 ) const { return GetAt(index); }
01425
01431 PBitArray & operator+=(
01432 PINDEX index
01433 ) { SetAt(index, TRUE); return *this; }
01434
01440 PBitArray & operator-=(
01441 PINDEX index
01442 ) { SetAt(index, FALSE); return *this; }
01443
01455 BOOL Concatenate(
01456 const PBitArray & array
01457 );
01459 };
01460
01461
01462 #endif
01463