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_RESOURCEMANAGER_HPP 00030 #define THOR_RESOURCEMANAGER_HPP 00031 00032 #include <Thor/Resources/ResourceStrategies.hpp> 00033 #include <Thor/Resources/ResourceKeyTraits.hpp> 00034 #include <Thor/Resources/ResourcePtr.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 00079 ~ResourceManager(); 00080 00085 ResourcePtr<Resource> Search(const ResourceKey& key); 00086 00091 ResourcePtr<const Resource> Search(const ResourceKey& key) const; 00092 00101 ResourcePtr<Resource> Acquire(const ResourceKey& key); 00102 00111 bool Release(const ResourceKey& key); 00112 00115 void SetLoadingFailureStrategy(Resources::LoadingFailureStrategy strategy); 00116 00121 void SetReleaseStrategy(Resources::ReleaseStrategy strategy); 00122 00123 00124 // --------------------------------------------------------------------------------------------------------------------------- 00125 // Private types 00126 private: 00127 typedef typename std::tr1::remove_const<Resource>::type MutableResource; 00128 typedef detail::ResourceSlot<MutableResource, ResourceKey> ResourceSlot; 00129 typedef std::map<ResourceKey, ResourceSlot*> ResourceMap; 00130 typedef typename ResourceMap::iterator SlotIterator; 00131 typedef typename ResourceMap::const_iterator SlotConstIterator; 00132 00133 00134 // --------------------------------------------------------------------------------------------------------------------------- 00135 // Private member functions 00136 private: 00137 // Loads and inserts a resource. Sets itr to the inserted resource. 00138 ResourcePtr<Resource> AddResource(SlotIterator itr, const ResourceKey& key); 00139 00140 // Unloads and releases a resource. 00141 // If considerAutoRelease is true, the resource is only released if auto-release is enabled. 00142 void RemoveResource(SlotIterator itr); 00143 00144 00145 // --------------------------------------------------------------------------------------------------------------------------- 00146 // Private variables 00147 private: 00148 ResourceMap mMap; 00149 Resources::ReleaseStrategy mReleaseStrategy; 00150 Resources::LoadingFailureStrategy mLoadingFailureStrategy; 00151 00152 00153 // --------------------------------------------------------------------------------------------------------------------------- 00154 // Friends 00155 friend class detail::ResourceSlot<MutableResource, ResourceKey>; 00156 }; 00157 00159 00160 } // namespace thor 00161 00162 #include <Thor/Detail/ResourceManager.inl> 00163 #endif // THOR_RESOURCEMANAGER_HPP