// Copyright (c) FIRST and other WPILib contributors. // Open Source Software; you can modify and/or share it under the terms of // the WPILib BSD license file in the root directory of this project. #ifndef CSCORE_INSTANCE_HPP_ #define CSCORE_INSTANCE_HPP_ #include #include #include "Log.hpp" #include "NetworkListener.hpp" #include "Notifier.hpp" #include "SinkImpl.hpp" #include "SourceImpl.hpp" #include "Telemetry.hpp" #include "UnlimitedHandleResource.hpp" #include "UsbCameraListener.hpp" #include "wpi/net/EventLoopRunner.hpp" #include "wpi/util/Logger.hpp" namespace wpi::cs { struct SourceData { SourceData(CS_SourceKind kind_, std::shared_ptr source_) : kind{kind_}, refCount{0}, source{std::move(source_)} {} CS_SourceKind kind; std::atomic_int refCount; std::shared_ptr source; }; struct SinkData { explicit SinkData(CS_SinkKind kind_, std::shared_ptr sink_) : kind{kind_}, refCount{0}, sourceHandle{0}, sink{std::move(sink_)} {} CS_SinkKind kind; std::atomic_int refCount; std::atomic sourceHandle; std::shared_ptr sink; }; class Instance { public: Instance(const Instance&) = delete; Instance& operator=(const Instance&) = delete; ~Instance(); static Instance& GetInstance(); void Shutdown(); wpi::util::Logger logger; Notifier notifier; Telemetry telemetry; NetworkListener networkListener; UsbCameraListener usbCameraListener; private: UnlimitedHandleResource m_sources; UnlimitedHandleResource m_sinks; public: wpi::net::EventLoopRunner eventLoop; std::pair> FindSink(const SinkImpl& sink); std::pair> FindSource( const SourceImpl& source); void SetDefaultLogger(); std::shared_ptr GetSource(CS_Source handle) { return m_sources.Get(handle); } std::shared_ptr GetSink(CS_Sink handle) { return m_sinks.Get(handle); } CS_Source CreateSource(CS_SourceKind kind, std::shared_ptr source); CS_Sink CreateSink(CS_SinkKind kind, std::shared_ptr sink); void DestroySource(CS_Source handle); void DestroySink(CS_Sink handle); std::span EnumerateSourceHandles( wpi::util::SmallVectorImpl& vec) { return m_sources.GetAll(vec); } std::span EnumerateSinkHandles( wpi::util::SmallVectorImpl& vec) { return m_sinks.GetAll(vec); } std::span EnumerateSourceSinks( CS_Source source, wpi::util::SmallVectorImpl& vec) { vec.clear(); m_sinks.ForEach([&](CS_Sink sinkHandle, const SinkData& data) { if (source == data.sourceHandle.load()) { vec.push_back(sinkHandle); } }); return vec; } private: Instance(); }; } // namespace wpi::cs #endif // CSCORE_INSTANCE_HPP_