]> git.sesse.net Git - casparcg/blob - dependencies64/sfml/include/SFML/System/Resource.hpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / dependencies64 / sfml / include / SFML / System / Resource.hpp
1 ////////////////////////////////////////////////////////////\r
2 //\r
3 // SFML - Simple and Fast Multimedia Library\r
4 // Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)\r
5 //\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
8 //\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
12 //\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
17 //\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
20 //\r
21 // 3. This notice may not be removed or altered from any source distribution.\r
22 //\r
23 ////////////////////////////////////////////////////////////\r
24 \r
25 #ifndef SFML_RESOURCE_HPP\r
26 #define SFML_RESOURCE_HPP\r
27 \r
28 ////////////////////////////////////////////////////////////\r
29 // Headers\r
30 ////////////////////////////////////////////////////////////\r
31 #include <set>\r
32 \r
33 \r
34 namespace sf\r
35 {\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
42 \r
43 template <typename> class ResourcePtr;\r
44 \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
50 class Resource\r
51 {\r
52 protected :\r
53 \r
54     ////////////////////////////////////////////////////////////\r
55     /// Default constructor\r
56     ///\r
57     ////////////////////////////////////////////////////////////\r
58     Resource();\r
59 \r
60     ////////////////////////////////////////////////////////////\r
61     /// Copy constructor\r
62     ///\r
63     /// \param Copy : Resource to copy\r
64     ///\r
65     ////////////////////////////////////////////////////////////\r
66     Resource(const Resource<T>& Copy);\r
67 \r
68     ////////////////////////////////////////////////////////////\r
69     /// Destructor\r
70     ///\r
71     ////////////////////////////////////////////////////////////\r
72     ~Resource();\r
73 \r
74     ////////////////////////////////////////////////////////////\r
75     /// Assignment operator\r
76     ///\r
77     /// \param Other : Resource to copy\r
78     ///\r
79     /// \return Reference to this\r
80     ///\r
81     ////////////////////////////////////////////////////////////\r
82     Resource<T>& operator =(const Resource<T>& Other);\r
83 \r
84 private :\r
85 \r
86     friend class ResourcePtr<T>;\r
87 \r
88     ////////////////////////////////////////////////////////////\r
89     /// Connect a ResourcePtr to this resource\r
90     ///\r
91     /// \param Observer : Observer to add\r
92     ///\r
93     ////////////////////////////////////////////////////////////\r
94     void Connect(ResourcePtr<T>& Observer) const;\r
95 \r
96     ////////////////////////////////////////////////////////////\r
97     /// Disconnect a ResourcePtr from this resource\r
98     ///\r
99     /// \param Observer : Observer to remove\r
100     ///\r
101     ////////////////////////////////////////////////////////////\r
102     void Disconnect(ResourcePtr<T>& Observer) const;\r
103 \r
104     ////////////////////////////////////////////////////////////\r
105     // Member data\r
106     ////////////////////////////////////////////////////////////\r
107     mutable std::set<ResourcePtr<T>*> myObservers;\r
108 };\r
109 \r
110 \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
116 class ResourcePtr\r
117 {\r
118 public :\r
119 \r
120     ////////////////////////////////////////////////////////////\r
121     /// Default constructor\r
122     ///\r
123     ////////////////////////////////////////////////////////////\r
124     ResourcePtr();\r
125 \r
126     ////////////////////////////////////////////////////////////\r
127     /// Construct from a raw resource\r
128     ///\r
129     /// \param Resource : Internal resource\r
130     ///\r
131     ////////////////////////////////////////////////////////////\r
132     ResourcePtr(const T* Resource);\r
133 \r
134     ////////////////////////////////////////////////////////////\r
135     /// Copy constructor\r
136     ///\r
137     /// \param Copy : Instance to copy\r
138     ///\r
139     ////////////////////////////////////////////////////////////\r
140     ResourcePtr(const ResourcePtr<T>& Copy);\r
141 \r
142     ////////////////////////////////////////////////////////////\r
143     /// Destructor\r
144     ///\r
145     ////////////////////////////////////////////////////////////\r
146     ~ResourcePtr();\r
147 \r
148     ////////////////////////////////////////////////////////////\r
149     /// Assignment operator from another ResourcePtr\r
150     ///\r
151     /// \param Other : Resource pointer to assign\r
152     ///\r
153     /// \return Reference to this\r
154     ///\r
155     ////////////////////////////////////////////////////////////\r
156     ResourcePtr<T>& operator =(const ResourcePtr<T>& Other);\r
157 \r
158     ////////////////////////////////////////////////////////////\r
159     /// Assignment operator from a raw resource\r
160     ///\r
161     /// \param Resource : Resource to assign\r
162     ///\r
163     /// \return Reference to this\r
164     ///\r
165     ////////////////////////////////////////////////////////////\r
166     ResourcePtr<T>& operator =(const T* Resource);\r
167 \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
173     ///\r
174     /// \return Pointer to the actual resource\r
175     ///\r
176     ////////////////////////////////////////////////////////////\r
177     operator const T*() const;\r
178 \r
179     ////////////////////////////////////////////////////////////\r
180     /// Operator * overload to return a reference to the actual resource\r
181     ///\r
182     /// \return Reference to the internal resource\r
183     ///\r
184     ////////////////////////////////////////////////////////////\r
185     const T& operator *() const;\r
186 \r
187     ////////////////////////////////////////////////////////////\r
188     /// Operator -> overload to return a pointer to the actual resource\r
189     ///\r
190     /// \return Pointer to the internal resource\r
191     ///\r
192     ////////////////////////////////////////////////////////////\r
193     const T* operator ->() const;\r
194 \r
195     ////////////////////////////////////////////////////////////\r
196     /// Function called when the observed resource is about to be\r
197     /// destroyed\r
198     ///\r
199     ////////////////////////////////////////////////////////////\r
200     void OnResourceDestroyed();\r
201 \r
202 private :\r
203 \r
204     ////////////////////////////////////////////////////////////\r
205     // Member data\r
206     ////////////////////////////////////////////////////////////\r
207     const T* myResource; /// Pointer to the actual resource\r
208 };\r
209 \r
210 #include <SFML/System/Resource.inl>\r
211 #include <SFML/System/ResourcePtr.inl>\r
212 \r
213 } // namespace sf\r
214 \r
215 \r
216 #endif // SFML_RESOURCE_HPP\r