refactor: Use Mutex to protect m_open and m_time in Clipboard.cpp

This commit is contained in:
sithlord48 2026-06-18 20:18:59 -04:00 committed by Chris Rizzitello
parent 5eb1d7068e
commit 81e6e13d94
2 changed files with 9 additions and 0 deletions

View file

@ -21,6 +21,7 @@ Clipboard::Clipboard()
bool Clipboard::empty() bool Clipboard::empty()
{ {
std::scoped_lock lock{m_mutex};
if (!m_open) { if (!m_open) {
LOG_WARN("cannot empty clipboard, not open"); LOG_WARN("cannot empty clipboard, not open");
return false; return false;
@ -43,6 +44,7 @@ bool Clipboard::empty()
void Clipboard::add(Format format, const std::string &data) void Clipboard::add(Format format, const std::string &data)
{ {
std::scoped_lock lock{m_mutex};
if (!m_open) { if (!m_open) {
LOG_WARN("cannot add to clipboard, not open"); LOG_WARN("cannot add to clipboard, not open");
return; return;
@ -60,6 +62,7 @@ void Clipboard::add(Format format, const std::string &data)
bool Clipboard::open(Time time) const bool Clipboard::open(Time time) const
{ {
std::scoped_lock lock{m_mutex};
if (m_open) { if (m_open) {
LOG_DEBUG("skipping clipboard open, already open"); LOG_DEBUG("skipping clipboard open, already open");
return true; return true;
@ -73,6 +76,7 @@ bool Clipboard::open(Time time) const
void Clipboard::close() const void Clipboard::close() const
{ {
std::scoped_lock lock{m_mutex};
if (!m_open) { if (!m_open) {
LOG_WARN("clipboard is not open"); LOG_WARN("clipboard is not open");
} }
@ -86,6 +90,7 @@ Clipboard::Time Clipboard::getTime() const
bool Clipboard::has(Format format) const bool Clipboard::has(Format format) const
{ {
std::scoped_lock lock{m_mutex};
if (!m_open) { if (!m_open) {
LOG_WARN("cannot check for clipboard format, not open"); LOG_WARN("cannot check for clipboard format, not open");
return false; return false;
@ -95,6 +100,7 @@ bool Clipboard::has(Format format) const
std::string Clipboard::get(Format format) const std::string Clipboard::get(Format format) const
{ {
std::scoped_lock lock{m_mutex};
if (!m_open) { if (!m_open) {
LOG_WARN("cannot get clipboard format, not open"); LOG_WARN("cannot get clipboard format, not open");
return ""; return "";

View file

@ -9,6 +9,8 @@
#include "deskflow/IClipboard.h" #include "deskflow/IClipboard.h"
#include <mutex>
//! Memory buffer clipboard //! Memory buffer clipboard
/*! /*!
This class implements a clipboard that stores data in memory. This class implements a clipboard that stores data in memory.
@ -53,6 +55,7 @@ public:
private: private:
mutable bool m_open = false; mutable bool m_open = false;
mutable std::mutex m_mutex;
mutable Time m_time; mutable Time m_time;
bool m_owner = false; bool m_owner = false;
Time m_timeOwned; Time m_timeOwned;