From a8b01382bfe16939c9ee1d392259bf7d915ba4ec Mon Sep 17 00:00:00 2001 From: "Arthur Brainville (Ybalrid)" Date: Sun, 5 Jan 2020 15:30:11 +0100 Subject: [PATCH] Add default ctor for sdl::SharedObject --- sources/cpp-sdl2/shared_object.hpp | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/sources/cpp-sdl2/shared_object.hpp b/sources/cpp-sdl2/shared_object.hpp index 95d2ae19..f66f83cf 100644 --- a/sources/cpp-sdl2/shared_object.hpp +++ b/sources/cpp-sdl2/shared_object.hpp @@ -32,15 +32,12 @@ class SharedObject if (!handle_) throw Exception("SDL_LoadObject"); } + ///Construct an empty shared object handler. You can move assign to it. + SharedObject() = default; + ///Automatically unload the library for you ~SharedObject() { SDL_UnloadObject(handle_); } - ///This class isn't copyable - SharedObject(SharedObject const&) = delete; - - ///This class isn't copyable - SharedObject& operator=(SharedObject const&) = delete; - /// Move ctor SharedObject(SharedObject&& other) noexcept { *this = std::move(other); } @@ -49,13 +46,27 @@ class SharedObject { if (handle_ != other.handle_) { - if (handle_) SDL_UnloadObject(handle_); + // If we currently hold an object + if (handle_) + { + // Free so we do not leak + SDL_UnloadObject(handle_); + } + + // Assign from other handle, and set it to null handle_ = other.handle_; other.handle_ = nullptr; } + return *this; } + ///This class isn't copyable + SharedObject(SharedObject const&) = delete; + + ///This class isn't copyable + SharedObject& operator=(SharedObject const&) = delete; + ///Retrieve the raw address of a function inside the owned object. User has to cast this to the correct function pointer type FunctionAddress function_pointer(std::string const& functionName) const {