Removed all manual memory management.

This commit is contained in:
binary1248 2017-04-02 01:12:29 +02:00
parent d6dcafbc90
commit a4acac813e
39 changed files with 163 additions and 267 deletions

View file

@ -5,6 +5,7 @@
#include "Effect.hpp" #include "Effect.hpp"
#include <vector> #include <vector>
#include <cmath> #include <cmath>
#include <memory>
const sf::Font* Effect::s_font = NULL; const sf::Font* Effect::s_font = NULL;
@ -357,12 +358,12 @@ int main()
Effect::setFont(font); Effect::setFont(font);
// Create the effects // Create the effects
std::vector<Effect*> effects; std::vector<std::unique_ptr<Effect>> effects;
effects.push_back(new Pixelate); effects.push_back(std::make_unique<Pixelate>());
effects.push_back(new WaveBlur); effects.push_back(std::make_unique<WaveBlur>());
effects.push_back(new StormBlink); effects.push_back(std::make_unique<StormBlink>());
effects.push_back(new Edge); effects.push_back(std::make_unique<Edge>());
effects.push_back(new Geometry); effects.push_back(std::make_unique<Geometry>());
std::size_t current = 0; std::size_t current = 0;
// Initialize them // Initialize them
@ -452,9 +453,5 @@ int main()
window.display(); window.display();
} }
// delete the effects
for (std::size_t i = 0; i < effects.size(); ++i)
delete effects[i];
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View file

@ -33,6 +33,7 @@
#include <SFML/System/Time.hpp> #include <SFML/System/Time.hpp>
#include <string> #include <string>
#include <algorithm> #include <algorithm>
#include <memory>
namespace sf namespace sf
@ -217,13 +218,13 @@ private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
SoundFileReader* m_reader; ///< Reader that handles I/O on the file's format std::unique_ptr<SoundFileReader> m_reader; ///< Reader that handles I/O on the file's format
InputStream* m_stream; ///< Input stream used to access the file's data std::unique_ptr<InputStream> m_ownedStream; ///< The stream we own if any
bool m_streamOwned; ///< Is the stream internal or external? InputStream* m_stream; ///< Input stream used to access the file's data
Uint64 m_sampleOffset; ///< Sample Read Position Uint64 m_sampleOffset; ///< Sample Read Position
Uint64 m_sampleCount; ///< Total number of samples in the file Uint64 m_sampleCount; ///< Total number of samples in the file
unsigned int m_channelCount; ///< Number of channels of the sound unsigned int m_channelCount; ///< Number of channels of the sound
unsigned int m_sampleRate; ///< Number of samples per second unsigned int m_sampleRate; ///< Number of samples per second
}; };
} // namespace sf } // namespace sf

View file

@ -31,6 +31,7 @@
#include <SFML/Audio/Export.hpp> #include <SFML/Audio/Export.hpp>
#include <SFML/System/NonCopyable.hpp> #include <SFML/System/NonCopyable.hpp>
#include <string> #include <string>
#include <memory>
namespace sf namespace sf
@ -93,7 +94,7 @@ private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
SoundFileWriter* m_writer; ///< Writer that handles I/O on the file's format std::unique_ptr<SoundFileWriter> m_writer; ///< Writer that handles I/O on the file's format
}; };
} // namespace sf } // namespace sf

View file

@ -31,6 +31,7 @@
#include <SFML/Audio/Export.hpp> #include <SFML/Audio/Export.hpp>
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory>
namespace sf namespace sf
@ -95,7 +96,7 @@ public:
/// \see createReaderFromMemory, createReaderFromStream /// \see createReaderFromMemory, createReaderFromStream
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
static SoundFileReader* createReaderFromFilename(const std::string& filename); static std::unique_ptr<SoundFileReader> createReaderFromFilename(const std::string& filename);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Instantiate the right codec for the given file in memory /// \brief Instantiate the right codec for the given file in memory
@ -110,7 +111,7 @@ public:
/// \see createReaderFromFilename, createReaderFromStream /// \see createReaderFromFilename, createReaderFromStream
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
static SoundFileReader* createReaderFromMemory(const void* data, std::size_t sizeInBytes); static std::unique_ptr<SoundFileReader> createReaderFromMemory(const void* data, std::size_t sizeInBytes);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Instantiate the right codec for the given file in stream /// \brief Instantiate the right codec for the given file in stream
@ -124,7 +125,7 @@ public:
/// \see createReaderFromFilename, createReaderFromMemory /// \see createReaderFromFilename, createReaderFromMemory
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
static SoundFileReader* createReaderFromStream(InputStream& stream); static std::unique_ptr<SoundFileReader> createReaderFromStream(InputStream& stream);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Instantiate the right writer for the given file on disk /// \brief Instantiate the right writer for the given file on disk
@ -136,7 +137,7 @@ public:
/// \return A new sound file writer that can write given file, or null if no writer can handle it /// \return A new sound file writer that can write given file, or null if no writer can handle it
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
static SoundFileWriter* createWriterFromFilename(const std::string& filename); static std::unique_ptr<SoundFileWriter> createWriterFromFilename(const std::string& filename);
private: private:
@ -146,14 +147,14 @@ private:
struct ReaderFactory struct ReaderFactory
{ {
bool (*check)(InputStream&); bool (*check)(InputStream&);
SoundFileReader* (*create)(); std::unique_ptr<SoundFileReader> (*create)();
}; };
typedef std::vector<ReaderFactory> ReaderFactoryArray; typedef std::vector<ReaderFactory> ReaderFactoryArray;
struct WriterFactory struct WriterFactory
{ {
bool (*check)(const std::string&); bool (*check)(const std::string&);
SoundFileWriter* (*create)(); std::unique_ptr<SoundFileWriter> (*create)();
}; };
typedef std::vector<WriterFactory> WriterFactoryArray; typedef std::vector<WriterFactory> WriterFactoryArray;

View file

