Question 1:
*pTempTexture
uses the built-in indirection operator, and so it's an lvalue expression. You're right to explicitly move from it, since it is not temporary.
It probably doesn't do you any good because SDL_Texture is POD (SDL is a C library), but it is correct C++ code unless the SDL says otherwise (which it might).
Question 2:
The explicit call to
reset()
is unnecessary.
*** Why did you decide you need shared ownership?
Also, by default, the smart pointers assume they manage memory allocated with
new
; they will
delete
their managed resource. You can avoid this by providing a custom deleter. Since
std::shared_ptr
type-erases it, you can give the constructor the corresponding function/functor:
1 2
|
std::shared_ptr<SDL_Texture>{SDL_CreateTextureFromSurface(pRenderer, pSurfaceTemp),
SDL_DestroyTexture};
|
For
std::unique_ptr
, the deleter is part of the type. I use this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
/** \brief Generic deleter functor for SDL resources. For use with std::unique_ptr.
*/
struct SDLDeleter {
/* Generate "free" calls. */
# if ! defined FREE_ME
# define FREE_ME(type, fn_name) void operator()(type * const thing) \
const { if (thing) SDL_##fn_name(thing); }
FREE_ME(SDL_RWops, FreeRW) FREE_ME(SDL_cond, DestroyCond)
FREE_ME(SDL_Cursor, FreeCursor) FREE_ME(SDL_PixelFormat, FreeFormat)
FREE_ME(SDL_mutex, DestroyMutex) FREE_ME(SDL_Palette, FreePalette)
FREE_ME(SDL_Renderer, DestroyRenderer) FREE_ME(SDL_sem, DestroySemaphore)
FREE_ME(SDL_Surface, FreeSurface) FREE_ME(SDL_Texture, DestroyTexture)
FREE_ME(Uint8, FreeWAV) FREE_ME(SDL_Window, DestroyWindow)
# undef FREE_ME
# else
# error "FREE_ME already defined"
# endif
};
|
Like this:
1 2
|
template <typename T>
using SDL_unique_ptr = std::unique_ptr<T, SDLDeleter>;
|
Question 3:
Let's say I use make_shared to store *SDL_Texture instead of SDL_Texture inside my map |
You're not storing
SDL_Texture
nor
SDL_Texture*
in your map. You're storing
std::shared_ptr<SDL_Texture>
.
make_shared
in this case is not useful because you cannot supply a custom deleter. You need to use the std::shared_ptr constructor to do that.
make_shared
allocates its' memory with
new
and then forwards its' arguments to the constructor of the newly managed resource. In this case, it move-constructs a
SDL_Texture
in situ. The result of
std::make_shared
manages that memory.