![]() |
00001 00002 // 00003 // Thor C++ Library 00004 // Copyright (c) 2011-2015 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_RESOURCEHOLDER_HPP 00030 #define THOR_RESOURCEHOLDER_HPP 00031 00032 #include <Thor/Resources/KnownIdStrategy.hpp> 00033 #include <Thor/Resources/OwnershipModels.hpp> 00034 #include <Thor/Resources/ResourceExceptions.hpp> 00035 #include <Thor/Resources/ResourceLoader.hpp> 00036 00037 #include <Aurora/Tools/NonCopyable.hpp> 00038 00039 #include <memory> 00040 #include <map> 00041 00042 00043 namespace thor 00044 { 00045 00048 00059 template <typename R, typename I, class O = Resources::CentralOwner> 00060 class ResourceHolder : private aurora::NonCopyable 00061 { 00062 // --------------------------------------------------------------------------------------------------------------------------- 00063 // Private types 00064 private: 00065 // Abbreviate class containing ownership policy types and functions 00066 typedef typename detail::OwnershipModel<O, R> Om; 00067 00068 00069 // --------------------------------------------------------------------------------------------------------------------------- 00070 // Public types 00071 public: 00077 typedef typename Om::Returned Resource; 00078 00084 typedef typename Om::ConstReturned ConstResource; 00085 00086 00087 // --------------------------------------------------------------------------------------------------------------------------- 00088 // Public member functions 00089 public: 00092 ResourceHolder(); 00093 00096 ResourceHolder(ResourceHolder&& source); 00097 00100 ResourceHolder& operator= (ResourceHolder&& source); 00101 00109 Resource acquire(const I& id, const ResourceLoader<R>& how, Resources::KnownIdStrategy known = Resources::AssumeNew); 00110 00117 void release(const I& id); 00118 00124 Resource operator[] (const I& id); 00125 00131 ConstResource operator[] (const I& id) const; 00132 00133 00134 // --------------------------------------------------------------------------------------------------------------------------- 00135 // Private member functions 00136 private: 00137 // Load resource (must be new) 00138 Resource load(const I& id, const ResourceLoader<R>& how); 00139 00140 00141 // --------------------------------------------------------------------------------------------------------------------------- 00142 // Private variables 00143 private: 00144 std::map<I, typename Om::Stored> mMap; 00145 }; 00146 00148 00149 } // namespace thor 00150 00151 #include <Thor/Resources/Detail/ResourceHolder.inl> 00152 #endif // THOR_RESOURCEHOLDER_HPP