@ -31,8 +31,8 @@ namespace sf
{ {
namespace priv namespace priv
{ {
template <typename T> SoundFileReader* createReader() {return new T;} template <typename T> std::unique_ptr<SoundFileReader> createReader() {return std::make_unique<T>();}
template <typename T> SoundFileWriter* createWriter() {return new T;} template <typename T> std::unique_ptr<SoundFileWriter> createWriter() {return std::make_unique<T>();}
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View file

@ -37,6 +37,7 @@
#include <map> #include <map>
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory>
namespace sf namespace sf
@ -348,14 +349,14 @@ private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void* m_library; ///< Pointer to the internal library interface (it is typeless to avoid exposing implementation details) void* m_library; ///< Pointer to the internal library interface (it is typeless to avoid exposing implementation details)
void* m_face; ///< Pointer to the internal font face (it is typeless to avoid exposing implementation details) void* m_face; ///< Pointer to the internal font face (it is typeless to avoid exposing implementation details)
void* m_streamRec; ///< Pointer to the stream rec instance (it is typeless to avoid exposing implementation details) std::shared_ptr<void> m_streamRec; ///< Pointer to the stream rec instance (it is typeless to avoid exposing implementation details)
void* m_stroker; ///< Pointer to the stroker (it is typeless to avoid exposing implementation details) void* m_stroker; ///< Pointer to the stroker (it is typeless to avoid exposing implementation details)
int* m_refCount; ///< Reference counter used by implicit sharing std::shared_ptr<int> m_refCount; ///< Reference counter used by implicit sharing
Info m_info; ///< Information about the font Info m_info; ///< Information about the font
mutable PageTable m_pages; ///< Table containing the glyphs pages by character size mutable PageTable m_pages; ///< Table containing the glyphs pages by character size
mutable std::vector<Uint8> m_pixelBuffer; ///< Pixel buffer holding a glyph's pixels before being written to the texture mutable std::vector<Uint8> m_pixelBuffer; ///< Pixel buffer holding a glyph's pixels before being written to the texture
#ifdef SFML_SYSTEM_ANDROID #ifdef SFML_SYSTEM_ANDROID
void* m_stream; ///< Asset file streamer (if loaded from file) std::shared_ptr<void> m_stream; ///< Asset file streamer (if loaded from file)
#endif #endif
}; };

View file

@ -33,7 +33,7 @@
#include <SFML/Graphics/Rect.hpp> #include <SFML/Graphics/Rect.hpp>
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory>
namespace sf namespace sf
{ {
@ -263,10 +263,10 @@ private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Vector2u m_size; ///< Image size Vector2u m_size; ///< Image size
std::vector<Uint8> m_pixels; ///< Pixels of the image std::vector<Uint8> m_pixels; ///< Pixels of the image
#ifdef SFML_SYSTEM_ANDROID #ifdef SFML_SYSTEM_ANDROID
void* m_stream; ///< Asset file streamer (if loaded from file) std::shared_ptr<void> m_stream; ///< Asset file streamer (if loaded from file)
#endif #endif
}; };

View file

@ -31,6 +31,7 @@
#include <SFML/Graphics/Export.hpp> #include <SFML/Graphics/Export.hpp>
#include <SFML/Graphics/Texture.hpp> #include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/RenderTarget.hpp> #include <SFML/Graphics/RenderTarget.hpp>
#include <memory>
namespace sf namespace sf
@ -207,8 +208,8 @@ private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
priv::RenderTextureImpl* m_impl; ///< Platform/hardware specific implementation std::unique_ptr<priv::RenderTextureImpl> m_impl; ///< Platform/hardware specific implementation
Texture m_texture; ///< Target texture to draw on Texture m_texture; ///< Target texture to draw on
}; };
} // namespace sf } // namespace sf

View file

@ -30,6 +30,7 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Network/Export.hpp> #include <SFML/Network/Export.hpp>
#include <SFML/System/Time.hpp> #include <SFML/System/Time.hpp>
#include <memory>
namespace sf namespace sf
@ -158,7 +159,7 @@ private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
SocketSelectorImpl* m_impl; ///< Opaque pointer to the implementation (which requires OS-specific types) std::unique_ptr<SocketSelectorImpl> m_impl; ///< Opaque pointer to the implementation (which requires OS-specific types)
}; };
} // namespace sf } // namespace sf

View file

@ -34,6 +34,7 @@
#include <SFML/System/NonCopyable.hpp> #include <SFML/System/NonCopyable.hpp>
#include <cstdio> #include <cstdio>
#include <string> #include <string>
#include <memory>
#ifdef ANDROID #ifdef ANDROID
namespace sf namespace sf
@ -123,7 +124,7 @@ private:
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#ifdef ANDROID #ifdef ANDROID
priv::ResourceStream* m_file; std::unique_ptr<priv::ResourceStream> m_file;
#else #else
std::FILE* m_file; ///< stdio file stream std::FILE* m_file; ///< stdio file stream
#endif #endif

View file

@ -30,6 +30,7 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/System/Export.hpp> #include <SFML/System/Export.hpp>
#include <SFML/System/NonCopyable.hpp> #include <SFML/System/NonCopyable.hpp>
#include <memory>
namespace sf namespace sf
@ -85,7 +86,7 @@ private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
priv::MutexImpl* m_mutexImpl; ///< OS-specific implementation std::shared_ptr<priv::MutexImpl> m_mutexImpl; ///< OS-specific implementation
}; };
} // namespace sf } // namespace sf

View file

@ -31,6 +31,7 @@
#include <SFML/System/Export.hpp> #include <SFML/System/Export.hpp>
#include <SFML/System/NonCopyable.hpp> #include <SFML/System/NonCopyable.hpp>
#include <cstdlib> #include <cstdlib>
#include <memory>
namespace sf namespace sf
@ -186,8 +187,8 @@ private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
priv::ThreadImpl* m_impl; ///< OS-specific implementation of the thread std::shared_ptr<priv::ThreadImpl> m_impl; ///< OS-specific implementation of the thread
priv::ThreadFunc* m_entryPoint; ///< Abstraction of the function to run std::shared_ptr<priv::ThreadFunc> m_entryPoint; ///< Abstraction of the function to run
}; };
#include <SFML/System/Thread.inl> #include <SFML/System/Thread.inl>

View file

@ -66,8 +66,7 @@ struct ThreadMemberFunc : ThreadFunc
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
template <typename F> template <typename F>
Thread::Thread(F functor) : Thread::Thread(F functor) :
m_impl (NULL), m_entryPoint(std::make_shared<priv::ThreadFunctor<F>>(functor))
m_entryPoint(new priv::ThreadFunctor<F>(functor))
{ {
} }
@ -75,8 +74,7 @@ m_entryPoint(new priv::ThreadFunctor<F>(functor))
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
template <typename F, typename A> template <typename F, typename A>
Thread::Thread(F function, A argument) : Thread::Thread(F function, A argument) :
m_impl (NULL), m_entryPoint(std::make_shared<priv::ThreadFunctorWithArg<F, A>>(function, argument))
m_entryPoint(new priv::ThreadFunctorWithArg<F, A>(function, argument))
{ {
} }
@ -84,7 +82,6 @@ m_entryPoint(new priv::ThreadFunctorWithArg<F, A>(function, argument))
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
template <typename C> template <typename C>
Thread::Thread(void(C::*function)(), C* object) : Thread::Thread(void(C::*function)(), C* object) :
m_impl (NULL), m_entryPoint(std::make_shared<priv::ThreadMemberFunc<C>>(function, object))
m_entryPoint(new priv::ThreadMemberFunc<C>(function, object))
{ {
} }

