File tree Expand file tree Collapse file tree 5 files changed +32
-48
lines changed Expand file tree Collapse file tree 5 files changed +32
-48
lines changed Original file line number Diff line number Diff line change @@ -35,28 +35,22 @@ sfMusic* sfMusic_createFromFile(const char* filename)
3535{
3636 assert (filename);
3737
38- auto * music = new sfMusic;
39- if (!music->This .openFromFile (filename))
40- {
41- delete music;
42- music = nullptr ;
43- }
44-
45- return music;
38+ sf::Music music;
39+ if (!music.openFromFile (filename))
40+ return nullptr ;
41+
42+ return new sfMusic{{}, std::move (music)};
4643}
4744
4845
4946// //////////////////////////////////////////////////////////
5047sfMusic* sfMusic_createFromMemory (const void * data, size_t sizeInBytes)
5148{
52- auto * music = new sfMusic;
53- if (!music->This .openFromMemory (data, sizeInBytes))
54- {
55- delete music;
56- music = nullptr ;
57- }
58-
59- return music;
49+ sf::Music music;
50+ if (!music.openFromMemory (data, sizeInBytes))
51+ return nullptr ;
52+
53+ return new sfMusic{{}, std::move (music)};
6054}
6155
6256
@@ -65,15 +59,12 @@ sfMusic* sfMusic_createFromStream(sfInputStream* stream)
6559{
6660 assert (stream);
6761
68- auto * music = new sfMusic;
69- music->Stream = CallbackStream (stream);
70- if (!music->This .openFromStream (music->Stream ))
71- {
72- delete music;
73- music = nullptr ;
74- }
62+ auto callbackStream = std::make_unique<CallbackStream>(stream);
63+ sf::Music music;
64+ if (!music.openFromStream (*callbackStream))
65+ return nullptr ;
7566
76- return music;
67+ return new sfMusic{ std::move (callbackStream), std::move ( music)} ;
7768}
7869
7970
Original file line number Diff line number Diff line change 3131
3232#include < SFML/Audio/Music.hpp>
3333
34+ #include < memory>
35+
3436
3537// //////////////////////////////////////////////////////////
3638// Internal structure of sfMusic
3739// //////////////////////////////////////////////////////////
3840struct sfMusic
3941{
40- CallbackStream Stream;
41- sf::Music This;
42+ std::unique_ptr< CallbackStream> Stream;
43+ sf::Music This;
4244};
Original file line number Diff line number Diff line change @@ -60,23 +60,20 @@ sfFont* sfFont_createFromStream(sfInputStream* stream)
6060{
6161 assert (stream);
6262
63- auto * font = new sfFont;
64- font->Stream = CallbackStream (stream);
65- if (!font->This .openFromStream (font->Stream ))
66- {
67- delete font;
68- font = nullptr ;
69- }
63+ auto callbackStream = std::make_unique<CallbackStream>(stream);
64+ sf::Font font;
65+ if (!font.openFromStream (*callbackStream))
66+ return nullptr ;
7067
71- return font;
68+ return new sfFont{ std::move ( font), {}, std::move (callbackStream)} ;
7269}
7370
7471
7572// //////////////////////////////////////////////////////////
7673sfFont* sfFont_copy (const sfFont* font)
7774{
7875 assert (font);
79- return new sfFont (*font) ;
76+ return new sfFont{font-> This , font-> Textures , std::make_unique<CallbackStream> (*font-> Stream )} ;
8077}
8178
8279
Original file line number Diff line number Diff line change @@ -42,5 +42,5 @@ struct sfFont
4242{
4343 sf::Font This;
4444 std::map<unsigned int , sfTexture> Textures;
45- CallbackStream Stream;
45+ std::unique_ptr< CallbackStream> Stream;
4646};
Original file line number Diff line number Diff line change 3232// //////////////////////////////////////////////////////////
3333sfVertexBuffer* sfVertexBuffer_create (unsigned int vertexCount, sfPrimitiveType type, sfVertexBufferUsage usage)
3434{
35- auto * buffer = new sfVertexBuffer;
35+ sf::VertexBuffer buffer;
36+ if (!buffer.create (vertexCount))
37+ return nullptr ;
3638
37- if (!buffer->This .create (vertexCount))
38- {
39- delete buffer;
40- buffer = nullptr ;
41- }
42- else
43- {
44- buffer->This .setPrimitiveType (static_cast <sf::PrimitiveType>(type));
45- buffer->This .setUsage (static_cast <sf::VertexBuffer::Usage>(usage));
46- }
39+ buffer.setPrimitiveType (static_cast <sf::PrimitiveType>(type));
40+ buffer.setUsage (static_cast <sf::VertexBuffer::Usage>(usage));
4741
48- return buffer;
42+ return new sfVertexBuffer{ std::move ( buffer)} ;
4943}
5044
5145
You can’t perform that action at this time.
0 commit comments