Resource.hpp
00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00025 #ifndef SFML_RESOURCE_HPP
00026 #define SFML_RESOURCE_HPP
00027 
00029 // Headers
00031 #include <SFML/System/Lock.hpp>
00032 #include <SFML/System/Mutex.hpp>
00033 #include <set>
00034 #include <cstddef>
00035 
00036 
00037 namespace sf
00038 {
00040 // These two classes are defined in the same header because
00041 // they depend on each other. And as they're template classes,
00042 // they must be entirely defined in header files, which
00043 // prevents from proper separate compiling
00045 
00046 template <typename> class ResourcePtr;
00047 
00053 template <typename T>
00054 class Resource
00055 {
00056 protected :
00057 
00062     Resource();
00063 
00070     Resource(const Resource<T>& copy);
00071 
00076     ~Resource();
00077 
00086     Resource<T>& operator =(const Resource<T>& right);
00087 
00088 private :
00089 
00090     friend class ResourcePtr<T>;
00091 
00101     void Connect(ResourcePtr<T>& observer) const;
00102 
00112     void Disconnect(ResourcePtr<T>& observer) const;
00113 
00115     // Member data
00117     mutable std::set<ResourcePtr<T>*> myObservers; 
00118     mutable Mutex                     myMutex;     
00119 };
00120 
00121 
00126 template <typename T>
00127 class ResourcePtr
00128 {
00129 public :
00130 
00137     ResourcePtr();
00138 
00145     ResourcePtr(const T* resource);
00146 
00155     ResourcePtr(const ResourcePtr<T>& copy);
00156 
00161     ~ResourcePtr();
00162 
00171     ResourcePtr<T>& operator =(const ResourcePtr<T>& right);
00172 
00181     ResourcePtr<T>& operator =(const T* resource);
00182 
00193     operator const T*() const;
00194 
00204     const T& operator *() const;
00205 
00215     const T* operator ->() const;
00216 
00226     void OnResourceDestroyed();
00227 
00228 private :
00229 
00231     // Member data
00233     const T* myResource; 
00234 };
00235 
00236 #include <SFML/System/Resource.inl>
00237 #include <SFML/System/ResourcePtr.inl>
00238 
00239 } // namespace sf
00240 
00241 
00242 #endif // SFML_RESOURCE_HPP
00243 
00244 
00257 
00258