Browse Source

libobs/util: Add WinModule RAII wrapper

jpark37 4 years ago
parent
commit
7864c8649a
1 changed files with 31 additions and 0 deletions
  1. 31 0
      libobs/util/windows/WinHandle.hpp

+ 31 - 0
libobs/util/windows/WinHandle.hpp

@@ -49,3 +49,34 @@ public:
 		return handle && handle != INVALID_HANDLE_VALUE;
 	}
 };
+
+class WinModule {
+	HMODULE handle = NULL;
+
+	inline void Clear()
+	{
+		if (handle)
+			FreeLibrary(handle);
+	}
+
+public:
+	inline WinModule() {}
+	inline WinModule(HMODULE handle_) : handle(handle_) {}
+	inline ~WinModule() { Clear(); }
+
+	inline operator HMODULE() const { return handle; }
+
+	inline WinModule &operator=(HMODULE handle_)
+	{
+		if (handle_ != handle) {
+			Clear();
+			handle = handle_;
+		}
+
+		return *this;
+	}
+
+	inline HMODULE *operator&() { return &handle; }
+
+	inline bool Valid() const { return handle != NULL; }
+};