View file

@ -31,6 +31,7 @@
#include <SFML/System/Export.hpp> #include <SFML/System/Export.hpp>
#include <SFML/System/NonCopyable.hpp> #include <SFML/System/NonCopyable.hpp>
#include <cstdlib> #include <cstdlib>
#include <memory>
namespace sf namespace sf
@ -83,7 +84,7 @@ private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
priv::ThreadLocalImpl* m_impl; ///< Pointer to the OS specific implementation std::unique_ptr<priv::ThreadLocalImpl> m_impl; ///< Pointer to the OS specific implementation
}; };
} // namespace sf } // namespace sf

View file

@ -32,6 +32,7 @@
#include <SFML/Window/GlResource.hpp> #include <SFML/Window/GlResource.hpp>
#include <SFML/Window/ContextSettings.hpp> #include <SFML/Window/ContextSettings.hpp>
#include <SFML/System/NonCopyable.hpp> #include <SFML/System/NonCopyable.hpp>
#include <memory>
namespace sf namespace sf
@ -135,7 +136,7 @@ private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
priv::GlContext* m_context; ///< Internal OpenGL context std::unique_ptr<priv::GlContext> m_context; ///< Internal OpenGL context
}; };
} // namespace sf } // namespace sf

View file

@ -38,6 +38,7 @@
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <SFML/System/NonCopyable.hpp> #include <SFML/System/NonCopyable.hpp>
#include <SFML/System/String.hpp> #include <SFML/System/String.hpp>
#include <memory>
namespace sf namespace sf
@ -522,11 +523,11 @@ private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
priv::WindowImpl* m_impl; ///< Platform-specific implementation of the window std::unique_ptr<priv::WindowImpl> m_impl; ///< Platform-specific implementation of the window
priv::GlContext* m_context; ///< Platform-specific implementation of the OpenGL context std::unique_ptr<priv::GlContext> m_context; ///< Platform-specific implementation of the OpenGL context
Clock m_clock; ///< Clock for measuring the elapsed time between frames Clock m_clock; ///< Clock for measuring the elapsed time between frames
Time m_frameTimeLimit; ///< Current framerate limit Time m_frameTimeLimit; ///< Current framerate limit
Vector2u m_size; ///< Current size of the window Vector2u m_size; ///< Current size of the window
}; };
} // namespace sf } // namespace sf

View file

@ -29,6 +29,7 @@
#include <SFML/Audio/AudioDevice.hpp> #include <SFML/Audio/AudioDevice.hpp>
#include <SFML/System/Mutex.hpp> #include <SFML/System/Mutex.hpp>
#include <SFML/System/Lock.hpp> #include <SFML/System/Lock.hpp>
#include <memory>
namespace namespace
@ -40,7 +41,7 @@ namespace
// The audio device is instantiated on demand rather than at global startup, // The audio device is instantiated on demand rather than at global startup,
// which solves a lot of weird crashes and errors. // which solves a lot of weird crashes and errors.
// It is destroyed when it is no longer needed. // It is destroyed when it is no longer needed.
sf::priv::AudioDevice* globalDevice; std::unique_ptr<sf::priv::AudioDevice> globalDevice;
} }
@ -54,7 +55,7 @@ AlResource::AlResource()
// If this is the very first resource, trigger the global device initialization // If this is the very first resource, trigger the global device initialization
if (count == 0) if (count == 0)
globalDevice = new priv::AudioDevice; globalDevice = std::make_unique<priv::AudioDevice>();
// Increment the resources counter // Increment the resources counter
count++; count++;
@ -72,7 +73,7 @@ AlResource::~AlResource()
// If there's no more resource alive, we can destroy the device // If there's no more resource alive, we can destroy the device
if (count == 0) if (count == 0)
delete globalDevice; globalDevice.reset();
} }
} // namespace sf } // namespace sf

View file

