Bromeon
Resources/ResourceManager.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_RESOURCEMANAGER_HPP
00030 #define THOR_RESOURCEMANAGER_HPP
00031 
00032 #include <Thor/Resources/ResourcePtr.hpp>
00033 #include <Thor/Resources/ResourceStrategies.hpp>
00034 #include <Thor/Resources/ResourceKeyTraits.hpp>
00035 #include <Thor/SmartPtr/MovedPtr.hpp>
00036 #include <Thor/Tools/Exceptions.hpp>
00037 #include <Thor/Tools/NonCopyable.hpp>
00038 #include <Thor/Tools/ForEach.hpp>
00039 #include <Thor/Detail/ResourceSlot.hpp>
00040 #include <Thor/Config.hpp>
00041 
00042 #include THOR_TR1_HEADER(functional)
00043 #include THOR_TR1_HEADER(type_traits)
00044 #include <map>
00045 
00046 
00047 namespace thor
00048 {
00049 
00052 
00065 template <class Resource, class ResourceKey = typename Resources::KeyTraits<Resource>::Type >
00066 class ResourceManager : private NonCopyable
00067 {
00068     // ---------------------------------------------------------------------------------------------------------------------------
00069     // Public member functions
00070     public:
00074                                     ResourceManager();
00075             
00080         ResourcePtr<Resource>       Search(const ResourceKey& key);
00081         
00086         ResourcePtr<const Resource> Search(const ResourceKey& key) const;
00087 
00096         ResourcePtr<Resource>       Acquire(const ResourceKey& key);
00097 
00106         bool                        Release(const ResourceKey& key);
00107 
00110         void                        SetLoadingFailureStrategy(Resources::LoadingFailureStrategy strategy);
00111 
00116         void                        SetReleaseStrategy(Resources::ReleaseStrategy strategy);
00117 
00118 
00119     // ---------------------------------------------------------------------------------------------------------------------------
00120     // Private types
00121     private:
00122         typedef typename std::tr1::remove_const<Resource>::type     MutableResource;
00123         typedef detail::ResourceSlot<MutableResource>               ResourceSlot;
00124         typedef std::map<ResourceKey, ResourceSlot>                 ResourceMap;
00125         typedef typename ResourceMap::iterator                      SlotIterator;
00126         typedef typename ResourceMap::const_iterator                SlotConstIterator;
00127 
00128 
00129     // ---------------------------------------------------------------------------------------------------------------------------
00130     // Private member functions
00131     private:
00132         // Loads and inserts a resource.
00133         ResourcePtr<Resource>                   AddResource(const ResourceKey& key);
00134     
00135         // Unloads and erases a resource.
00136         void                                    RemoveResource(SlotIterator itr);
00137         
00138 
00139     // ---------------------------------------------------------------------------------------------------------------------------
00140     // Private variables
00141     private:
00142         std::tr1::shared_ptr<ResourceMap>       mMap;
00143         Resources::ReleaseStrategy              mReleaseStrategy;
00144         Resources::LoadingFailureStrategy       mLoadingFailureStrategy;
00145 
00146 
00147     // ---------------------------------------------------------------------------------------------------------------------------
00148     // Friends
00149     friend class detail::ResourceDeleter<MutableResource, ResourceKey>;
00150 };
00151 
00153 
00154 } // namespace thor
00155 
00156 #include <Thor/Detail/ResourceManager.inl>
00157 #endif // THOR_RESOURCEMANAGER_HPP