📄 array.h
字号:
The following classes are instantiated automatically for the basic scalar types:\begin{itemize} \item #PCharArray# \item #PBYTEArray# \item #PShortArray# \item #PWORDArray# \item #PIntArray# \item #PUnsignedArray# \item #PLongArray# \item #PDWORDArray#\end{itemize} */template <class T> class PBaseArray : public PAbstractArray{ PCLASSINFO(PBaseArray, PAbstractArray); public: /**@name Construction */ //@{ /**Construct a new dynamic array of elements of the specified type. The array is initialised to all zero bytes. Note that this may not be logically equivalent to the zero value for the type, though this would be very rare. */ PBaseArray( PINDEX initialSize = 0 /// Initial number of elements in the array. ) : PAbstractArray(sizeof(T), initialSize) { } /**Construct a new dynamic array of elements of the specified type. */ PBaseArray( T const * buffer, /// Pointer to an array of the elements of type {\bf T}. PINDEX length, /// Number of elements pointed to by #buffer#. BOOL dynamic = TRUE /// Buffer is copied and dynamically allocated. ) : PAbstractArray(sizeof(T), buffer, length, dynamic) { } //@} /**@name Overrides from class PObject */ //@{ /** Clone the object. */ virtual PObject * Clone() const { return PNEW PBaseArray<T>(*this, GetSize()); } //@} /**@name Overrides from class PContainer */ //@{ /**Set the specific element in the array. The array will automatically expand, if necessary, to fit the new element in. @return TRUE if new memory for the array was successfully allocated. */ BOOL SetAt( PINDEX index, /// Position in the array to set the new value. T val /// Value to set in the array. ) { return SetMinSize(index+1) && val==(((T *)theArray)[index] = val); } /**Get a value from the array. If the #index# is beyond the end of the allocated array then a zero value is returned. @return value at the array position. */ T GetAt( PINDEX index /// Position on the array to get value from. ) const { PASSERTINDEX(index); return index < GetSize() ? ((T *)theArray)[index] : (T)0; } /**Attach a pointer to a static block to the base array type. The pointer is used directly and will not be copied to a dynamically allocated buffer. If the SetSize() function is used to change the size of the buffer, the object will be converted to a dynamic form with the contents of the static buffer copied to the allocated buffer. Any dynamically allocated buffer will be freed. */ void Attach( const T * buffer, /// Pointer to an array of elements. PINDEX bufferSize /// Number of elements pointed to by buffer. ) { PAbstractArray::Attach(buffer, bufferSize); } /**Get a pointer to the internal array and assure that it is of at least the specified size. This is useful when the array contents are being set by some external or system function eg file read. 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 pointer to the array memory. */ T * GetPointer( PINDEX minSize = 0 /// Minimum size for returned buffer pointer. ) { return (T *)PAbstractArray::GetPointer(minSize); } //@} /**@name New functions for class */ //@{ /**Get a value from the array. If the #index# is beyond the end of the allocated array then a zero value is returned. 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); } //@} protected: virtual void PrintElementOn( ostream & stream, PINDEX index ) const { stream << GetAt(index); }};/*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) \ PDECLARE_CLASS(cls, PBaseArray<T>) \ cls(PINDEX initialSize = 0) \ : PBaseArray<T>(initialSize) { } \ cls(T const * buffer, PINDEX length, BOOL dynamic = TRUE) \ : PBaseArray<T>(buffer, length, dynamic) { } \ virtual PObject * Clone() const \ { return PNEW cls(*this, GetSize()); } \/**This template class maps the #PAbstractArray# to a specific element type. The functions in this class primarily do all the appropriate casting of types. Note that if templates are not used the #PSCALAR_ARRAY# macro will simulate the template instantiation. The following classes are instantiated automatically for the basic scalar types:\begin{itemize} \item #PBYTEArray# \item #PShortArray# \item #PWORDArray# \item #PIntArray# \item #PUnsignedArray# \item #PLongArray# \item #PDWORDArray#\end{itemize} */template <class T> class PScalarArray : public PBaseArray<T>{ public: /**@name Construction */ //@{ /**Construct a new dynamic array of elements of the specified type. The array is initialised to all zero bytes. Note that this may not be logically equivalent to the zero value for the type, though this would be very rare. */ PScalarArray( PINDEX initialSize = 0 /// Initial number of elements in the array. ) : PBaseArray<T>(initialSize) { } /**Construct a new dynamic array of elements of the specified type. */ PScalarArray( T const * buffer, /// Pointer to an array of the elements of type {\bf T}. PINDEX length, /// Number of elements pointed to by #buffer#. BOOL dynamic = TRUE /// Buffer is copied and dynamically allocated. ) : PBaseArray<T>(buffer, length, dynamic) { } //@} protected: virtual void ReadElementFrom( istream & stream, PINDEX index ) { T t; stream >> t; if (!stream.fail()) SetAt(index, t); }};/*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 PSCALAR_ARRAY(cls, T) typedef PScalarArray<T> cls#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()); } \#define PSCALAR_ARRAY(cls, T) PBASEARRAY(cls, T)#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. */ PCharArray( PINDEX initialSize = 0 /// Initial number of elements in the array. ); /**Construct a new dynamic array of char. */ PCharArray( 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -