#pragma once

#include <mutex>
#include <optional>
#include <utility>

namespace ta::foundation
{
template <class T>
class CopySnapshotPublisher
{
public:
    void publish(T snapshot)
    {
        std::lock_guard lock(mutex_);
        snapshot_ = std::move(snapshot);
    }

    [[nodiscard]] std::optional<T> latest() const
    {
        std::lock_guard lock(mutex_);
        return snapshot_;
    }


    void clear()
    {
        std::lock_guard lock(mutex_);
        snapshot_.reset();
    }

private:
    mutable std::mutex mutex_;
    std::optional<T> snapshot_;
};
}
