00001 /* 00002 * psharedptr.h 00003 * 00004 * SharedPtr template 00005 * 00006 * Portable Windows Library 00007 * 00008 * Copyright (C) 2004 Post Increment 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 Post Increment 00023 * 00024 * Contributor(s): ______________________________________. 00025 * 00026 * $Log: psharedptr.h,v $ 00027 * Revision 1.3 2004/10/21 09:20:33 csoutheren 00028 * Fixed compile problems on gcc 2.95.x 00029 * 00030 * Revision 1.2 2004/10/01 08:08:50 csoutheren 00031 * Added Reset and auto_ptr conversions 00032 * 00033 * Revision 1.1 2004/10/01 07:17:18 csoutheren 00034 * Added PSharedptr class 00035 * 00036 */ 00037 00038 #ifndef _PSHAREDPTR_H 00039 #define _PSHAREDPTR_H 00040 00041 #ifdef P_USE_PRAGMA 00042 #pragma interface 00043 #endif 00044 00045 #include <ptlib.h> 00046 #include <memory> 00047 00056 template <class T> 00057 class PSharedPtr : public PContainer 00058 { 00059 PCLASSINFO(PSharedPtr, PContainer); 00060 public: 00061 typedef T element_type; 00062 00063 PSharedPtr(element_type * _ptr = NULL) 00064 { ptr = _ptr; } 00065 00066 PSharedPtr(const PSharedPtr & c) 00067 : PContainer(c) 00068 { CopyContents(c); } 00069 00070 PSharedPtr(std::auto_ptr<element_type> & v) 00071 { ptr = v.release(); } 00072 00073 PSharedPtr & operator=(const PSharedPtr & c) 00074 { AssignContents(c); return *this; } 00075 00076 virtual ~PSharedPtr() 00077 { Destruct(); } 00078 00079 virtual BOOL MakeUnique() 00080 { if (PContainer::MakeUnique()) return TRUE; CloneContents(this); return FALSE; } 00081 00082 BOOL SetSize(PINDEX) 00083 { return false; } 00084 00085 T * Get() const 00086 { return ptr; } 00087 00088 void Reset() const 00089 { AssignContents(PSharedPtr()); } 00090 00091 T & operator*() const 00092 { return *ptr; } 00093 00094 T * operator->() const 00095 { return ptr; } 00096 00097 00098 protected: 00099 PSharedPtr(int dummy, const PSharedPtr * c) 00100 : PContainer(dummy, c) 00101 { CloneContents(c); } 00102 00103 void AssignContents(const PContainer & c) 00104 { PContainer::AssignContents(c); CopyContents((const PSharedPtr &)c); } 00105 00106 void DestroyContents() 00107 { delete(ptr); } 00108 00109 void CloneContents(const PContainer * src) 00110 { ptr = new element_type(*((const PSharedPtr *)src)->ptr); } 00111 00112 void CopyContents(const PContainer & c) 00113 { ptr = ((const PSharedPtr &)c).ptr; } 00114 00115 protected: 00116 T * ptr; 00117 }; 00118 00119 #endif // _PSHAREDPTR_H 00120