1 ////////////////////////////////////////////////////////////
\r
3 // SFML - Simple and Fast Multimedia Library
\r
4 // Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
\r
6 // This software is provided 'as-is', without any express or implied warranty.
\r
7 // In no event will the authors be held liable for any damages arising from the use of this software.
\r
9 // Permission is granted to anyone to use this software for any purpose,
\r
10 // including commercial applications, and to alter it and redistribute it freely,
\r
11 // subject to the following restrictions:
\r
13 // 1. The origin of this software must not be misrepresented;
\r
14 // you must not claim that you wrote the original software.
\r
15 // If you use this software in a product, an acknowledgment
\r
16 // in the product documentation would be appreciated but is not required.
\r
18 // 2. Altered source versions must be plainly marked as such,
\r
19 // and must not be misrepresented as being the original software.
\r
21 // 3. This notice may not be removed or altered from any source distribution.
\r
23 ////////////////////////////////////////////////////////////
\r
25 #ifndef SFML_RESOURCE_HPP
\r
26 #define SFML_RESOURCE_HPP
\r
28 ////////////////////////////////////////////////////////////
\r
30 ////////////////////////////////////////////////////////////
\r
36 ////////////////////////////////////////////////////////////
\r
37 // These two classes are defined in the same header because
\r
38 // they depend on each other. And as they're template classes,
\r
39 // they must be entirely defined in header files, which
\r
40 // prevents from proper separate compiling
\r
41 ////////////////////////////////////////////////////////////
\r
43 template <typename> class ResourcePtr;
\r
45 ////////////////////////////////////////////////////////////
\r
46 /// Base class for every resource that needs to notify
\r
47 /// dependent classes about its destruction
\r
48 ////////////////////////////////////////////////////////////
\r
49 template <typename T>
\r
54 ////////////////////////////////////////////////////////////
\r
55 /// Default constructor
\r
57 ////////////////////////////////////////////////////////////
\r
60 ////////////////////////////////////////////////////////////
\r
61 /// Copy constructor
\r
63 /// \param Copy : Resource to copy
\r
65 ////////////////////////////////////////////////////////////
\r
66 Resource(const Resource<T>& Copy);
\r
68 ////////////////////////////////////////////////////////////
\r
71 ////////////////////////////////////////////////////////////
\r
74 ////////////////////////////////////////////////////////////
\r
75 /// Assignment operator
\r
77 /// \param Other : Resource to copy
\r
79 /// \return Reference to this
\r
81 ////////////////////////////////////////////////////////////
\r
82 Resource<T>& operator =(const Resource<T>& Other);
\r
86 friend class ResourcePtr<T>;
\r
88 ////////////////////////////////////////////////////////////
\r
89 /// Connect a ResourcePtr to this resource
\r
91 /// \param Observer : Observer to add
\r
93 ////////////////////////////////////////////////////////////
\r
94 void Connect(ResourcePtr<T>& Observer) const;
\r
96 ////////////////////////////////////////////////////////////
\r
97 /// Disconnect a ResourcePtr from this resource
\r
99 /// \param Observer : Observer to remove
\r
101 ////////////////////////////////////////////////////////////
\r
102 void Disconnect(ResourcePtr<T>& Observer) const;
\r
104 ////////////////////////////////////////////////////////////
\r
106 ////////////////////////////////////////////////////////////
\r
107 mutable std::set<ResourcePtr<T>*> myObservers;
\r
111 ////////////////////////////////////////////////////////////
\r
112 /// Safe pointer to a T resource (inheriting from sf::Resource<T>),
\r
113 /// its pointer is automatically reseted when the resource is destroyed
\r
114 ////////////////////////////////////////////////////////////
\r
115 template <typename T>
\r
120 ////////////////////////////////////////////////////////////
\r
121 /// Default constructor
\r
123 ////////////////////////////////////////////////////////////
\r
126 ////////////////////////////////////////////////////////////
\r
127 /// Construct from a raw resource
\r
129 /// \param Resource : Internal resource
\r
131 ////////////////////////////////////////////////////////////
\r
132 ResourcePtr(const T* Resource);
\r
134 ////////////////////////////////////////////////////////////
\r
135 /// Copy constructor
\r
137 /// \param Copy : Instance to copy
\r
139 ////////////////////////////////////////////////////////////
\r
140 ResourcePtr(const ResourcePtr<T>& Copy);
\r
142 ////////////////////////////////////////////////////////////
\r
145 ////////////////////////////////////////////////////////////
\r
148 ////////////////////////////////////////////////////////////
\r
149 /// Assignment operator from another ResourcePtr
\r
151 /// \param Other : Resource pointer to assign
\r
153 /// \return Reference to this
\r
155 ////////////////////////////////////////////////////////////
\r
156 ResourcePtr<T>& operator =(const ResourcePtr<T>& Other);
\r
158 ////////////////////////////////////////////////////////////
\r
159 /// Assignment operator from a raw resource
\r
161 /// \param Resource : Resource to assign
\r
163 /// \return Reference to this
\r
165 ////////////////////////////////////////////////////////////
\r
166 ResourcePtr<T>& operator =(const T* Resource);
\r
168 ////////////////////////////////////////////////////////////
\r
169 /// Cast operator to implicitely convert the resource pointer to
\r
170 /// its raw pointer type.
\r
171 /// This might be dangerous in the general case, but in this context
\r
172 /// it is safe enough to define this operator
\r
174 /// \return Pointer to the actual resource
\r
176 ////////////////////////////////////////////////////////////
\r
177 operator const T*() const;
\r
179 ////////////////////////////////////////////////////////////
\r
180 /// Operator * overload to return a reference to the actual resource
\r
182 /// \return Reference to the internal resource
\r
184 ////////////////////////////////////////////////////////////
\r
185 const T& operator *() const;
\r
187 ////////////////////////////////////////////////////////////
\r
188 /// Operator -> overload to return a pointer to the actual resource
\r
190 /// \return Pointer to the internal resource
\r
192 ////////////////////////////////////////////////////////////
\r
193 const T* operator ->() const;
\r
195 ////////////////////////////////////////////////////////////
\r
196 /// Function called when the observed resource is about to be
\r
199 ////////////////////////////////////////////////////////////
\r
200 void OnResourceDestroyed();
\r
204 ////////////////////////////////////////////////////////////
\r
206 ////////////////////////////////////////////////////////////
\r
207 const T* myResource; /// Pointer to the actual resource
\r
210 #include <SFML/System/Resource.inl>
\r
211 #include <SFML/System/ResourcePtr.inl>
\r
216 #endif // SFML_RESOURCE_HPP
\r