2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2018-10-31 20:22:58 -07:00
|
|
|
|
|
|
|
|
#ifndef CSCORE_INSTANCE_H_
|
|
|
|
|
#define CSCORE_INSTANCE_H_
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
|
#include <wpi/EventLoopRunner.h>
|
|
|
|
|
#include <wpi/Logger.h>
|
|
|
|
|
|
|
|
|
|
#include "Log.h"
|
|
|
|
|
#include "NetworkListener.h"
|
|
|
|
|
#include "Notifier.h"
|
|
|
|
|
#include "SinkImpl.h"
|
|
|
|
|
#include "SourceImpl.h"
|
|
|
|
|
#include "Telemetry.h"
|
|
|
|
|
#include "UnlimitedHandleResource.h"
|
|
|
|
|
|
|
|
|
|
namespace cs {
|
|
|
|
|
|
|
|
|
|
struct SourceData {
|
|
|
|
|
SourceData(CS_SourceKind kind_, std::shared_ptr<SourceImpl> source_)
|
|
|
|
|
: kind{kind_}, refCount{0}, source{source_} {}
|
|
|
|
|
|
|
|
|
|
CS_SourceKind kind;
|
|
|
|
|
std::atomic_int refCount;
|
|
|
|
|
std::shared_ptr<SourceImpl> source;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct SinkData {
|
|
|
|
|
explicit SinkData(CS_SinkKind kind_, std::shared_ptr<SinkImpl> sink_)
|
|
|
|
|
: kind{kind_}, refCount{0}, sourceHandle{0}, sink{sink_} {}
|
|
|
|
|
|
|
|
|
|
CS_SinkKind kind;
|
|
|
|
|
std::atomic_int refCount;
|
|
|
|
|
std::atomic<CS_Source> sourceHandle;
|
|
|
|
|
std::shared_ptr<SinkImpl> sink;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Instance {
|
|
|
|
|
public:
|
|
|
|
|
Instance(const Instance&) = delete;
|
|
|
|
|
Instance& operator=(const Instance&) = delete;
|
|
|
|
|
~Instance();
|
|
|
|
|
|
|
|
|
|
static Instance& GetInstance();
|
|
|
|
|
|
2018-11-07 00:01:27 -08:00
|
|
|
void Shutdown();
|
|
|
|
|
|
2018-10-31 20:22:58 -07:00
|
|
|
wpi::Logger logger;
|
|
|
|
|
Notifier notifier;
|
|
|
|
|
Telemetry telemetry;
|
2018-11-06 22:13:52 -08:00
|
|
|
NetworkListener networkListener;
|
2018-11-06 19:42:39 -08:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
UnlimitedHandleResource<Handle, SourceData, Handle::kSource> m_sources;
|
|
|
|
|
UnlimitedHandleResource<Handle, SinkData, Handle::kSink> m_sinks;
|
|
|
|
|
|
|
|
|
|
public:
|
2018-11-06 22:13:52 -08:00
|
|
|
wpi::EventLoopRunner eventLoop;
|
2018-10-31 20:22:58 -07:00
|
|
|
|
|
|
|
|
std::pair<CS_Sink, std::shared_ptr<SinkData>> FindSink(const SinkImpl& sink);
|
|
|
|
|
std::pair<CS_Source, std::shared_ptr<SourceData>> FindSource(
|
|
|
|
|
const SourceImpl& source);
|
|
|
|
|
|
|
|
|
|
void SetDefaultLogger();
|
|
|
|
|
|
2018-11-06 19:42:39 -08:00
|
|
|
std::shared_ptr<SourceData> GetSource(CS_Source handle) {
|
|
|
|
|
return m_sources.Get(handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<SinkData> GetSink(CS_Sink handle) {
|
|
|
|
|
return m_sinks.Get(handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CS_Source CreateSource(CS_SourceKind kind,
|
|
|
|
|
std::shared_ptr<SourceImpl> source);
|
|
|
|
|
|
|
|
|
|
CS_Sink CreateSink(CS_SinkKind kind, std::shared_ptr<SinkImpl> sink);
|
|
|
|
|
|
|
|
|
|
void DestroySource(CS_Source handle);
|
|
|
|
|
void DestroySink(CS_Sink handle);
|
|
|
|
|
|
|
|
|
|
wpi::ArrayRef<CS_Source> EnumerateSourceHandles(
|
|
|
|
|
wpi::SmallVectorImpl<CS_Source>& vec) {
|
|
|
|
|
return m_sources.GetAll(vec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wpi::ArrayRef<CS_Sink> EnumerateSinkHandles(
|
|
|
|
|
wpi::SmallVectorImpl<CS_Sink>& vec) {
|
|
|
|
|
return m_sinks.GetAll(vec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wpi::ArrayRef<CS_Sink> EnumerateSourceSinks(
|
|
|
|
|
CS_Source source, wpi::SmallVectorImpl<CS_Sink>& vec) {
|
|
|
|
|
vec.clear();
|
|
|
|
|
m_sinks.ForEach([&](CS_Sink sinkHandle, const SinkData& data) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (source == data.sourceHandle.load()) {
|
|
|
|
|
vec.push_back(sinkHandle);
|
|
|
|
|
}
|
2018-11-06 19:42:39 -08:00
|
|
|
});
|
|
|
|
|
return vec;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 20:22:58 -07:00
|
|
|
private:
|
|
|
|
|
Instance();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace cs
|
|
|
|
|
|
|
|
|
|
#endif // CSCORE_INSTANCE_H_
|