PTLib  Version 2.14.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
contain.h
Go to the documentation of this file.
1 /*
2  * contain.h
3  *
4  * Umbrella include for Container Classes.
5  *
6  * Portable Windows 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: 32084 $
30  * $Author: rjongbloed $
31  * $Date: 2014-06-06 17:36:09 +1000 (Fri, 06 Jun 2014) $
32  */
33 
34 #ifndef PTLIB_CONTAIN_H
35 #define PTLIB_CONTAIN_H
36 
37 #ifdef P_USE_PRAGMA
38 #pragma interface
39 #endif
40 
41 
42 #include <ptlib/object.h>
43 #include <ptlib/critsec.h>
44 
45 
47 // Abstract container class
48 
49 // The type below cannot be nested into PContainer as DevStudio 2005 AUTOEXP.DAT doesn't like it
51 {
52  public:
53  __inline PContainerReference(PINDEX initialSize, bool isConst = false)
54  : size(initialSize)
55  , count(1)
56  , deleteObjects(true)
57  , constObject(isConst)
58  {
59  }
60 
62  : size(ref.size)
63  , count(1)
65  , constObject(false)
66  {
67  }
68 
69  PINDEX size; // Size of what the container contains
70  PAtomicInteger count; // reference count to the container content - guaranteed to be atomic
71  bool deleteObjects; // Used by PCollection but put here for efficiency
72  bool constObject; // Indicates object is constant/static, copy on write.
73 
75 
76  private:
77  void operator=(const PContainerReference &) { }
78 };
79 
80 
103 class PContainer : public PObject
104 {
105  PCLASSINFO(PContainer, PObject);
106 
107  public:
112  PContainer(
113  PINDEX initialSize = 0
114  );
115 
120  PContainer(
121  const PContainer & cont
122  );
123 
132  const PContainer & cont
133  );
134 
139  virtual ~PContainer()
140  { Destruct(); }
141 
143 
152  virtual PINDEX GetSize() const;
153  __inline size_t size() const { return GetSize(); }
154 
168  virtual PBoolean SetSize(
169  PINDEX newSize
170  ) = 0;
171 
178  PINDEX minSize
179  );
180 
187  virtual PBoolean IsEmpty() const;
188  __inline bool empty() const { return IsEmpty(); }
189 
196  PBoolean IsUnique() const;
197 
206  virtual PBoolean MakeUnique();
208 
209  protected:
220  PContainer(
221  int dummy,
222  const PContainer * cont
223  );
224 
227 
238  virtual void DestroyContents() = 0;
239 
249  virtual void AssignContents(const PContainer & c);
250 
262  void CopyContents(const PContainer & c);
263 
280  void CloneContents(const PContainer * src);
281 
285  void Destruct();
286 
290  virtual void DestroyReference();
291 
293 };
294 
295 
296 
344 #define PCONTAINERINFO(cls, par) \
345  PCLASSINFO(cls, par) \
346  public: \
347  cls(const cls & c) : par(c) { CopyContents(c); } \
348  cls & operator=(const cls & c) \
349  { AssignContents(c); return *this; } \
350  virtual ~cls() { Destruct(); } \
351  virtual PBoolean MakeUnique() \
352  { if(par::MakeUnique())return true; CloneContents(this);return false; } \
353  protected: \
354  cls(int dummy, const cls * c) : par(dummy, c) { CloneContents(c); } \
355  virtual void DestroyContents(); \
356  void CloneContents(const cls * c); \
357  void CopyContents(const cls & c); \
358  virtual void AssignContents(const PContainer & c) \
359  { par::AssignContents(c); CopyContents((const cls &)c); }
360 
361 
363 // Abstract collection of objects class
364 
396 class PCollection : public PContainer
397 {
398  PCLASSINFO(PCollection, PContainer);
399 
400  public:
405  PCollection(
406  PINDEX initialSize = 0
407  );
409 
425  virtual void PrintOn(
426  ostream &strm
427  ) const;
429 
441  virtual PINDEX Append(
442  PObject * obj
443  ) = 0;
444 
461  virtual PINDEX Insert(
462  const PObject & before,
463  PObject * obj
464  ) = 0;
465 
477  virtual PINDEX InsertAt(
478  PINDEX index,
479  PObject * obj
480  ) = 0;
481 
491  virtual PBoolean Remove(
492  const PObject * obj
493  ) = 0;
494  __inline void remove(const PObject * obj) { Remove(obj); }
495 
504  virtual PObject * RemoveAt(
505  PINDEX index
506  ) = 0;
507 
514  virtual void RemoveAll();
515  __inline void clear() { RemoveAll(); }
516 
530  virtual PBoolean SetAt(
531  PINDEX index,
532  PObject * val
533  ) = 0;
534 
540  virtual PObject * GetAt(
541  PINDEX index
542  ) const = 0;
543 
550  virtual PINDEX GetObjectsIndex(
551  const PObject * obj
552  ) const = 0;
553 
562  virtual PINDEX GetValuesIndex(
563  const PObject & obj
564  ) const = 0;
565 
580  PBoolean yes = true
581  );
582 
586  void DisallowDeleteObjects();
588 
589  protected:
601  int dummy,
602  const PCollection * coll
603  );
604 };
605 
606 
607 
609 // The abstract array class
610 
611 #include <ptlib/array.h>
612 
614 // The abstract array class
615 
616 #include <ptlib/lists.h>
617 
619 // PString class (specialised version of PBASEARRAY(char))
620 
621 #include <ptlib/dict.h>
622 
623 
625 // PString class
626 
627 #include <ptlib/pstring.h>
628 
629 
630 
632 // Fill in all the inline functions
633 
634 #if P_USE_INLINES
635 #include <ptlib/contain.inl>
636 #endif
637 
638 
639 #endif // PTLIB_CONTAIN_H
640 
641 
642 // End Of File ///////////////////////////////////////////////////////////////