early-access version 3345

This commit is contained in:
pineappleEA
2023-01-29 10:08:08 +01:00
parent 19de7fc82d
commit 3457f75f51
95 changed files with 567 additions and 994 deletions

View File

@@ -130,8 +130,6 @@ struct ButtonStatus {
bool inverted{};
// Press once to activate, press again to release
bool toggle{};
// Spams the button when active
bool turbo{};
// Internal lock for the toggle status
bool locked{};
};

View File

@@ -41,18 +41,17 @@ bool StoppableTimedWait(std::stop_token token, const std::chrono::duration<Rep,
#include <chrono>
#include <condition_variable>
#include <functional>
#include <map>
#include <list>
#include <memory>
#include <mutex>
#include <optional>
#include <thread>
#include <type_traits>
#include <utility>
namespace std {
namespace polyfill {
using stop_state_callback = size_t;
using stop_state_callbacks = list<function<void()>>;
class stop_state {
public:
@@ -60,69 +59,61 @@ public:
~stop_state() = default;
bool request_stop() {
unique_lock lk{m_lock};
stop_state_callbacks callbacks;
if (m_stop_requested) {
// Already set, nothing to do.
return false;
{
scoped_lock lk{m_lock};
if (m_stop_requested.load()) {
// Already set, nothing to do
return false;
}
// Set as requested
m_stop_requested = true;
// Copy callback list
callbacks = m_callbacks;
}
// Mark stop requested.
m_stop_requested = true;
while (!m_callbacks.empty()) {
// Get an iterator to the first element.
const auto it = m_callbacks.begin();
// Move the callback function out of the map.
function<void()> f;
swap(it->second, f);
// Erase the now-empty map element.
m_callbacks.erase(it);
// Run the callback.
if (f) {
f();
}
for (auto callback : callbacks) {
callback();
}
return true;
}
bool stop_requested() const {
unique_lock lk{m_lock};
return m_stop_requested;
return m_stop_requested.load();
}
stop_state_callback insert_callback(function<void()> f) {
unique_lock lk{m_lock};
stop_state_callbacks::const_iterator insert_callback(function<void()> f) {
stop_state_callbacks::const_iterator ret{};
bool should_run{};
if (m_stop_requested) {
// Stop already requested. Don't insert anything,
// just run the callback synchronously.
if (f) {
f();
}
return 0;
{
scoped_lock lk{m_lock};
should_run = m_stop_requested.load();
m_callbacks.push_front(f);
ret = m_callbacks.begin();
}
if (should_run) {
f();
}
// Insert the callback.
stop_state_callback ret = ++m_next_callback;
m_callbacks.emplace(ret, move(f));
return ret;
}
void remove_callback(stop_state_callback cb) {
unique_lock lk{m_lock};
m_callbacks.erase(cb);
void remove_callback(stop_state_callbacks::const_iterator it) {
scoped_lock lk{m_lock};
m_callbacks.erase(it);
}
private:
mutable recursive_mutex m_lock;
map<stop_state_callback, function<void()>> m_callbacks;
stop_state_callback m_next_callback{0};
bool m_stop_requested{false};
mutex m_lock;
atomic<bool> m_stop_requested;
stop_state_callbacks m_callbacks;
};
} // namespace polyfill
@@ -232,7 +223,7 @@ public:
}
~stop_callback() {
if (m_stop_state && m_callback) {
m_stop_state->remove_callback(m_callback);
m_stop_state->remove_callback(*m_callback);
}
}
@@ -243,7 +234,7 @@ public:
private:
shared_ptr<polyfill::stop_state> m_stop_state;
polyfill::stop_state_callback m_callback;
optional<polyfill::stop_state_callbacks::const_iterator> m_callback;
};
template <typename Callback>

View File

@@ -30,7 +30,7 @@ std::string ToUpper(std::string str) {
return str;
}
std::string StringFromBuffer(std::span<const u8> data) {
std::string StringFromBuffer(const std::vector<u8>& data) {
return std::string(data.begin(), std::find(data.begin(), data.end(), '\0'));
}

View File

@@ -5,7 +5,6 @@
#pragma once
#include <cstddef>
#include <span>
#include <string>
#include <vector>
#include "common/common_types.h"
@@ -18,7 +17,7 @@ namespace Common {
/// Make a string uppercase
[[nodiscard]] std::string ToUpper(std::string str);
[[nodiscard]] std::string StringFromBuffer(std::span<const u8> data);
[[nodiscard]] std::string StringFromBuffer(const std::vector<u8>& data);
[[nodiscard]] std::string StripSpaces(const std::string& s);
[[nodiscard]] std::string StripQuotes(const std::string& s);