Skip to content

Commit 3ee3955

Browse files
committed
Merge branch 'thumbbehavior'
2 parents 3b98258 + aff6e61 commit 3ee3955

17 files changed

+176
-207
lines changed

XMapLib/CPPLambdaBase.h

Lines changed: 0 additions & 102 deletions
This file was deleted.

XMapLib/CPPLambdaRunner.h

Lines changed: 0 additions & 38 deletions
This file was deleted.

XMapLib/CPPLambdaVectorRunner.h

Lines changed: 0 additions & 34 deletions
This file was deleted.

XMapLib/CPPRunnerGeneric.h

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#pragma once
2+
#include "stdafx.h"
3+
#include <ranges>
4+
#include <concepts>
5+
namespace sds
6+
{
7+
/// <summary>Contains using declarations for first two args of the user-supplied lambda function.</summary>
8+
struct LambdaArgs
9+
{
10+
using LambdaArg1 = std::atomic<bool>;
11+
using LambdaArg2 = std::mutex;
12+
};
13+
/// <summary>All aboard the SFINAE train</summary>
14+
template<typename InternalData>
15+
requires std::is_default_constructible_v<InternalData>
16+
class CPPRunnerGeneric
17+
{
18+
public:
19+
using LambdaType = std::function<void(std::atomic<bool>&, std::mutex&, InternalData&)>;
20+
using ScopedLockType = std::lock_guard<std::mutex>;
21+
22+
CPPRunnerGeneric(LambdaType lambdaToRun) : m_lambda(std::move(lambdaToRun)) { }
23+
CPPRunnerGeneric(const CPPRunnerGeneric& other) = delete;
24+
CPPRunnerGeneric(CPPRunnerGeneric&& other) = delete;
25+
CPPRunnerGeneric& operator=(const CPPRunnerGeneric& other) = delete;
26+
CPPRunnerGeneric& operator=(CPPRunnerGeneric&& other) = delete;
27+
~CPPRunnerGeneric()
28+
{
29+
StopThread();
30+
}
31+
protected:
32+
LambdaType m_lambda;
33+
InternalData m_local_state{}; // default constructed type InternalData
34+
std::atomic<bool> m_is_stop_requested = false;
35+
std::unique_ptr<std::thread> m_local_thread;
36+
std::mutex m_state_mutex;
37+
public:
38+
/// <summary>Starts running a new thread for the lambda.</summary>
39+
/// <returns>true on success, false on failure.</returns>
40+
bool StartThread() noexcept
41+
{
42+
if (m_local_thread != nullptr)
43+
return false;
44+
m_is_stop_requested = false;
45+
m_local_thread = std::make_unique<std::thread>(m_lambda, std::ref(m_is_stop_requested), std::ref(m_state_mutex), std::ref(m_local_state));
46+
return m_local_thread->joinable();
47+
}
48+
/// <summary>Returns true if thread is running.</summary>
49+
bool IsRunning() const noexcept
50+
{
51+
if (m_local_thread != nullptr)
52+
return m_local_thread->joinable() && !m_is_stop_requested;
53+
return false;
54+
}
55+
/// <summary>Non-blocking way to stop a running thread.</summary>
56+
void RequestStop() noexcept
57+
{
58+
//Get this setting out of the way.
59+
this->m_is_stop_requested = true;
60+
//If there is a thread obj..
61+
if (this->m_local_thread != nullptr)
62+
{
63+
this->m_local_thread->detach();
64+
this->m_local_thread.reset();
65+
}
66+
}
67+
/// <summary>Blocking way to stop a running thread, joins to current thread and waits.</summary>
68+
void StopThread() noexcept
69+
{
70+
//Get this setting out of the way.
71+
this->m_is_stop_requested = true;
72+
//If there is a thread obj..
73+
if (this->m_local_thread != nullptr)
74+
{
75+
if (this->m_local_thread->joinable())
76+
{
77+
//join to wait for thread to stop, then reset to a nullptr.
78+
this->m_local_thread->join();
79+
this->m_local_thread.reset();
80+
}
81+
else
82+
//if it is not joinable, set to nullptr
83+
this->m_local_thread.reset();
84+
}
85+
}
86+
/// <summary>Container type function, adds an element to say, a vector.</summary>
87+
void AddState(const auto& state) requires std::ranges::range<InternalData>
88+
{
89+
ScopedLockType tempLock(this->m_state_mutex);
90+
this->m_local_state.push_back(state);
91+
}
92+
/// <summary>Container type function, returns copy and clears internal one.</summary>
93+
auto GetAndClearCurrentStates() requires std::ranges::range<InternalData>
94+
{
95+
ScopedLockType tempLock(this->m_state_mutex);
96+
auto temp = this->m_local_state;
97+
this->m_local_state.clear();
98+
return temp;
99+
}
100+
/// <summary>Utility function to update the InternalData with mutex locking thread safety.</summary>
101+
/// <param name="state">InternalData obj to be copied to the internal one.</param>
102+
void UpdateState(const InternalData& state)
103+
{
104+
ScopedLockType tempLock(this->m_state_mutex);
105+
this->m_local_state = state;
106+
}
107+
/// <summary>Returns a copy of the internal InternalData obj with mutex locking thread safety.</summary>
108+
InternalData GetCurrentState()
109+
{
110+
ScopedLockType tempLock(this->m_state_mutex);
111+
return this->m_local_state;
112+
}
113+
};
114+
}

