contain.h

Go to the documentation of this file.
00001 /*
00002  * contain.h
00003  *
00004  * Umbrella include for Container Classes.
00005  *
00006  * Portable Windows Library
00007  *
00008  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
00025  * All Rights Reserved.
00026  *
00027  * Contributor(s): ______________________________________.
00028  *
00029  * $Revision: 20385 $
00030  * $Author: rjongbloed $
00031  * $Date: 2008-06-04 10:40:38 +0000 (Wed, 04 Jun 2008) $
00032  */
00033 
00034 #ifndef _CONTAIN_H
00035 #define _CONTAIN_H
00036 
00037 #ifdef P_USE_PRAGMA
00038 #pragma interface
00039 #endif
00040 
00041 #include <ptlib/object.h>
00042 #include <ptlib/critsec.h>
00043 
00045 // Abstract container class
00046 
00047 // The type below cannot be nested into PContainer as DevStudio 2005 AUTOEXP.DAT doesn't like it
00048 class PContainerReference {
00049   public:
00050     inline PContainerReference(PINDEX initialSize)
00051       : size(initialSize), count(1), deleteObjects(PTrue)
00052     {
00053     }
00054 
00055     inline PContainerReference(const PContainerReference & ref)
00056       : size(ref.size), count(1), deleteObjects(ref.deleteObjects)
00057     {  
00058     }
00059 
00060     PINDEX   size;         // Size of what the container contains
00061     PAtomicInteger count;  // reference count to the container content - guaranteed to be atomic
00062     PBoolean deleteObjects;    // Used by PCollection but put here for efficiency
00063 
00064   private:
00065     PContainerReference & operator=(const PContainerReference &) 
00066     { return *this; }
00067 };
00068 
00091 class PContainer : public PObject
00092 {
00093   PCLASSINFO(PContainer, PObject);
00094 
00095   public:
00100     PContainer(
00101       PINDEX initialSize = 0  
00102     );
00103 
00108     PContainer(
00109       const PContainer & cont  
00110     );
00111 
00119     PContainer & operator=(
00120       const PContainer & cont  
00121     );
00122 
00127     virtual ~PContainer()
00128     { Destruct(); }
00129 
00131 
00140     virtual PINDEX GetSize() const;
00141 
00155     virtual PBoolean SetSize(
00156       PINDEX newSize  
00157     ) = 0;
00158 
00164     PBoolean SetMinSize(
00165       PINDEX minSize  
00166     );
00167 
00174     virtual PBoolean IsEmpty() const;
00175 
00182     PBoolean IsUnique() const;
00183 
00192     virtual PBoolean MakeUnique();
00194 
00195   protected:
00206     PContainer(
00207       int dummy,        
00208       const PContainer * cont  
00209     );
00210 
00221     virtual void DestroyContents() = 0;
00222 
00232     virtual void AssignContents(const PContainer & c);
00233 
00245     void CopyContents(const PContainer & c);
00246 
00263     void CloneContents(const PContainer * src);
00264 
00268     void Destruct();
00269 
00270 
00271     PContainerReference * reference;
00272 };
00273 
00274 
00275 
00323 #define PCONTAINERINFO(cls, par) \
00324     PCLASSINFO(cls, par) \
00325   public: \
00326     cls(const cls & c) : par(c) { CopyContents(c); } \
00327     cls & operator=(const cls & c) \
00328       { AssignContents(c); return *this; } \
00329     virtual ~cls() { Destruct(); } \
00330     virtual PBoolean MakeUnique() \
00331       { if(par::MakeUnique())return PTrue; CloneContents(this);return PFalse; } \
00332   protected: \
00333     cls(int dummy, const cls * c) : par(dummy, c) { CloneContents(c); } \
00334     virtual void DestroyContents(); \
00335     void CloneContents(const cls * c); \
00336     void CopyContents(const cls & c); \
00337     virtual void AssignContents(const PContainer & c) \
00338       { par::AssignContents(c); CopyContents((const cls &)c); }
00339 
00340 
00342 // Abstract collection of objects class
00343 
00375 class PCollection : public PContainer
00376 {
00377   PCLASSINFO(PCollection, PContainer);
00378 
00379   public:
00384     PCollection(
00385       PINDEX initialSize = 0  
00386     );
00388 
00404     virtual void PrintOn(
00405       ostream &strm   
00406     ) const;
00408 
00420     virtual PINDEX Append(
00421       PObject * obj   
00422     ) = 0;
00423 
00440     virtual PINDEX Insert(
00441       const PObject & before,   
00442       PObject * obj             
00443     ) = 0;
00444 
00456     virtual PINDEX InsertAt(
00457       PINDEX index,   
00458       PObject * obj   
00459     ) = 0;
00460 
00470     virtual PBoolean Remove(
00471       const PObject * obj   
00472     ) = 0;
00473 
00482     virtual PObject * RemoveAt(
00483       PINDEX index   
00484     ) = 0;
00485 
00492     virtual void RemoveAll();
00493 
00507     virtual PBoolean SetAt(
00508       PINDEX index,   
00509       PObject * val   
00510     ) = 0;
00511 
00517     virtual PObject * GetAt(
00518       PINDEX index  
00519     ) const = 0;
00520 
00527     virtual PINDEX GetObjectsIndex(
00528       const PObject * obj  
00529     ) const = 0;
00530 
00539     virtual PINDEX GetValuesIndex(
00540       const PObject & obj  
00541     ) const = 0;
00542 
00556     PINLINE void AllowDeleteObjects(
00557       PBoolean yes = PTrue   
00558     );
00559 
00563     void DisallowDeleteObjects();
00565 
00566   protected:
00577     PINLINE PCollection(
00578       int dummy,        
00579       const PCollection * coll  
00580     );
00581 };
00582 
00583 
00584 
00586 // The abstract array class
00587 
00588 #include <ptlib/array.h>
00589 
00591 // The abstract array class
00592 
00593 #include <ptlib/lists.h>
00594 
00596 // PString class (specialised version of PBASEARRAY(char))
00597 
00598 #include <ptlib/dict.h>
00599 
00600 
00602 // PString class
00603 
00604 #include <ptlib/pstring.h>
00605 
00606 
00607 
00609 // Fill in all the inline functions
00610 
00611 #if P_USE_INLINES
00612 #include <ptlib/contain.inl>
00613 #endif
00614 
00615 #endif // _CONTAIN_H
00616 
00617 
00618 // End Of File ///////////////////////////////////////////////////////////////

Generated on Mon Feb 23 01:57:53 2009 for PTLib by  doxygen 1.5.1