#pragma once

#include <atomic>
#include <type_traits>

namespace ta::foundation
{
/**
 * Small publication primitive for runtime bridges and hook callbacks.
 * Writers publish with release semantics; readers acquire a coherent value.
 */
template <class T>
class AtomicPort final
{
    static_assert(std::is_trivially_copyable_v<T>, "AtomicPort requires a trivially copyable value");

public:
    constexpr AtomicPort() noexcept = default;
    explicit constexpr AtomicPort(const T initial) noexcept : value_(initial) {}

    void publish(const T value) noexcept
    {
        value_.store(value, std::memory_order_release);
    }

    [[nodiscard]] T acquire() const noexcept
    {
        return value_.load(std::memory_order_acquire);
    }

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

    bool clear_if(T expected) noexcept
    {
        return value_.compare_exchange_strong(
            expected,
            T{},
            std::memory_order_acq_rel,
            std::memory_order_acquire);
    }

private:
    std::atomic<T> value_{T{}};
};
}
