autoptr.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2009 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib may link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 */
00028 
00029 #pragma once
00030 
00031 #include "../api_core.h"
00032 
00035 
00036 template<typename Type>
00040 class CL_AutoPtr
00041 {
00042 public:
00043         CL_AutoPtr()
00044         : ptr(0)
00045         {
00046         }
00047 
00048         CL_AutoPtr<Type>(CL_AutoPtr<Type> &other)
00049         : ptr(other.release())
00050         {
00051         }
00052 
00053         explicit CL_AutoPtr<Type>(Type *ptr)
00054         : ptr(ptr)
00055         {
00056         }
00057 
00058         ~CL_AutoPtr()
00059         {
00060                 delete ptr;
00061         }
00062 
00063         CL_AutoPtr<Type> &operator =(CL_AutoPtr<Type> &other)
00064         {
00065                 if (this != &other)
00066                         reset(other.release());
00067                 return *this;
00068         }
00069 
00070         CL_AutoPtr<Type> &operator =(Type *ptr)
00071         {
00072                 reset(ptr);
00073                 return *this;
00074         }
00075 
00076         void reset(Type *p = 0)
00077         {
00078                 delete ptr;
00079                 ptr = p;
00080         }
00081 
00082         Type *release()
00083         {
00084                 Type *p = ptr;
00085                 ptr = 0;
00086                 return p;
00087         }
00088 
00089         Type *get() { return ptr; }
00090         const Type *get() const { return ptr; }
00091 
00092         operator Type *() { return get(); }
00093         operator const Type *() const { return get(); }
00094 
00095         Type *operator->() { return get(); }
00096         const Type *operator->() const { return get(); }
00097 
00098 private:
00099         Type *ptr;
00100 };
00101 
00102 template<typename Container>
00103 void cl_delete_container(Container &container)
00104 {
00105         for (typename Container::iterator it = container.begin(); it != container.end(); ++it)
00106                 delete (*it);
00107         container.clear();
00108 }
00109 

Generated on Thu Dec 3 02:39:28 2009 for ClanLib by  doxygen 1.4.6