Bromeon
Resources/ResourcePtr.hpp
Go to the documentation of this file.
00001 
00002 //
00003 // Thor C++ Library
00004 // Copyright (c) 2011-2012 Jan Haller
00005 // 
00006 // This software is provided 'as-is', without any express or implied
00007 // warranty. In no event will the authors be held liable for any damages
00008 // arising from the use of this software.
00009 // 
00010 // Permission is granted to anyone to use this software for any purpose,
00011 // including commercial applications, and to alter it and redistribute it
00012 // freely, subject to the following restrictions:
00013 // 
00014 // 1. The origin of this software must not be misrepresented; you must not
00015 //    claim that you wrote the original software. If you use this software
00016 //    in a product, an acknowledgment in the product documentation would be
00017 //    appreciated but is not required.
00018 // 
00019 // 2. Altered source versions must be plainly marked as such, and must not be
00020 //    misrepresented as being the original software.
00021 // 
00022 // 3. This notice may not be removed or altered from any source distribution.
00023 //
00025 
00028 
00029 #ifndef THOR_RESOURCEPTR_HPP
00030 #define THOR_RESOURCEPTR_HPP
00031 
00032 #include <Thor/Detail/SafeBoolIdiom.hpp>
00033 #include <Thor/Config.hpp>
00034 
00035 #include THOR_TR1_HEADER(memory)
00036 #include <cassert>
00037 
00038 
00039 namespace thor
00040 {
00041 
00044 
00054 template <class Resource>
00055 class ResourcePtr
00056 {
00057     // ---------------------------------------------------------------------------------------------------------------------------
00058     // Public member functions
00059     public:
00062                                 ResourcePtr();
00063 
00067         explicit                ResourcePtr(Resource* rawPointer);
00068 
00074         template <typename D>
00075                                 ResourcePtr(Resource* rawPointer, D deleter);
00076 
00079                                 ResourcePtr(const ResourcePtr& origin);
00080                                 
00083         template <class Resource2>
00084                                 ResourcePtr(const ResourcePtr<Resource2>& origin);
00085 
00088         ResourcePtr&            operator= (const ResourcePtr& origin);
00089 
00092         template <class Resource2>
00093         ResourcePtr&            operator= (const ResourcePtr<Resource2>& origin);
00094 
00098                                 ~ResourcePtr();
00099 
00101         void                    Swap(ResourcePtr& other);
00102 
00105         void                    Reset();
00106 
00110         void                    Reset(Resource* rawPointer);
00111 
00117         template <typename D>
00118         void                    Reset(Resource* rawPointer, D deleter);
00119 
00125                                 operator THOR_DETAIL SafeBool() const;
00126 
00129         Resource&               operator* () const;
00130         
00133         Resource*               operator-> () const;
00134     
00135 
00136     // ---------------------------------------------------------------------------------------------------------------------------
00137     // Implementation details
00138     public:
00139         // Construct from shared_ptr
00140         explicit                ResourcePtr(std::tr1::shared_ptr<Resource> sharedPtr);
00141 
00142 
00143     // ---------------------------------------------------------------------------------------------------------------------------
00144     // Private variables
00145     private:
00146         std::tr1::shared_ptr<Resource>  mPointer;
00147 
00148 
00149     // ---------------------------------------------------------------------------------------------------------------------------
00150     // Friends
00152     template <class Resource1, class Resource2>
00153     friend bool operator== (const ResourcePtr<Resource1>& lhs, const ResourcePtr<Resource2>& rhs);
00154     
00155     template <class Resource2>
00156     friend class ResourcePtr;
00158 };
00159 
00162 template <class Resource>
00163 void swap(ResourcePtr<Resource>& lhs, ResourcePtr<Resource>& rhs);
00164 
00169 template <class Resource1, class Resource2>
00170 bool operator== (const ResourcePtr<Resource1>& lhs, const ResourcePtr<Resource2>& rhs);
00171 
00176 template <class Resource1, class Resource2>
00177 bool operator!= (const ResourcePtr<Resource1>& lhs, const ResourcePtr<Resource2>& rhs);
00178 
00185 template <class Resource>
00186 ResourcePtr<Resource> NoDeletePtr(Resource* pointer);
00187 
00189 
00190 } // namespace thor
00191 
00192 #include <Thor/Detail/ResourcePtr.inl>
00193 #endif // THOR_RESOURCEPTR_HPP