XMapLib/KeyboardInputPoller.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace sds
1111
class KeyboardInputPoller
1212
{
1313
using InternalType = std::vector<XINPUT_KEYSTROKE>;
14-
using LambdaRunnerType = sds::CPPLambdaVectorRunner<InternalType>;
14+
using LambdaRunnerType = sds::CPPRunnerGeneric<InternalType>;
1515
using lock = LambdaRunnerType::ScopedLockType;
1616
const int EMPTY_COUNT = 5000;
1717
KeyboardPlayerInfo m_local_player;

XMapLib/KeyboardMapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace sds
1212
class KeyboardMapper
1313
{
1414
using InternalType = int;
15-
using LambdaRunnerType = sds::CPPLambdaVectorRunner<InternalType>;
15+
using LambdaRunnerType = sds::CPPRunnerGeneric<InternalType>;
1616
using lock = LambdaRunnerType::ScopedLockType;
1717
sds::KeyboardPlayerInfo m_localPlayerInfo;
1818
sds::KeyboardInputPoller m_poller;

XMapLib/MouseInputPoller.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ namespace sds
1111
class MouseInputPoller
1212
{
1313
using InternalType = XINPUT_STATE;
14-
using LambdaRunnerType = sds::CPPLambdaRunner<InternalType>;
14+
using LambdaRunnerType = sds::CPPRunnerGeneric<InternalType>;
1515
using lock = LambdaRunnerType::ScopedLockType;
1616
MousePlayerInfo m_local_player;
17-
std::unique_ptr<sds::CPPLambdaRunner<InternalType>> m_workThread;
17+
std::unique_ptr<LambdaRunnerType> m_workThread;
1818
void InitWorkThread() noexcept
1919
{
2020
m_workThread =
21-
std::make_unique<sds::CPPLambdaRunner<InternalType>>
21+
std::make_unique<LambdaRunnerType>
2222
([this](sds::LambdaArgs::LambdaArg1& stopCondition, sds::LambdaArgs::LambdaArg2& mut, auto& protectedData) { workThread(stopCondition, mut, protectedData); });
2323
}
2424
public:

XMapLib/MouseMapper.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@ namespace sds
1414
/// </summary>
1515
class MouseMapper
1616
{
17-
private:
17+
using InternalType = int;
18+
using LambdaRunnerType = sds::CPPRunnerGeneric<InternalType>;
19+
using lock = LambdaRunnerType::ScopedLockType;
1820
std::atomic<StickMap> m_stickmap_info = StickMap::NEITHER_STICK;
1921
std::atomic<SHORT> m_thread_x = 0;
2022
std::atomic<SHORT> m_thread_y = 0;
2123
std::atomic<int> m_mouse_sensitivity = MouseSettings::SENSITIVITY_DEFAULT;
2224
sds::MousePlayerInfo m_local_player;
2325
sds::MouseInputPoller m_poller;
24-
std::unique_ptr<sds::CPPLambdaRunner<int>> m_workThread;
26+
std::unique_ptr<LambdaRunnerType> m_workThread;
2527
void InitWorkThread() noexcept
2628
{
2729
m_workThread =
28-
std::make_unique<sds::CPPLambdaRunner<int>>
30+
std::make_unique<LambdaRunnerType>
2931
([this](sds::LambdaArgs::LambdaArg1& stopCondition, sds::LambdaArgs::LambdaArg2& mut, int& protectedData) { workThread(stopCondition, mut, protectedData); });
3032
}
3133
public:

XMapLib/MouseMoveThread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace sds
1111
class MouseMoveThread
1212
{
1313
using InternalType = int;
14-
using LambdaRunnerType = sds::CPPLambdaRunner<InternalType>;
14+
using LambdaRunnerType = sds::CPPRunnerGeneric<InternalType>;
1515
using lock = LambdaRunnerType::ScopedLockType;
1616
std::atomic<size_t> m_x_axis_delay = 1;
1717
std::atomic<size_t> m_y_axis_delay = 1;

XMapLib/MouseSettings.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ namespace sds
4848
static_assert(MICROSECONDS_MIN < MICROSECONDS_MAX);
4949
static_assert(MICROSECONDS_MIN_MAX < MICROSECONDS_MAX);
5050
static_assert(MICROSECONDS_MIN_MAX > MICROSECONDS_MIN);
51-
static bool IsValidSensitivityValue(int newSens)
51+
static constexpr bool IsValidSensitivityValue(int newSens) noexcept
5252
{
5353
return (newSens <= SENSITIVITY_MAX) && (newSens >= SENSITIVITY_MIN);
5454
}
55-
static bool IsValidDeadzoneValue(int dz)
55+
static constexpr bool IsValidDeadzoneValue(int dz) noexcept
5656
{
5757
return (dz <= DEADZONE_MAX) && (dz >= DEADZONE_MIN);
5858
}
59-
static bool IsValidThumbstickValue(int thumb)
59+
static constexpr bool IsValidThumbstickValue(int thumb) noexcept
6060
{
6161
return (thumb <= SMax) && (thumb >= SMin);
6262
}

0 commit comments

Comments
 (0)