From c5b44beb6d8a62775d11a4141e9fb77de0a3131b Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 29 Oct 2026 21:56:49 -0700 Subject: [PATCH 1/7] Common: add MoveOnlyFunction, a move-only callable wrapper std::function is copyable: copying it forks a stateful lambda's state, and it cannot hold move-only captures. MoveOnlyFunction is an alias of std::move_only_function where the standard library has it, and a minimal R(Args...) fallback for Apple's libc++, which lacks it. Tests: move-only capture, moving keeps one state, empty and nullptr comparison, reassignment; copy is rejected at compile time. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- Common/Cpp/MoveOnlyFunction.h | 118 +++++++++++ Common/Cpp/MoveOnlyFunction_Tests.cpp | 191 ++++++++++++++++++ Common/Cpp/MoveOnlyFunction_Tests.h | 21 ++ .../ComputerPrograms_UnitTestRunner.cpp | 2 + SerialPrograms/cmake/SourceFiles.cmake | 3 + 5 files changed, 335 insertions(+) create mode 100644 Common/Cpp/MoveOnlyFunction.h create mode 100644 Common/Cpp/MoveOnlyFunction_Tests.cpp create mode 100644 Common/Cpp/MoveOnlyFunction_Tests.h diff --git a/Common/Cpp/MoveOnlyFunction.h b/Common/Cpp/MoveOnlyFunction.h new file mode 100644 index 0000000000..9cc71f3422 --- /dev/null +++ b/Common/Cpp/MoveOnlyFunction.h @@ -0,0 +1,118 @@ +/* Move-Only Function + * + * From: https://github.com/PokemonAutomation/ + * + * A callable wrapper that can be moved but not copied. + * + * Prefer this over std::function for a callback that has one owner. + * Copying a std::function copies its callable, so a stateful lambda + * silently splits into two independent states. And std::function cannot + * hold a lambda that captures a move-only object (e.g. std::unique_ptr). + * + * This is std::move_only_function (C++23) where the standard library has + * it. Apple's libc++ (Xcode 26 / Apple Clang 21) does not, so this header + * has a minimal fallback for the plain "R(Args...)" signature. Delete the + * fallback once every supported compiler defines + * __cpp_lib_move_only_function. + * + */ + +#ifndef PokemonAutomation_MoveOnlyFunction_H +#define PokemonAutomation_MoveOnlyFunction_H + +#include +#include + +#ifndef __cpp_lib_move_only_function +#include +#include +#include +#include +#endif + +namespace PokemonAutomation{ + + + +#ifdef __cpp_lib_move_only_function + +template +using MoveOnlyFunction = std::move_only_function; + +#else + +template +class MoveOnlyFunction; + +template +class MoveOnlyFunction{ +public: + MoveOnlyFunction() noexcept = default; + MoveOnlyFunction(std::nullptr_t) noexcept {} + + template + requires ( + !std::is_same_v, MoveOnlyFunction> && + std::is_invocable_r_v&, Args...> + ) + MoveOnlyFunction(Function&& function){ + using Stored = std::decay_t; + // Like std::move_only_function, a null function pointer makes an + // empty wrapper. + if constexpr (std::is_pointer_v || std::is_member_pointer_v){ + if (function == nullptr){ + return; + } + } + m_callable = std::make_unique>(std::forward(function)); + } + + MoveOnlyFunction(MoveOnlyFunction&&) noexcept = default; + MoveOnlyFunction& operator=(MoveOnlyFunction&&) noexcept = default; + MoveOnlyFunction(const MoveOnlyFunction&) = delete; + MoveOnlyFunction& operator=(const MoveOnlyFunction&) = delete; + + explicit operator bool() const noexcept{ + return m_callable != nullptr; + } + friend bool operator==(const MoveOnlyFunction& function, std::nullptr_t) noexcept{ + return !function; + } + + // Calling an empty wrapper is undefined, as for std::move_only_function. + R operator()(Args... args){ + return m_callable->call(std::forward(args)...); + } + + +private: + class Callable{ + public: + virtual ~Callable() = default; + virtual R call(Args&&... args) = 0; + }; + + template + class Holder final : public Callable{ + public: + template + explicit Holder(Source&& source) + : m_stored(std::forward(source)) + {} + virtual R call(Args&&... args) override{ + return std::invoke_r(m_stored, std::forward(args)...); + } + + private: + Stored m_stored; + }; + + std::unique_ptr m_callable; +}; + +#endif + + + +} +#endif diff --git a/Common/Cpp/MoveOnlyFunction_Tests.cpp b/Common/Cpp/MoveOnlyFunction_Tests.cpp new file mode 100644 index 0000000000..c133e8b8ec --- /dev/null +++ b/Common/Cpp/MoveOnlyFunction_Tests.cpp @@ -0,0 +1,191 @@ +/* Move-Only Function Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "TestRunners/UnitTestDatabase.h" +#include "MoveOnlyFunction.h" +#include "MoveOnlyFunction_Tests.h" + +namespace PokemonAutomation{ +namespace{ + + + +// Copying would fork a stateful callable, so it must not compile. +static_assert(!std::is_copy_constructible_v>); +static_assert(!std::is_copy_assignable_v>); +static_assert(std::is_nothrow_move_constructible_v>); +static_assert(std::is_nothrow_move_assignable_v>); + + + +std::string expected_int(const char* what, int expected, int actual){ + return std::string(what) + ": expected " + std::to_string(expected) + + ", got " + std::to_string(actual); +} + + + +class Test_MoveOnlyFunction_MoveOnlyCapture : public UnitTest{ +public: + Test_MoveOnlyFunction_MoveOnlyCapture() + : UnitTest("Common::MoveOnlyFunction - Move-only capture") + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + auto value = std::make_unique(42); + MoveOnlyFunction function = [value = std::move(value)](int add){ + return *value + add; + }; + int actual = function(1); + if (actual != 43){ + return UnitTestResult(expected_int("call with a unique_ptr capture", 43, actual)); + } + + MoveOnlyFunction moved = std::move(function); + actual = moved(2); + if (actual != 44){ + return UnitTestResult(expected_int("call after a move", 44, actual)); + } + return true; + } +}; + + + +class Test_MoveOnlyFunction_StateNotForked : public UnitTest{ +public: + Test_MoveOnlyFunction_StateNotForked() + : UnitTest("Common::MoveOnlyFunction - Moving keeps one state") + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + MoveOnlyFunction counter = [count = 0]() mutable{ + return ++count; + }; + int actual = counter(); + if (actual != 1){ + return UnitTestResult(expected_int("first call", 1, actual)); + } + + // The moved-to wrapper continues the same count. + MoveOnlyFunction constructed = std::move(counter); + actual = constructed(); + if (actual != 2){ + return UnitTestResult(expected_int("call after a move construction", 2, actual)); + } + + MoveOnlyFunction assigned; + assigned = std::move(constructed); + actual = assigned(); + if (actual != 3){ + return UnitTestResult(expected_int("call after a move assignment", 3, actual)); + } + return true; + } +}; + + + +int return_seven(){ + return 7; +} + +class Test_MoveOnlyFunction_Empty : public UnitTest{ +public: + Test_MoveOnlyFunction_Empty() + : UnitTest("Common::MoveOnlyFunction - Empty is false") + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + MoveOnlyFunction defaulted; + if (defaulted){ + return UnitTestResult("default constructed: expected empty, got non-empty"); + } + MoveOnlyFunction from_nullptr = nullptr; + if (from_nullptr){ + return UnitTestResult("constructed from nullptr: expected empty, got non-empty"); + } + int (*null_pointer)() = nullptr; + MoveOnlyFunction from_null_pointer = null_pointer; + if (from_null_pointer){ + return UnitTestResult("constructed from a null function pointer: expected empty, got non-empty"); + } + + MoveOnlyFunction from_pointer = &return_seven; + if (!from_pointer){ + return UnitTestResult("constructed from a function pointer: expected non-empty, got empty"); + } + int actual = from_pointer(); + if (actual != 7){ + return UnitTestResult(expected_int("call through a function pointer", 7, actual)); + } + return true; + } +}; + + + +class Test_MoveOnlyFunction_Reassignment : public UnitTest{ +public: + Test_MoveOnlyFunction_Reassignment() + : UnitTest("Common::MoveOnlyFunction - Reassignment") + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + // The use count shows whether the wrapper still holds the first + // callable. + auto token = std::make_shared(1); + MoveOnlyFunction function = [token]{ + return *token; + }; + int actual = function(); + if (actual != 1){ + return UnitTestResult(expected_int("first callable", 1, actual)); + } + + function = []{ + return 2; + }; + actual = function(); + if (actual != 2){ + return UnitTestResult(expected_int("call after reassignment", 2, actual)); + } + actual = (int)token.use_count(); + if (actual != 1){ + return UnitTestResult(expected_int("token owners after reassignment", 1, actual)); + } + + function = nullptr; + if (function){ + return UnitTestResult("assigned nullptr: expected empty, got non-empty"); + } + if (function != nullptr){ + return UnitTestResult("assigned nullptr: expected == nullptr, got != nullptr"); + } + return true; + } +}; + + + +} // anonymous namespace + + + +void add_tests_MoveOnlyFunction(UnitTestDatabase& database){ + database.add(); + database.add(); + database.add(); + database.add(); +} + + + +} diff --git a/Common/Cpp/MoveOnlyFunction_Tests.h b/Common/Cpp/MoveOnlyFunction_Tests.h new file mode 100644 index 0000000000..c43f518bdc --- /dev/null +++ b/Common/Cpp/MoveOnlyFunction_Tests.h @@ -0,0 +1,21 @@ +/* Move-Only Function Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_MoveOnlyFunction_Tests_H +#define PokemonAutomation_MoveOnlyFunction_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ + + + +void add_tests_MoveOnlyFunction(UnitTestDatabase& database); + + + +} +#endif diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index 1c19573466..d434036d6e 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -5,6 +5,7 @@ */ #include "Common/Cpp/ListenerSet.h" +#include "Common/Cpp/MoveOnlyFunction_Tests.h" #include "Common/Cpp/ScopeExit.h" #include "Common/Cpp/PrettyPrint.h" #include "Common/Cpp/TestRunners/TestImageDistortionManager.h" @@ -51,6 +52,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ add_tests_QtEventThreadPool(ret, GlobalThreadPools::computation_normal()); add_tests_BlackBorderDetector(ret); add_tests_ListenerSet(ret); + add_tests_MoveOnlyFunction(ret); add_tests_AudioInfo(ret); add_tests_ChannelMixerMatrix(ret); add_tests_FFTStreamer(ret); diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 9e4e0895bb..f640a174ce 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -118,6 +118,9 @@ file(GLOB CORE_LIBRARY_SOURCES ../Common/Cpp/Logging/MultiOutputLogger.h ../Common/Cpp/Logging/TaggedLogger.cpp ../Common/Cpp/Logging/TaggedLogger.h + ../Common/Cpp/MoveOnlyFunction.h + ../Common/Cpp/MoveOnlyFunction_Tests.cpp + ../Common/Cpp/MoveOnlyFunction_Tests.h ../Common/Cpp/Options/BatchOption.cpp ../Common/Cpp/Options/BatchOption.h ../Common/Cpp/Options/BooleanCheckBoxOption.cpp From 38bb6aeddb7c2511735de09caf893978529477d6 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 29 Oct 2026 22:07:21 -0700 Subject: [PATCH 2/7] Common: drop the noexcept move-assignment check from MoveOnlyFunction tests The standard does not make std::move_only_function's move assignment noexcept, and MSVC's STL does not. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- Common/Cpp/MoveOnlyFunction_Tests.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Common/Cpp/MoveOnlyFunction_Tests.cpp b/Common/Cpp/MoveOnlyFunction_Tests.cpp index c133e8b8ec..72ed5998ae 100644 --- a/Common/Cpp/MoveOnlyFunction_Tests.cpp +++ b/Common/Cpp/MoveOnlyFunction_Tests.cpp @@ -21,7 +21,6 @@ namespace{ static_assert(!std::is_copy_constructible_v>); static_assert(!std::is_copy_assignable_v>); static_assert(std::is_nothrow_move_constructible_v>); -static_assert(std::is_nothrow_move_assignable_v>); From 93487aaa4a54a5bc0eb325d16e27ff7224338796 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 31 Oct 2026 08:39:41 -0700 Subject: [PATCH 3/7] Common: hold thread pool tasks in MoveOnlyFunction ThreadPool::dispatch(), dispatch_now_blocking() and try_dispatch_now() take a task that the pool owns until it runs, so it never needs to be copied. Taking MoveOnlyFunction lets a task capture move-only state and rules out forking a stateful task by accident. Callers that pass a lambda are unchanged; the two try_dispatch_now() callers that build the task first now build a MoveOnlyFunction. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- Common/Cpp/Concurrency/Backends/AsyncTask_Default.h | 6 +++--- Common/Cpp/Concurrency/Backends/ThreadPool_Default.cpp | 6 +++--- Common/Cpp/Concurrency/Backends/ThreadPool_Default.h | 6 +++--- Common/Cpp/Concurrency/ThreadPool.h | 7 ++++--- .../VideoPipeline/Backends/SnapshotManager.cpp | 3 ++- .../ShinyDetection/PokemonSwSh_ShinySparkleSet.cpp | 3 ++- 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Common/Cpp/Concurrency/Backends/AsyncTask_Default.h b/Common/Cpp/Concurrency/Backends/AsyncTask_Default.h index 6e83ebde75..e797904803 100644 --- a/Common/Cpp/Concurrency/Backends/AsyncTask_Default.h +++ b/Common/Cpp/Concurrency/Backends/AsyncTask_Default.h @@ -7,9 +7,9 @@ #ifndef PokemonAutomation_AsyncTask_Default_H #define PokemonAutomation_AsyncTask_Default_H -#include #include #include +#include "Common/Cpp/MoveOnlyFunction.h" #include "Common/Cpp/Concurrency/SpinPause.h" #include "Common/Cpp/Concurrency/Mutex.h" #include "Common/Cpp/Concurrency/ConditionVariable.h" @@ -50,7 +50,7 @@ class AsyncTask_Cpp : public AsyncTaskCore{ pause(); } } - AsyncTask_Cpp(std::function task) + AsyncTask_Cpp(MoveOnlyFunction task) : m_task(std::move(task)) , m_state(State::NOT_STARTED) {} @@ -102,7 +102,7 @@ class AsyncTask_Cpp : public AsyncTaskCore{ private: - std::function m_task; + MoveOnlyFunction m_task; std::atomic m_state; std::exception_ptr m_exception; diff --git a/Common/Cpp/Concurrency/Backends/ThreadPool_Default.cpp b/Common/Cpp/Concurrency/Backends/ThreadPool_Default.cpp index 297ba275cb..bf5ed9ce7a 100644 --- a/Common/Cpp/Concurrency/Backends/ThreadPool_Default.cpp +++ b/Common/Cpp/Concurrency/Backends/ThreadPool_Default.cpp @@ -90,7 +90,7 @@ WallDuration ThreadPool_Default::cpu_time() const{ return ret; } -AsyncTask ThreadPool_Default::dispatch(std::function&& func){ +AsyncTask ThreadPool_Default::dispatch(MoveOnlyFunction&& func){ AsyncTask task; { std::unique_lock lg(m_lock); @@ -114,7 +114,7 @@ AsyncTask ThreadPool_Default::dispatch(std::function&& func){ m_thread_cv.notify_one(); return task; } -AsyncTask ThreadPool_Default::dispatch_now_blocking(std::function&& func){ +AsyncTask ThreadPool_Default::dispatch_now_blocking(MoveOnlyFunction&& func){ AsyncTask task; { std::unique_lock lg(m_lock); @@ -138,7 +138,7 @@ AsyncTask ThreadPool_Default::dispatch_now_blocking(std::function&& func m_thread_cv.notify_one(); return task; } -AsyncTask ThreadPool_Default::try_dispatch_now(std::function& func){ +AsyncTask ThreadPool_Default::try_dispatch_now(MoveOnlyFunction& func){ AsyncTask task; { std::lock_guard lg(m_lock); diff --git a/Common/Cpp/Concurrency/Backends/ThreadPool_Default.h b/Common/Cpp/Concurrency/Backends/ThreadPool_Default.h index b2944c630f..d60a7378bb 100644 --- a/Common/Cpp/Concurrency/Backends/ThreadPool_Default.h +++ b/Common/Cpp/Concurrency/Backends/ThreadPool_Default.h @@ -49,9 +49,9 @@ class ThreadPool_Default final : public ThreadPool{ public: - [[nodiscard]] virtual AsyncTask dispatch(std::function&& func) override; - [[nodiscard]] virtual AsyncTask dispatch_now_blocking(std::function&& func) override; - [[nodiscard]] virtual AsyncTask try_dispatch_now(std::function& func) override; + [[nodiscard]] virtual AsyncTask dispatch(MoveOnlyFunction&& func) override; + [[nodiscard]] virtual AsyncTask dispatch_now_blocking(MoveOnlyFunction&& func) override; + [[nodiscard]] virtual AsyncTask try_dispatch_now(MoveOnlyFunction& func) override; virtual void run_in_parallel( const std::function& func, diff --git a/Common/Cpp/Concurrency/ThreadPool.h b/Common/Cpp/Concurrency/ThreadPool.h index 29ea2cf01c..6a47ab3337 100644 --- a/Common/Cpp/Concurrency/ThreadPool.h +++ b/Common/Cpp/Concurrency/ThreadPool.h @@ -17,6 +17,7 @@ #define PokemonAutomation_ThreadPool_H #include +#include "Common/Cpp/MoveOnlyFunction.h" #include "Common/Cpp/Time.h" namespace PokemonAutomation{ @@ -40,17 +41,17 @@ class ThreadPool{ // Dispatch the function and return immediately. // The function is not guaranteed to begin running immediately. - [[nodiscard]] virtual AsyncTask dispatch(std::function&& func) = 0; + [[nodiscard]] virtual AsyncTask dispatch(MoveOnlyFunction&& func) = 0; // Dispatch the function and begin running now. // If there are no threads available, it waits until there are. // This function will not return until the function has begin running. - [[nodiscard]] virtual AsyncTask dispatch_now_blocking(std::function&& func) = 0; + [[nodiscard]] virtual AsyncTask dispatch_now_blocking(MoveOnlyFunction&& func) = 0; // Try to dispatch the function and begin running now. // Returns null if no threads are available. "func" will be moved-from only on success. // This function returns immediately. - [[nodiscard]] virtual AsyncTask try_dispatch_now(std::function& func) = 0; + [[nodiscard]] virtual AsyncTask try_dispatch_now(MoveOnlyFunction& func) = 0; // Run function for all the indices [start, end). // Lower indices are not allowed to block on higher indices. diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/SnapshotManager.cpp b/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/SnapshotManager.cpp index 068d6ec418..1f5369a0ab 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/SnapshotManager.cpp +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/SnapshotManager.cpp @@ -5,6 +5,7 @@ */ //#include "Common/Cpp/Concurrency/ReverseLockGuard.h" +#include "Common/Cpp/MoveOnlyFunction.h" #include "Common/Cpp/Concurrency/AsyncTask.h" #include "CommonFramework/ImageTypes/ImageRGB32_Qt.h" #include "CommonFramework/Tools/GlobalThreadPools.h" @@ -100,7 +101,7 @@ bool SnapshotManager::try_dispatch_conversion(uint64_t seqnum, QVideoFrame frame } try{ - std::function lambda = [=, this, frame = std::move(frame)](){ + MoveOnlyFunction lambda = [=, this, frame = std::move(frame)](){ convert(seqnum, std::move(frame), timestamp); }; diff --git a/SerialPrograms/Source/VideoGames/PokemonSwSh/Inference/ShinyDetection/PokemonSwSh_ShinySparkleSet.cpp b/SerialPrograms/Source/VideoGames/PokemonSwSh/Inference/ShinyDetection/PokemonSwSh_ShinySparkleSet.cpp index ed375c9948..c3b2ae25f9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSwSh/Inference/ShinyDetection/PokemonSwSh_ShinySparkleSet.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSwSh/Inference/ShinyDetection/PokemonSwSh_ShinySparkleSet.cpp @@ -6,6 +6,7 @@ #include #include +#include "Common/Cpp/MoveOnlyFunction.h" #include "Common/Cpp/Concurrency/SpinLock.h" #include "Common/Cpp/Concurrency/AsyncTask.h" #include "Kernels/Waterfill/Kernels_Waterfill_Session.h" @@ -134,7 +135,7 @@ ShinySparkleSetSwSh find_sparkles(size_t screen_area, WaterfillSession& session) auto finder = session.make_iterator(20); WaterfillObject object; while (finder->find_next(object, true)){ - std::function func = [&, screen_area, object = std::move(object)]{ + MoveOnlyFunction func = [&, screen_area, object = std::move(object)]{ find_sparkles( screen_area, lock, From ce9c53142024d19a22f17e4db04d4bd3be80ae5c Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 31 Oct 2026 08:43:34 -0700 Subject: [PATCH 4/7] Common: hold Thread, FireForgetDispatcher and ScheduledTaskRunner tasks in MoveOnlyFunction Each of these owns its task until it runs and never copies it. The detached std::thread backend, which is not built today, needs its wrapper lambda to be mutable to call the task. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- Common/Cpp/Concurrency/Backends/Thread_Qt.tpp | 6 +++--- Common/Cpp/Concurrency/Backends/Thread_StdThread.tpp | 4 ++-- Common/Cpp/Concurrency/Backends/Thread_StdThreadDetach.tpp | 6 +++--- Common/Cpp/Concurrency/FireForgetDispatcher.cpp | 4 ++-- Common/Cpp/Concurrency/FireForgetDispatcher.h | 6 +++--- Common/Cpp/Concurrency/ScheduledTaskRunner.cpp | 4 ++-- Common/Cpp/Concurrency/ScheduledTaskRunner.h | 7 ++++--- Common/Cpp/Concurrency/Thread.h | 4 ++-- 8 files changed, 21 insertions(+), 20 deletions(-) diff --git a/Common/Cpp/Concurrency/Backends/Thread_Qt.tpp b/Common/Cpp/Concurrency/Backends/Thread_Qt.tpp index 860c4505c7..3117d8a7b3 100644 --- a/Common/Cpp/Concurrency/Backends/Thread_Qt.tpp +++ b/Common/Cpp/Concurrency/Backends/Thread_Qt.tpp @@ -21,7 +21,7 @@ namespace PokemonAutomation{ // PIMPL implementation - hides std::thread from header struct Thread::Data : public QThread{ - Data(std::function&& function) + Data(MoveOnlyFunction&& function) : m_function(std::move(function)) // , m_finished(false) { @@ -69,7 +69,7 @@ struct Thread::Data : public QThread{ // cout << "exiting: " << this << endl; } - std::function m_function; + MoveOnlyFunction m_function; std::atomic m_finished; }; @@ -95,7 +95,7 @@ Thread::Thread() = default; // Main constructor - starts thread immediately with given function // - Uses CONSTRUCT_TOKEN to construct Pimpl with Data constructor // - Thread starts running immediately (same as std::thread behavior) -Thread::Thread(std::function&& function) +Thread::Thread(MoveOnlyFunction&& function) : m_data(CONSTRUCT_TOKEN, std::move(function)) {} diff --git a/Common/Cpp/Concurrency/Backends/Thread_StdThread.tpp b/Common/Cpp/Concurrency/Backends/Thread_StdThread.tpp index b3e42146f8..9fb090098a 100644 --- a/Common/Cpp/Concurrency/Backends/Thread_StdThread.tpp +++ b/Common/Cpp/Concurrency/Backends/Thread_StdThread.tpp @@ -19,7 +19,7 @@ namespace PokemonAutomation{ struct Thread::Data{ std::thread m_thread; - Data(std::function&& function) + Data(MoveOnlyFunction&& function) : m_thread(std::move(function)) {} }; @@ -45,7 +45,7 @@ Thread::Thread() = default; // Main constructor - starts thread immediately with given function // - Uses CONSTRUCT_TOKEN to construct Pimpl with Data constructor // - Thread starts running immediately (same as std::thread behavior) -Thread::Thread(std::function&& function) +Thread::Thread(MoveOnlyFunction&& function) : m_data(CONSTRUCT_TOKEN, std::move(function)) {} diff --git a/Common/Cpp/Concurrency/Backends/Thread_StdThreadDetach.tpp b/Common/Cpp/Concurrency/Backends/Thread_StdThreadDetach.tpp index 4f78e38949..02c3bcd8fb 100644 --- a/Common/Cpp/Concurrency/Backends/Thread_StdThreadDetach.tpp +++ b/Common/Cpp/Concurrency/Backends/Thread_StdThreadDetach.tpp @@ -25,10 +25,10 @@ struct Thread::Data{ std::thread m_thread; - Data(std::function&& function) + Data(MoveOnlyFunction&& function) : m_stopped(false) , m_thread( - [this, function = std::move(function)]{ + [this, function = std::move(function)]() mutable{ function(); // std::lock_guard lg(m_lock); // m_stopped = true; @@ -60,7 +60,7 @@ Thread::Thread() = default; // Main constructor - starts thread immediately with given function // - Uses CONSTRUCT_TOKEN to construct Pimpl with Data constructor // - Thread starts running immediately (same as std::thread behavior) -Thread::Thread(std::function&& function) +Thread::Thread(MoveOnlyFunction&& function) : m_data(CONSTRUCT_TOKEN, std::move(function)) {} diff --git a/Common/Cpp/Concurrency/FireForgetDispatcher.cpp b/Common/Cpp/Concurrency/FireForgetDispatcher.cpp index 10e1a8a0cb..df021077c3 100644 --- a/Common/Cpp/Concurrency/FireForgetDispatcher.cpp +++ b/Common/Cpp/Concurrency/FireForgetDispatcher.cpp @@ -38,7 +38,7 @@ FireForgetDispatcher::~FireForgetDispatcher(){ stop(); } -void FireForgetDispatcher::dispatch(std::function&& func){ +void FireForgetDispatcher::dispatch(MoveOnlyFunction&& func){ std::lock_guard lg(m_lock); m_queue.emplace_back(std::move(func)); m_cv.notify_one(); @@ -58,7 +58,7 @@ void FireForgetDispatcher::dispatch(std::function&& func){ void FireForgetDispatcher::thread_loop(){ while (true){ - std::function task; + MoveOnlyFunction task; { std::unique_lock lg(m_lock); if (m_stopping){ diff --git a/Common/Cpp/Concurrency/FireForgetDispatcher.h b/Common/Cpp/Concurrency/FireForgetDispatcher.h index 3364284ad7..cb18cc49bc 100644 --- a/Common/Cpp/Concurrency/FireForgetDispatcher.h +++ b/Common/Cpp/Concurrency/FireForgetDispatcher.h @@ -8,7 +8,7 @@ #define PokemonAutomation_FireForgetDispatcher_H #include -#include +#include "Common/Cpp/MoveOnlyFunction.h" #include "Mutex.h" #include "ConditionVariable.h" #include "Thread.h" @@ -23,7 +23,7 @@ class FireForgetDispatcher{ // Dispatch the specified task and return a handle to it. // Call "handle->wait()" to wait for the task to finish. - void dispatch(std::function&& func); + void dispatch(MoveOnlyFunction&& func); void stop(); @@ -32,7 +32,7 @@ class FireForgetDispatcher{ void thread_loop(); private: - std::deque> m_queue; + std::deque> m_queue; Thread m_thread; bool m_stopping; Mutex m_lock; diff --git a/Common/Cpp/Concurrency/ScheduledTaskRunner.cpp b/Common/Cpp/Concurrency/ScheduledTaskRunner.cpp index 488d95bac7..a3378a67e4 100644 --- a/Common/Cpp/Concurrency/ScheduledTaskRunner.cpp +++ b/Common/Cpp/Concurrency/ScheduledTaskRunner.cpp @@ -52,7 +52,7 @@ WallClock ScheduledTaskRunner::next_event() const{ } return m_schedule.begin()->first; } -void ScheduledTaskRunner::add_event(WallClock time, std::function callback){ +void ScheduledTaskRunner::add_event(WallClock time, MoveOnlyFunction callback){ std::lock_guard lg(m_lock); if (m_stopped){ return; @@ -60,7 +60,7 @@ void ScheduledTaskRunner::add_event(WallClock time, std::function callba m_schedule.emplace(time, std::move(callback)); m_cv.notify_all(); } -void ScheduledTaskRunner::add_event(std::chrono::milliseconds time_from_now, std::function callback){ +void ScheduledTaskRunner::add_event(std::chrono::milliseconds time_from_now, MoveOnlyFunction callback){ std::lock_guard lg(m_lock); if (m_stopped){ return; diff --git a/Common/Cpp/Concurrency/ScheduledTaskRunner.h b/Common/Cpp/Concurrency/ScheduledTaskRunner.h index 45b4bc19a0..c7b10f7dae 100644 --- a/Common/Cpp/Concurrency/ScheduledTaskRunner.h +++ b/Common/Cpp/Concurrency/ScheduledTaskRunner.h @@ -10,6 +10,7 @@ #define PokemonAutomation_ScheduledTaskRunner_H #include +#include "Common/Cpp/MoveOnlyFunction.h" #include "Common/Cpp/Time.h" //#include "Common/Cpp/CancellableScope.h" #include "Common/Cpp/Concurrency/Mutex.h" @@ -33,8 +34,8 @@ class ScheduledTaskRunner{ // events are scheduled. WallClock next_event() const; - void add_event(WallClock time, std::function callback); - void add_event(std::chrono::milliseconds time_from_now, std::function callback); + void add_event(WallClock time, MoveOnlyFunction callback); + void add_event(std::chrono::milliseconds time_from_now, MoveOnlyFunction callback); // virtual bool cancel(std::exception_ptr exception) noexcept override; @@ -46,7 +47,7 @@ class ScheduledTaskRunner{ ConditionVariable m_cv; bool m_stopped; - std::multimap> m_schedule; + std::multimap> m_schedule; AsyncTask m_runner; }; diff --git a/Common/Cpp/Concurrency/Thread.h b/Common/Cpp/Concurrency/Thread.h index 759d8b3707..97bede7d39 100644 --- a/Common/Cpp/Concurrency/Thread.h +++ b/Common/Cpp/Concurrency/Thread.h @@ -21,7 +21,7 @@ #ifndef PokemonAutomation_Concurrency_Thread_H #define PokemonAutomation_Concurrency_Thread_H -#include +#include "Common/Cpp/MoveOnlyFunction.h" #include "Common/Cpp/Containers/Pimpl.h" namespace PokemonAutomation{ @@ -42,7 +42,7 @@ class Thread{ // Constructor - starts thread immediately with given function like std::thread // Function is moved (not copied) for efficiency - Thread(std::function&& function); + Thread(MoveOnlyFunction&& function); // Returns true if Thread has valid data (not default-constructed or moved-from) // and not joined. From 1657c8dc00f3d5f03955fa1a419b012ca02ef633 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 31 Oct 2026 08:45:21 -0700 Subject: [PATCH 5/7] CommonTools: hold async session commands and detector callbacks in MoveOnlyFunction AsyncCommandSession, SuperControlSession, DetectionDebouncer and LetsGoKillWatcher each own their callback and never copy it. LetsGoKillWatcher now moves its callback in instead of copying it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Source/CommonTools/Async/InterruptableCommands.h | 4 ++-- .../Source/CommonTools/Async/InterruptableCommands.tpp | 8 ++++---- .../Source/CommonTools/Async/SuperControlSession.h | 6 +++--- .../Source/CommonTools/Async/SuperControlSession.tpp | 2 +- SerialPrograms/Source/CommonTools/DetectionDebouncer.h | 6 +++--- .../PokemonLA/Programs/PokemonLA_EscapeFromAttack.h | 2 +- .../PokemonLA/Programs/PokemonLA_FlagNavigationAir.h | 2 +- .../Inference/Overworld/PokemonSV_LetsGoKillDetector.cpp | 4 ++-- .../Inference/Overworld/PokemonSV_LetsGoKillDetector.h | 6 +++--- 9 files changed, 20 insertions(+), 20 deletions(-) diff --git a/SerialPrograms/Source/CommonTools/Async/InterruptableCommands.h b/SerialPrograms/Source/CommonTools/Async/InterruptableCommands.h index f0cab8171e..86d360c23e 100644 --- a/SerialPrograms/Source/CommonTools/Async/InterruptableCommands.h +++ b/SerialPrograms/Source/CommonTools/Async/InterruptableCommands.h @@ -7,7 +7,7 @@ #ifndef PokemonAutomation_CommonTools_InterruptableCommands_H #define PokemonAutomation_CommonTools_InterruptableCommands_H -#include +#include "Common/Cpp/MoveOnlyFunction.h" #include "Common/Cpp/Concurrency/Mutex.h" #include "Common/Cpp/Concurrency/ConditionVariable.h" #include "Common/Cpp/Concurrency/AsyncTask.h" @@ -44,7 +44,7 @@ class AsyncCommandSession final : private Cancellable{ // previous `dispatch()` while a new `dispatch()` presses button A and button B // at the same time, button A is not released during the transition. // You can use it to adjust button state continuously without interruption. - void dispatch(std::function&& lambda); + void dispatch(MoveOnlyFunction&& lambda); // Wait for currently running command to finish. void wait(); diff --git a/SerialPrograms/Source/CommonTools/Async/InterruptableCommands.tpp b/SerialPrograms/Source/CommonTools/Async/InterruptableCommands.tpp index 193dad2d9b..bc96bbc675 100644 --- a/SerialPrograms/Source/CommonTools/Async/InterruptableCommands.tpp +++ b/SerialPrograms/Source/CommonTools/Async/InterruptableCommands.tpp @@ -21,11 +21,11 @@ template struct AsyncCommandSession::CommandSet{ CommandSet( CancellableScope& parent, ControllerType& controller, - std::function&& lambda + MoveOnlyFunction&& lambda ); CancellableHolder scope; ControllerContextType context; - std::function commands; + MoveOnlyFunction commands; }; @@ -33,7 +33,7 @@ struct AsyncCommandSession::CommandSet{ template AsyncCommandSession::CommandSet::CommandSet( CancellableScope& parent, ControllerType& controller, - std::function&& lambda + MoveOnlyFunction&& lambda ) : scope(parent) , context(scope, controller) @@ -97,7 +97,7 @@ void AsyncCommandSession::stop_command(){ } } template -void AsyncCommandSession::dispatch(std::function&& lambda){ +void AsyncCommandSession::dispatch(MoveOnlyFunction&& lambda){ auto scope_check = m_sanitizer.check_scope(); // Construct the CommandSet outside the lock. diff --git a/SerialPrograms/Source/CommonTools/Async/SuperControlSession.h b/SerialPrograms/Source/CommonTools/Async/SuperControlSession.h index 62d5ec99e3..b13532bf23 100644 --- a/SerialPrograms/Source/CommonTools/Async/SuperControlSession.h +++ b/SerialPrograms/Source/CommonTools/Async/SuperControlSession.h @@ -11,7 +11,7 @@ #include #include #include -#include +#include "Common/Cpp/MoveOnlyFunction.h" #include "Common/Cpp/Time.h" namespace PokemonAutomation{ @@ -58,7 +58,7 @@ class SuperControlSession{ void operator+=(VisualInferenceCallback& callback); void operator+=(AudioInferenceCallback& callback); - void register_state_command(size_t state, std::function&& action); + void register_state_command(size_t state, MoveOnlyFunction&& action); protected: @@ -91,7 +91,7 @@ class SuperControlSession{ std::vector m_audio_callbacks; // These are actions - std::map> m_state_actions; + std::map> m_state_actions; private: // Run-time state. diff --git a/SerialPrograms/Source/CommonTools/Async/SuperControlSession.tpp b/SerialPrograms/Source/CommonTools/Async/SuperControlSession.tpp index c8343ecf0e..a5fae0b94e 100644 --- a/SerialPrograms/Source/CommonTools/Async/SuperControlSession.tpp +++ b/SerialPrograms/Source/CommonTools/Async/SuperControlSession.tpp @@ -49,7 +49,7 @@ void SuperControlSession::operator+=(AudioInferenceCallback& cal m_audio_callbacks.emplace_back(&callback); } template -void SuperControlSession::register_state_command(size_t state, std::function&& action){ +void SuperControlSession::register_state_command(size_t state, MoveOnlyFunction&& action){ auto iter = m_state_actions.find(state); if (iter != m_state_actions.end()){ throw InternalProgramError(&m_stream.logger(), PA_CURRENT_FUNCTION, "Duplicate State Enum: " + std::to_string(state)); diff --git a/SerialPrograms/Source/CommonTools/DetectionDebouncer.h b/SerialPrograms/Source/CommonTools/DetectionDebouncer.h index ee4f41cf24..6b071efe88 100644 --- a/SerialPrograms/Source/CommonTools/DetectionDebouncer.h +++ b/SerialPrograms/Source/CommonTools/DetectionDebouncer.h @@ -7,8 +7,8 @@ #ifndef PokemonAutomation_CommonTools_DetectionDebouncer_H #define PokemonAutomation_CommonTools_DetectionDebouncer_H -#include #include +#include "Common/Cpp/MoveOnlyFunction.h" #include "Common/Cpp/Time.h" namespace PokemonAutomation{ @@ -20,7 +20,7 @@ class DetectionDebouncer{ DetectionDebouncer( Type initial_state, std::chrono::milliseconds min_streak, - std::function&& state_reporter + MoveOnlyFunction&& state_reporter ) : m_min_streak(min_streak) , m_state_reporter(std::move(state_reporter)) @@ -57,7 +57,7 @@ class DetectionDebouncer{ private: std::chrono::milliseconds m_min_streak; - std::function m_state_reporter; + MoveOnlyFunction m_state_reporter; std::atomic m_current_reported; WallClock m_last_match; diff --git a/SerialPrograms/Source/VideoGames/PokemonLA/Programs/PokemonLA_EscapeFromAttack.h b/SerialPrograms/Source/VideoGames/PokemonLA/Programs/PokemonLA_EscapeFromAttack.h index 370b76c572..d3fe64694e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLA/Programs/PokemonLA_EscapeFromAttack.h +++ b/SerialPrograms/Source/VideoGames/PokemonLA/Programs/PokemonLA_EscapeFromAttack.h @@ -46,7 +46,7 @@ class EscapeFromAttack : public SuperControlSession{ GET_ON_SNEASLER, CLIMBING, }; - void register_state_command(State state, std::function&& action){ + void register_state_command(State state, MoveOnlyFunction&& action){ SuperControlSession::register_state_command((size_t)state, std::move(action)); } bool run_state_action(State state){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLA/Programs/PokemonLA_FlagNavigationAir.h b/SerialPrograms/Source/VideoGames/PokemonLA/Programs/PokemonLA_FlagNavigationAir.h index 3a5c1ddd8e..8c2d10cd60 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLA/Programs/PokemonLA_FlagNavigationAir.h +++ b/SerialPrograms/Source/VideoGames/PokemonLA/Programs/PokemonLA_FlagNavigationAir.h @@ -62,7 +62,7 @@ class FlagNavigationAir : public SuperControlSession{ TURN_RIGHT, FIND_FLAG, }; - void register_state_command(State state, std::function&& action){ + void register_state_command(State state, MoveOnlyFunction&& action){ SuperControlSession::register_state_command((size_t)state, std::move(action)); } bool run_state_action(State state){ diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_LetsGoKillDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_LetsGoKillDetector.cpp index 2ca84ccb49..adabbccbf2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_LetsGoKillDetector.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_LetsGoKillDetector.cpp @@ -181,13 +181,13 @@ LetsGoKillWatcher::LetsGoKillWatcher( Logger& logger, Color color, bool trigger_if_detected, const ImageFloatBox& box, - std::function on_kill_callback, + MoveOnlyFunction on_kill_callback, std::chrono::milliseconds duration ) : DetectorToFinder("LetsGoKillWatcher", duration, color, box) , m_logger(logger) , m_trigger_if_detected(trigger_if_detected) - , m_on_kill_callback(on_kill_callback) + , m_on_kill_callback(std::move(on_kill_callback)) , m_last_detection(false) , m_last_detected(WallClock::min()) {} diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_LetsGoKillDetector.h b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_LetsGoKillDetector.h index dde7152198..985e0f4359 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_LetsGoKillDetector.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_LetsGoKillDetector.h @@ -7,9 +7,9 @@ #ifndef PokemonAutomation_PokemonSV_LetsGoKillDetector_H #define PokemonAutomation_PokemonSV_LetsGoKillDetector_H -#include #include #include "Common/Cpp/Color.h" +#include "Common/Cpp/MoveOnlyFunction.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonTools/Audio/AudioPerSpectrumDetectorBase.h" @@ -39,7 +39,7 @@ class LetsGoKillWatcher : public DetectorToFinder{ Logger& logger, Color color, bool trigger_if_detected = true, const ImageFloatBox& box = {0.71, 0.15, 0.04, 0.08}, - std::function on_kill_callback = nullptr, + MoveOnlyFunction on_kill_callback = nullptr, std::chrono::milliseconds duration = std::chrono::milliseconds(250) ); @@ -52,7 +52,7 @@ class LetsGoKillWatcher : public DetectorToFinder{ private: Logger& m_logger; bool m_trigger_if_detected; - std::function m_on_kill_callback; + MoveOnlyFunction m_on_kill_callback; bool m_last_detection; std::atomic m_last_detected; From be7bcf8ab6ed7cbeef43e9ea2f7c75b407e6a9bc Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 31 Oct 2026 08:45:44 -0700 Subject: [PATCH 6/7] PABotBase2: hold message handlers and the rumble callback in MoveOnlyFunction DeviceHandle owns each message handler and the OEM controller owns its rumble callback; neither is copied. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Controllers/PABotBase2/PABotBase2_DeviceHandle.cpp | 2 +- .../Source/Controllers/PABotBase2/PABotBase2_DeviceHandle.h | 5 +++-- .../PABotBase2/NintendoSwitch_PABotBase2_Joycon.h | 2 +- .../PABotBase2/NintendoSwitch_PABotBase2_OemController.cpp | 2 +- .../PABotBase2/NintendoSwitch_PABotBase2_OemController.h | 5 +++-- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/SerialPrograms/Source/Controllers/PABotBase2/PABotBase2_DeviceHandle.cpp b/SerialPrograms/Source/Controllers/PABotBase2/PABotBase2_DeviceHandle.cpp index 27142afdf0..af7338714c 100644 --- a/SerialPrograms/Source/Controllers/PABotBase2/PABotBase2_DeviceHandle.cpp +++ b/SerialPrograms/Source/Controllers/PABotBase2/PABotBase2_DeviceHandle.cpp @@ -79,7 +79,7 @@ DeviceHandle::~DeviceHandle(){ void DeviceHandle::add_message_handler( uint8_t opcode, - std::function handler + MoveOnlyFunction handler ){ auto ret = m_message_handlers.emplace(opcode, std::move(handler)); if (!ret.second){ diff --git a/SerialPrograms/Source/Controllers/PABotBase2/PABotBase2_DeviceHandle.h b/SerialPrograms/Source/Controllers/PABotBase2/PABotBase2_DeviceHandle.h index ffbee53f0a..7da7a0bc20 100644 --- a/SerialPrograms/Source/Controllers/PABotBase2/PABotBase2_DeviceHandle.h +++ b/SerialPrograms/Source/Controllers/PABotBase2/PABotBase2_DeviceHandle.h @@ -11,6 +11,7 @@ #include #include #include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/MoveOnlyFunction.h" #include "Common/Cpp/PrettyPrint.h" #include "Common/Cpp/Logging/AbstractLogger.h" #include "Common/Cpp/Concurrency/Mutex.h" @@ -38,7 +39,7 @@ class DeviceHandle final : public CancellableScope, private StreamListener{ virtual ~DeviceHandle(); void add_message_handler( uint8_t opcode, - std::function handler + MoveOnlyFunction handler ); virtual bool cancel(std::exception_ptr exception) noexcept override; @@ -196,7 +197,7 @@ class DeviceHandle final : public CancellableScope, private StreamListener{ std::deque m_buffer; MessageLogger m_message_loggers; - std::map> m_message_handlers; + std::map> m_message_handlers; }; diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/PABotBase2/NintendoSwitch_PABotBase2_Joycon.h b/SerialPrograms/Source/NintendoSwitch/Controllers/PABotBase2/NintendoSwitch_PABotBase2_Joycon.h index f722b93909..90caef4a51 100644 --- a/SerialPrograms/Source/NintendoSwitch/Controllers/PABotBase2/NintendoSwitch_PABotBase2_Joycon.h +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/PABotBase2/NintendoSwitch_PABotBase2_Joycon.h @@ -30,7 +30,7 @@ class PABotBase2_JoyCon : PABotBase2::Connection& connection, ControllerClass controller_class, ControllerType controller_type, - std::function on_rumble + MoveOnlyFunction on_rumble ) : JoyconType(logger, controller_class) , PABotBase2_OemController( diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/PABotBase2/NintendoSwitch_PABotBase2_OemController.cpp b/SerialPrograms/Source/NintendoSwitch/Controllers/PABotBase2/NintendoSwitch_PABotBase2_OemController.cpp index 082d60c759..51f575e858 100644 --- a/SerialPrograms/Source/NintendoSwitch/Controllers/PABotBase2/NintendoSwitch_PABotBase2_OemController.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/PABotBase2/NintendoSwitch_PABotBase2_OemController.cpp @@ -122,7 +122,7 @@ PABotBase2_OemController::PABotBase2_OemController( RecursiveThrottler& logging_throttler, PABotBase2::Connection& connection, ControllerType controller_type, - std::function on_rumble + MoveOnlyFunction on_rumble ) : PABotBase2_Controller(logger, logging_throttler, connection) , m_controller_type(controller_type) diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/PABotBase2/NintendoSwitch_PABotBase2_OemController.h b/SerialPrograms/Source/NintendoSwitch/Controllers/PABotBase2/NintendoSwitch_PABotBase2_OemController.h index f586f83cc4..28bdd3be0d 100644 --- a/SerialPrograms/Source/NintendoSwitch/Controllers/PABotBase2/NintendoSwitch_PABotBase2_OemController.h +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/PABotBase2/NintendoSwitch_PABotBase2_OemController.h @@ -8,6 +8,7 @@ #define PokemonAutomation_NintendoSwitch_PABotBase2_OemController_H #include "Common/ControllerStates/NintendoSwitch_OemController_State.h" +#include "Common/Cpp/MoveOnlyFunction.h" #include "Controllers/ControllerStatusThread.h" #include "Controllers/JoystickTools.h" //#include "NintendoSwitch/NintendoSwitch_Settings.h" @@ -36,7 +37,7 @@ class PABotBase2_OemController : RecursiveThrottler& logging_throttler, PABotBase2::Connection& connection, ControllerType controller_type, - std::function on_rumble + MoveOnlyFunction on_rumble ); ~PABotBase2_OemController(); void stop(); @@ -166,7 +167,7 @@ class PABotBase2_OemController : Milliseconds m_cooldown; Milliseconds m_timing_variation; - std::function m_on_rumble; + MoveOnlyFunction m_on_rumble; std::unique_ptr m_status_thread; From 352d5ae046b0aafa9a476582ee6a2be9ef46c69d Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 31 Oct 2026 08:46:49 -0700 Subject: [PATCH 7/7] Integrations: hold the Discord webhook finish callback in MoveOnlyFunction The webhook sender moves the callback into its scheduled send and never copies it. The send lambdas become mutable so they can call it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- SerialPrograms/Source/Integrations/DiscordWebhook.cpp | 8 ++++---- SerialPrograms/Source/Integrations/DiscordWebhook.h | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/SerialPrograms/Source/Integrations/DiscordWebhook.cpp b/SerialPrograms/Source/Integrations/DiscordWebhook.cpp index 30e58621a6..fe7b187e5b 100644 --- a/SerialPrograms/Source/Integrations/DiscordWebhook.cpp +++ b/SerialPrograms/Source/Integrations/DiscordWebhook.cpp @@ -62,7 +62,7 @@ void DiscordWebhookSender::send( const QUrl& url, std::chrono::milliseconds delay, const JsonObject& obj, std::shared_ptr file, - std::function finish_callback + MoveOnlyFunction finish_callback ){ cleanup_stuck_requests(); std::shared_ptr json(new JsonValue(obj.clone())); @@ -75,7 +75,7 @@ void DiscordWebhookSender::send( json = std::move(json), file = std::move(file), finish_callback = std::move(finish_callback) - ]{ + ]() mutable{ throttle(); std::vector attachments; if (file){ @@ -95,7 +95,7 @@ void DiscordWebhookSender::send( const QUrl& url, std::chrono::milliseconds delay, const JsonObject& obj, std::vector> files, - std::function finish_callback + MoveOnlyFunction finish_callback ){ cleanup_stuck_requests(); std::shared_ptr json(new JsonValue(obj.clone())); @@ -108,7 +108,7 @@ void DiscordWebhookSender::send( json = std::move(json), files = std::move(files), finish_callback = std::move(finish_callback) - ]{ + ]() mutable{ throttle(); std::vector attachments; for (auto& file : files){ diff --git a/SerialPrograms/Source/Integrations/DiscordWebhook.h b/SerialPrograms/Source/Integrations/DiscordWebhook.h index 425d3ac7f3..c380e6a172 100644 --- a/SerialPrograms/Source/Integrations/DiscordWebhook.h +++ b/SerialPrograms/Source/Integrations/DiscordWebhook.h @@ -9,6 +9,7 @@ #include #include +#include "Common/Cpp/MoveOnlyFunction.h" #include "Common/Cpp/Time.h" #include "Common/Cpp/Concurrency/Mutex.h" #include "Common/Cpp/Concurrency/ConditionVariable.h" @@ -52,14 +53,14 @@ class DiscordWebhookSender : public QObject{ const QUrl& url, std::chrono::milliseconds delay, const JsonObject& obj, std::shared_ptr file, - std::function finish_callback = nullptr + MoveOnlyFunction finish_callback = nullptr ); void send( Logger& logger, const QUrl& url, std::chrono::milliseconds delay, const JsonObject& obj, std::vector> files, - std::function finish_callback = nullptr + MoveOnlyFunction finish_callback = nullptr ); static DiscordWebhookSender& instance();