ResourceHolder.hpp
Go to the documentation of this file.
1 //
3 // Thor C++ Library
4 // Copyright (c) 2011-2016 Jan Haller
5 //
6 // This software is provided 'as-is', without any express or implied
7 // warranty. In no event will the authors be held liable for any damages
8 // arising from the use of this software.
9 //
10 // Permission is granted to anyone to use this software for any purpose,
11 // including commercial applications, and to alter it and redistribute it
12 // freely, subject to the following restrictions:
13 //
14 // 1. The origin of this software must not be misrepresented; you must not
15 // claim that you wrote the original software. If you use this software
16 // in a product, an acknowledgment in the product documentation would be
17 // appreciated but is not required.
18 //
19 // 2. Altered source versions must be plainly marked as such, and must not be
20 // misrepresented as being the original software.
21 //
22 // 3. This notice may not be removed or altered from any source distribution.
23 //
25 
28 
29 #ifndef THOR_RESOURCEHOLDER_HPP
30 #define THOR_RESOURCEHOLDER_HPP
31 
36 
37 #include <Aurora/Tools/NonCopyable.hpp>
38 
39 #include <memory>
40 #include <map>
41 
42 
43 namespace thor
44 {
45 
48 
59 template <typename R, typename I, class O = Resources::CentralOwner>
60 class ResourceHolder : private aurora::NonCopyable
61 {
62  // ---------------------------------------------------------------------------------------------------------------------------
63  // Private types
64  private:
65  // Abbreviate class containing ownership policy types and functions
66  typedef typename detail::OwnershipModel<O, R> Om;
67 
68 
69  // ---------------------------------------------------------------------------------------------------------------------------
70  // Public types
71  public:
77  typedef typename Om::Returned Resource;
78 
84  typedef typename Om::ConstReturned ConstResource;
85 
86 
87  // ---------------------------------------------------------------------------------------------------------------------------
88  // Public member functions
89  public:
93 
97 
101 
109  Resource acquire(const I& id, const ResourceLoader<R>& how, Resources::KnownIdStrategy known = Resources::AssumeNew);
110 
117  void release(const I& id);
118 
124  Resource operator[] (const I& id);
125 
131  ConstResource operator[] (const I& id) const;
132 
133 
134  // ---------------------------------------------------------------------------------------------------------------------------
135  // Private member functions
136  private:
137  // Load resource (must be new)
138  Resource load(const I& id, const ResourceLoader<R>& how);
139 
140 
141  // ---------------------------------------------------------------------------------------------------------------------------
142  // Private variables
143  private:
144  std::map<I, typename Om::Stored> mMap;
145 };
146 
148 
149 } // namespace thor
150 
151 #include <Thor/Resources/Detail/ResourceHolder.inl>
152 #endif // THOR_RESOURCEHOLDER_HPP
Classes thor::ResourceLoadingException, thor::ResourceAccessException.
Ownership models for thor::ResourceHolder.
void release(const I &id)
Unloads the resource currently identified as id.
Enum KnownIdStrategy, used by thor::ResourceHolder.
Loads a new resource if the ID is unknown. Otherwise, a ResourceAccessException is thrown...
Definition: KnownIdStrategy.hpp:47
KnownIdStrategy
Strategy to deal with already known resource IDs.
Definition: KnownIdStrategy.hpp:45
Definition: AnimationMap.hpp:42
ResourceHolder()
Default constructor.
Om::ConstReturned ConstResource
Resource handle for constant access.
Definition: ResourceHolder.hpp:84
Class that is responsible of the management of resources like images, fonts or music.
Definition: ResourceHolder.hpp:60
Resource acquire(const I &id, const ResourceLoader< R > &how, Resources::KnownIdStrategy known=Resources::AssumeNew)
Loads a new resource, identified as id.
Resource operator[](const I &id)
Accesses a resource using the identifier id.
ResourceHolder & operator=(ResourceHolder &&source)
Move assignment operator.
Class storing loading information for resources.
Definition: ResourceLoader.hpp:48
Class template thor::ResourceLoader.
Om::Returned Resource
Resource handle for mutable access.
Definition: ResourceHolder.hpp:77