diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2016-08-10 16:54:06 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2016-08-10 16:54:06 +0200 |
commit | 28ab0947c7fbf4224ece0d902be9f946807015d8 (patch) | |
tree | 5730670e1ae6f58334c05718cc414045d92e5696 /opentrack-dinput/dinput.hpp | |
parent | cdacaf690a48a3cc1549d9c6e57de2100dc1fc21 (diff) |
dinput: prevent freeing handle despite static initializer order
Diffstat (limited to 'opentrack-dinput/dinput.hpp')
-rw-r--r-- | opentrack-dinput/dinput.hpp | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/opentrack-dinput/dinput.hpp b/opentrack-dinput/dinput.hpp index db901887..37a6d723 100644 --- a/opentrack-dinput/dinput.hpp +++ b/opentrack-dinput/dinput.hpp @@ -1,28 +1,55 @@ +/* Copyright (c) 2016, Stanislaw Halik <sthalik@misaki.pl> + + * Permission to use, copy, modify, and/or distribute this + * software for any purpose with or without fee is hereby granted, + * provided that the above copyright notice and this permission + * notice appear in all copies. + */ + #pragma once #ifdef _WIN32 +#include "export.hpp" + #ifndef DIRECTINPUT_VERSION # define DIRECTINPUT_VERSION 0x800 #endif -#include "export.hpp" #include <dinput.h> -#include <windows.h> -struct OPENTRACK_DINPUT_EXPORT dinput_handle final +#include <atomic> + +class OPENTRACK_DINPUT_EXPORT dinput_handle final { - using di_t = LPDIRECTINPUT8; +public: + class di_t; + private: - static dinput_handle self; - dinput_handle(); - ~dinput_handle(); - static di_t init_di(); - di_t handle; + static std::atomic<int> refcnt; + static std::atomic_flag init_lock; + static di_t handle; + + static LPDIRECTINPUT8& init_di(); public: - static di_t make_di(); + class di_t final + { + friend class dinput_handle; + + LPDIRECTINPUT8& handle; + + di_t(LPDIRECTINPUT8& handle); + void free_di(); + + public: + LPDIRECTINPUT8 operator->() { return handle; } + operator LPDIRECTINPUT8() { return handle; } + LPDIRECTINPUT8 di() { return handle; } + + ~di_t(); + }; - dinput_handle(const dinput_handle&) = delete; - dinput_handle(dinput_handle&&) = delete; + static di_t make_di(); + dinput_handle() = delete; }; #endif |