PTLib  Version 2.18.8
 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 
30 #ifndef PTLIB_CONTAIN_H
31 #define PTLIB_CONTAIN_H
32 
33 #ifdef P_USE_PRAGMA
34 #pragma interface
35 #endif
36 
37 
38 #include <ptlib/object.h>
39 #include <ptlib/atomic.h>
40 
41 
43 // Abstract container class
44 
45 // The type below cannot be nested into PContainer as DevStudio 2005 AUTOEXP.DAT doesn't like it
47 {
48  public:
49  __inline PContainerReference(PINDEX initialSize, bool isConst = false)
50  : size(initialSize)
51  , count(1)
52  , deleteObjects(true)
53  , constObject(isConst)
54  {
55  }
56 
58  : size(ref.size)
59  , count(1)
61  , constObject(false)
62  {
63  }
64 
65  PINDEX size; // Size of what the container contains
66  atomic<uint32_t> count; // reference count to the container content - guaranteed to be atomic
67  bool deleteObjects; // Used by PCollection but put here for efficiency
68  bool constObject; // Indicates object is constant/static, copy on write.
69 
71 
72  private:
73  void operator=(const PContainerReference &) { }
74 };
75 
76 
99 class PContainer : public PObject
100 {
101  PCLASSINFO(PContainer, PObject);
102 
103  public:
108  PContainer(
109  PINDEX initialSize = 0
110  );
111 
116  PContainer(
117  const PContainer & cont
118  );
119 
128  const PContainer & cont
129  );
130 
135  virtual ~PContainer()
136  { Destruct(); }
137 
139 
148  virtual PINDEX GetSize() const;
149  __inline size_t size() const { return GetSize(); }
150 
164  virtual PBoolean SetSize(
165  PINDEX newSize
166  ) = 0;
167 
174  PINDEX minSize
175  );
176 
183  virtual PBoolean IsEmpty() const;
184  __inline bool empty() const { return IsEmpty(); }
185 
192  PBoolean IsUnique() const;
193 
202  virtual PBoolean MakeUnique();
204 
205  protected:
216  PContainer(
217  int dummy,
218  const PContainer * cont
219  );
220 
223 
234  virtual void DestroyContents() = 0;
235 
245  virtual void AssignContents(const PContainer & c);
246 
258  void CopyContents(const PContainer & c);
259 
276  void CloneContents(const PContainer * src);
277 
281  void Destruct();
282 
286  virtual void DestroyReference();
287 
289 };
290 
291 
292 
340 #define PCONTAINERINFO(cls, par) \
341  PCLASSINFO(cls, par) \
342  public: \
343  cls(const cls & c) : par(c) { CopyContents(c); } \
344  cls & operator=(const cls & c) \
345  { AssignContents(c); return *this; } \
346  virtual ~cls() { Destruct(); } \
347  virtual PBoolean MakeUnique() \
348  { if(par::MakeUnique())return true; CloneContents(this);return false; } \
349  protected: \
350  cls(int dummy, const cls * c) : par(dummy, c) { CloneContents(c); } \
351  virtual void DestroyContents(); \
352  void CloneContents(const cls * c); \
353  void CopyContents(const cls & c); \
354  virtual void AssignContents(const PContainer & c) \
355  { par::AssignContents(c); CopyContents((const cls &)c); }
356 
357 
359 // Abstract collection of objects class
360 
392 class PCollection : public PContainer
393 {
394  PCLASSINFO(PCollection, PContainer);
395 
396  public:
401  PCollection(
402  PINDEX initialSize = 0
403  );
405 
421  virtual void PrintOn(
422  ostream &strm
423  ) const;
425 
437  virtual PINDEX Append(
438  PObject * obj
439  ) = 0;
440 
457  virtual PINDEX Insert(
458  const PObject & before,
459  PObject * obj
460  ) = 0;
461 
473  virtual PINDEX InsertAt(
474  PINDEX index,
475  PObject * obj
476  ) = 0;
477 
487  virtual PBoolean Remove(
488  const PObject * obj
489  ) = 0;
490  __inline void remove(const PObject * obj) { Remove(obj); }
491 
500  virtual PObject * RemoveAt(
501  PINDEX index
502  ) = 0;
503 
510  virtual void RemoveAll();
511  __inline void clear() { RemoveAll(); }
512 
526  virtual PBoolean SetAt(
527  PINDEX index,
528  PObject * val
529  ) = 0;
530 
536  virtual PObject * GetAt(
537  PINDEX index
538  ) const = 0;
539 
546  virtual PINDEX GetObjectsIndex(
547  const PObject * obj
548  ) const = 0;
549 
558  virtual PINDEX GetValuesIndex(
559  const PObject & obj
560  ) const = 0;
561 
576  PBoolean yes = true
577  );
578 
582  void DisallowDeleteObjects();
584 
585  protected:
597  int dummy,
598  const PCollection * coll
599  );
600 };
601 
602 
603 
605 // The abstract array class
606 
607 #include <ptlib/array.h>
608 
610 // The abstract array class
611 
612 #include <ptlib/lists.h>
613 
615 // PString class (specialised version of PBASEARRAY(char))
616 
617 #include <ptlib/dict.h>
618 
619 
621 // PString class
622 
623 #include <ptlib/pstring.h>
624 
625 
626 
628 // Fill in all the inline functions
629 
630 #if P_USE_INLINES
631 #include <ptlib/contain.inl>
632 #endif
633 
634 
635 #endif // PTLIB_CONTAIN_H
636 
637 
638 // End Of File ///////////////////////////////////////////////////////////////
virtual PBoolean IsEmpty() const
Determine if the container is empty.
virtual PObject * GetAt(PINDEX index) const =0
Get the object at the specified ordinal position.
void CopyContents(const PContainer &c)
Copy the container contents.
virtual PBoolean MakeUnique()
Make this instance to be the one and only reference to the container contents.
virtual PINDEX Insert(const PObject &before, PObject *obj)=0
Insert a new object immediately before the specified object.
void Destruct()
Internal function called from container destructors.
atomic< uint32_t > count
Definition: contain.h:66
virtual PBoolean Remove(const PObject *obj)=0
Remove the object from the collection.
PINLINE void AllowDeleteObjects(PBoolean yes=true)
Allow or disallow the deletion of the objects contained in the collection.
virtual PINDEX InsertAt(PINDEX index, PObject *obj)=0
Insert a new object at the specified ordinal index.
#define PINLINE
Definition: object.h:194
virtual PINDEX Append(PObject *obj)=0
Append a new object to the collection.
PContainer & operator=(const PContainer &cont)
Assign one container reference to another.
virtual PObject * RemoveAt(PINDEX index)=0
Remove the object at the specified ordinal index from the collection.
virtual PINDEX GetValuesIndex(const PObject &obj) const =0
Search the collection for the specified value of the object.
virtual void RemoveAll()
Remove all of the elements in the collection.
PINDEX size
Definition: contain.h:65
virtual void AssignContents(const PContainer &c)
Copy the container contents.
PBoolean IsUnique() const
Determine if container is unique reference.
virtual void PrintOn(ostream &strm) const
Print the collection on the stream.
virtual PBoolean SetSize(PINDEX newSize)=0
Set the new current size of the container.
virtual void DestroyContents()=0
Destroy the container contents.
PContainer(PINDEX initialSize=0)
Create a new unique container.
virtual PINDEX GetSize() const
Get the current size of the container.
bool constObject
Definition: contain.h:68
bool PBoolean
Definition: object.h:174
Definition: contain.h:46
bool deleteObjects
Definition: contain.h:67
virtual void DestroyReference()
Destroy the PContainerReference instance.
virtual ~PContainer()
Destroy the container class.
Definition: contain.h:135
Abstract class to embody the base functionality of a container.
Definition: contain.h:99
__inline bool empty() const
Definition: contain.h:184
virtual PBoolean SetAt(PINDEX index, PObject *val)=0
Set the object at the specified ordinal position to the new value.
virtual PINDEX GetObjectsIndex(const PObject *obj) const =0
Search the collection for the specific instance of the object.
__inline PContainerReference(PINDEX initialSize, bool isConst=false)
Definition: contain.h:49
PBoolean SetMinSize(PINDEX minSize)
Set the minimum size of container.
void CloneContents(const PContainer *src)
Create a duplicate of the container contents.
PCollection(PINDEX initialSize=0)
Create a new collection.
PDECLARE_POOL_ALLOCATOR(PContainerReference)
PContainerReference * reference
Definition: contain.h:288
__inline PContainerReference(const PContainerReference &ref)
Definition: contain.h:57
__inline size_t size() const
Definition: contain.h:149
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
void DisallowDeleteObjects()
Disallow the deletion of the objects contained in the collection.
__inline void clear()
Definition: contain.h:511