#pragma once

#include <version>

#include <psemek/audio/stream.hpp>
#ifdef __cpp_lib_atomic_shared_ptr
	#include <atomic>
#else
	#include <psemek/util/mutexed.hpp>
#endif

#include <memory>

namespace psemek::audio
{

	struct channel
	{
		channel() = default;

		channel(stream_ptr stream)
			: stream_(std::move(stream))
		{}

		stream_ptr stream() const
		{
			return stream_.load();
		}

		stream_ptr stream(stream_ptr new_stream)
		{
			return stream_.exchange(std::move(new_stream));
		}

		stream_ptr stop()
		{
			return stream(nullptr);
		}

		bool is_stopped() const
		{
			return stream() == nullptr;
		}

	private:
		#ifdef __cpp_lib_atomic_shared_ptr
			std::atomic<stream_ptr> stream_;
		#else
			util::mutexed<stream_ptr> stream_;
		#endif
	};

	using channel_ptr = std::shared_ptr<channel>;

}
