📄 array.h
字号:
This is functionally identical to the #PContainer::GetAt()# function. @return value at the array position. */ T operator[]( PINDEX index /// Position on the array to get value from. ) const { return GetAt(index); } /**Get a reference to value from the array. If the #index# is beyond the end of the allocated array then the array is expanded. If a memory allocation failure occurs the function asserts. This is functionally similar to the #SetAt()# function and allows the array subscript to be an lvalue. @return reference to value at the array position. */ T & operator[]( PINDEX index /// Position on the array to get value from. ) { PASSERTINDEX(index); PAssert(SetMinSize(index+1), POutOfMemory); return ((T *)theArray)[index]; } /**Get a pointer to the internal array. The user may not modify the contents of this pointer/ This is useful when the array contents are required by some external or system function eg file write. It is unsafe to assume that the pointer is valid for very long after return from this function. The array may be resized or otherwise changed and the pointer returned invalidated. It should be used for simple calls to atomic functions, or very careful examination of the program logic must be performed. @return constant pointer to the array memory. */ operator T const *() const { return (T const *)theArray; } /**Concatenate one array to the end of this array. This function will allocate a new array large enough for the existing contents and the contents of the parameter. The paramters contents is then copied to the end of the existing array. Note this does nothing and returns FALSE if the target array is not dynamically allocated. @return TRUE if the memory allocation succeeded. */ BOOL Concatenate( const PBaseArray & array /// Other array to concatenate ) { return PAbstractArray::Concatenate(array); } //@}};/*Declare a dynamic array base type. This macro is used to declare a descendent of PAbstractArray class, customised for a particular element type {\bf T}. This macro closes the class declaration off so no additional members can be added. If the compilation is using templates then this macro produces a typedef of the #PBaseArray# template class. */#define PBASEARRAY(cls, T) typedef PBaseArray<T> cls/**Begin a declaration of an array of base types. This macro is used to declare a descendent of PAbstractArray class, customised for a particular element type {\bf T}. If the compilation is using templates then this macro produces a descendent of the #PBaseArray# template class. If templates are not being used then the macro defines a set of inline functions to do all casting of types. The resultant classes have an identical set of functions in either case. See the #PBaseArray# and #PAbstractArray# classes for more information. */#define PDECLARE_BASEARRAY(cls, T) \ PBASEARRAY(PBaseArray_##cls, T); \ PDECLARE_CLASS(cls, PBaseArray_##cls) \ cls(PINDEX initialSize = 0) \ : PBaseArray_##cls(initialSize) { } \ cls(T const * buffer, PINDEX length, BOOL dynamic = TRUE) \ : PBaseArray_##cls(buffer, length, dynamic) { } \ virtual PObject * Clone() const \ { return PNEW cls(*this, GetSize()); } \#else // PHAS_TEMPLATES#define PBASEARRAY(cls, T) \ typedef T P_##cls##_Base_Type; \ class cls : public PAbstractArray { \ PCLASSINFO(cls, PAbstractArray) \ public: \ inline cls(PINDEX initialSize = 0) \ : PAbstractArray(sizeof(P_##cls##_Base_Type), initialSize) { } \ inline cls(P_##cls##_Base_Type const * buffer, PINDEX length, BOOL dynamic = TRUE) \ : PAbstractArray(sizeof(P_##cls##_Base_Type), buffer, length, dynamic) { } \ virtual PObject * Clone() const \ { return PNEW cls(*this, GetSize()); } \ inline BOOL SetAt(PINDEX index, P_##cls##_Base_Type val) \ { return SetMinSize(index+1) && \ val==(((P_##cls##_Base_Type *)theArray)[index] = val); } \ inline P_##cls##_Base_Type GetAt(PINDEX index) const \ { PASSERTINDEX(index); return index < GetSize() ? \ ((P_##cls##_Base_Type*)theArray)[index] : (P_##cls##_Base_Type)0; } \ inline P_##cls##_Base_Type operator[](PINDEX index) const \ { PASSERTINDEX(index); return GetAt(index); } \ inline P_##cls##_Base_Type & operator[](PINDEX index) \ { PASSERTINDEX(index); PAssert(SetMinSize(index+1), POutOfMemory); \ return ((P_##cls##_Base_Type *)theArray)[index]; } \ inline void Attach(const P_##cls##_Base_Type * buffer, PINDEX bufferSize) \ { PAbstractArray::Attach(buffer, bufferSize); } \ inline P_##cls##_Base_Type * GetPointer(PINDEX minSize = 0) \ { return (P_##cls##_Base_Type *)PAbstractArray::GetPointer(minSize); } \ inline operator P_##cls##_Base_Type const *() const \ { return (P_##cls##_Base_Type const *)theArray; } \ inline BOOL Concatenate(cls const & array) \ { return PAbstractArray::Concatenate(array); } \ }#define PDECLARE_BASEARRAY(cls, T) \ PBASEARRAY(cls##_PTemplate, T); \ PDECLARE_CLASS(cls, cls##_PTemplate) \ cls(PINDEX initialSize = 0) \ : cls##_PTemplate(initialSize) { } \ cls(T const * buffer, PINDEX length, BOOL dynamic = TRUE) \ : cls##_PTemplate(buffer, length, dynamic) { } \ virtual PObject * Clone() const \ { return PNEW cls(*this, GetSize()); } \#endif // PHAS_TEMPLATES/// Array of characters.#ifdef DOC_PLUS_PLUSclass PCharArray : public PBaseArray { public: /**@name Construction */ //@{ /**Construct a new dynamic array of char. The array is initialised to all zero bytes. */ PBaseArray( PINDEX initialSize = 0 /// Initial number of elements in the array. ); /**Construct a new dynamic array of char. */ PBaseArray( char const * buffer, /// Pointer to an array of chars. PINDEX length, /// Number of elements pointed to by #buffer#. BOOL dynamic = TRUE /// Buffer is copied and dynamically allocated. ); //@}#endifPDECLARE_BASEARRAY(PCharArray, char); public: /**@name Overrides from class PObject */ //@{ /// Print the array virtual void PrintOn( ostream & strm /// Stream to output to. ) const; //@}};/// Array of short integers.#ifdef DOC_PLUS_PLUSclass PShortArray : public PBaseArray { public: /**@name Construction */ //@{ /**Construct a new dynamic array of shorts. The array is initialised to all zeros. */ PBaseArray( PINDEX initialSize = 0 /// Initial number of elements in the array. ); /**Construct a new dynamic array of shorts. */ PBaseArray( short const * buffer, /// Pointer to an array of shorts. PINDEX length, /// Number of elements pointed to by #buffer#. BOOL dynamic = TRUE /// Buffer is copied and dynamically allocated. ); //@}#endifPDECLARE_BASEARRAY(PShortArray, short); public: /**@name Overrides from class PObject */ //@{ /// Print the array virtual void PrintOn( ostream & strm /// Stream to output to. ) const; //@} virtual long GetNumberValueAt(PINDEX idx) const;};/// Array of integers.#ifdef DOC_PLUS_PLUSclass PIntArray : public PBaseArray { public: /**@name Construction */ //@{ /**Construct a new dynamic array of ints. The array is initialised to all zeros. */ PBaseArray( PINDEX initialSize = 0 /// Initial number of elements in the array. ); /**Construct a new dynamic array of ints. */ PBaseArray( int const * buffer, /// Pointer to an array of ints. PINDEX length, /// Number of elements pointed to by #buffer#. BOOL dynamic = TRUE /// Buffer is copied and dynamically allocated. ); //@}#endifPDECLARE_BASEARRAY(PIntArray, int); public: /**@name Overrides from class PObject */ //@{ /// Print the array virtual void PrintOn( ostream & strm /// Stream to output to. ) const; //@} virtual long GetNumberValueAt(PINDEX idx) const;};/// Array of long integers.#ifdef DOC_PLUS_PLUSclass PLongArray : public PBaseArray { public: /**@name Construction */ //@{ /**Construct a new dynamic array of longs. The array is initialised to all zeros. */ PBaseArray( PINDEX initialSize = 0 /// Initial number of elements in the array. ); /**Construct a new dynamic array of longs. */ PBaseArray( long const * buffer, /// Pointer to an array of longs. PINDEX length, /// Number of elements pointed to by #buffer#. BOOL dynamic = TRUE /// Buffer is copied and dynamically allocated. ); //@}#endifPDECLARE_BASEARRAY(PLongArray, long); public: /**@name Overrides from class PObject */ //@{ /// Print the array virtual void PrintOn( ostream & strm /// Stream to output to. ) const; //@} virtual long GetNumberValueAt(PINDEX idx) const;};/// Array of unsigned characters.#ifdef DOC_PLUS_PLUSclass PBYTEArray : public PBaseArray { public: /**@name Construction */ //@{ /**Construct a new dynamic array of unsigned chars. The array is initialised to all zeros. */ PBaseArray( PINDEX initialSize = 0 /// Initial number of elements in the array. ); /**Construct a new dynamic array of unsigned chars. */ PBaseArray( BYTE const * buffer, /// Pointer to an array of BYTEs. PINDEX length, /// Number of elements pointed to by #buffer#. BOOL dynamic = TRUE /// Buffer is copied and dynamically allocated. ); //@}#endifPDECLARE_BASEARRAY(PBYTEArray, BYTE); public: /**@name Overrides from class PObject */ //@{ /// Print the array virtual void PrintOn( ostream & strm /// Stream to output to. ) const; //@} virtual long GetNumberValueAt(PINDEX idx) const;};/// Array of unsigned short integers.#ifdef DOC_PLUS_PLUSclass PWORDArray : public PBaseArray { public: /**@name Construction */ //@{ /**Construct a new dynamic array of unsigned shorts. The array is initialised to all zeros. */ PBaseArray( PINDEX initialSize = 0 /// Initial number of elements in the array. ); /**Construct a new dynamic array of unsigned shorts. */ PBaseArray( WORD const * buffer, /// Pointer to an array of WORDs. PINDEX length, /// Number of elements pointed to by #buffer#. BOOL dynamic = TRUE /// Buffer is copied and dynamically allocated. ); //@}#endifPDECLARE_BASEARRAY(PWORDArray, WORD); public: /**@name Overrides from class PObject */ //@{ /// Print the array virtual void PrintOn( ostream & strm /// Stream to output to. ) const; //@} virtual long GetNumberValueAt(PINDEX idx) const;};/// Array of unsigned integers.#ifdef DOC_PLUS_PLUSclass PUnsignedArray : public PBaseArray { public: /**@name Construction */ //@{ /**Construct a new dynamic array of unsigned ints. The array is initialised to all zeros. */ PBaseArray( PINDEX initialSize = 0 /// Initial number of elements in the array. ); /**Construct a new dynamic array of unsigned ints. */ PBaseArray( unsigned const * buffer, /// Pointer to an array of unsigned ints. PINDEX length, /// Number of elements pointed to by #buffer#. BOOL dynamic = TRUE /// Buffer is copied and dynamically allocated. ); //@}#endifPDECLARE_BASEARRAY(PUnsignedArray, unsigned); public: /**@name Overrides from class PObject */ //@{ /// Print the array virtual void PrintOn( ostream & strm /// Stream to output to. ) const; //@} virtual long GetNumberValueAt(PINDEX idx) const;};/// Array of unsigned long integers.#ifdef DOC_PLUS_PLUSclass PDWORDArray : public PBaseArray { public: /**@name Construction */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -