psharedptr.h

Go to the documentation of this file.
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  * $Revision: 20385 $
00027  * $Author: rjongbloed $
00028  * $Date: 2008-06-04 10:40:38 +0000 (Wed, 04 Jun 2008) $
00029  */
00030 
00031 #ifndef _PSHAREDPTR_H
00032 #define _PSHAREDPTR_H
00033 
00034 #ifdef P_USE_PRAGMA
00035 #pragma interface
00036 #endif
00037 
00038 #include <ptlib.h>
00039 #include <memory>
00040 
00049 template <class T>
00050 class PSharedPtr : public PContainer
00051 {
00052   PCLASSINFO(PSharedPtr, PContainer);
00053   public:
00054     typedef T element_type;
00055 
00056     PSharedPtr(element_type * _ptr = NULL)
00057     { ptr = _ptr; }
00058 
00059     PSharedPtr(const PSharedPtr & c)
00060       : PContainer(c)
00061     { CopyContents(c); } 
00062 
00063     PSharedPtr(std::auto_ptr<element_type> & v)
00064     { ptr = v.release(); }
00065 
00066     PSharedPtr & operator=(const PSharedPtr & c) 
00067     { AssignContents(c); return *this; } 
00068 
00069     virtual ~PSharedPtr()
00070     { Destruct(); } 
00071 
00072     virtual PBoolean MakeUnique() 
00073     { if (PContainer::MakeUnique()) return PTrue; CloneContents(this); return PFalse; } 
00074 
00075     PBoolean SetSize(PINDEX)
00076     { return false; }
00077 
00078     T * Get() const
00079     { return ptr; }
00080 
00081     void Reset() const
00082     { AssignContents(PSharedPtr()); }
00083 
00084     T & operator*() const
00085     { return *ptr; }
00086 
00087     T * operator->() const
00088     { return ptr; }
00089 
00090 
00091   protected: 
00092     PSharedPtr(int dummy, const PSharedPtr * c)
00093     : PContainer(dummy, c)
00094     { CloneContents(c); } 
00095 
00096     void AssignContents(const PContainer & c) 
00097     { PContainer::AssignContents(c); CopyContents((const PSharedPtr &)c); }
00098 
00099     void DestroyContents()
00100     { delete(ptr); }
00101 
00102     void CloneContents(const PContainer * src)
00103     { ptr = new element_type(*((const PSharedPtr *)src)->ptr); }
00104 
00105     void CopyContents(const PContainer & c)
00106     { ptr = ((const PSharedPtr &)c).ptr; }
00107 
00108   protected:
00109     T * ptr;
00110 };
00111 
00112 #endif // _PSHAREDPTR_H
00113 

Generated on Mon Sep 15 01:21:35 2008 for PTLib by  doxygen 1.5.1