]> git.sesse.net Git - casparcg/blob - SFML-1.6/include/SFML/System/ResourcePtr.inl
(no commit message)
[casparcg] / SFML-1.6 / include / SFML / System / ResourcePtr.inl
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 \r
26 ////////////////////////////////////////////////////////////\r
27 /// Default constructor\r
28 ////////////////////////////////////////////////////////////\r
29 template <typename T>\r
30 ResourcePtr<T>::ResourcePtr() :\r
31 myResource(NULL)\r
32 {\r
33 \r
34 }\r
35 \r
36 \r
37 ////////////////////////////////////////////////////////////\r
38 /// Construct from a raw resource\r
39 ////////////////////////////////////////////////////////////\r
40 template <typename T>\r
41 ResourcePtr<T>::ResourcePtr(const T* Resource) :\r
42 myResource(Resource)\r
43 {\r
44     if (myResource)\r
45         myResource->Connect(*this);\r
46 }\r
47 \r
48 \r
49 ////////////////////////////////////////////////////////////\r
50 /// Copy constructor\r
51 ////////////////////////////////////////////////////////////\r
52 template <typename T>\r
53 ResourcePtr<T>::ResourcePtr(const ResourcePtr<T>& Copy) :\r
54 myResource(Copy.myResource)\r
55 {\r
56     if (myResource)\r
57         myResource->Connect(*this);\r
58 }\r
59 \r
60 \r
61 ////////////////////////////////////////////////////////////\r
62 /// Destructor\r
63 ////////////////////////////////////////////////////////////\r
64 template <typename T>\r
65 ResourcePtr<T>::~ResourcePtr()\r
66 {\r
67     if (myResource)\r
68         myResource->Disconnect(*this);\r
69 }\r
70 \r
71 \r
72 ////////////////////////////////////////////////////////////\r
73 /// Assignment operator from another ResourcePtr\r
74 ////////////////////////////////////////////////////////////\r
75 template <typename T>\r
76 ResourcePtr<T>& ResourcePtr<T>::operator =(const ResourcePtr<T>& Other)\r
77 {\r
78     if (myResource)\r
79         myResource->Disconnect(*this);\r
80 \r
81     myResource = Other.myResource;\r
82 \r
83     if (myResource)\r
84         myResource->Connect(*this);\r
85 \r
86     return *this;\r
87 }\r
88 \r
89 \r
90 ////////////////////////////////////////////////////////////\r
91 /// Assignment operator from a raw resource\r
92 ////////////////////////////////////////////////////////////\r
93 template <typename T>\r
94 ResourcePtr<T>& ResourcePtr<T>::operator =(const T* Resource)\r
95 {\r
96     if (myResource)\r
97         myResource->Disconnect(*this);\r
98 \r
99     myResource = Resource;\r
100 \r
101     if (myResource)\r
102         myResource->Connect(*this);\r
103 \r
104     return *this;\r
105 }\r
106 \r
107 \r
108 ////////////////////////////////////////////////////////////\r
109 /// Cast operator to implicitely convert the resource pointer to\r
110 /// its raw pointer type.\r
111 /// This might be dangerous in the general case, but in this context\r
112 /// it is safe enough to define this operator\r
113 ////////////////////////////////////////////////////////////\r
114 template <typename T>\r
115 ResourcePtr<T>::operator const T*() const\r
116 {\r
117     return myResource;\r
118 }\r
119 \r
120 \r
121 ////////////////////////////////////////////////////////////\r
122 /// Operator * overload to return a reference to the actual resource\r
123 ////////////////////////////////////////////////////////////\r
124 template <typename T>\r
125 const T& ResourcePtr<T>::operator *() const\r
126 {\r
127     return *myResource;\r
128 }\r
129 \r
130 \r
131 ////////////////////////////////////////////////////////////\r
132 /// Operator -> overload to return a pointer to the actual resource\r
133 ////////////////////////////////////////////////////////////\r
134 template <typename T>\r
135 const T* ResourcePtr<T>::operator ->() const\r
136 {\r
137     return myResource;\r
138 }\r
139 \r
140 \r
141 ////////////////////////////////////////////////////////////\r
142 /// Function called when the observed resource is about to be\r
143 /// destroyed\r
144 ////////////////////////////////////////////////////////////\r
145 template <typename T>\r
146 void ResourcePtr<T>::OnResourceDestroyed()\r
147 {\r
148     myResource = NULL;\r
149 }\r