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 #ifdef P_USE_PRAGMA
00142 #pragma interface
00143 #endif
00144
00146
00147
00169 class PAbstractArray : public PContainer
00170 {
00171 PCONTAINERINFO(PAbstractArray, PContainer);
00172
00173 public:
00185 PAbstractArray(
00186 PINDEX elementSizeInBytes,
00187
00188 PINDEX initialSize = 0
00189 );
00190
00208 PAbstractArray(
00209 PINDEX elementSizeInBytes,
00210
00211 const void *buffer,
00212 PINDEX bufferSizeInElements,
00213 BOOL dynamicAllocation
00214 );
00216
00225 virtual void PrintOn(
00226 ostream &strm
00227 ) const;
00228
00235 virtual void ReadFrom(
00236 istream &strm
00237 );
00238
00259 virtual Comparison Compare(
00260 const PObject & obj
00261 ) const;
00263
00274 virtual BOOL SetSize(
00275 PINDEX newSize
00276 );
00278
00289 void Attach(
00290 const void *buffer,
00291 PINDEX bufferSize
00292 );
00293
00307 void * GetPointer(
00308 PINDEX minSize = 1
00309 );
00310
00323 BOOL Concatenate(
00324 const PAbstractArray & array
00325 );
00327
00328 protected:
00329 BOOL InternalSetSize(PINDEX newSize, BOOL force);
00330
00331 virtual void PrintElementOn(
00332 ostream & stream,
00333 PINDEX index
00334 ) const;
00335 virtual void ReadElementFrom(
00336 istream & stream,
00337 PINDEX index
00338 );
00339
00341 PINDEX elementSize;
00342
00344 char * theArray;
00345
00347 BOOL allocatedDynamically;
00348
00349 friend class PArrayObjects;
00350 };
00351
00352
00354
00355
00356 #ifdef PHAS_TEMPLATES
00357
00377 template <class T> class PBaseArray : public PAbstractArray
00378 {
00379 PCLASSINFO(PBaseArray, PAbstractArray);
00380
00381 public:
00389 PBaseArray(
00390 PINDEX initialSize = 0
00391 ) : PAbstractArray(sizeof(T), initialSize) { }
00392
00395 PBaseArray(
00396 T const * buffer,
00397 PINDEX length,
00398 BOOL dynamic = TRUE
00399 ) : PAbstractArray(sizeof(T), buffer, length, dynamic) { }
00401
00406 virtual PObject * Clone() const
00407 {
00408 return PNEW PBaseArray<T>(*this, GetSize());
00409 }
00411
00420 BOOL SetAt(
00421 PINDEX index,
00422 T val
00423 ) {
00424 return SetMinSize(index+1) && val==(((T *)theArray)[index] = val);
00425 }
00426
00433 T GetAt(
00434 PINDEX index
00435 ) const {
00436 PASSERTINDEX(index);
00437 return index < GetSize() ? ((T *)theArray)[index] : (T)0;
00438 }
00439
00448 void Attach(
00449 const T * buffer,
00450 PINDEX bufferSize
00451 ) {
00452 PAbstractArray::Attach(buffer, bufferSize);
00453 }
00454
00468 T * GetPointer(
00469 PINDEX minSize = 0
00470 ) {
00471 return (T *)PAbstractArray::GetPointer(minSize);
00472 }
00474
00486 T operator[](
00487 PINDEX index
00488 ) const {
00489 return GetAt(index);
00490 }
00491
00502 T & operator[](
00503 PINDEX index
00504 ) {
00505 PASSERTINDEX(index);
00506 PAssert(SetMinSize(index+1), POutOfMemory);
00507 return ((T *)theArray)[index];
00508 }
00509
00523 operator T const *() const {
00524 return (T const *)theArray;
00525 }
00526
00538 BOOL Concatenate(
00539 const PBaseArray & array
00540 ) {
00541 return PAbstractArray::Concatenate(array);
00542 }
00544
00545 protected:
00546 virtual void PrintElementOn(
00547 ostream & stream,
00548 PINDEX index
00549 ) const {
00550 stream << GetAt(index);
00551 }
00552 };
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562 #define PBASEARRAY(cls, T) typedef PBaseArray<T> cls
00563
00576 #define PDECLARE_BASEARRAY(cls, T) \
00577 PDECLARE_CLASS(cls, PBaseArray<T>) \
00578 cls(PINDEX initialSize = 0) \
00579 : PBaseArray<T>(initialSize) { } \
00580 cls(T const * buffer, PINDEX length, BOOL dynamic = TRUE) \
00581 : PBaseArray<T>(buffer, length, dynamic) { } \
00582 virtual PObject * Clone() const \
00583 { return PNEW cls(*this, GetSize()); } \
00584
00585
00604 template <class T> class PScalarArray : public PBaseArray<T>
00605 {
00606 public:
00614 PScalarArray(
00615 PINDEX initialSize = 0
00616 ) : PBaseArray<T>(initialSize) { }
00617
00620 PScalarArray(
00621 T const * buffer,
00622 PINDEX length,
00623 BOOL dynamic = TRUE
00624 ) : PBaseArray<T>(buffer, length, dynamic) { }
00626
00627 protected:
00628 virtual void ReadElementFrom(
00629 istream & stream,
00630 PINDEX index
00631 ) {
00632 T t;
00633 stream >> t;
00634 if (!stream.fail())
00635 SetAt(index, t);
00636 }
00637 };
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648 #define PSCALAR_ARRAY(cls, T) typedef PScalarArray<T> cls
00649
00650 #else // PHAS_TEMPLATES
00651
00652 #define PBASEARRAY(cls, T) \
00653 typedef T P_##cls##_Base_Type; \
00654 class cls : public PAbstractArray { \
00655 PCLASSINFO(cls, PAbstractArray) \
00656 public: \
00657 inline cls(PINDEX initialSize = 0) \
00658 : PAbstractArray(sizeof(P_##cls##_Base_Type), initialSize) { } \
00659 inline cls(P_##cls##_Base_Type const * buffer, PINDEX length, BOOL dynamic = TRUE) \
00660 : PAbstractArray(sizeof(P_##cls##_Base_Type), buffer, length, dynamic) { } \
00661 virtual PObject * Clone() const \
00662 { return PNEW cls(*this, GetSize()); } \
00663 inline BOOL SetAt(PINDEX index, P_##cls##_Base_Type val) \
00664 { return SetMinSize(index+1) && \
00665 val==(((P_##cls##_Base_Type *)theArray)[index] = val); } \
00666 inline P_##cls##_Base_Type GetAt(PINDEX index) const \
00667 { PASSERTINDEX(index); return index < GetSize() ? \
00668 ((P_##cls##_Base_Type*)theArray)[index] : (P_##cls##_Base_Type)0; } \
00669 inline P_##cls##_Base_Type operator[](PINDEX index) const \
00670 { PASSERTINDEX(index); return GetAt(index); } \
00671 inline P_##cls##_Base_Type & operator[](PINDEX index) \
00672 { PASSERTINDEX(index); PAssert(SetMinSize(index+1), POutOfMemory); \
00673 return ((P_##cls##_Base_Type *)theArray)[index]; } \
00674 inline void Attach(const P_##cls##_Base_Type * buffer, PINDEX bufferSize) \
00675 { PAbstractArray::Attach(buffer, bufferSize); } \
00676 inline P_##cls##_Base_Type * GetPointer(PINDEX minSize = 0) \
00677 { return (P_##cls##_Base_Type *)PAbstractArray::GetPointer(minSize); } \
00678 inline operator P_##cls##_Base_Type const *() const \
00679 { return (P_##cls##_Base_Type const *)theArray; } \
00680 inline BOOL Concatenate(cls const & array) \
00681 { return PAbstractArray::Concatenate(array); } \
00682 }
00683
00684 #define PDECLARE_BASEARRAY(cls, T) \
00685 PBASEARRAY(cls##_PTemplate, T); \
00686 PDECLARE_CLASS(cls, cls##_PTemplate) \
00687 cls(PINDEX initialSize = 0) \
00688 : cls##_PTemplate(initialSize) { } \
00689 cls(T const * buffer, PINDEX length, BOOL dynamic = TRUE) \
00690 : cls##_PTemplate(buffer, length, dynamic) { } \
00691 virtual PObject * Clone() const \
00692 { return PNEW cls(*this, GetSize()); } \
00693
00694 #define PSCALAR_ARRAY(cls, T) PBASEARRAY(cls, T)
00695
00696 #endif // PHAS_TEMPLATES
00697
00698
00700 #ifdef DOC_PLUS_PLUS
00701 class PCharArray : public PBaseArray {
00702 public:
00708 PCharArray(
00709 PINDEX initialSize = 0
00710 );
00711
00714 PCharArray(
00715 char const * buffer,
00716 PINDEX length,
00717 BOOL dynamic = TRUE
00718 );
00720 #endif
00721 PDECLARE_BASEARRAY(PCharArray, char);
00722 public:
00725
00726 virtual void PrintOn(
00727 ostream & strm
00728 ) const;
00730 virtual void ReadFrom(
00731 istream &strm
00732 );
00734 };
00735
00737 #ifdef DOC_PLUS_PLUS
00738 class PShortArray : public PBaseArray {
00739 public:
00745 PShortArray(
00746 PINDEX initialSize = 0
00747 );
00748
00751 PShortArray(
00752 short const * buffer,
00753 PINDEX length,
00754 BOOL dynamic = TRUE
00755 );
00757 };
00758 #endif
00759 PSCALAR_ARRAY(PShortArray, short);
00760
00761
00763 #ifdef DOC_PLUS_PLUS
00764 class PIntArray : public PBaseArray {
00765 public:
00771 PIntArray(
00772 PINDEX initialSize = 0
00773 );
00774
00777 PIntArray(
00778 int const * buffer,
00779 PINDEX length,
00780 BOOL dynamic = TRUE
00781 );
00783 };
00784 #endif
00785 PSCALAR_ARRAY(PIntArray, int);
00786
00787
00789 #ifdef DOC_PLUS_PLUS
00790 class PLongArray : public PBaseArray {
00791 public:
00797 PLongArray(
00798 PINDEX initialSize = 0
00799 );
00800
00803 PLongArray(
00804 long const * buffer,
00805 PINDEX length,
00806 BOOL dynamic = TRUE
00807 );
00809 };
00810 #endif
00811 PSCALAR_ARRAY(PLongArray, long);
00812
00813
00815 #ifdef DOC_PLUS_PLUS
00816 class PBYTEArray : public PBaseArray {
00817 public:
00823 PBYTEArray(
00824 PINDEX initialSize = 0
00825 );
00826
00829 PBYTEArray(
00830 BYTE const * buffer,
00831 PINDEX length,
00832 BOOL dynamic = TRUE
00833 );
00835 };
00836 #endif
00837 PDECLARE_BASEARRAY(PBYTEArray, BYTE);
00838 public:
00841
00842 virtual void PrintOn(
00843 ostream & strm
00844 ) const;
00846 virtual void ReadFrom(
00847 istream &strm
00848 );
00850 };
00851
00852
00854 #ifdef DOC_PLUS_PLUS
00855 class PWORDArray : public PBaseArray {
00856 public:
00862 PWORDArray(
00863 PINDEX initialSize = 0
00864 );
00865
00868 PWORDArray(
00869 WORD const * buffer,
00870 PINDEX length,
00871 BOOL dynamic = TRUE
00872 );
00874 };
00875 #endif
00876 PSCALAR_ARRAY(PWORDArray, WORD);
00877
00878
00880 #ifdef DOC_PLUS_PLUS
00881 class PUnsignedArray : public PBaseArray {
00882 public:
00888 PUnsignedArray(
00889 PINDEX initialSize = 0
00890 );
00891
00894 PUnsignedArray(
00895 unsigned const * buffer,
00896 PINDEX length,
00897 BOOL dynamic = TRUE
00898 );
00900 };
00901 #endif
00902 PSCALAR_ARRAY(PUnsignedArray, unsigned);
00903
00904
00906 #ifdef DOC_PLUS_PLUS
00907 class PDWORDArray : public PBaseArray {
00908 public:
00914 PDWORDArray(
00915 PINDEX initialSize = 0
00916 );
00917
00920 PDWORDArray(
00921 DWORD const * buffer,
00922 PINDEX length,
00923 BOOL dynamic = TRUE
00924 );
00926 #endif
00927 PSCALAR_ARRAY(PDWORDArray, DWORD);
00928
00929
00931
00932
00954 class PArrayObjects : public PCollection
00955 {
00956 PCONTAINERINFO(PArrayObjects, PCollection);
00957
00958 public:
00967 PINLINE PArrayObjects(
00968 PINDEX initialSize = 0
00969 );
00971
01004 virtual Comparison Compare(
01005 const PObject & obj
01006 ) const;
01008
01011
01012 virtual PINDEX GetSize() const;
01013
01022 virtual BOOL SetSize(
01023 PINDEX newSize
01024 );
01026
01035 virtual PINDEX Append(
01036 PObject * obj
01037 );
01038
01054 virtual PINDEX Insert(
01055 const PObject & before,
01056 PObject * obj
01057 );
01058
01069 virtual PINDEX InsertAt(
01070 PINDEX index,
01071 PObject * obj
01072 );
01073
01082 virtual BOOL Remove(
01083 const PObject * obj
01084 );
01085
01097 virtual PObject * RemoveAt(
01098 PINDEX index
01099 );
01100
01108 virtual BOOL SetAt(
01109 PINDEX index,
01110 PObject * val
01111 );
01112
01119 virtual PObject * GetAt(
01120 PINDEX index
01121 ) const;
01122
01130 virtual PINDEX GetObjectsIndex(
01131 const PObject * obj
01132 ) const;
01133
01143 virtual PINDEX GetValuesIndex(
01144 const PObject & obj
01145 ) const;
01146
01153 virtual void RemoveAll();
01155
01156 protected:
01157 PBASEARRAY(ObjPtrArray, PObject *);
01158 ObjPtrArray * theArray;
01159 };
01160
01161
01162 #ifdef PHAS_TEMPLATES
01163
01170 template <class T> class PArray : public PArrayObjects
01171 {
01172 PCLASSINFO(PArray, PArrayObjects);
01173
01174 public:
01183 PArray(
01184 PINDEX initialSize = 0
01185 ) : PArrayObjects(initialSize) { }
01187
01193 virtual PObject * Clone() const
01194 { return PNEW PArray(0, this); }
01196
01206 T & operator[](
01207 PINDEX index
01208 ) const {
01209 PObject * obj = GetAt(index);
01210 PAssert(obj != NULL, PInvalidArrayElement);
01211 return (T &)*obj;
01212 }
01214
01215 protected:
01216 PArray(int dummy, const PArray * c) : PArrayObjects(dummy, c) { }
01217 };
01218
01219
01231 #define PARRAY(cls, T) typedef PArray<T> cls
01232
01233
01246 #define PDECLARE_ARRAY(cls, T) \
01247 PARRAY(cls##_PTemplate, T); \
01248 PDECLARE_CLASS(cls, cls##_PTemplate) \
01249 protected: \
01250 inline cls(int dummy, const cls * c) \
01251 : cls##_PTemplate(dummy, c) { } \
01252 public: \
01253 inline cls(PINDEX initialSize = 0) \
01254 : cls##_PTemplate(initialSize) { } \
01255 virtual PObject * Clone() const \
01256 { return PNEW cls(0, this); } \
01257
01258 #else // PHAS_TEMPLATES
01259
01260
01261 #define PARRAY(cls, T) \
01262 class cls : public PArrayObjects { \
01263 PCLASSINFO(cls, PArrayObjects); \
01264 protected: \
01265 inline cls(int dummy, const cls * c) \
01266 : PArrayObjects(dummy, c) { } \
01267 public: \
01268 inline cls(PINDEX initialSize = 0) \
01269 : PArrayObjects(initialSize) { } \
01270 virtual PObject * Clone() const \
01271 { return PNEW cls(0, this); } \
01272 inline T & operator[](PINDEX index) const\
01273 { PObject * obj = GetAt(index); \
01274 PAssert(obj != NULL, PInvalidArrayElement); \
01275 \
01276 return (T &)*obj; } \
01277 }
01278
01279 #define PDECLARE_ARRAY(cls, T) \
01280 PARRAY(cls##_PTemplate, T); \
01281 PDECLARE_CLASS(cls, cls##_PTemplate) \
01282 protected: \
01283 inline cls(int dummy, const cls * c) \
01284 : cls##_PTemplate(dummy, c) { } \
01285 public: \
01286 inline cls(PINDEX initialSize = 0) \
01287 : cls##_PTemplate(initialSize) { } \
01288 virtual PObject * Clone() const \
01289 { return PNEW cls(0, this); } \
01290
01291 #endif // PHAS_TEMPLATES
01292
01293
01296 class PBitArray : public PBYTEArray
01297 {
01298 PCLASSINFO(PBitArray, PBYTEArray);
01299
01300 public:
01305 PBitArray(
01306 PINDEX initialSize = 0
01307 );
01308
01311 PBitArray(
01312 const void * buffer,
01313 PINDEX length,
01314 BOOL dynamic = TRUE
01315 );
01317
01322 virtual PObject * Clone() const;
01324
01333 virtual PINDEX GetSize() const;
01334
01343 virtual BOOL SetSize(
01344 PINDEX newSize
01345 );
01346
01353 BOOL SetAt(
01354 PINDEX index,
01355 BOOL val
01356 );
01357
01364 BOOL GetAt(
01365 PINDEX index
01366 ) const;
01367
01376 void Attach(
01377 const void * buffer,
01378 PINDEX bufferSize
01379 );
01380
01394 BYTE * GetPointer(
01395 PINDEX minSize = 0
01396 );
01398
01410 BOOL operator[](
01411 PINDEX index
01412 ) const { return GetAt(index); }
01413
01419 PBitArray & operator+=(
01420 PINDEX index
01421 ) { SetAt(index, TRUE); return *this; }
01422
01428 PBitArray & operator-=(
01429 PINDEX index
01430 ) { SetAt(index, FALSE); return *this; }
01431
01443 BOOL Concatenate(
01444 const PBitArray & array
01445 );
01447 };
01448
01449