@ -38,10 +38,8 @@ namespace sf
{ {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
InputSoundFile::InputSoundFile() : InputSoundFile::InputSoundFile() :
m_reader (NULL), m_stream (nullptr),
m_stream (NULL), m_sampleOffset(0),
m_streamOwned (false),
m_sampleOffset (0),
m_sampleCount (0), m_sampleCount (0),
m_channelCount(0), m_channelCount(0),
m_sampleRate (0) m_sampleRate (0)
@ -69,9 +67,7 @@ bool InputSoundFile::openFromFile(const std::string& filename)
return false; return false;
// Wrap the file into a stream // Wrap the file into a stream
FileInputStream* file = new FileInputStream; auto file = std::make_unique<FileInputStream>();
m_stream = file;
m_streamOwned = true;
// Open it // Open it
if (!file->open(filename)) if (!file->open(filename))
@ -93,6 +89,9 @@ bool InputSoundFile::openFromFile(const std::string& filename)
m_channelCount = info.channelCount; m_channelCount = info.channelCount;
m_sampleRate = info.sampleRate; m_sampleRate = info.sampleRate;
m_ownedStream = std::move(file);
m_stream = m_ownedStream.get();
return true; return true;
} }
@ -109,9 +108,7 @@ bool InputSoundFile::openFromMemory(const void* data, std::size_t sizeInBytes)
return false; return false;
// Wrap the memory file into a stream // Wrap the memory file into a stream
MemoryInputStream* memory = new MemoryInputStream; auto memory = std::make_unique<MemoryInputStream>();
m_stream = memory;
m_streamOwned = true;
// Open it // Open it
memory->open(data, sizeInBytes); memory->open(data, sizeInBytes);
@ -129,6 +126,9 @@ bool InputSoundFile::openFromMemory(const void* data, std::size_t sizeInBytes)
m_channelCount = info.channelCount; m_channelCount = info.channelCount;
m_sampleRate = info.sampleRate; m_sampleRate = info.sampleRate;
m_ownedStream = std::move(memory);
m_stream = m_ownedStream.get();
return true; return true;
} }
@ -146,7 +146,6 @@ bool InputSoundFile::openFromStream(InputStream& stream)
// store the stream // store the stream
m_stream = &stream; m_stream = &stream;
m_streamOwned = false;
// Don't forget to reset the stream to its beginning before re-opening it // Don't forget to reset the stream to its beginning before re-opening it
if (stream.seek(0) != 0) if (stream.seek(0) != 0)
@ -257,16 +256,11 @@ Uint64 InputSoundFile::read(Int16* samples, Uint64 maxCount)
void InputSoundFile::close() void InputSoundFile::close()
{ {
// Destroy the reader // Destroy the reader
delete m_reader; m_reader.reset();
m_reader = NULL;
// Destroy the stream if we own it m_ownedStream.reset();
if (m_streamOwned)
{ m_stream = nullptr;
delete m_stream;
m_streamOwned = false;
}
m_stream = NULL;
m_sampleOffset = 0; m_sampleOffset = 0;
// Reset the sound file attributes // Reset the sound file attributes

View file

@ -33,8 +33,7 @@
namespace sf namespace sf
{ {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
OutputSoundFile::OutputSoundFile() : OutputSoundFile::OutputSoundFile()
m_writer(NULL)
{ {
} }
@ -80,9 +79,8 @@ void OutputSoundFile::write(const Int16* samples, Uint64 count)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void OutputSoundFile::close() void OutputSoundFile::close()
{ {
// Destroy the reader // Destroy the writer
delete m_writer; m_writer.reset();
m_writer = NULL;
} }
} // namespace sf } // namespace sf

View file

@ -63,7 +63,7 @@ SoundFileFactory::WriterFactoryArray SoundFileFactory::s_writers;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
SoundFileReader* SoundFileFactory::createReaderFromFilename(const std::string& filename) std::unique_ptr<SoundFileReader> SoundFileFactory::createReaderFromFilename(const std::string& filename)
{ {
// Register the built-in readers/writers on first call // Register the built-in readers/writers on first call
ensureDefaultReadersWritersRegistered(); ensureDefaultReadersWritersRegistered();
@ -90,7 +90,7 @@ SoundFileReader* SoundFileFactory::createReaderFromFilename(const std::string& f
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
SoundFileReader* SoundFileFactory::createReaderFromMemory(const void* data, std::size_t sizeInBytes) std::unique_ptr<SoundFileReader> SoundFileFactory::createReaderFromMemory(const void* data, std::size_t sizeInBytes)
{ {
// Register the built-in readers/writers on first call // Register the built-in readers/writers on first call
ensureDefaultReadersWritersRegistered(); ensureDefaultReadersWritersRegistered();
@ -114,7 +114,7 @@ SoundFileReader* SoundFileFactory::createReaderFromMemory(const void* data, std:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
SoundFileReader* SoundFileFactory::createReaderFromStream(InputStream& stream) std::unique_ptr<SoundFileReader> SoundFileFactory::createReaderFromStream(InputStream& stream)
{ {
// Register the built-in readers/writers on first call // Register the built-in readers/writers on first call
ensureDefaultReadersWritersRegistered(); ensureDefaultReadersWritersRegistered();
@ -134,7 +134,7 @@ SoundFileReader* SoundFileFactory::createReaderFromStream(InputStream& stream)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
SoundFileWriter* SoundFileFactory::createWriterFromFilename(const std::string& filename) std::unique_ptr<SoundFileWriter> SoundFileFactory::createWriterFromFilename(const std::string& filename)
{ {
// Register the built-in readers/writers on first call // Register the built-in readers/writers on first call
ensureDefaultReadersWritersRegistered(); ensureDefaultReadersWritersRegistered();

View file

@ -70,14 +70,9 @@ namespace sf
Font::Font() : Font::Font() :
m_library (NULL), m_library (NULL),
m_face (NULL), m_face (NULL),
m_streamRec(NULL),
m_stroker (NULL), m_stroker (NULL),
m_refCount (NULL),
m_info () m_info ()
{ {
#ifdef SFML_SYSTEM_ANDROID
m_stream = NULL;
#endif
} }
@ -92,10 +87,6 @@ m_info (copy.m_info),
m_pages (copy.m_pages), m_pages (copy.m_pages),
m_pixelBuffer(copy.m_pixelBuffer) m_pixelBuffer(copy.m_pixelBuffer)
{ {
#ifdef SFML_SYSTEM_ANDROID
m_stream = NULL;
#endif
// Note: as FreeType doesn't provide functions for copying/cloning, // Note: as FreeType doesn't provide functions for copying/cloning,
// we must share all the FreeType pointers // we must share all the FreeType pointers
@ -108,13 +99,6 @@ m_pixelBuffer(copy.m_pixelBuffer)
Font::~Font() Font::~Font()
{ {
cleanup(); cleanup();
#ifdef SFML_SYSTEM_ANDROID
if (m_stream)
delete (priv::ResourceStream*)m_stream;
#endif
} }
@ -125,7 +109,7 @@ bool Font::loadFromFile(const std::string& filename)
// Cleanup the previous resources // Cleanup the previous resources
cleanup(); cleanup();
m_refCount = new int(1); m_refCount = std::make_shared<int>(1);
// Initialize FreeType // Initialize FreeType
// Note: we initialize FreeType for every font instance in order to avoid having a single // Note: we initialize FreeType for every font instance in order to avoid having a single
@ -173,11 +157,8 @@ bool Font::loadFromFile(const std::string& filename)
#else #else
if (m_stream) m_stream = std::make_shared<priv::ResourceStream>(filename);
delete (priv::ResourceStream*)m_stream; return loadFromStream(*std::static_pointer_cast<priv::ResourceStream>(m_stream));
m_stream = new priv::ResourceStream(filename);
return loadFromStream(*(priv::ResourceStream*)m_stream);
#endif #endif
} }
@ -188,7 +169,7 @@ bool Font::loadFromMemory(const void* data, std::size_t sizeInBytes)
{ {
// Cleanup the previous resources // Cleanup the previous resources
cleanup(); cleanup();
m_refCount = new int(1); m_refCount = std::make_shared<int>(1);
// Initialize FreeType // Initialize FreeType
// Note: we initialize FreeType for every font instance in order to avoid having a single // Note: we initialize FreeType for every font instance in order to avoid having a single
@ -241,7 +222,7 @@ bool Font::loadFromStream(InputStream& stream)
{ {
// Cleanup the previous resources // Cleanup the previous resources
cleanup(); cleanup();
m_refCount = new int(1); m_refCount = std::make_shared<int>(1);
// Initialize FreeType // Initialize FreeType
// Note: we initialize FreeType for every font instance in order to avoid having a single // Note: we initialize FreeType for every font instance in order to avoid having a single
@ -258,8 +239,8 @@ bool Font::loadFromStream(InputStream& stream)
stream.seek(0); stream.seek(0);
// Prepare a wrapper for our stream, that we'll pass to FreeType callbacks // Prepare a wrapper for our stream, that we'll pass to FreeType callbacks
FT_StreamRec* rec = new FT_StreamRec; auto rec = std::make_shared<FT_StreamRec>();
std::memset(rec, 0, sizeof(*rec)); std::memset(rec.get(), 0, sizeof(FT_StreamRec));
rec->base = NULL; rec->base = NULL;
rec->size = static_cast<unsigned long>(stream.getSize()); rec->size = static_cast<unsigned long>(stream.getSize());
rec->pos = 0; rec->pos = 0;
@ -270,7 +251,7 @@ bool Font::loadFromStream(InputStream& stream)
// Setup the FreeType callbacks that will read our stream // Setup the FreeType callbacks that will read our stream
FT_Open_Args args; FT_Open_Args args;
args.flags = FT_OPEN_STREAM; args.flags = FT_OPEN_STREAM;
args.stream = rec; args.stream = rec.get();
args.driver = 0; args.driver = 0;
// Load the new font face from the specified stream // Load the new font face from the specified stream
@ -278,7 +259,6 @@ bool Font::loadFromStream(InputStream& stream)
if (FT_Open_Face(static_cast<FT_Library>(m_library), &args, 0, &face) != 0) if (FT_Open_Face(static_cast<FT_Library>(m_library), &args, 0, &face) != 0)
{ {
err() << "Failed to load font from stream (failed to create the font face)" << std::endl; err() << "Failed to load font from stream (failed to create the font face)" << std::endl;
delete rec;
return false; return false;
} }
@ -296,7 +276,6 @@ bool Font::loadFromStream(InputStream& stream)
{ {
err() << "Failed to load font from stream (failed to set the Unicode character set)" << std::endl; err() << "Failed to load font from stream (failed to set the Unicode character set)" << std::endl;
FT_Done_Face(face); FT_Done_Face(face);
delete rec;
return false; return false;
} }
@ -477,7 +456,7 @@ void Font::cleanup()
if (*m_refCount == 0) if (*m_refCount == 0)
{ {
// Delete the reference counter // Delete the reference counter
delete m_refCount; m_refCount.reset();
// Destroy the stroker // Destroy the stroker
if (m_stroker) if (m_stroker)
@ -488,8 +467,7 @@ void Font::cleanup()
FT_Done_Face(static_cast<FT_Face>(m_face)); FT_Done_Face(static_cast<FT_Face>(m_face));
// Destroy the stream rec instance, if any (must be done after FT_Done_Face!) // Destroy the stream rec instance, if any (must be done after FT_Done_Face!)
if (m_streamRec) m_streamRec.reset();
delete static_cast<FT_StreamRec*>(m_streamRec);
// Close the library // Close the library
if (m_library) if (m_library)
@ -502,7 +480,6 @@ void Font::cleanup()
m_face = NULL; m_face = NULL;
m_stroker = NULL; m_stroker = NULL;
m_streamRec = NULL; m_streamRec = NULL;
m_refCount = NULL;
m_pages.clear(); m_pages.clear();
std::vector<Uint8>().swap(m_pixelBuffer); std::vector<Uint8>().swap(m_pixelBuffer);
} }

View file

@ -41,24 +41,11 @@ namespace sf
Image::Image() : Image::Image() :
m_size(0, 0) m_size(0, 0)
{ {
#ifdef SFML_SYSTEM_ANDROID
m_stream = NULL;
#endif
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Image::~Image() Image::~Image() = default;
{
#ifdef SFML_SYSTEM_ANDROID
if (m_stream)
delete (priv::ResourceStream*)m_stream;
#endif
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
@ -135,11 +122,8 @@ bool Image::loadFromFile(const std::string& filename)
#else #else
if (m_stream) m_stream = std::make_shared<priv::ResourceStream>(filename);
delete (priv::ResourceStream*)m_stream; return loadFromStream(*std::static_pointer_cast<priv::ResourceStream*>(m_stream));
m_stream = new priv::ResourceStream(filename);
return loadFromStream(*(priv::ResourceStream*)m_stream);
#endif #endif
} }

View file

@ -34,18 +34,11 @@
namespace sf namespace sf
{ {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
RenderTexture::RenderTexture() : RenderTexture::RenderTexture() = default;
m_impl(NULL)
{
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
RenderTexture::~RenderTexture() RenderTexture::~RenderTexture() = default;
{
delete m_impl;
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
@ -62,11 +55,10 @@ bool RenderTexture::create(unsigned int width, unsigned int height, bool depthBu
setSmooth(false); setSmooth(false);
// Create the implementation // Create the implementation
delete m_impl;
if (priv::RenderTextureImplFBO::isAvailable()) if (priv::RenderTextureImplFBO::isAvailable())
{ {
// Use frame-buffer object (FBO) // Use frame-buffer object (FBO)
m_impl = new priv::RenderTextureImplFBO; m_impl = std::make_unique<priv::RenderTextureImplFBO>();
// Mark the texture as being a framebuffer object attachment // Mark the texture as being a framebuffer object attachment
m_texture.m_fboAttachment = true; m_texture.m_fboAttachment = true;
@ -74,7 +66,7 @@ bool RenderTexture::create(unsigned int width, unsigned int height, bool depthBu
else else
{ {
// Use default implementation // Use default implementation
m_impl = new priv::RenderTextureImplDefault; m_impl = std::make_unique<priv::RenderTextureImplDefault>();
} }
// Initialize the render texture // Initialize the render texture

View file

@ -38,7 +38,6 @@ namespace priv
{ {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
RenderTextureImplDefault::RenderTextureImplDefault() : RenderTextureImplDefault::RenderTextureImplDefault() :
m_context(0),
m_width (0), m_width (0),
m_height (0) m_height (0)
{ {
@ -46,14 +45,6 @@ m_height (0)
} }
////////////////////////////////////////////////////////////
RenderTextureImplDefault::~RenderTextureImplDefault()
{
// Destroy the context
delete m_context;
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool RenderTextureImplDefault::create(unsigned int width, unsigned int height, unsigned int, bool depthBuffer) bool RenderTextureImplDefault::create(unsigned int width, unsigned int height, unsigned int, bool depthBuffer)
{ {
@ -62,7 +53,7 @@ bool RenderTextureImplDefault::create(unsigned int width, unsigned int height, u
m_height = height; m_height = height;
// Create the in-memory OpenGL context // Create the in-memory OpenGL context
m_context = new Context(ContextSettings(depthBuffer ? 32 : 0), width, height); m_context = std::make_unique<Context>(ContextSettings(depthBuffer ? 32 : 0), width, height);
return true; return true;
} }

View file

@ -31,6 +31,7 @@
#include <SFML/Graphics/RenderTextureImpl.hpp> #include <SFML/Graphics/RenderTextureImpl.hpp>
#include <SFML/Window/GlResource.hpp> #include <SFML/Window/GlResource.hpp>
#include <SFML/Window/Context.hpp> #include <SFML/Window/Context.hpp>
#include <memory>
namespace sf namespace sf
@ -52,12 +53,6 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
RenderTextureImplDefault(); RenderTextureImplDefault();
////////////////////////////////////////////////////////////
/// \brief Destructor
///
////////////////////////////////////////////////////////////
~RenderTextureImplDefault();
private: private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
@ -94,9 +89,9 @@ private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Context* m_context; ///< P-Buffer based context std::unique_ptr<Context> m_context; ///< P-Buffer based context
unsigned int m_width; ///< Width of the P-Buffer unsigned int m_width; ///< Width of the P-Buffer
unsigned int m_height; ///< Height of the P-Buffer unsigned int m_height; ///< Height of the P-Buffer
}; };
} // namespace priv } // namespace priv

View file

@ -37,7 +37,6 @@ namespace priv
{ {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
RenderTextureImplFBO::RenderTextureImplFBO() : RenderTextureImplFBO::RenderTextureImplFBO() :
m_context (NULL),
m_frameBuffer(0), m_frameBuffer(0),
m_depthBuffer(0) m_depthBuffer(0)
{ {
@ -63,9 +62,6 @@ RenderTextureImplFBO::~RenderTextureImplFBO()
GLuint frameBuffer = static_cast<GLuint>(m_frameBuffer); GLuint frameBuffer = static_cast<GLuint>(m_frameBuffer);
glCheck(GLEXT_glDeleteFramebuffers(1, &frameBuffer)); glCheck(GLEXT_glDeleteFramebuffers(1, &frameBuffer));
} }
// Delete the context
delete m_context;
} }
@ -85,7 +81,7 @@ bool RenderTextureImplFBO::isAvailable()
bool RenderTextureImplFBO::create(unsigned int width, unsigned int height, unsigned int textureId, bool depthBuffer) bool RenderTextureImplFBO::create(unsigned int width, unsigned int height, unsigned int textureId, bool depthBuffer)
{ {
// Create the context // Create the context
m_context = new Context; m_context = std::make_unique<Context>();
// Create the framebuffer object // Create the framebuffer object
GLuint frameBuffer = 0; GLuint frameBuffer = 0;

View file

@ -31,6 +31,7 @@
#include <SFML/Graphics/RenderTextureImpl.hpp> #include <SFML/Graphics/RenderTextureImpl.hpp>
#include <SFML/Window/Context.hpp> #include <SFML/Window/Context.hpp>
#include <SFML/Window/GlResource.hpp> #include <SFML/Window/GlResource.hpp>
#include <memory>
namespace sf namespace sf
@ -102,9 +103,9 @@ private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Context* m_context; ///< Needs a separate OpenGL context for not messing up the other ones std::unique_ptr<Context> m_context; ///< Needs a separate OpenGL context for not messing up the other ones
unsigned int m_frameBuffer; ///< OpenGL frame buffer object unsigned int m_frameBuffer; ///< OpenGL frame buffer object
unsigned int m_depthBuffer; ///< Optional depth buffer attached to the frame buffer unsigned int m_depthBuffer; ///< Optional depth buffer attached to the frame buffer
}; };
} // namespace priv } // namespace priv

View file

@ -51,7 +51,7 @@ struct SocketSelector::SocketSelectorImpl
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
SocketSelector::SocketSelector() : SocketSelector::SocketSelector() :
m_impl(new SocketSelectorImpl) m_impl(std::make_unique<SocketSelectorImpl>())
{ {
clear(); clear();
} }
@ -59,17 +59,14 @@ m_impl(new SocketSelectorImpl)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
SocketSelector::SocketSelector(const SocketSelector& copy) : SocketSelector::SocketSelector(const SocketSelector& copy) :
m_impl(new SocketSelectorImpl(*copy.m_impl)) m_impl(std::make_unique<SocketSelectorImpl>(*copy.m_impl))
{ {
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
SocketSelector::~SocketSelector() SocketSelector::~SocketSelector() = default;
{
delete m_impl;
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View file

@ -40,19 +40,13 @@ public:
DefaultErrStreamBuf() DefaultErrStreamBuf()
{ {
// Allocate the write buffer setp(m_buffer, m_buffer + sizeof(m_buffer));
constexpr auto size = 64;
char* buffer = new char[size];
setp(buffer, buffer + size);
} }
~DefaultErrStreamBuf() ~DefaultErrStreamBuf()
{ {
// Synchronize // Synchronize
sync(); sync();
// Delete the write buffer
delete[] pbase();
} }
private: private:
@ -92,6 +86,8 @@ private:
return 0; return 0;
} }
char m_buffer[64];
}; };
} }

View file

@ -44,10 +44,7 @@ FileInputStream::FileInputStream()
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
FileInputStream::~FileInputStream() FileInputStream::~FileInputStream()
{ {
#ifdef ANDROID #ifndef ANDROID
if (m_file)
delete m_file;
#else
if (m_file) if (m_file)
std::fclose(m_file); std::fclose(m_file);
#endif #endif
@ -58,9 +55,7 @@ FileInputStream::~FileInputStream()
bool FileInputStream::open(const std::string& filename) bool FileInputStream::open(const std::string& filename)
{ {
#ifdef ANDROID #ifdef ANDROID
if (m_file) m_file = std::make_unique<priv::ResourceStream>(filename);
delete m_file;
m_file = new priv::ResourceStream(filename);
return m_file->tell() != -1; return m_file->tell() != -1;
#else #else
if (m_file) if (m_file)

View file

@ -39,15 +39,12 @@ namespace sf
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Mutex::Mutex() Mutex::Mutex()
{ {
m_mutexImpl = new priv::MutexImpl; m_mutexImpl = std::make_shared<priv::MutexImpl>();
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Mutex::~Mutex() Mutex::~Mutex() = default;
{
delete m_mutexImpl;
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View file

@ -41,7 +41,6 @@ namespace sf
Thread::~Thread() Thread::~Thread()
{ {
wait(); wait();
delete m_entryPoint;
} }
@ -49,7 +48,7 @@ Thread::~Thread()
void Thread::launch() void Thread::launch()
{ {
wait(); wait();
m_impl = new priv::ThreadImpl(this); m_impl = std::make_shared<priv::ThreadImpl>(this);
} }
@ -59,8 +58,7 @@ void Thread::wait()
if (m_impl) if (m_impl)
{ {
m_impl->wait(); m_impl->wait();
delete m_impl; m_impl.reset();
m_impl = NULL;
} }
} }
@ -71,8 +69,7 @@ void Thread::terminate()
if (m_impl) if (m_impl)
{ {
m_impl->terminate(); m_impl->terminate();
delete m_impl; m_impl.reset();
m_impl = NULL;
} }
} }

View file

@ -39,16 +39,13 @@ namespace sf
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
ThreadLocal::ThreadLocal(void* value) ThreadLocal::ThreadLocal(void* value)
{ {
m_impl = new priv::ThreadLocalImpl; m_impl = std::make_unique<priv::ThreadLocalImpl>();
setValue(value); setValue(value);
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
ThreadLocal::~ThreadLocal() ThreadLocal::~ThreadLocal() = default;
{
delete m_impl;
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View file

@ -50,7 +50,6 @@ Context::Context()
Context::~Context() Context::~Context()
{ {
setActive(false); setActive(false);
delete m_context;
} }

View file

@ -143,7 +143,7 @@ namespace
sf::ThreadLocalPtr<sf::priv::GlContext> currentContext(NULL); sf::ThreadLocalPtr<sf::priv::GlContext> currentContext(NULL);
// The hidden, inactive context that will be shared with all other contexts // The hidden, inactive context that will be shared with all other contexts
ContextType* sharedContext = NULL; std::unique_ptr<ContextType> sharedContext;
// This structure contains all the state necessary to // This structure contains all the state necessary to
// track TransientContext usage // track TransientContext usage
@ -155,17 +155,15 @@ namespace
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
TransientContext() : TransientContext() :
referenceCount (0), referenceCount (0),
context (0),
sharedContextLock(0),
useSharedContext (false) useSharedContext (false)
{ {
if (resourceCount == 0) if (resourceCount == 0)
{ {
context = new sf::Context; context = std::make_unique<sf::Context>();
} }
else if (!currentContext) else if (!currentContext)
{ {
sharedContextLock = new sf::Lock(mutex); sharedContextLock = std::make_unique<sf::Lock>(mutex);
useSharedContext = true; useSharedContext = true;
sharedContext->setActive(true); sharedContext->setActive(true);
} }
@ -179,23 +177,20 @@ namespace
{ {
if (useSharedContext) if (useSharedContext)
sharedContext->setActive(false); sharedContext->setActive(false);
delete sharedContextLock;
delete context;
} }
/////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
unsigned int referenceCount; unsigned int referenceCount;
sf::Context* context; std::unique_ptr<sf::Context> context;
sf::Lock* sharedContextLock; std::unique_ptr<sf::Lock> sharedContextLock;
bool useSharedContext; bool useSharedContext;
}; };
// This per-thread variable tracks if and how a transient // This per-thread variable tracks if and how a transient
// context is currently being used on the current thread // context is currently being used on the current thread
sf::ThreadLocalPtr<TransientContext> transientContext(NULL); thread_local std::unique_ptr<TransientContext> transientContext;
// Supported OpenGL extensions // Supported OpenGL extensions
std::vector<std::string> extensions; std::vector<std::string> extensions;
@ -224,7 +219,7 @@ void GlContext::initResource()
} }
// Create the shared context // Create the shared context
sharedContext = new ContextType(NULL); sharedContext = std::make_unique<ContextType>(nullptr);
sharedContext->initialize(ContextSettings()); sharedContext->initialize(ContextSettings());
// Load our extensions vector // Load our extensions vector
@ -294,12 +289,8 @@ void GlContext::cleanupResource()
// If there's no more resource alive, we can trigger the global context cleanup // If there's no more resource alive, we can trigger the global context cleanup
if (resourceCount == 0) if (resourceCount == 0)
{ {
if (!sharedContext)
return;
// Destroy the shared context // Destroy the shared context
delete sharedContext; sharedContext.reset();
sharedContext = NULL;
} }
} }
@ -313,7 +304,7 @@ void GlContext::acquireTransientContext()
// If this is the first TransientContextLock on this thread // If this is the first TransientContextLock on this thread
// construct the state object // construct the state object
if (!transientContext) if (!transientContext)
transientContext = new TransientContext; transientContext = std::make_unique<TransientContext>();
// Increase the reference count // Increase the reference count
transientContext->referenceCount++; transientContext->referenceCount++;
@ -335,22 +326,19 @@ void GlContext::releaseTransientContext()
// If this is the last TransientContextLock that is released // If this is the last TransientContextLock that is released
// destroy the state object // destroy the state object
if (transientContext->referenceCount == 0) if (transientContext->referenceCount == 0)
{ transientContext.reset();
delete transientContext;
transientContext = NULL;
}
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
GlContext* GlContext::create() std::unique_ptr<GlContext> GlContext::create()
{ {
// Make sure that there's an active context (context creation may need extensions, and thus a valid context) // Make sure that there's an active context (context creation may need extensions, and thus a valid context)
assert(sharedContext != NULL); assert(sharedContext != NULL);
Lock lock(mutex); Lock lock(mutex);
GlContext* context = NULL; std::unique_ptr<GlContext> context;
// We don't use acquireTransientContext here since we have // We don't use acquireTransientContext here since we have
// to ensure we have exclusive access to the shared context // to ensure we have exclusive access to the shared context
@ -359,7 +347,7 @@ GlContext* GlContext::create()
sharedContext->setActive(true); sharedContext->setActive(true);
// Create the context // Create the context
context = new ContextType(sharedContext); context = std::make_unique<ContextType>(sharedContext.get());
sharedContext->setActive(false); sharedContext->setActive(false);
} }
@ -371,14 +359,14 @@ GlContext* GlContext::create()
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
GlContext* GlContext::create(const ContextSettings& settings, const WindowImpl* owner, unsigned int bitsPerPixel) std::unique_ptr<GlContext> GlContext::create(const ContextSettings& settings, const WindowImpl* owner, unsigned int bitsPerPixel)
{ {
// Make sure that there's an active context (context creation may need extensions, and thus a valid context) // Make sure that there's an active context (context creation may need extensions, and thus a valid context)
assert(sharedContext != NULL); assert(sharedContext != NULL);
Lock lock(mutex); Lock lock(mutex);
GlContext* context = NULL; std::unique_ptr<GlContext> context;
// We don't use acquireTransientContext here since we have // We don't use acquireTransientContext here since we have
// to ensure we have exclusive access to the shared context // to ensure we have exclusive access to the shared context
@ -387,7 +375,7 @@ GlContext* GlContext::create(const ContextSettings& settings, const WindowImpl*
sharedContext->setActive(true); sharedContext->setActive(true);
// Create the context // Create the context
context = new ContextType(sharedContext, settings, owner, bitsPerPixel); context = std::make_unique<ContextType>(sharedContext.get(), settings, owner, bitsPerPixel);
sharedContext->setActive(false); sharedContext->setActive(false);
} }
@ -400,14 +388,14 @@ GlContext* GlContext::create(const ContextSettings& settings, const WindowImpl*
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
GlContext* GlContext::create(const ContextSettings& settings, unsigned int width, unsigned int height) std::unique_ptr<GlContext> GlContext::create(const ContextSettings& settings, unsigned int width, unsigned int height)
{ {
// Make sure that there's an active context (context creation may need extensions, and thus a valid context) // Make sure that there's an active context (context creation may need extensions, and thus a valid context)
assert(sharedContext != NULL); assert(sharedContext != NULL);
Lock lock(mutex); Lock lock(mutex);
GlContext* context = NULL; std::unique_ptr<GlContext> context;
// We don't use acquireTransientContext here since we have // We don't use acquireTransientContext here since we have
// to ensure we have exclusive access to the shared context // to ensure we have exclusive access to the shared context
@ -416,7 +404,7 @@ GlContext* GlContext::create(const ContextSettings& settings, unsigned int width
sharedContext->setActive(true); sharedContext->setActive(true);
// Create the context // Create the context
context = new ContextType(sharedContext, settings, width, height); context = std::make_unique<ContextType>(sharedContext.get(), settings, width, height);
sharedContext->setActive(false); sharedContext->setActive(false);
} }

View file

@ -32,6 +32,7 @@
#include <SFML/Window/Context.hpp> #include <SFML/Window/Context.hpp>
#include <SFML/Window/ContextSettings.hpp> #include <SFML/Window/ContextSettings.hpp>
#include <SFML/System/NonCopyable.hpp> #include <SFML/System/NonCopyable.hpp>
#include <memory>
namespace sf namespace sf
@ -90,7 +91,7 @@ public:
/// \return Pointer to the created context (don't forget to delete it) /// \return Pointer to the created context (don't forget to delete it)
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
static GlContext* create(); static std::unique_ptr<GlContext> create();
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Create a new context attached to a window /// \brief Create a new context attached to a window
@ -105,7 +106,7 @@ public:
/// \return Pointer to the created context /// \return Pointer to the created context
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
static GlContext* create(const ContextSettings& settings, const WindowImpl* owner, unsigned int bitsPerPixel); static std::unique_ptr<GlContext> create(const ContextSettings& settings, const WindowImpl* owner, unsigned int bitsPerPixel);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Create a new context that embeds its own rendering target /// \brief Create a new context that embeds its own rendering target
@ -120,7 +121,7 @@ public:
/// \return Pointer to the created context /// \return Pointer to the created context
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
static GlContext* create(const ContextSettings& settings, unsigned int width, unsigned int height); static std::unique_ptr<GlContext> create(const ContextSettings& settings, unsigned int width, unsigned int height);
public: public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View file

@ -42,8 +42,6 @@ namespace sf
{ {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Window::Window() : Window::Window() :
m_impl (NULL),
m_context (NULL),
m_frameTimeLimit(Time::Zero), m_frameTimeLimit(Time::Zero),
m_size (0, 0) m_size (0, 0)
{ {
@ -53,8 +51,6 @@ m_size (0, 0)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Window::Window(VideoMode mode, const String& title, Uint32 style, const ContextSettings& settings) : Window::Window(VideoMode mode, const String& title, Uint32 style, const ContextSettings& settings) :
m_impl (NULL),
m_context (NULL),
m_frameTimeLimit(Time::Zero), m_frameTimeLimit(Time::Zero),
m_size (0, 0) m_size (0, 0)
{ {
@ -64,8 +60,6 @@ m_size (0, 0)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Window::Window(WindowHandle handle, const ContextSettings& settings) : Window::Window(WindowHandle handle, const ContextSettings& settings) :
m_impl (NULL),
m_context (NULL),
m_frameTimeLimit(Time::Zero), m_frameTimeLimit(Time::Zero),
m_size (0, 0) m_size (0, 0)
{ {
@ -124,7 +118,7 @@ void Window::create(VideoMode mode, const String& title, Uint32 style, const Con
m_impl = priv::WindowImpl::create(mode, title, style, settings); m_impl = priv::WindowImpl::create(mode, title, style, settings);
// Recreate the context // Recreate the context
m_context = priv::GlContext::create(settings, m_impl, mode.bitsPerPixel); m_context = priv::GlContext::create(settings, m_impl.get(), mode.bitsPerPixel);
// Perform common initializations // Perform common initializations
initialize(); initialize();
@ -141,7 +135,7 @@ void Window::create(WindowHandle handle, const ContextSettings& settings)
m_impl = priv::WindowImpl::create(handle); m_impl = priv::WindowImpl::create(handle);
// Recreate the context // Recreate the context
m_context = priv::GlContext::create(settings, m_impl, VideoMode::getDesktopMode().bitsPerPixel); m_context = priv::GlContext::create(settings, m_impl.get(), VideoMode::getDesktopMode().bitsPerPixel);
// Perform common initializations // Perform common initializations
initialize(); initialize();
@ -151,13 +145,11 @@ void Window::create(WindowHandle handle, const ContextSettings& settings)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void Window::close() void Window::close()
{ {
// Delete the context // Reset the context
delete m_context; m_context.reset();
m_context = NULL;
// Delete the window implementation // Reset the window implementation
delete m_impl; m_impl.reset();
m_impl = NULL;
// Update the fullscreen window // Update the fullscreen window
if (this == fullscreenWindow) if (this == fullscreenWindow)

View file

@ -66,16 +66,16 @@ namespace sf
namespace priv namespace priv
{ {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
WindowImpl* WindowImpl::create(VideoMode mode, const String& title, Uint32 style, const ContextSettings& settings) std::unique_ptr<WindowImpl> WindowImpl::create(VideoMode mode, const String& title, Uint32 style, const ContextSettings& settings)
{ {
return new WindowImplType(mode, title, style, settings); return std::make_unique<WindowImplType>(mode, title, style, settings);
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
WindowImpl* WindowImpl::create(WindowHandle handle) std::unique_ptr<WindowImpl> WindowImpl::create(WindowHandle handle)
{ {
return new WindowImplType(handle); return std::make_unique<WindowImplType>(handle);
} }

View file

@ -41,6 +41,8 @@
#include <SFML/Window/ContextSettings.hpp> #include <SFML/Window/ContextSettings.hpp>
#include <queue> #include <queue>
#include <set> #include <set>
#include <memory>
namespace sf namespace sf
{ {
@ -67,7 +69,7 @@ public:
/// \return Pointer to the created window (don't forget to delete it) /// \return Pointer to the created window (don't forget to delete it)
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
static WindowImpl* create(VideoMode mode, const String& title, Uint32 style, const ContextSettings& settings); static std::unique_ptr<WindowImpl> create(VideoMode mode, const String& title, Uint32 style, const ContextSettings& settings);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Create a new window depending on to the current OS /// \brief Create a new window depending on to the current OS
@ -77,7 +79,7 @@ public:
/// \return Pointer to the created window (don't forget to delete it) /// \return Pointer to the created window (don't forget to delete it)
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
static WindowImpl* create(WindowHandle handle); static std::unique_ptr<WindowImpl> create(WindowHandle handle);
public: public: