Smart pointers with SDL

不想你离开。 提交于 2019-11-27 19:31:57

You could create a functor that has several overloaded operator() implementations, each of which call the correct destroy function for the respective argument type.

struct sdl_deleter
{
  void operator()(SDL_Window *p) const { SDL_DestroyWindow(p); }
  void operator()(SDL_Renderer *p) const { SDL_DestroyRenderer(p); }
  void operator()(SDL_Texture *p) const { SDL_DestroyTexture(p); }
};

Pass this as the deleter to a unique_ptr, and you could write wrapper functions if you wanted to, to create the unique_ptrs

unique_ptr<SDL_Window, sdl_deleter>
create_window(char const *title, int x, int y, int w, int h, Uint32 flags)
{
    return unique_ptr<SDL_Window, sdl_deleter>(
             SDL_CreateWindow(title, x, y, w, h, flags), 
             sdl_deleter());
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!