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 * $Log: contain.h,v $ 00030 * Revision 1.64 2004/04/15 22:44:52 csoutheren 00031 * Re-applied gcc 2.95 patch as CVS screwed up 00032 * 00033 * Revision 1.63 2004/04/14 23:34:52 csoutheren 00034 * Added plugin for data access 00035 * 00036 * Revision 1.61 2004/04/12 00:36:04 csoutheren 00037 * Added new class PAtomicInteger and added Windows implementation 00038 * 00039 * Revision 1.60 2004/04/11 06:15:27 csoutheren 00040 * Modified to use Atomic_word if available 00041 * 00042 * Revision 1.59 2004/04/11 02:55:17 csoutheren 00043 * Added PCriticalSection for Windows 00044 * Added compile time option for PContainer to use critical sections to provide thread safety under some circumstances 00045 * 00046 * Revision 1.58 2004/04/09 03:42:34 csoutheren 00047 * Removed all usages of "virtual inline" and "inline virtual" 00048 * 00049 * Revision 1.57 2003/09/17 05:41:58 csoutheren 00050 * Removed recursive includes 00051 * 00052 * Revision 1.56 2003/09/17 01:18:02 csoutheren 00053 * Removed recursive include file system and removed all references 00054 * to deprecated coooperative threading support 00055 * 00056 * Revision 1.55 2003/03/31 01:23:56 robertj 00057 * Added ReadFrom functions for standard container classes such as 00058 * PIntArray and PStringList etc 00059 * 00060 * Revision 1.54 2002/09/16 01:08:59 robertj 00061 * Added #define so can select if #pragma interface/implementation is used on 00062 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan. 00063 * 00064 * Revision 1.53 2001/02/13 04:39:08 robertj 00065 * Fixed problem with operator= in container classes. Some containers will 00066 * break unless the copy is virtual (eg PStringStream's buffer pointers) so 00067 * needed to add a new AssignContents() function to all containers. 00068 * 00069 * Revision 1.52 1999/11/30 00:22:54 robertj 00070 * Updated documentation for doc++ 00071 * 00072 * Revision 1.51 1999/08/22 12:13:42 robertj 00073 * Fixed warning when using inlines on older GNU compiler 00074 * 00075 * Revision 1.50 1999/08/17 03:46:40 robertj 00076 * Fixed usage of inlines in optimised version. 00077 * 00078 * Revision 1.49 1999/03/09 02:59:49 robertj 00079 * Changed comments to doc++ compatible documentation. 00080 * 00081 * Revision 1.48 1999/02/16 08:07:11 robertj 00082 * MSVC 6.0 compatibility changes. 00083 * 00084 * Revision 1.47 1998/09/23 06:20:23 robertj 00085 * Added open source copyright license. 00086 * 00087 * Revision 1.46 1997/07/08 13:15:04 robertj 00088 * DLL support. 00089 * 00090 * Revision 1.45 1996/08/17 10:00:20 robertj 00091 * Changes for Windows DLL support. 00092 * 00093 * Revision 1.44 1996/08/08 10:08:41 robertj 00094 * Directory structure changes for common files. 00095 * 00096 * Revision 1.43 1995/06/17 11:12:26 robertj 00097 * Documentation update. 00098 * 00099 * Revision 1.42 1995/03/14 12:41:13 robertj 00100 * Updated documentation to use HTML codes. 00101 * 00102 * Revision 1.41 1995/01/09 12:36:18 robertj 00103 * Removed unnecesary return value from I/O functions. 00104 * Changes due to Mac port. 00105 * 00106 * Revision 1.40 1994/12/13 11:50:45 robertj 00107 * Added MakeUnique() function to all container classes. 00108 * 00109 * Revision 1.39 1994/12/12 10:16:18 robertj 00110 * Restructuring and documentation of container classes. 00111 * Renaming of some macros for declaring container classes. 00112 * Added some extra functionality to PString. 00113 * Added start to 2 byte characters in PString. 00114 * Fixed incorrect overrides in PCaselessString. 00115 * 00116 * Revision 1.38 1994/12/05 11:18:58 robertj 00117 * Moved SetMinSize from PAbstractArray to PContainer. 00118 * 00119 * Revision 1.37 1994/11/28 12:33:44 robertj 00120 * Added dummy parameter for cls* constructor in containers. This prevents some very 00121 * strange an undesirable default construction of clones. 00122 * 00123 * Revision 1.36 1994/10/30 11:50:09 robertj 00124 * Split into Object classes and Container classes. 00125 * Changed mechanism for doing notification callback functions. 00126 * 00127 * Revision 1.35 1994/10/23 04:40:50 robertj 00128 * Made container * constractor protected. 00129 * Shorted OS Error assert. 00130 * Added printf constructor to PString. 00131 * 00132 * Revision 1.34 1994/09/25 10:36:41 robertj 00133 * Improved const behavious of container class macros. 00134 * 00135 * Revision 1.33 1994/08/23 11:32:52 robertj 00136 * Oops 00137 * 00138 * Revision 1.32 1994/08/22 00:46:48 robertj 00139 * Added pragma fro GNU C++ compiler. 00140 * 00141 * Revision 1.31 1994/08/21 23:43:02 robertj 00142 * Changed parameter before variable argument list to NOT be a reference. 00143 * Added object serialisation classes. 00144 * 00145 * Revision 1.30 1994/08/04 11:51:39 robertj 00146 * Rewrite of memory check functions. 00147 * 00148 * Revision 1.29 1994/07/27 05:58:07 robertj 00149 * Synchronisation. 00150 * 00151 * Revision 1.28 1994/07/25 03:33:50 robertj 00152 * Extra memory tests. 00153 * 00154 * Revision 1.27 1994/07/17 10:46:06 robertj 00155 * Added functions to strings in containers. 00156 * 00157 * Revision 1.26 1994/07/02 03:03:49 robertj 00158 * Addition of container searching facilities. 00159 * 00160 * Revision 1.25 1994/06/25 11:55:15 robertj 00161 * Unix version synchronisation. 00162 * 00163 * Revision 1.24 1994/04/20 12:17:44 robertj 00164 * Added code to assert 00165 * 00166 * Revision 1.23 1994/04/11 14:17:27 robertj 00167 * Made standard operators new and delete only declared for GNU C++ 00168 * 00169 * Revision 1.22 1994/04/01 14:09:46 robertj 00170 * Removed PDECLARE_ABSTRACT_CONTAINER. 00171 * Added string stream class. 00172 * Added string containers. 00173 * 00174 * Revision 1.21 1994/03/07 07:38:19 robertj 00175 * Major enhancementsacross the board. 00176 * 00177 * Revision 1.20 1994/01/13 08:42:29 robertj 00178 * Fixed missing copy constuctor and assignment operator for PString. 00179 * 00180 * Revision 1.19 1994/01/13 05:33:41 robertj 00181 * Added contructor to get caseless string from ordinary string. 00182 * 00183 * Revision 1.18 1994/01/03 04:42:23 robertj 00184 * Mass changes to common container classes and interactors etc etc etc. 00185 * 00186 * Revision 1.17 1993/12/31 06:40:34 robertj 00187 * Made inlines optional for debugging purposes. 00188 * Added default to DeleteObjects() function. 00189 * 00190 * Revision 1.16 1993/12/24 04:20:52 robertj 00191 * Mac CFront port. 00192 * 00193 * Revision 1.15 1993/12/16 00:51:46 robertj 00194 * Made some container functions const. 00195 * 00196 * Revision 1.14 1993/12/15 21:10:10 robertj 00197 * Changes to fix inadequate reference system for containers. 00198 * 00199 * Revision 1.13 1993/12/14 18:44:56 robertj 00200 * Added RemoveAll() to collection classes. 00201 * Fixed incorrect destruction of objects in containers. 00202 * 00203 * Revision 1.12 1993/12/04 05:23:58 robertj 00204 * Added more string functions 00205 * 00206 * Revision 1.11 1993/09/27 16:35:25 robertj 00207 * Fixed bug in sorted lists. 00208 * Changed simple function for array of strings to a constructor. 00209 * Capitalised all macros. 00210 * 00211 * Revision 1.10 1993/08/27 18:17:47 robertj 00212 * Fixed bug with default number of elements in a collection. 00213 * Added missing Compare function to PAbstractSortedList 00214 * Added inline keywords for CFront compatibility. 00215 * 00216 * Revision 1.9 1993/08/21 01:50:33 robertj 00217 * Made Clone() function optional, default will assert if called. 00218 * 00219 * Revision 1.8 1993/08/19 18:00:32 robertj 00220 * Added two more standard base array classes 00221 * 00222 * Revision 1.7 1993/08/01 14:05:27 robertj 00223 * Added const to ToLower() and ToUpper() in the PString class. 00224 * 00225 * Revision 1.6 1993/07/16 14:40:55 robertj 00226 * Added PString constructor for individual characters. 00227 * Added string to C style literal format. 00228 * 00229 * Revision 1.5 1993/07/15 05:02:57 robertj 00230 * Removed redundant word in PString enum for string types. 00231 * 00232 * Revision 1.4 1993/07/15 04:23:39 robertj 00233 * Added constructor to PString to allow conversion from other string formats. 00234 * Fixed problem with variable parameter lists in sprintf() functions. 00235 * 00236 * Revision 1.3 1993/07/14 12:49:16 robertj 00237 * Fixed RCS keywords. 00238 * 00239 */ 00240 00241 #ifndef _CONTAIN_H 00242 #define _CONTAIN_H 00243 00244 #ifdef P_USE_PRAGMA 00245 #pragma interface 00246 #endif 00247 00248 #include <ptlib/object.h> 00249 #include <ptlib/critsec.h> 00250 00252 // Abstract container class 00253 00276 class PContainer : public PObject 00277 { 00278 PCLASSINFO(PContainer, PObject); 00279 00280 public: 00285 PContainer( 00286 PINDEX initialSize = 0 00287 ); 00288 00293 PContainer( 00294 const PContainer & cont 00295 ); 00296 00304 PContainer & operator=( 00305 const PContainer & cont 00306 ); 00307 00312 virtual ~PContainer() 00313 { Destruct(); } 00314 00316 00325 virtual PINDEX GetSize() const; 00326 00340 virtual BOOL SetSize( 00341 PINDEX newSize 00342 ) = 0; 00343 00349 BOOL SetMinSize( 00350 PINDEX minSize 00351 ); 00352 00359 virtual BOOL IsEmpty() const; 00360 00367 BOOL IsUnique() const; 00368 00377 virtual BOOL MakeUnique(); 00379 00380 protected: 00391 PContainer( 00392 int dummy, 00393 const PContainer * cont 00394 ); 00395 00406 virtual void DestroyContents() = 0; 00407 00417 virtual void AssignContents(const PContainer & c); 00418 00430 void CopyContents(const PContainer & c); 00431 00448 void CloneContents(const PContainer * src); 00449 00453 void Destruct(); 00454 00455 00456 class Reference { 00457 public: 00458 inline Reference(PINDEX initialSize) 00459 : size(initialSize), count(1), deleteObjects(TRUE) { } 00460 00461 Reference(const Reference & ref) 00462 : count(1) 00463 { 00464 #if PCONTAINER_USES_CRITSEC 00465 PEnterAndLeave m(((Reference &)ref).critSec); 00466 #endif 00467 size = ref.size; 00468 deleteObjects = ref.deleteObjects; 00469 } 00470 00471 PINDEX size; // Size of what the container contains 00472 PAtomicInteger count; // reference count to the container content - guaranteed to be atomic 00473 BOOL deleteObjects; // Used by PCollection but put here for efficiency 00474 #if PCONTAINER_USES_CRITSEC 00475 PCriticalSection critSec; 00476 #endif 00477 private: 00478 Reference & operator=(const Reference &) 00479 { return *this; } 00480 } * reference; 00481 }; 00482 00483 00484 00532 #define PCONTAINERINFO(cls, par) \ 00533 PCLASSINFO(cls, par) \ 00534 public: \ 00535 cls(const cls & c) : par(c) { CopyContents(c); } \ 00536 cls & operator=(const cls & c) \ 00537 { AssignContents(c); return *this; } \ 00538 virtual ~cls() { Destruct(); } \ 00539 virtual BOOL MakeUnique() \ 00540 { if(par::MakeUnique())return TRUE; CloneContents(this);return FALSE; } \ 00541 protected: \ 00542 cls(int dummy, const cls * c) : par(dummy, c) { CloneContents(c); } \ 00543 virtual void DestroyContents(); \ 00544 void CloneContents(const cls * c); \ 00545 void CopyContents(const cls & c); \ 00546 virtual void AssignContents(const PContainer & c) \ 00547 { par::AssignContents(c); CopyContents((const cls &)c); } 00548 00549 00551 // Abstract collection of objects class 00552 00584 class PCollection : public PContainer 00585 { 00586 PCLASSINFO(PCollection, PContainer); 00587 00588 public: 00593 PCollection( 00594 PINDEX initialSize = 0 00595 ); 00597 00613 virtual void PrintOn( 00614 ostream &strm 00615 ) const; 00617 00629 virtual PINDEX Append( 00630 PObject * obj 00631 ) = 0; 00632 00649 virtual PINDEX Insert( 00650 const PObject & before, 00651 PObject * obj 00652 ) = 0; 00653 00665 virtual PINDEX InsertAt( 00666 PINDEX index, 00667 PObject * obj 00668 ) = 0; 00669 00679 virtual BOOL Remove( 00680 const PObject * obj 00681 ) = 0; 00682 00691 virtual PObject * RemoveAt( 00692 PINDEX index 00693 ) = 0; 00694 00701 virtual void RemoveAll(); 00702 00716 virtual BOOL SetAt( 00717 PINDEX index, 00718 PObject * val 00719 ) = 0; 00720 00726 virtual PObject * GetAt( 00727 PINDEX index 00728 ) const = 0; 00729 00736 virtual PINDEX GetObjectsIndex( 00737 const PObject * obj 00738 ) const = 0; 00739 00748 virtual PINDEX GetValuesIndex( 00749 const PObject & obj 00750 ) const = 0; 00751 00765 PINLINE void AllowDeleteObjects( 00766 BOOL yes = TRUE 00767 ); 00768 00772 void DisallowDeleteObjects(); 00774 00775 protected: 00786 PINLINE PCollection( 00787 int dummy, 00788 const PCollection * coll 00789 ); 00790 }; 00791 00792 00793 00795 // The abstract array class 00796 00797 #include <ptlib/array.h> 00798 00800 // The abstract array class 00801 00802 #include <ptlib/lists.h> 00803 00805 // PString class (specialised version of PBASEARRAY(char)) 00806 00807 #include <ptlib/dict.h> 00808 00809 00811 // PString class 00812 00813 #include <ptlib/pstring.h> 00814 00815 00816 00818 // Fill in all the inline functions 00819 00820 #if P_USE_INLINES 00821 #include <ptlib/contain.inl> 00822 #endif 00823 00824 #endif // _CONTAIN_H 00825 00826 00827 // End Of File ///////////////////////////////////////////////////////////////