Mercurial > fife-parpg
comparison engine/core/util/resource/resource_ptr.h @ 150:6e7d228def30
Lazy loading for animations.
Uses a Resource-Pointer class that behaves like
a pointer to an IResource, but only loads the
resource on demand.
There's a slight change of API, which is already
adapted to in the XML loaders. If you use
Animation.addFrame directly, either adapt to the
change or wait a few hours for a backwards compatibility
solution.
author | phoku@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sat, 11 Oct 2008 12:03:59 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
149:823544621b5b | 150:6e7d228def30 |
---|---|
1 /*************************************************************************** | |
2 * Copyright (C) 2005-2008 by the FIFE team * | |
3 * http://www.fifengine.de * | |
4 * This file is part of FIFE. * | |
5 * * | |
6 * FIFE is free software; you can redistribute it and/or * | |
7 * modify it under the terms of the GNU Lesser General Public * | |
8 * License as published by the Free Software Foundation; either * | |
9 * version 2.1 of the License, or (at your option) any later version. * | |
10 * * | |
11 * This library is distributed in the hope that it will be useful, * | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * | |
14 * Lesser General Public License for more details. * | |
15 * * | |
16 * You should have received a copy of the GNU Lesser General Public * | |
17 * License along with this library; if not, write to the * | |
18 * Free Software Foundation, Inc., * | |
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * | |
20 ***************************************************************************/ | |
21 | |
22 #ifndef FIFE_RESOURE_PTR_H | |
23 #define FIFE_RESOURE_PTR_H | |
24 | |
25 // Standard C++ library includes | |
26 | |
27 // 3rd party library includes | |
28 | |
29 // FIFE includes | |
30 // These includes are split up in two parts, separated by one empty line | |
31 // First block: files included from the FIFE root src directory | |
32 // Second block: files included from the same folder | |
33 #include "resource.h" | |
34 #include "pool.h" | |
35 | |
36 namespace FIFE { | |
37 | |
38 /** A lazy loading resource. | |
39 */ | |
40 class ResourcePtr { | |
41 public: | |
42 ResourcePtr(IResource* ptr = 0) : m_ptr(ptr),m_pool(0),m_index(Pool::INVALID_ID) { | |
43 if( m_ptr ) | |
44 m_ptr->addRef(); | |
45 } | |
46 | |
47 ResourcePtr(Pool* pool,int index) : m_ptr(0),m_pool(pool),m_index(index) { | |
48 } | |
49 | |
50 ResourcePtr(const ResourcePtr& r) | |
51 : m_ptr(r.m_ptr),m_pool(r.m_pool),m_index(r.m_index) { | |
52 if(m_ptr) { | |
53 m_ptr->addRef(); | |
54 } | |
55 } | |
56 | |
57 ResourcePtr& operator=(const ResourcePtr& r) { | |
58 if( this == &r ) | |
59 return *this; | |
60 release(); | |
61 m_ptr = r.m_ptr; | |
62 m_pool = r.m_pool; | |
63 m_index = r.m_index; | |
64 if(m_ptr) { | |
65 m_ptr->addRef(); | |
66 } | |
67 return *this; | |
68 } | |
69 | |
70 ~ResourcePtr() { | |
71 release(); | |
72 } | |
73 | |
74 IResource& operator->() { | |
75 load(); | |
76 return *m_ptr; | |
77 } | |
78 | |
79 const IResource& operator->() const { | |
80 constLoad(); | |
81 return *m_ptr; | |
82 } | |
83 | |
84 operator bool() const { | |
85 return isValid(); | |
86 } | |
87 | |
88 bool isValid() const { | |
89 return m_ptr || isLoadable(); | |
90 } | |
91 | |
92 bool isLoadable() const { | |
93 return m_pool && m_index != Pool::INVALID_ID; | |
94 } | |
95 | |
96 bool operator==(const ResourcePtr& r) const { | |
97 if( isLoadable() && r.isLoadable() ) | |
98 return m_index == r.m_index && m_pool == r.m_pool; | |
99 if( !isLoadable() && !r.isLoadable() ) | |
100 return m_ptr == r.m_ptr; | |
101 return false; | |
102 } | |
103 | |
104 bool operator<(const ResourcePtr& r) const { | |
105 if( isLoadable() && r.isLoadable() ) | |
106 { | |
107 if( m_pool == r.m_pool ) | |
108 return m_index < r.m_index; | |
109 return m_pool < r.m_pool; | |
110 } | |
111 if( !isLoadable() && !r.isLoadable() ) | |
112 return m_ptr < r.m_ptr; | |
113 return isLoadable() < r.isLoadable(); | |
114 } | |
115 | |
116 void release() { | |
117 if(m_ptr) { | |
118 m_ptr->decRef(); | |
119 } | |
120 m_ptr = 0; | |
121 } | |
122 | |
123 void load() { | |
124 constLoad(); | |
125 } | |
126 | |
127 void unload() { | |
128 release(); | |
129 if( isLoadable() ) { | |
130 m_pool->release(m_index); | |
131 } | |
132 } | |
133 | |
134 template<class T> | |
135 T* get() { | |
136 if( isValid() ) | |
137 load(); | |
138 return dynamic_cast<T*>(m_ptr); | |
139 } | |
140 | |
141 private: | |
142 void constLoad() const { | |
143 if( m_ptr ) | |
144 return; | |
145 m_ptr = &m_pool->get(m_index); | |
146 m_ptr->addRef(); | |
147 } | |
148 mutable IResource* m_ptr; | |
149 Pool* m_pool; | |
150 int m_index; | |
151 }; | |
152 } | |
153 #endif | |
154 |