PTLib  Version 2.10.11
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: 25387 $
30  * $Author: rjongbloed $
31  * $Date: 2011-03-22 22:51:09 -0500 (Tue, 22 Mar 2011) $
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/critsec.h>
43 #include <ptlib/object.h>
44 
45 
46 
48 // Abstract container class
49 
50 // The type below cannot be nested into PContainer as DevStudio 2005 AUTOEXP.DAT doesn't like it
52 {
53  public:
54  __inline PContainerReference(PINDEX initialSize, bool isConst = false)
55  : size(initialSize)
56  , count(1)
57  , deleteObjects(true)
58  , constObject(isConst)
59  {
60  }
61 
63  : size(ref.size)
64  , count(1)
66  , constObject(false)
67  {
68  }
69 
70  PINDEX size; // Size of what the container contains
71  PAtomicInteger count; // reference count to the container content - guaranteed to be atomic
72  bool deleteObjects; // Used by PCollection but put here for efficiency
73  bool constObject; // Indicates object is constant/static, copy on write.
74 
76 
77  private:
78  void operator=(const PContainerReference &) { }
79 };
80 
81 
104 class PContainer : public PObject
105 {
107 
108  public:
113  PContainer(
114  PINDEX initialSize = 0
115  );
116 
121  PContainer(
122  const PContainer & cont
123  );
124 
132  PContainer & operator=(
133  const PContainer & cont
134  );
135 
140  virtual ~PContainer()
141  { Destruct(); }
142 
144 
153  virtual PINDEX GetSize() const;
154 
168  virtual PBoolean SetSize(
169  PINDEX newSize
170  ) = 0;
171 
177  PBoolean SetMinSize(
178  PINDEX minSize
179  );
180 
187  virtual PBoolean IsEmpty() const;
188 
195  PBoolean IsUnique() const;
196 
205  virtual PBoolean MakeUnique();
207 
208  protected:
219  PContainer(
220  int dummy,
221  const PContainer * cont
222  );
223 
225  PContainer(PContainerReference & reference);
226 
237  virtual void DestroyContents() = 0;
238 
248  virtual void AssignContents(const PContainer & c);
249 
261  void CopyContents(const PContainer & c);
262 
279  void CloneContents(const PContainer * src);
280 
284  void Destruct();
285 
289  virtual void DestroyReference();
290 
292 };
293 
294 
295 
343 #define PCONTAINERINFO(cls, par) \
344  PCLASSINFO(cls, par) \
345  public: \
346  cls(const cls & c) : par(c) { CopyContents(c); } \
347  cls & operator=(const cls & c) \
348  { AssignContents(c); return *this; } \
349  virtual ~cls() { Destruct(); } \
350  virtual PBoolean MakeUnique() \
351  { if(par::MakeUnique())return true; CloneContents(this);return false; } \
352  protected: \
353  cls(int dummy, const cls * c) : par(dummy, c) { CloneContents(c); } \
354  virtual void DestroyContents(); \
355  void CloneContents(const cls * c); \
356  void CopyContents(const cls & c); \
357  virtual void AssignContents(const PContainer & c) \
358  { par::AssignContents(c); CopyContents((const cls &)c); }
359 
360 
362 // Abstract collection of objects class
363 
395 class PCollection : public PContainer
396 {
398 
399  public:
404  PCollection(
405  PINDEX initialSize = 0
406  );
408 
424  virtual void PrintOn(
425  ostream &strm
426  ) const;
428 
440  virtual PINDEX Append(
441  PObject * obj
442  ) = 0;
443 
460  virtual PINDEX Insert(
461  const PObject & before,
462  PObject * obj
463  ) = 0;
464 
476  virtual PINDEX InsertAt(
477  PINDEX index,
478  PObject * obj
479  ) = 0;
480 
490  virtual PBoolean Remove(
491  const PObject * obj
492  ) = 0;
493 
502  virtual PObject * RemoveAt(
503  PINDEX index
504  ) = 0;
505 
512  virtual void RemoveAll();
513 
527  virtual PBoolean SetAt(
528  PINDEX index,
529  PObject * val
530  ) = 0;
531 
537  virtual PObject * GetAt(
538  PINDEX index
539  ) const = 0;
540 
547  virtual PINDEX GetObjectsIndex(
548  const PObject * obj
549  ) const = 0;
550 
559  virtual PINDEX GetValuesIndex(
560  const PObject & obj
561  ) const = 0;
562 
576  PINLINE void AllowDeleteObjects(
577  PBoolean yes = true
578  );
579 
583  void DisallowDeleteObjects();
585 
586  protected:
598  int dummy,
599  const PCollection * coll
600  );
601 };
602 
603 
604 
606 // The abstract array class
607 
608 #include <ptlib/array.h>
609 
611 // The abstract array class
612 
613 #include <ptlib/lists.h>
614 
616 // PString class (specialised version of PBASEARRAY(char))
617 
618 #include <ptlib/dict.h>
619 
620 
622 // PString class
623 
624 #include <ptlib/pstring.h>
625 
626 
627 
629 // Fill in all the inline functions
630 
631 #if P_USE_INLINES
632 #include <ptlib/contain.inl>
633 #endif
634 
635 
636 #endif // PTLIB_CONTAIN_H
637 
638 
639 // End Of File ///////////////////////////////////////////////////////////////
virtual void PrintOn(ostream &strm) const
Print the array.
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
#define PINLINE
Definition: object.h:127
PINDEX size
Definition: contain.h:70
BOOL PBoolean
Definition: object.h:102
This class implements an integer that can be atomically incremented and decremented in a thread-safe ...
Definition: critsec.h:171
bool constObject
Definition: contain.h:73
Definition: contain.h:51
bool deleteObjects
Definition: contain.h:72
virtual ~PContainer()
Destroy the container class.
Definition: contain.h:140
Abstract class to embody the base functionality of a container.
Definition: contain.h:104
__inline PContainerReference(PINDEX initialSize, bool isConst=false)
Definition: contain.h:54
PAtomicInteger count
Definition: contain.h:71
PContainerReference * reference
Definition: contain.h:291
__inline PContainerReference(const PContainerReference &ref)
Definition: contain.h:62
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
A collection is a container that collects together descendents of the PObject class.
Definition: contain.h:395