PTLib Version 2.12.9

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

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: 31109 $
30 * $Author: ededu $
31 * $Date: 2013-12-18 22:26:43 +1100 (Wed, 18 Dec 2013) $
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:
84 PAbstractArray(
85 PINDEX elementSizeInBytes,
86
87 PINDEX initialSize = 0
88 );
89
107 PAbstractArray(
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
221 PBoolean Concatenate(
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
238 PAbstractArray(
239 PContainerReference & reference,
240 PINDEX elementSizeInBytes
241 );
242
244 PINDEX elementSize;
245
247 char * theArray;
248
250 PBoolean allocatedDynamically;
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:
287 PBaseArray(
288 PINDEX initialSize = 0
289 ) : PAbstractArray(sizeof(T), initialSize) { }
290
293 PBaseArray(
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
318 PBoolean SetAt(
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() ? ((T *)theArray)[index] : (T)0;
336 }
337
346 void Attach(
347 const T * buffer,
348 PINDEX bufferSize
349 ) {
350 PAbstractArray::Attach(buffer, bufferSize);
351 }
352
366 T * GetPointer(
367 PINDEX minSize = 0
368 ) {
369 return (T *)PAbstractArray::GetPointer(minSize);
370 }
372
384 T operator[](
385 PINDEX index
386 ) const {
387 return GetAt(index);
388 }
389
400 T & operator[](
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
436 PBoolean Concatenate(
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:
514 PScalarArray(
515 PINDEX initialSize = 0
516 ) : PBaseArray<T>(initialSize) { }
517
520 PScalarArray(
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 {
554 typedef PBaseArray<char> ParentClass;
555 PCLASSINFO(PCharArray, ParentClass);
556 public:
562 PCharArray(
563 PINDEX initialSize = 0
564 ) : ParentClass(initialSize) { }
565
568 PCharArray(
569 char const * buffer,
570 PINDEX length,
571 PBoolean dynamic = true
572 ) : ParentClass(buffer, length, dynamic) { }
573
574 PCharArray(PContainerReference & reference_)
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
596 typedef PScalarArray<short> PShortArray;
597
599 typedef PScalarArray<int> PIntArray;
600
602 typedef PScalarArray<long> PLongArray;
603
605 class PBYTEArray : public PBaseArray<BYTE>
606 {
607 typedef PBaseArray<BYTE> ParentClass;
608 PCLASSINFO(PCharArray, ParentClass);
609 public:
615 PBYTEArray(
616 PINDEX initialSize = 0
617 ) : ParentClass(initialSize) { }
618
621 PBYTEArray(
622 BYTE const * buffer,
623 PINDEX length,
624 PBoolean dynamic = true
625 ) : ParentClass(buffer, length, dynamic) { }
626
627 PBYTEArray(PContainerReference & reference_)
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
658 typedef PScalarArray<WORD> PWORDArray;
659
661 typedef PScalarArray<unsigned> PUnsignedArray;
662
664 typedef PScalarArray<DWORD> PDWORDArray;
665
666
668 // Linear array of objects
669
691 class PArrayObjects : public PCollection
692 {
693 PCONTAINERINFO(PArrayObjects, PCollection);
694 public:
703 PINLINE PArrayObjects(
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
891 PBaseArray<PObject *> * theArray;
892 };
893
894
902 template <class T> class PArray : public PArrayObjects
903 {
904 PCLASSINFO(PArray, PArrayObjects);
905 public:
914 PArray(
915 PINDEX initialSize = 0
916 ) : PArrayObjects(initialSize) { }
918
924 virtual PObject * Clone() const
925 { return PNEW PArray(0, this); }
927
937 T & operator[](
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
1106 PBoolean operator[](
1107 PINDEX index
1108 ) const { return GetAt(index); }
1109
1115 PBitArray & operator+=(
1116 PINDEX index
1117 ) { SetAt(index, true); return *this; }
1118
1124 PBitArray & operator-=(
1125 PINDEX index
1126 ) { SetAt(index, false); return *this; }
1127
1139 PBoolean Concatenate(
1140 const PBitArray & array
1141 );
1143 };
1144
1145
1146 template <class ObjClass>
1147 class PPtrVector : public std::vector<ObjClass *>
1148 {
1149 public:
1150 typedef std::vector<ObjClass *> Parent;
1151 virtual ~PPtrVector()
1152 {
1153 Clear();
1154 }
1155
1156 void Clear()
1157 {
1158 while (Parent::size() > 0) {
1159 delete Parent::front();
1160 Parent::erase(Parent::begin());
1161 }
1162 }
1163 };
1164
1165
1166
1167 #endif // PTLIB_ARRAY_H
1168
1169
1170 // End Of File ///////////////////////////////////////////////////////////////