Bromeon
Resources/ResourcePtr.hpp
Go to the documentation of this file.
00001 
00002 //
00003 // Thor C++ Library
00004 // Copyright (c) 2011 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/ResourceSlotBase.hpp>
00033 #include <Thor/Detail/SafeBoolIdiom.hpp>
00034 #include <Thor/Detail/Metaprogramming.hpp>
00035 #include <Thor/Config.hpp>
00036 
00037 #include THOR_TR1_HEADER(type_traits)
00038 #include <algorithm>
00039 #include <cassert>
00040 
00041 
00042 namespace thor
00043 {
00044 
00047 
00058 template <class Resource>
00059 class ResourcePtr
00060 {
00061     // ---------------------------------------------------------------------------------------------------------------------------
00062     // Private types
00063     private:
00064         // Resource without const qualifier
00065         typedef typename std::tr1::remove_const<Resource>::type         MutableResource;
00066 
00067         // ResourceSlot with the same const qualification as Resource
00068         typedef typename detail::Conditional<
00069             std::tr1::is_const<Resource>::value,
00070             const detail::ResourceSlotBase<MutableResource>, 
00071             detail::ResourceSlotBase<MutableResource> >::Type           ResourceSlot;
00072 
00073 
00074     // ---------------------------------------------------------------------------------------------------------------------------
00075     // Public member functions
00076     public:
00079                                 ResourcePtr();
00080     
00083                                 ResourcePtr(const ResourcePtr& origin);
00084                                 
00087         template <class Resource2>
00088                                 ResourcePtr(const ResourcePtr<Resource2>& origin);
00089 
00092         ResourcePtr&            operator= (const ResourcePtr& origin);
00093 
00094 
00097         template <class Resource2>
00098         ResourcePtr&            operator= (const ResourcePtr<Resource2>& origin);
00099 
00103                                 ~ResourcePtr();
00104 
00108         void                    Swap(ResourcePtr& other);
00109 
00113         void                    Reset();
00114         
00120                                 operator THOR_DETAIL SafeBool() const;
00121 
00124         Resource&               operator* () const;
00125         
00128         Resource*               operator-> () const;
00129 
00130 
00131     // ---------------------------------------------------------------------------------------------------------------------------
00132     // Implementation details
00133     public:
00134         // Constructor from resource slot, internally used by ResourceManager
00135         explicit                ResourcePtr(ResourceSlot& slot);
00136 
00137     
00138     // ---------------------------------------------------------------------------------------------------------------------------
00139     // Private member functions
00140     private:        // Increases the reference counter, if required.
00141         void                    IncRef();
00142         
00143         // Decreases the reference counter, if required.    
00144         void                    DecRef();
00145         
00146         // Sets the internal pointer to NULL without affecting the reference counter.
00147         void                    Invalidate();
00148         
00149 
00150     // ---------------------------------------------------------------------------------------------------------------------------
00151     // Private variables
00152     private:
00153         ResourceSlot*           mSlot;
00154 
00155 
00156     // ---------------------------------------------------------------------------------------------------------------------------
00157     // Friends
00159     template <class Resource1, class Resource2>
00160     friend bool operator== (const ResourcePtr<Resource1>& lhs, const ResourcePtr<Resource2>& rhs);
00161     
00162     template <class Resource2>
00163     friend class ResourcePtr;
00164     
00165     friend class detail::ResourceSlotBase<MutableResource>;
00167 };
00168 
00171 template <class Resource>
00172 void swap(ResourcePtr<Resource>& lhs, ResourcePtr<Resource>& rhs);
00173 
00178 template <class Resource1, class Resource2>
00179 bool operator== (const ResourcePtr<Resource1>& lhs, const ResourcePtr<Resource2>& rhs);
00180 
00185 template <class Resource1, class Resource2>
00186 bool operator!= (const ResourcePtr<Resource1>& lhs, const ResourcePtr<Resource2>& rhs);
00187 
00189 
00190 } // namespace thor
00191 
00192 #include <Thor/Detail/ResourcePtr.inl>
00193 #endif // THOR_RESOURCEPTR_HPP