2016-09-05 12:00:04 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:16:20 -08:00
|
|
|
/* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */
|
2016-09-05 12:00:04 -07:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "SinkImpl.h"
|
|
|
|
|
|
2016-11-18 10:20:56 -08:00
|
|
|
#include "Notifier.h"
|
2016-09-05 12:00:04 -07:00
|
|
|
#include "SourceImpl.h"
|
|
|
|
|
|
|
|
|
|
using namespace cs;
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
SinkImpl::SinkImpl(wpi::StringRef name) : m_name{name} {}
|
2016-09-08 23:52:23 -07:00
|
|
|
|
|
|
|
|
SinkImpl::~SinkImpl() {
|
|
|
|
|
if (m_source) {
|
2016-10-13 00:13:18 -07:00
|
|
|
if (m_enabledCount > 0) m_source->DisableSink();
|
2016-09-08 23:52:23 -07:00
|
|
|
m_source->RemoveSink();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void SinkImpl::SetDescription(wpi::StringRef description) {
|
2017-11-13 09:51:26 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_mutex);
|
2016-10-26 23:31:48 -07:00
|
|
|
m_description = description;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::StringRef SinkImpl::GetDescription(
|
|
|
|
|
wpi::SmallVectorImpl<char>& buf) const {
|
2017-11-13 09:51:26 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_mutex);
|
2016-10-26 23:31:48 -07:00
|
|
|
buf.append(m_description.begin(), m_description.end());
|
2018-04-29 23:33:19 -07:00
|
|
|
return wpi::StringRef{buf.data(), buf.size()};
|
2016-10-26 23:31:48 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-18 10:20:56 -08:00
|
|
|
void SinkImpl::Enable() {
|
2017-11-13 09:51:26 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_mutex);
|
2016-11-18 10:20:56 -08:00
|
|
|
++m_enabledCount;
|
|
|
|
|
if (m_enabledCount == 1) {
|
|
|
|
|
if (m_source) m_source->EnableSink();
|
|
|
|
|
Notifier::GetInstance().NotifySink(*this, CS_SINK_ENABLED);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SinkImpl::Disable() {
|
2017-11-13 09:51:26 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_mutex);
|
2016-11-18 10:20:56 -08:00
|
|
|
--m_enabledCount;
|
|
|
|
|
if (m_enabledCount == 0) {
|
|
|
|
|
if (m_source) m_source->DisableSink();
|
|
|
|
|
Notifier::GetInstance().NotifySink(*this, CS_SINK_DISABLED);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SinkImpl::SetEnabled(bool enabled) {
|
2017-11-13 09:51:26 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_mutex);
|
2016-11-18 10:20:56 -08:00
|
|
|
if (enabled && m_enabledCount == 0) {
|
|
|
|
|
if (m_source) m_source->EnableSink();
|
|
|
|
|
m_enabledCount = 1;
|
|
|
|
|
Notifier::GetInstance().NotifySink(*this, CS_SINK_ENABLED);
|
|
|
|
|
} else if (!enabled && m_enabledCount > 0) {
|
|
|
|
|
if (m_source) m_source->DisableSink();
|
|
|
|
|
m_enabledCount = 0;
|
|
|
|
|
Notifier::GetInstance().NotifySink(*this, CS_SINK_DISABLED);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-08 23:52:23 -07:00
|
|
|
void SinkImpl::SetSource(std::shared_ptr<SourceImpl> source) {
|
2016-11-11 00:01:58 -08:00
|
|
|
{
|
2017-11-13 09:51:26 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_mutex);
|
2016-12-28 00:47:26 -08:00
|
|
|
if (m_source == source) return;
|
2016-11-11 00:01:58 -08:00
|
|
|
if (m_source) {
|
|
|
|
|
if (m_enabledCount > 0) m_source->DisableSink();
|
|
|
|
|
m_source->RemoveSink();
|
|
|
|
|
}
|
|
|
|
|
m_source = source;
|
2016-12-28 00:47:26 -08:00
|
|
|
if (m_source) {
|
|
|
|
|
m_source->AddSink();
|
|
|
|
|
if (m_enabledCount > 0) m_source->EnableSink();
|
|
|
|
|
}
|
2016-09-08 23:52:23 -07:00
|
|
|
}
|
2016-11-11 00:01:58 -08:00
|
|
|
SetSourceImpl(source);
|
2016-09-08 23:52:23 -07:00
|
|
|
}
|
2016-10-26 23:31:48 -07:00
|
|
|
|
|
|
|
|
std::string SinkImpl::GetError() const {
|
2017-11-13 09:51:26 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_mutex);
|
2016-10-26 23:31:48 -07:00
|
|
|
if (!m_source) return "no source connected";
|
2016-12-20 20:48:31 -08:00
|
|
|
return m_source->GetCurFrame().GetError();
|
2016-10-26 23:31:48 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::StringRef SinkImpl::GetError(wpi::SmallVectorImpl<char>& buf) const {
|
2017-11-13 09:51:26 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_mutex);
|
2016-10-26 23:31:48 -07:00
|
|
|
if (!m_source) return "no source connected";
|
2016-12-20 20:48:31 -08:00
|
|
|
// Make a copy as it's shared data
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::StringRef error = m_source->GetCurFrame().GetError();
|
2016-12-20 20:48:31 -08:00
|
|
|
buf.clear();
|
|
|
|
|
buf.append(error.data(), error.data() + error.size());
|
2018-04-29 23:33:19 -07:00
|
|
|
return wpi::StringRef{buf.data(), buf.size()};
|
2016-10-26 23:31:48 -07:00
|
|
|
}
|
2016-11-11 00:01:58 -08:00
|
|
|
|
|
|
|
|
void SinkImpl::SetSourceImpl(std::shared_ptr<SourceImpl> source) {}
|