PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
array.h
Go to the documentation of this file.
1 /*
2  * array.h
3  *
4  * Linear Array Container classes.
5  *
6  * Portable Tools Library
7  *
8  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25  * All Rights Reserved.
26  *
27  * Contributor(s): ______________________________________.
28  */
29 
30 #ifndef PTLIB_ARRAY_H
31 #define PTLIB_ARRAY_H
32 
33 #ifdef P_USE_PRAGMA
34 #pragma interface
35 #endif
36 
37 #include <vector>
38 
39 #include <ptlib/contain.h>
40 
42 // The abstract array class
43 
65 class PAbstractArray : public PContainer
66 {
67  PCONTAINERINFO(PAbstractArray, PContainer);
68  public:
81  PINDEX elementSizeInBytes,
82  PINDEX initialSize = 0
84  );
85 
104  PINDEX elementSizeInBytes,
105  const void *buffer,
107  PINDEX bufferSizeInElements,
108  PBoolean dynamicAllocation
109  );
111 
120  virtual void PrintOn(
121  ostream &strm // Stream to print the object into.
122  ) const;
123 
130  virtual void ReadFrom(
131  istream &strm // Stream to read the objects contents from.
132  );
133 
153  virtual Comparison Compare(
154  const PObject & obj
155  ) const;
157 
168  virtual PBoolean SetSize(
169  PINDEX newSize
170  );
172 
183  void Attach(
184  const void *buffer,
185  PINDEX bufferSize
186  );
187 
201  void * GetPointer(
202  PINDEX minSize = 1
203  );
204  const void * GetPointer() const { return theArray; }
205 
207  virtual PINDEX GetLength() const;
208 
222  const PAbstractArray & array
223  );
225 
226  protected:
227  PBoolean InternalSetSize(PINDEX newSize, PBoolean force);
228 
229  virtual void PrintElementOn(
230  ostream & stream,
231  PINDEX index
232  ) const;
233  virtual void ReadElementFrom(
234  istream & stream,
235  PINDEX index
236  );
237 
240  PINDEX elementSizeInBytes
241  );
242 
244  PINDEX elementSize;
245 
247  char * theArray;
248 
251 
252  friend class PArrayObjects;
253 };
254 
255 
257 // An array of some base type
258 
276 template <class T> class PBaseArray : public PAbstractArray
277 {
278  PCLASSINFO(PBaseArray, PAbstractArray);
279  public:
288  PINDEX initialSize = 0
289  ) : PAbstractArray(sizeof(T), initialSize) { }
290 
294  T const * buffer,
295  PINDEX length,
296  PBoolean dynamic = true
297  ) : PAbstractArray(sizeof(T), buffer, length, dynamic) { }
299 
304  virtual PObject * Clone() const
305  {
306  return PNEW PBaseArray<T>(*this, GetSize());
307  }
309 
319  PINDEX index,
320  T val
321  ) {
322  return SetMinSize(index+1) && val==(((T *)theArray)[index] = val);
323  }
324 
331  T GetAt(
332  PINDEX index
333  ) const {
334  PASSERTINDEX(index);
335  return index < GetSize() ? (reinterpret_cast<T *>(theArray))[index] : T();
336  }
337 
346  void Attach(
347  const T * buffer,
348  PINDEX bufferSize
349  ) {
350  PAbstractArray::Attach(buffer, bufferSize);
351  }
352 
367  PINDEX minSize = 0
368  ) {
369  return (T *)PAbstractArray::GetPointer(minSize);
370  }
372 
385  PINDEX index
386  ) const {
387  return GetAt(index);
388  }
389 
401  PINDEX index
402  ) {
403  PASSERTINDEX(index);
404  PAssert(SetMinSize(index+1), POutOfMemory);
405  return ((T *)theArray)[index];
406  }
407 
421  operator T const *() const {
422  return (T const *)theArray;
423  }
424 
437  const PBaseArray & array
438  ) {
439  return PAbstractArray::Concatenate(array);
440  }
442 
443  protected:
444  virtual void PrintElementOn(
445  ostream & stream,
446  PINDEX index
447  ) const {
448  stream << GetAt(index);
449  }
450 
451  PBaseArray(PContainerReference & reference_) : PAbstractArray(reference_, sizeof(T)) { }
452 };
453 
462 #define PBASEARRAY(cls, T) typedef PBaseArray<T> cls
463 
476 #define PDECLARE_BASEARRAY(cls, T) \
477  PDECLARE_CLASS(cls, PBaseArray<T>) \
478  cls(PINDEX initialSize = 0) \
479  : PBaseArray<T>(initialSize) { } \
480  cls(PContainerReference & reference_) \
481  : PBaseArray<T>(reference_) { } \
482  cls(T const * buffer, PINDEX length, PBoolean dynamic = true) \
483  : PBaseArray<T>(buffer, length, dynamic) { } \
484  virtual PObject * Clone() const \
485  { return PNEW cls(*this, GetSize()); } \
486 
487 
504 template <class T> class PScalarArray : public PBaseArray<T>
505 {
506  public:
515  PINDEX initialSize = 0
516  ) : PBaseArray<T>(initialSize) { }
517 
521  T const * buffer,
522  PINDEX length,
523  PBoolean dynamic = true
524  ) : PBaseArray<T>(buffer, length, dynamic) { }
526 
527  protected:
528  virtual void ReadElementFrom(
529  istream & stream,
530  PINDEX index
531  ) {
532  T t;
533  stream >> t;
534  if (!stream.fail())
535  this->SetAt(index, t);
536  }
537 };
538 
539 
548 #define PSCALAR_ARRAY(cls, T) typedef PScalarArray<T> cls
549 
550 
552 class PCharArray : public PBaseArray<char>
553 {
555  PCLASSINFO(PCharArray, ParentClass);
556  public:
563  PINDEX initialSize = 0
564  ) : ParentClass(initialSize) { }
565 
569  char const * buffer,
570  PINDEX length,
571  PBoolean dynamic = true
572  ) : ParentClass(buffer, length, dynamic) { }
573 
575  : ParentClass(reference_) { }
577 
580  virtual void PrintOn(
582  ostream & strm
583  ) const;
585  virtual void ReadFrom(
586  istream &strm // Stream to read the objects contents from.
587  );
588  virtual PObject * Clone() const
589  {
590  return PNEW PCharArray(*this, GetSize());
591  }
593 };
594 
597 
600 
603 
605 class PBYTEArray : public PBaseArray<BYTE>
606 {
608  PCLASSINFO(PCharArray, ParentClass);
609  public:
616  PINDEX initialSize = 0
617  ) : ParentClass(initialSize) { }
618 
622  BYTE const * buffer,
623  PINDEX length,
624  PBoolean dynamic = true
625  ) : ParentClass(buffer, length, dynamic) { }
626 
628  : ParentClass(reference_) { }
630 
633  virtual void PrintOn(
635  ostream & strm
636  ) const;
638  virtual void ReadFrom(
639  istream &strm
640  );
641  virtual PObject * Clone() const
642  {
643  return PNEW PBYTEArray(*this, GetSize());
644  }
646 
649  template <typename T> const T & GetAs(PINDEX offset = 0)
650  {
651  PAssert(offset+(PINDEX)sizeof(T) <= GetSize(), PInvalidParameter);
652  return *(const T *)(theArray+offset);
653  }
654 };
655 
656 
659 class PHexDump : public PBYTEArray
660 {
662 public:
663  PHexDump(const void * data, PINDEX length, bool compact = true)
664  : PBYTEArray(static_cast<const BYTE *>(data), length, false)
665  , m_compact(compact)
666  { }
667 
668  explicit PHexDump(const PBYTEArray & data, bool compact = true)
669  : PBYTEArray(data, data.GetSize(), false)
670  , m_compact(compact)
671  { }
672 
673  virtual void PrintOn(ostream & strm) const;
674 
675 protected:
676  bool m_compact;
677 };
678 
679 
682 
685 
688 
689 
691 // Linear array of objects
692 
715 {
716  PCONTAINERINFO(PArrayObjects, PCollection);
717  public:
727  PINDEX initialSize = 0
728  );
730 
760  virtual Comparison Compare(
761  const PObject & obj
762  ) const;
764 
767  virtual PINDEX GetSize() const;
769 
778  virtual PBoolean SetSize(
779  PINDEX newSize
780  );
782 
791  virtual PINDEX Append(
792  PObject * obj
793  );
794 
810  virtual PINDEX Insert(
811  const PObject & before,
812  PObject * obj
813  );
814 
825  virtual PINDEX InsertAt(
826  PINDEX index,
827  PObject * obj
828  );
829 
838  virtual PBoolean Remove(
839  const PObject * obj
840  );
841 
853  virtual PObject * RemoveAt(
854  PINDEX index
855  );
856 
864  virtual PBoolean SetAt(
865  PINDEX index,
866  PObject * val
867  );
868 
875  virtual PObject * GetAt(
876  PINDEX index
877  ) const;
878 
886  virtual PINDEX GetObjectsIndex(
887  const PObject * obj
888  ) const;
889 
899  virtual PINDEX GetValuesIndex(
900  const PObject & obj // Object to find equal of.
901  ) const;
902 
909  virtual void RemoveAll();
911 
912  protected:
913  // The type below cannot be nested as DevStudio 2005 AUTOEXP.DAT doesn't like it
915 };
916 
917 
925 template <class T> class PArray : public PArrayObjects
926 {
927  PCLASSINFO(PArray, PArrayObjects);
928  public:
938  PINDEX initialSize = 0
939  ) : PArrayObjects(initialSize) { }
941 
947  virtual PObject * Clone() const
948  { return PNEW PArray(0, this); }
950 
961  PINDEX index
962  ) const {
963  PObject * obj = this->GetAt(index);
964  PAssert(obj != NULL, PInvalidArrayElement);
965  return dynamic_cast<T &>(*obj);
966  }
968 
969  protected:
970  PArray(int dummy, const PArray * c) : PArrayObjects(dummy, c) { }
971 };
972 
973 
985 #define PARRAY(cls, T) typedef PArray<T> cls
986 
987 
1000 #define PDECLARE_ARRAY(cls, T) \
1001  PARRAY(cls##_PTemplate, T); \
1002  PDECLARE_CLASS(cls, cls##_PTemplate) \
1003  protected: \
1004  inline cls(int dummy, const cls * c) \
1005  : cls##_PTemplate(dummy, c) { } \
1006  public: \
1007  inline cls(PINDEX initialSize = 0) \
1008  : cls##_PTemplate(initialSize) { } \
1009  virtual PObject * Clone() const \
1010  { return PNEW cls(0, this); } \
1011 
1012 
1015 class PBitArray : public PBYTEArray
1016 {
1017  PCLASSINFO(PBitArray, PBYTEArray);
1018 
1019  public:
1024  PBitArray(
1025  PINDEX initialSize = 0
1026  );
1027 
1030  PBitArray(
1031  const void * buffer,
1032  PINDEX length,
1033  PBoolean dynamic = true
1034  );
1036 
1041  virtual PObject * Clone() const;
1043 
1052  virtual PINDEX GetSize() const;
1053 
1062  virtual PBoolean SetSize(
1063  PINDEX newSize
1064  );
1065 
1072  PBoolean SetAt(
1073  PINDEX index,
1074  PBoolean val
1075  );
1076 
1083  PBoolean GetAt(
1084  PINDEX index
1085  ) const;
1086 
1095  void Attach(
1096  const void * buffer,
1097  PINDEX bufferSize
1098  );
1099 
1113  BYTE * GetPointer(
1114  PINDEX minSize = 0
1115  );
1117 
1130  PINDEX index
1131  ) const { return GetAt(index); }
1132 
1139  PINDEX index
1140  ) { SetAt(index, true); return *this; }
1141 
1148  PINDEX index
1149  ) { SetAt(index, false); return *this; }
1150 
1163  const PBitArray & array
1164  );
1166 };
1167 
1168 
1169 #endif // PTLIB_ARRAY_H
1170 
1171 
1172 // End Of File ///////////////////////////////////////////////////////////////
virtual PObject * RemoveAt(PINDEX index)
Remove the object at the specified ordinal index from the collection.
PBaseArray(PContainerReference &reference_)
Definition: array.h:451
PBYTEArray(PINDEX initialSize=0)
Construct a new dynamic array of unsigned chars.
Definition: array.h:615
PBYTEArray(PContainerReference &reference_)
Definition: array.h:627
An array of objects.
Definition: array.h:714
virtual PBoolean SetAt(PINDEX index, PObject *val)
Set the object at the specified ordinal position to the new value.
PHexDump(const void *data, PINDEX length, bool compact=true)
Definition: array.h:663
const T & GetAs(PINDEX offset=0)
Function to cast block of memory in PBYTEArray to another structure.
Definition: array.h:649
virtual PINDEX GetSize() const
Get size of array.
virtual void ReadElementFrom(istream &stream, PINDEX index)
Definition: array.h:528
Array of characters.
Definition: array.h:552
T & operator[](PINDEX index) const
Retrieve a reference to the object in the array.
Definition: array.h:960
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:2164
virtual PObject * GetAt(PINDEX index) const
Get the object at the specified ordinal position.
PBitArray & operator+=(PINDEX index)
Set a bit to the array.
Definition: array.h:1138
T & operator[](PINDEX index)
Get a reference to value from the array.
Definition: array.h:400
void Attach(const void *buffer, PINDEX bufferSize)
Attach a pointer to a static block to the bit array type.
This template class maps the PAbstractArray to a specific element type.
Definition: array.h:276
PBaseArray(PINDEX initialSize=0)
Construct a new dynamic array of elements of the specified type.
Definition: array.h:287
virtual void RemoveAll()
Remove all of the elements in the collection.
void Attach(const T *buffer, PINDEX bufferSize)
Attach a pointer to a static block to the base array type.
Definition: array.h:346
PHexDump(const PBYTEArray &data, bool compact=true)
Definition: array.h:668
PScalarArray< DWORD > PDWORDArray
Array of unsigned long integers.
Definition: array.h:687
PINLINE PArrayObjects(PINDEX initialSize=0)
Create a new array of objects.
#define PINLINE
Definition: object.h:194
PBoolean SetAt(PINDEX index, T val)
Set the specific element in the array.
Definition: array.h:318
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:2251
A new or malloc failed.
Definition: object.h:371
PBoolean Concatenate(const PBitArray &array)
Concatenate one array to the end of this array.
T operator[](PINDEX index) const
Get a value from the array.
Definition: array.h:384
PScalarArray< WORD > PWORDArray
Array of unsigned short integers.
Definition: array.h:681
virtual Comparison Compare(const PObject &obj) const
Get the relative rank of the two arrays.
virtual PBoolean SetSize(PINDEX newSize)
Set the size of the array in bits.
PBoolean operator[](PINDEX index) const
Get a value from the array.
Definition: array.h:1129
virtual void ReadElementFrom(istream &stream, PINDEX index)
PScalarArray(PINDEX initialSize=0)
Construct a new dynamic array of elements of the specified type.
Definition: array.h:514
virtual void ReadFrom(istream &strm)
Read the array.
PBaseArray(T const *buffer, PINDEX length, PBoolean dynamic=true)
Construct a new dynamic array of elements of the specified type.
Definition: array.h:293
virtual PObject * Clone() const
Make a complete duplicate of the array.
Definition: array.h:947
PCharArray(PINDEX initialSize=0)
Construct a new dynamic array of char.
Definition: array.h:562
virtual void PrintOn(ostream &strm) const
Print the array.
This class contains a variable length array of arbitrary memory blocks.
Definition: array.h:65
PScalarArray< short > PShortArray
Array of short integers.
Definition: array.h:596
PBoolean GetAt(PINDEX index) const
Get a bit from the array.
virtual PObject * Clone() const
Clone the object.
Definition: array.h:641
virtual PINDEX GetLength() const
Retrun the length in bytes for the array.
PScalarArray< long > PLongArray
Array of long integers.
Definition: array.h:602
PScalarArray< unsigned > PUnsignedArray
Array of unsigned integers.
Definition: array.h:684
PCharArray(PContainerReference &reference_)
Definition: array.h:574
PBaseArray< PObject * > * theArray
Definition: array.h:914
Array of unsigned characters.
Definition: array.h:605
void Attach(const void *buffer, PINDEX bufferSize)
Attach a pointer to a static block to the base array type.
char * theArray
Pointer to the allocated block of memory.
Definition: array.h:247
virtual void PrintOn(ostream &strm) const
Output the contents of the object to the stream.
PINDEX elementSize
Size of an element in bytes.
Definition: array.h:244
virtual PINDEX Append(PObject *obj)
Append a new object to the collection.
PBoolean SetAt(PINDEX index, PBoolean val)
Set the specific bit in the array.
virtual PINDEX GetSize() const
Get the current size of the container.
virtual PObject * Clone() const
Clone the object.
Definition: array.h:588
bool PBoolean
Definition: object.h:174
virtual PINDEX InsertAt(PINDEX index, PObject *obj)
Insert a new object at the specified ordinal index.
virtual void ReadFrom(istream &strm)
Input the contents of the object from the stream.
virtual PINDEX GetSize() const
Get the current size of the container.
Definition: contain.h:46
This class represents a dynamic bit array.
Definition: array.h:1015
virtual void PrintElementOn(ostream &stream, PINDEX index) const
virtual void PrintOn(ostream &strm) const
Print the array.
const void * GetPointer() const
Definition: array.h:204
T * GetPointer(PINDEX minSize=0)
Get a pointer to the internal array and assure that it is of at least the specified size...
Definition: array.h:366
PBYTEArray(BYTE const *buffer, PINDEX length, PBoolean dynamic=true)
Construct a new dynamic array of unsigned chars.
Definition: array.h:621
Abstract class to embody the base functionality of a container.
Definition: contain.h:99
#define PASSERTINDEX(idx)
Definition: object.h:88
virtual PBoolean Remove(const PObject *obj)
Remove the object from the collection.
virtual PObject * Clone() const
Clone the object.
PBoolean InternalSetSize(PINDEX newSize, PBoolean force)
PScalarArray< int > PIntArray
Array of integers.
Definition: array.h:599
Helper class for outputting a block of memory in hex.
Definition: array.h:659
virtual Comparison Compare(const PObject &obj) const
Get the relative rank of the two arrays.
PArray(int dummy, const PArray *c)
Definition: array.h:970
PBitArray & operator-=(PINDEX index)
Set a bit to the array.
Definition: array.h:1147
virtual PObject * Clone() const
Clone the object.
Definition: array.h:304
Invalid parameter was passed to a function.
Definition: object.h:378
T GetAt(PINDEX index) const
Get a value from the array.
Definition: array.h:331
PBoolean allocatedDynamically
Flag indicating the array was allocated on the heap.
Definition: array.h:250
bool m_compact
Definition: array.h:676
virtual PINDEX Insert(const PObject &before, PObject *obj)
Insert a new object immediately before the specified object.
PBoolean SetMinSize(PINDEX minSize)
Set the minimum size of container.
This template class maps the PArrayObjects to a specific object type.
Definition: array.h:925
virtual void PrintElementOn(ostream &stream, PINDEX index) const
Definition: array.h:444
#define PAssert(b, msg)
This macro is used to assert that a condition must be true.
Definition: object.h:400
This template class maps the PAbstractArray to a specific element type.
Definition: array.h:504
PArray(PINDEX initialSize=0)
Create a new array of objects.
Definition: array.h:937
A NULL array element object was accessed.
Definition: object.h:375
PCharArray(char const *buffer, PINDEX length, PBoolean dynamic=true)
Construct a new dynamic array of char.
Definition: array.h:568
virtual PBoolean SetSize(PINDEX newSize)
Set the size of the array in objects.
virtual PBoolean SetSize(PINDEX newSize)
Set the size of the array in elements.
PContainerReference * reference
Definition: contain.h:288
PBoolean Concatenate(const PBaseArray &array)
Concatenate one array to the end of this array.
Definition: array.h:436
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
A collection is a container that collects together descendents of the PObject class.
Definition: contain.h:392
virtual PINDEX GetValuesIndex(const PObject &obj) const
Search the collection for the specified value of the object.
virtual PINDEX GetObjectsIndex(const PObject *obj) const
Search the collection for the specific instance of the object.
PBitArray(PINDEX initialSize=0)
Construct a new dynamic array of bits.
PScalarArray(T const *buffer, PINDEX length, PBoolean dynamic=true)
Construct a new dynamic array of elements of the specified type.
Definition: array.h:520
PBoolean Concatenate(const PAbstractArray &array)
Concatenate one array to the end of this array.
#define PNEW
Macro for overriding system default new operator.
Definition: object.h:1896
virtual void ReadFrom(istream &strm)
Read the array.
PAbstractArray(PINDEX elementSizeInBytes, PINDEX initialSize=0)
Create a new dynamic array of initalSize elements of elementSizeInBytes bytes each.
virtual void PrintOn(ostream &strm) const
Print the array.