mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 03:07:04 +02:00
31 lines
482 B
C
31 lines
482 B
C
|
|
#pragma once
|
||
|
|
|
||
|
|
#include <functional>
|
||
|
|
#include <shared_mutex>
|
||
|
|
|
||
|
|
template<typename StateT>
|
||
|
|
class Serialized
|
||
|
|
{
|
||
|
|
mutable std::shared_mutex m;
|
||
|
|
StateT s;
|
||
|
|
|
||
|
|
public:
|
||
|
|
void Read(std::function<void(const StateT&)> fn) const
|
||
|
|
{
|
||
|
|
std::shared_lock lock{ m };
|
||
|
|
fn(s);
|
||
|
|
}
|
||
|
|
|
||
|
|
void Access(std::function<void(StateT&)> fn)
|
||
|
|
{
|
||
|
|
std::unique_lock lock{ m };
|
||
|
|
fn(s);
|
||
|
|
}
|
||
|
|
|
||
|
|
void Reset()
|
||
|
|
{
|
||
|
|
std::unique_lock lock{ m };
|
||
|
|
s = {};
|
||
|
|
}
|
||
|
|
};
|