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 #ifndef _ARRAY_H_
00035 #define _ARRAY_H_
00036
00037 #ifdef P_USE_PRAGMA
00038 #pragma interface
00039 #endif
00040
00041 #include <ptlib/contain.h>
00042
00044
00045
00067 class PAbstractArray : public PContainer
00068 {
00069 PCONTAINERINFO(PAbstractArray, PContainer);
00070
00071 public:
00083 PAbstractArray(
00084 PINDEX elementSizeInBytes,
00085
00086 PINDEX initialSize = 0
00087 );
00088
00106 PAbstractArray(
00107 PINDEX elementSizeInBytes,
00108
00109 const void *buffer,
00110 PINDEX bufferSizeInElements,
00111 PBoolean dynamicAllocation
00112 );
00114
00123 virtual void PrintOn(
00124 ostream &strm
00125 ) const;
00126
00133 virtual void ReadFrom(
00134 istream &strm
00135 );
00136
00156 virtual Comparison Compare(
00157 const PObject & obj
00158 ) const;
00160
00171 virtual PBoolean SetSize(
00172 PINDEX newSize
00173 );
00175
00186 void Attach(
00187 const void *buffer,
00188 PINDEX bufferSize
00189 );
00190
00204 void * GetPointer(
00205 PINDEX minSize = 1
00206 );
00207
00220 PBoolean Concatenate(
00221 const PAbstractArray & array
00222 );
00224
00225 protected:
00226 PBoolean InternalSetSize(PINDEX newSize, PBoolean force);
00227
00228 virtual void PrintElementOn(
00229 ostream & stream,
00230 PINDEX index
00231 ) const;
00232 virtual void ReadElementFrom(
00233 istream & stream,
00234 PINDEX index
00235 );
00236
00238 PINDEX elementSize;
00239
00241 char * theArray;
00242
00244 PBoolean allocatedDynamically;
00245
00246 friend class PArrayObjects;
00247 };
00248
00249
00251
00252
00270 template <class T> class PBaseArray : public PAbstractArray
00271 {
00272 PCLASSINFO(PBaseArray, PAbstractArray);
00273
00274 public:
00282 PBaseArray(
00283 PINDEX initialSize = 0
00284 ) : PAbstractArray(sizeof(T), initialSize) { }
00285
00288 PBaseArray(
00289 T const * buffer,
00290 PINDEX length,
00291 PBoolean dynamic = PTrue
00292 ) : PAbstractArray(sizeof(T), buffer, length, dynamic) { }
00294
00299 virtual PObject * Clone() const
00300 {
00301 return PNEW PBaseArray<T>(*this, GetSize());
00302 }
00304
00313 PBoolean SetAt(
00314 PINDEX index,
00315 T val
00316 ) {
00317 return SetMinSize(index+1) && val==(((T *)theArray)[index] = val);
00318 }
00319
00326 T GetAt(
00327 PINDEX index
00328 ) const {
00329 PASSERTINDEX(index);
00330 return index < GetSize() ? ((T *)theArray)[index] : (T)0;
00331 }
00332
00341 void Attach(
00342 const T * buffer,
00343 PINDEX bufferSize
00344 ) {
00345 PAbstractArray::Attach(buffer, bufferSize);
00346 }
00347
00361 T * GetPointer(
00362 PINDEX minSize = 0
00363 ) {
00364 return (T *)PAbstractArray::GetPointer(minSize);
00365 }
00367
00379 T operator[](
00380 PINDEX index
00381 ) const {
00382 return GetAt(index);
00383 }
00384
00395 T & operator[](
00396 PINDEX index
00397 ) {
00398 PASSERTINDEX(index);
00399 PAssert(SetMinSize(index+1), POutOfMemory);
00400 return ((T *)theArray)[index];
00401 }
00402
00416 operator T const *() const {
00417 return (T const *)theArray;
00418 }
00419
00431 PBoolean Concatenate(
00432 const PBaseArray & array
00433 ) {
00434 return PAbstractArray::Concatenate(array);
00435 }
00437
00438 protected:
00439 virtual void PrintElementOn(
00440 ostream & stream,
00441 PINDEX index
00442 ) const {
00443 stream << GetAt(index);
00444 }
00445 };
00446
00455 #define PBASEARRAY(cls, T) typedef PBaseArray<T> cls
00456
00469 #define PDECLARE_BASEARRAY(cls, T) \
00470 PDECLARE_CLASS(cls, PBaseArray<T>) \
00471 cls(PINDEX initialSize = 0) \
00472 : PBaseArray<T>(initialSize) { } \
00473 cls(T const * buffer, PINDEX length, PBoolean dynamic = PTrue) \
00474 : PBaseArray<T>(buffer, length, dynamic) { } \
00475 virtual PObject * Clone() const \
00476 { return PNEW cls(*this, GetSize()); } \
00477
00478
00495 template <class T> class PScalarArray : public PBaseArray<T>
00496 {
00497 public:
00505 PScalarArray(
00506 PINDEX initialSize = 0
00507 ) : PBaseArray<T>(initialSize) { }
00508
00511 PScalarArray(
00512 T const * buffer,
00513 PINDEX length,
00514 PBoolean dynamic = PTrue
00515 ) : PBaseArray<T>(buffer, length, dynamic) { }
00517
00518 protected:
00519 virtual void ReadElementFrom(
00520 istream & stream,
00521 PINDEX index
00522 ) {
00523 T t;
00524 stream >> t;
00525 if (!stream.fail())
00526 SetAt(index, t);
00527 }
00528 };
00529
00530
00539 #define PSCALAR_ARRAY(cls, T) typedef PScalarArray<T> cls
00540
00541
00543 #ifdef DOC_PLUS_PLUS
00544 class PCharArray : public PBaseArray {
00545 public:
00551 PCharArray(
00552 PINDEX initialSize = 0
00553 );
00554
00557 PCharArray(
00558 char const * buffer,
00559 PINDEX length,
00560 PBoolean dynamic = PTrue
00561 );
00563 #endif
00564 PDECLARE_BASEARRAY(PCharArray, char);
00565 public:
00568
00569 virtual void PrintOn(
00570 ostream & strm
00571 ) const;
00573 virtual void ReadFrom(
00574 istream &strm
00575 );
00577 };
00578
00580 #ifdef DOC_PLUS_PLUS
00581 class PShortArray : public PBaseArray {
00582 public:
00588 PShortArray(
00589 PINDEX initialSize = 0
00590 );
00591
00594 PShortArray(
00595 short const * buffer,
00596 PINDEX length,
00597 PBoolean dynamic = PTrue
00598 );
00600 };
00601 #endif
00602 PSCALAR_ARRAY(PShortArray, short);
00603
00604
00606 #ifdef DOC_PLUS_PLUS
00607 class PIntArray : public PBaseArray {
00608 public:
00614 PIntArray(
00615 PINDEX initialSize = 0
00616 );
00617
00620 PIntArray(
00621 int const * buffer,
00622 PINDEX length,
00623 PBoolean dynamic = PTrue
00624 );
00626 };
00627 #endif
00628 PSCALAR_ARRAY(PIntArray, int);
00629
00630
00632 #ifdef DOC_PLUS_PLUS
00633 class PLongArray : public PBaseArray {
00634 public:
00640 PLongArray(
00641 PINDEX initialSize = 0
00642 );
00643
00646 PLongArray(
00647 long const * buffer,
00648 PINDEX length,
00649 PBoolean dynamic = PTrue
00650 );
00652 };
00653 #endif
00654 PSCALAR_ARRAY(PLongArray, long);
00655
00656
00658 #ifdef DOC_PLUS_PLUS
00659 class PBYTEArray : public PBaseArray {
00660 public:
00666 PBYTEArray(
00667 PINDEX initialSize = 0
00668 );
00669
00672 PBYTEArray(
00673 BYTE const * buffer,
00674 PINDEX length,
00675 PBoolean dynamic = PTrue
00676 );
00678 };
00679 #endif
00680 PDECLARE_BASEARRAY(PBYTEArray, BYTE);
00681 public:
00684
00685 virtual void PrintOn(
00686 ostream & strm
00687 ) const;
00689 virtual void ReadFrom(
00690 istream &strm
00691 );
00693 };
00694
00695
00697 #ifdef DOC_PLUS_PLUS
00698 class PWORDArray : public PBaseArray {
00699 public:
00705 PWORDArray(
00706 PINDEX initialSize = 0
00707 );
00708
00711 PWORDArray(
00712 WORD const * buffer,
00713 PINDEX length,
00714 PBoolean dynamic = PTrue
00715 );
00717 };
00718 #endif
00719 PSCALAR_ARRAY(PWORDArray, WORD);
00720
00721
00723 #ifdef DOC_PLUS_PLUS
00724 class PUnsignedArray : public PBaseArray {
00725 public:
00731 PUnsignedArray(
00732 PINDEX initialSize = 0
00733 );
00734
00737 PUnsignedArray(
00738 unsigned const * buffer,
00739 PINDEX length,
00740 PBoolean dynamic = PTrue
00741 );
00743 };
00744 #endif
00745 PSCALAR_ARRAY(PUnsignedArray, unsigned);
00746
00747
00749 #ifdef DOC_PLUS_PLUS
00750 class PDWORDArray : public PBaseArray {
00751 public:
00757 PDWORDArray(
00758 PINDEX initialSize = 0
00759 );
00760
00763 PDWORDArray(
00764 DWORD const * buffer,
00765 PINDEX length,
00766 PBoolean dynamic = PTrue
00767 );
00769 #endif
00770 PSCALAR_ARRAY(PDWORDArray, DWORD);
00771
00772
00774
00775
00797 class PArrayObjects : public PCollection
00798 {
00799 PCONTAINERINFO(PArrayObjects, PCollection);
00800
00801 public:
00810 PINLINE PArrayObjects(
00811 PINDEX initialSize = 0
00812 );
00814
00844 virtual Comparison Compare(
00845 const PObject & obj
00846 ) const;
00848
00851
00852 virtual PINDEX GetSize() const;
00853
00862 virtual PBoolean SetSize(
00863 PINDEX newSize
00864 );
00866
00875 virtual PINDEX Append(
00876 PObject * obj
00877 );
00878
00894 virtual PINDEX Insert(
00895 const PObject & before,
00896 PObject * obj
00897 );
00898
00909 virtual PINDEX InsertAt(
00910 PINDEX index,
00911 PObject * obj
00912 );
00913
00922 virtual PBoolean Remove(
00923 const PObject * obj
00924 );
00925
00937 virtual PObject * RemoveAt(
00938 PINDEX index
00939 );
00940
00948 virtual PBoolean SetAt(
00949 PINDEX index,
00950 PObject * val
00951 );
00952
00959 virtual PObject * GetAt(
00960 PINDEX index
00961 ) const;
00962
00970 virtual PINDEX GetObjectsIndex(
00971 const PObject * obj
00972 ) const;
00973
00983 virtual PINDEX GetValuesIndex(
00984 const PObject & obj
00985 ) const;
00986
00993 virtual void RemoveAll();
00995
00996 protected:
00997
00998 PBaseArray<PObject *> * theArray;
00999 };
01000
01001
01008 template <class T> class PArray : public PArrayObjects
01009 {
01010 PCLASSINFO(PArray, PArrayObjects);
01011
01012 public:
01021 PArray(
01022 PINDEX initialSize = 0
01023 ) : PArrayObjects(initialSize) { }
01025
01031 virtual PObject * Clone() const
01032 { return PNEW PArray(0, this); }
01034
01044 T & operator[](
01045 PINDEX index
01046 ) const {
01047 PObject * obj = GetAt(index);
01048 PAssert(obj != NULL, PInvalidArrayElement);
01049 return (T &)*obj;
01050 }
01052
01053 protected:
01054 PArray(int dummy, const PArray * c) : PArrayObjects(dummy, c) { }
01055 };
01056
01057
01069 #define PARRAY(cls, T) typedef PArray<T> cls
01070
01071
01084 #define PDECLARE_ARRAY(cls, T) \
01085 PARRAY(cls##_PTemplate, T); \
01086 PDECLARE_CLASS(cls, cls##_PTemplate) \
01087 protected: \
01088 inline cls(int dummy, const cls * c) \
01089 : cls##_PTemplate(dummy, c) { } \
01090 public: \
01091 inline cls(PINDEX initialSize = 0) \
01092 : cls##_PTemplate(initialSize) { } \
01093 virtual PObject * Clone() const \
01094 { return PNEW cls(0, this); } \
01095
01096
01099 class PBitArray : public PBYTEArray
01100 {
01101 PCLASSINFO(PBitArray, PBYTEArray);
01102
01103 public:
01108 PBitArray(
01109 PINDEX initialSize = 0
01110 );
01111
01114 PBitArray(
01115 const void * buffer,
01116 PINDEX length,
01117 PBoolean dynamic = PTrue
01118 );
01120
01125 virtual PObject * Clone() const;
01127
01136 virtual PINDEX GetSize() const;
01137
01146 virtual PBoolean SetSize(
01147 PINDEX newSize
01148 );
01149
01156 PBoolean SetAt(
01157 PINDEX index,
01158 PBoolean val
01159 );
01160
01167 PBoolean GetAt(
01168 PINDEX index
01169 ) const;
01170
01179 void Attach(
01180 const void * buffer,
01181 PINDEX bufferSize
01182 );
01183
01197 BYTE * GetPointer(
01198 PINDEX minSize = 0
01199 );
01201
01213 PBoolean operator[](
01214 PINDEX index
01215 ) const { return GetAt(index); }
01216
01222 PBitArray & operator+=(
01223 PINDEX index
01224 ) { SetAt(index, PTrue); return *this; }
01225
01231 PBitArray & operator-=(
01232 PINDEX index
01233 ) { SetAt(index, PFalse); return *this; }
01234
01246 PBoolean Concatenate(
01247 const PBitArray & array
01248 );
01250 };
01251
01252
01253 #endif
01254