#pragma once

#include <atomic>
#include <memory>
#include <utility>

namespace ta::foundation
{
template <class T>
class SharedSnapshotPublisher
{
public:
    using Snapshot = std::shared_ptr<const T>;

    void publish(T snapshot)
    {
        publish(std::make_shared<const T>(std::move(snapshot)));
    }

    void publish(Snapshot snapshot) noexcept
    {
        snapshot_.store(std::move(snapshot), std::memory_order_release);
    }

    [[nodiscard]] Snapshot latest() const noexcept
    {
        return snapshot_.load(std::memory_order_acquire);
    }


    void clear() noexcept
    {
        publish(Snapshot{});
    }

private:
    std::atomic<Snapshot> snapshot_{Snapshot{}};
};
}
