PTLib  Version 2.14.3
 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  * $Revision: 31538 $
30  * $Author: rjongbloed $
31  * $Date: 2014-02-19 16:06:05 +1100 (Wed, 19 Feb 2014) $
32  */
33 
34 #ifndef PTLIB_ARRAY_H
35 #define PTLIB_ARRAY_H
36 
37 #ifdef P_USE_PRAGMA
38 #pragma interface
39 #endif
40 
41 #include <vector>
42 
43 #include <ptlib/contain.h>
44 
46 // The abstract array class
47 
69 class PAbstractArray : public PContainer
70 {
71  PCONTAINERINFO(PAbstractArray, PContainer);
72  public:
85  PINDEX elementSizeInBytes,
86 
87  PINDEX initialSize = 0
88  );
89 
108  PINDEX elementSizeInBytes,
109 
110  const void *buffer,
111  PINDEX bufferSizeInElements,
112  PBoolean dynamicAllocation
113  );
115 
124  virtual void PrintOn(
125  ostream &strm // Stream to print the object into.
126  ) const;
127 
134  virtual void ReadFrom(
135  istream &strm // Stream to read the objects contents from.
136  );
137 
157  virtual Comparison Compare(
158  const PObject & obj
159  ) const;
161 
172  virtual PBoolean SetSize(
173  PINDEX newSize
174  );
176 
187  void Attach(
188  const void *buffer,
189  PINDEX bufferSize
190  );
191 
205  void * GetPointer(
206  PINDEX minSize = 1
207  );
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 
581  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 
634  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 
662 
665 
666 
668 // Linear array of objects
669 
692 {
693  PCONTAINERINFO(PArrayObjects, PCollection);
694  public:
704  PINDEX initialSize = 0
705  );
707 
737  virtual Comparison Compare(
738  const PObject & obj
739  ) const;
741 
744 
745  virtual PINDEX GetSize() const;
746 
755  virtual PBoolean SetSize(
756  PINDEX newSize
757  );
759 
768  virtual PINDEX Append(
769  PObject * obj
770  );
771 
787  virtual PINDEX Insert(
788  const PObject & before,
789  PObject * obj
790  );
791 
802  virtual PINDEX InsertAt(
803  PINDEX index,
804  PObject * obj
805  );
806 
815  virtual PBoolean Remove(
816  const PObject * obj
817  );
818 
830  virtual PObject * RemoveAt(
831  PINDEX index
832  );
833 
841  virtual PBoolean SetAt(
842  PINDEX index,
843  PObject * val
844  );
845 
852  virtual PObject * GetAt(
853  PINDEX index
854  ) const;
855 
863  virtual PINDEX GetObjectsIndex(
864  const PObject * obj
865  ) const;
866 
876  virtual PINDEX GetValuesIndex(
877  const PObject & obj // Object to find equal of.
878  ) const;
879 
886  virtual void RemoveAll();
888 
889  protected:
890  // The type below cannot be nested as DevStudio 2005 AUTOEXP.DAT doesn't like it
892 };
893 
894 
902 template <class T> class PArray : public PArrayObjects
903 {
904  PCLASSINFO(PArray, PArrayObjects);
905  public:
915  PINDEX initialSize = 0
916  ) : PArrayObjects(initialSize) { }
918 
924  virtual PObject * Clone() const
925  { return PNEW PArray(0, this); }
927 
938  PINDEX index
939  ) const {
940  PObject * obj = this->GetAt(index);
941  PAssert(obj != NULL, PInvalidArrayElement);
942  return dynamic_cast<T &>(*obj);
943  }
945 
946  protected:
947  PArray(int dummy, const PArray * c) : PArrayObjects(dummy, c) { }
948 };
949 
950 
962 #define PARRAY(cls, T) typedef PArray<T> cls
963 
964 
977 #define PDECLARE_ARRAY(cls, T) \
978  PARRAY(cls##_PTemplate, T); \
979  PDECLARE_CLASS(cls, cls##_PTemplate) \
980  protected: \
981  inline cls(int dummy, const cls * c) \
982  : cls##_PTemplate(dummy, c) { } \
983  public: \
984  inline cls(PINDEX initialSize = 0) \
985  : cls##_PTemplate(initialSize) { } \
986  virtual PObject * Clone() const \
987  { return PNEW cls(0, this); } \
988 
989 
992 class PBitArray : public PBYTEArray
993 {
994  PCLASSINFO(PBitArray, PBYTEArray);
995 
996  public:
1001  PBitArray(
1002  PINDEX initialSize = 0
1003  );
1004 
1007  PBitArray(
1008  const void * buffer,
1009  PINDEX length,
1010  PBoolean dynamic = true
1011  );
1013 
1018  virtual PObject * Clone() const;
1020 
1029  virtual PINDEX GetSize() const;
1030 
1039  virtual PBoolean SetSize(
1040  PINDEX newSize
1041  );
1042 
1049  PBoolean SetAt(
1050  PINDEX index,
1051  PBoolean val
1052  );
1053 
1060  PBoolean GetAt(
1061  PINDEX index
1062  ) const;
1063 
1072  void Attach(
1073  const void * buffer,
1074  PINDEX bufferSize
1075  );
1076 
1090  BYTE * GetPointer(
1091  PINDEX minSize = 0
1092  );
1094 
1107  PINDEX index
1108  ) const { return GetAt(index); }
1109 
1116  PINDEX index
1117  ) { SetAt(index, true); return *this; }
1118 
1125  PINDEX index
1126  ) { SetAt(index, false); return *this; }
1127 
1140  const PBitArray & array
1141  );
1143 };
1144 
1145 
1146 #endif // PTLIB_ARRAY_H
1147 
1148 
1149 // End Of File ///////////////////////////////////////////////////////////////