Removed the Resource and ResourcePtr classes

This commit is contained in:
Laurent Gomila 2011-11-05 15:50:44 +01:00
parent 46b2c096c4
commit 017ef652f9
15 changed files with 18 additions and 518 deletions

View File

@ -29,7 +29,6 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio/SoundSource.hpp>
#include <SFML/System/Resource.hpp>
#include <cstdlib>
@ -214,7 +213,7 @@ private :
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
ResourcePtr<SoundBuffer> myBuffer; ///< Sound buffer bound to the source
const SoundBuffer* myBuffer; ///< Sound buffer bound to the source
};
} // namespace sf

View File

@ -29,7 +29,6 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/System/Resource.hpp>
#include <string>
#include <vector>
#include <set>
@ -49,7 +48,7 @@ class InputStream;
/// \brief Storage for audio samples defining a sound
///
////////////////////////////////////////////////////////////
class SFML_API SoundBuffer : public Resource<SoundBuffer>
class SFML_API SoundBuffer
{
public :

View File

@ -28,7 +28,6 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/Resource.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/System/String.hpp>
#include <SFML/Graphics/Glyph.hpp>
@ -47,7 +46,7 @@ class InputStream;
/// \brief Class for loading and manipulating character fonts
///
////////////////////////////////////////////////////////////
class SFML_API Font : public Resource<Font>
class SFML_API Font
{
public :

View File

@ -28,7 +28,6 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/Resource.hpp>
#include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/Rect.hpp>
@ -204,7 +203,7 @@ private :
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
ResourcePtr<Texture> myTexture; ///< Texture used to draw the sprite
const Texture* myTexture; ///< Texture used to draw the sprite
IntRect mySubRect; ///< Sub-rectangle of source texture to assign to the sprite
bool myIsFlippedX; ///< Is the sprite flipped on the X axis ?
bool myIsFlippedY; ///< Is the sprite flipped on the Y axis ?

View File

@ -28,7 +28,6 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/Resource.hpp>
#include <SFML/System/String.hpp>
#include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/Font.hpp>
@ -239,7 +238,7 @@ private :
// Member data
////////////////////////////////////////////////////////////
String myString; ///< String to display
ResourcePtr<Font> myFont; ///< Font used to display the string
const Font* myFont; ///< Font used to display the string
unsigned int myCharacterSize; ///< Base size of characters, in pixels
unsigned long myStyle; ///< Text style (see Style enum)
mutable FloatRect myBaseRect; ///< Bounding rectangle of the text in object coordinates

View File

@ -28,9 +28,8 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/Resource.hpp>
#include <SFML/Window/GlResource.hpp>
#include <SFML/Graphics/Image.hpp>
#include <SFML/Window/GlResource.hpp>
namespace sf
@ -44,7 +43,7 @@ class InputStream;
/// \brief Image living on the graphics card that can be used for drawing
///
////////////////////////////////////////////////////////////
class SFML_API Texture : public Resource<Texture>, GlResource
class SFML_API Texture : GlResource
{
public :

View File

@ -1,288 +0,0 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_RESOURCE_HPP
#define SFML_RESOURCE_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/Lock.hpp>
#include <SFML/System/Mutex.hpp>
#include <set>
#include <cstddef>
namespace sf
{
////////////////////////////////////////////////////////////
// These two classes are defined in the same header because
// they depend on each other. And as they're template classes,
// they must be entirely defined in header files, which
// prevents from proper separate compiling
////////////////////////////////////////////////////////////
template <typename> class ResourcePtr;
////////////////////////////////////////////////////////////
/// \brief Base class for resources that need to notify
/// dependent classes about their destruction
///
////////////////////////////////////////////////////////////
template <typename T>
class Resource
{
protected :
////////////////////////////////////////////////////////////
/// \brief Default constructor
///
////////////////////////////////////////////////////////////
Resource();
////////////////////////////////////////////////////////////
/// \brief Copy constructor
///
/// \param copy Instance to copy
///
////////////////////////////////////////////////////////////
Resource(const Resource<T>& copy);
////////////////////////////////////////////////////////////
/// \brief Destructor
///
////////////////////////////////////////////////////////////
~Resource();
////////////////////////////////////////////////////////////
/// \brief Assignment operator
///
/// \param right Instance to assign
///
/// \return Reference to self
///
////////////////////////////////////////////////////////////
Resource<T>& operator =(const Resource<T>& right);
private :
friend class ResourcePtr<T>;
////////////////////////////////////////////////////////////
/// \brief Connect a ResourcePtr to this resource
///
/// A connected ResourcePtr will be notified of the
/// destruction of this instance.
///
/// \param observer ResourcePtr to connect
///
////////////////////////////////////////////////////////////
void Connect(ResourcePtr<T>& observer) const;
////////////////////////////////////////////////////////////
/// \brief Disconnect a ResourcePtr from this resource
///
/// The disconnected ResourcePtr will no longer be notified
/// if this instance is destroyed.
///
/// \param observer ResourcePtr to disconnect
///
////////////////////////////////////////////////////////////
void Disconnect(ResourcePtr<T>& observer) const;
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
mutable std::set<ResourcePtr<T>*> myObservers; ///< List of pointers to this resource
mutable Mutex myMutex; ///< Mutex for preventing concurrent access to the pointer list
};
////////////////////////////////////////////////////////////
/// \brief Safe pointer to a sf::Resource<T>
///
////////////////////////////////////////////////////////////
template <typename T>
class ResourcePtr
{
public :
////////////////////////////////////////////////////////////
/// \brief Default constructor
///
/// A default constructed ResourcePtr is empty (null).
///
////////////////////////////////////////////////////////////
ResourcePtr();
////////////////////////////////////////////////////////////
/// \brief Construct from a raw pointer
///
/// \param resource Raw pointer to the resource to wrap
///
////////////////////////////////////////////////////////////
ResourcePtr(const T* resource);
////////////////////////////////////////////////////////////
/// \brief Copy constructor
///
/// The new ResourcePtr will share the same resource as \a copy.
///
/// \param copy Instance to copy
///
////////////////////////////////////////////////////////////
ResourcePtr(const ResourcePtr<T>& copy);
////////////////////////////////////////////////////////////
/// \brief Destructor
///
////////////////////////////////////////////////////////////
~ResourcePtr();
////////////////////////////////////////////////////////////
/// \brief Assignment operator for a ResourcePtr parameter
///
/// \param right Instance to assign
///
/// \return Reference to self
///
////////////////////////////////////////////////////////////
ResourcePtr<T>& operator =(const ResourcePtr<T>& right);
////////////////////////////////////////////////////////////
/// \brief Assignment operator for a raw pointer parameter
///
/// \param resource Resource to assign
///
/// \return Reference to self
///
////////////////////////////////////////////////////////////
ResourcePtr<T>& operator =(const T* resource);
////////////////////////////////////////////////////////////
/// \brief Cast operator to implicitely convert the resource
/// pointer to its raw pointer type (T*)
///
/// This might be dangerous in the general case, but in this context
/// it is safe enough to define this operator.
///
/// \return Read-only pointer to the actual resource
///
////////////////////////////////////////////////////////////
operator const T*() const;
////////////////////////////////////////////////////////////
/// \brief Overload of unary operator *
///
/// Like raw pointers, applying the * operator returns a
/// reference to the pointed object.
///
/// \return Reference to the pointed resource
///
////////////////////////////////////////////////////////////
const T& operator *() const;
////////////////////////////////////////////////////////////
/// \brief Overload of operator ->
///
/// Like raw pointers, applying the -> operator returns the
/// pointed object.
///
/// \return Pointed resource
///
////////////////////////////////////////////////////////////
const T* operator ->() const;
////////////////////////////////////////////////////////////
/// \brief Function called when the observed resource
/// is about to be destroyed
///
/// This functions is called by the destructor of the pointed
/// resource. It allows this instance to reset its internal pointer
/// when the resource is destroyed, and avoid dangling pointers.
///
////////////////////////////////////////////////////////////
void OnResourceDestroyed();
private :
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
const T* myResource; /// Pointer to the actual resource
};
#include <SFML/System/Resource.inl>
#include <SFML/System/ResourcePtr.inl>
} // namespace sf
#endif // SFML_RESOURCE_HPP
////////////////////////////////////////////////////////////
/// \class sf::Resource
/// \ingroup system
///
/// sf::Resource is a base for classes that want to be
/// compatible with the sf::ResourcePtr safe pointer.
///
/// See sf::ResourcePtr for a complete explanation.
///
/// \see sf::ResourcePtr
///
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// \class sf::ResourcePtr
/// \ingroup system
///
/// sf::ResourcePtr is a special kind of smart pointer for
/// resources. Its main feature is to automatically
/// reset its internal pointer to 0 when the resource
/// gets destroyed, so that pointers to a resource never
/// become invalid when the resource is destroyed. Instead,
/// it properly returns 0 when the resource no longer exists.
///
/// Its usage is completely transparent, so that it is similar
/// to manipulating the raw resource directly (like any smart pointer).
///
/// For sf::ResourcePtr<T> to work, T must inherit from
/// the sf::Resource class.
///
/// These two classes are heavily used internally in SFML
/// to safely handle resources and the classes that use them:
/// \li sf::Texture / sf::Sprite
/// \li sf::Font / sf::Text
/// \li sf::SoundBuffer / sf::Sound
///
/// sf::Resource and sf::ResourcePtr are designed for internal use,
/// but if you feel like they would fit well in your implementation
/// there's no problem to use them.
///
/// \see sf::Resource
///
////////////////////////////////////////////////////////////

View File

@ -1,78 +0,0 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
template <typename T>
Resource<T>::Resource()
{
// Nothing to do
}
////////////////////////////////////////////////////////////
template <typename T>
Resource<T>::Resource(const Resource<T>&)
{
// Nothing to do, we don't want to copy observers
}
////////////////////////////////////////////////////////////
template <typename T>
Resource<T>::~Resource()
{
// Notify all observers
for (typename std::set<ResourcePtr<T>*>::iterator i = myObservers.begin(); i != myObservers.end(); ++i)
{
(*i)->OnResourceDestroyed();
}
}
////////////////////////////////////////////////////////////
template <typename T>
Resource<T>& Resource<T>::operator =(const Resource<T>&)
{
// Nothing to do, we don't want to copy observers
return *this;
}
////////////////////////////////////////////////////////////
template <typename T>
void Resource<T>::Connect(ResourcePtr<T>& observer) const
{
sf::Lock lock(myMutex);
myObservers.insert(&observer);
}
////////////////////////////////////////////////////////////
template <typename T>
void Resource<T>::Disconnect(ResourcePtr<T>& observer) const
{
sf::Lock lock(myMutex);
myObservers.erase(&observer);
}

View File

@ -1,125 +0,0 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
template <typename T>
ResourcePtr<T>::ResourcePtr() :
myResource(NULL)
{
}
////////////////////////////////////////////////////////////
template <typename T>
ResourcePtr<T>::ResourcePtr(const T* resource) :
myResource(resource)
{
if (myResource)
myResource->Connect(*this);
}
////////////////////////////////////////////////////////////
template <typename T>
ResourcePtr<T>::ResourcePtr(const ResourcePtr<T>& copy) :
myResource(copy.myResource)
{
if (myResource)
myResource->Connect(*this);
}
////////////////////////////////////////////////////////////
template <typename T>
ResourcePtr<T>::~ResourcePtr()
{
if (myResource)
myResource->Disconnect(*this);
}
////////////////////////////////////////////////////////////
template <typename T>
ResourcePtr<T>& ResourcePtr<T>::operator =(const ResourcePtr<T>& right)
{
if (myResource)
myResource->Disconnect(*this);
myResource = right.myResource;
if (myResource)
myResource->Connect(*this);
return *this;
}
////////////////////////////////////////////////////////////
template <typename T>
ResourcePtr<T>& ResourcePtr<T>::operator =(const T* resource)
{
if (myResource)
myResource->Disconnect(*this);
myResource = resource;
if (myResource)
myResource->Connect(*this);
return *this;
}
////////////////////////////////////////////////////////////
template <typename T>
ResourcePtr<T>::operator const T*() const
{
return myResource;
}
////////////////////////////////////////////////////////////
template <typename T>
const T& ResourcePtr<T>::operator *() const
{
return *myResource;
}
////////////////////////////////////////////////////////////
template <typename T>
const T* ResourcePtr<T>::operator ->() const
{
return myResource;
}
////////////////////////////////////////////////////////////
template <typename T>
void ResourcePtr<T>::OnResourceDestroyed()
{
myResource = NULL;
}

View File

@ -33,7 +33,8 @@
namespace sf
{
////////////////////////////////////////////////////////////
Sound::Sound()
Sound::Sound() :
myBuffer(NULL)
{
}

View File

@ -50,7 +50,6 @@ myDuration(0)
////////////////////////////////////////////////////////////
SoundBuffer::SoundBuffer(const SoundBuffer& copy) :
Resource<SoundBuffer>(),
myBuffer (0),
mySamples (copy.mySamples),
myDuration(copy.myDuration),

View File

@ -77,7 +77,6 @@ myRefCount (NULL)
////////////////////////////////////////////////////////////
Font::Font(const Font& copy) :
Resource<Font>(),
myLibrary (copy.myLibrary),
myFace (copy.myFace),
myStreamRec (copy.myStreamRec),

View File

@ -36,6 +36,7 @@ namespace sf
////////////////////////////////////////////////////////////
Sprite::Sprite() :
Drawable (),
myTexture (NULL),
mySubRect (0, 0, 1, 1),
myIsFlippedX(false),
myIsFlippedY(false)
@ -47,6 +48,7 @@ myIsFlippedY(false)
////////////////////////////////////////////////////////////
Sprite::Sprite(const Texture& texture) :
Drawable (),
myTexture (NULL),
mySubRect (0, 0, 1, 1),
myIsFlippedX(false),
myIsFlippedY(false)

View File

@ -52,7 +52,6 @@ myPixelsFlipped(false)
////////////////////////////////////////////////////////////
Texture::Texture(const Texture& copy) :
Resource<Texture>(),
myWidth (0),
myHeight (0),
myTextureWidth (0),

View File

@ -14,9 +14,6 @@ set(SRC
${INCROOT}/Mutex.hpp
${INCROOT}/NonCopyable.hpp
${SRCROOT}/Platform.hpp
${INCROOT}/Resource.hpp
${INCROOT}/Resource.inl
${INCROOT}/ResourcePtr.inl
${SRCROOT}/Sleep.cpp
${INCROOT}/Sleep.hpp
${SRCROOT}/String.cpp