/*
 * SPDX-FileCopyrightText: 2025 Copyright contributors to the cppTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */
#ifndef TANGO_INTERNAL_EVENT_PERF_MONITOR_SAMPLEPUSHER_H
#define TANGO_INTERNAL_EVENT_PERF_MONITOR_SAMPLEPUSHER_H

#include <tango/internal/event_perf_monitor/RingBuffer.h>

#include <mutex>

namespace Tango
{
template <typename T>
struct SamplePusher
{
    SamplePusher(bool enabled, T &sample, RingBuffer<T> &buffer, std::mutex &mutex) :
        enabled(enabled),
        sample(sample),
        buffer(buffer),
        mutex(mutex)
    {
    }

    ~SamplePusher()
    {
        if(enabled)
        {
            if(mutex.try_lock())
            {
                buffer.push(sample);
                mutex.unlock();
            }

            sample = T{};
        }
    }

    bool enabled;
    T &sample;
    RingBuffer<T> &buffer;
    std::mutex &mutex;
};
} // namespace Tango

#endif // TANGO_INTERNAL_EVENT_PERF_MONITOR_SAMPLEPUSHER_H
