From 142a16128363e709a5955f32f82ad6393407432b Mon Sep 17 00:00:00 2001 From: "J.A. de Jong - Redu-Sone B.V., ASCEE V.O.F" Date: Wed, 19 Oct 2022 11:57:51 +0200 Subject: [PATCH] Improved stream handling code in stream manager. No pointers to pointers anymore. --- src/lasp/device/lasp_streammgr.cpp | 38 +++++++++++++++++------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/lasp/device/lasp_streammgr.cpp b/src/lasp/device/lasp_streammgr.cpp index dea3662..4fc4d4b 100644 --- a/src/lasp/device/lasp_streammgr.cpp +++ b/src/lasp/device/lasp_streammgr.cpp @@ -270,36 +270,45 @@ void StreamMgr::startStream(const DaqConfiguration &config) { using namespace std::placeholders; std::unique_ptr daq = Daq::createDaq(devinfo, config); - std::unique_ptr *stream_placeholder; if (isInput) { + /// Give incallback as parameter to stream inCallback = std::bind(&StreamMgr::inCallback, this, _1); - stream_placeholder = std::addressof(_inputStream); + + /// Reset handlers in case of an input stream for (auto &handler : _inDataHandlers) { handler->reset(daq.get()); } } + if (isOutput) { + /// Give outcallback as parameter to stream + outCallback = std::bind(&StreamMgr::outCallback, this, _1); + + /// Reset signal generator in case of an output stream if (_siggen) { DEBUGTRACE_PRINT("Resetting _siggen with new samplerate of "); DEBUGTRACE_PRINT(daq->samplerate()); _siggen->reset(daq->samplerate()); } - outCallback = std::bind(&StreamMgr::outCallback, this, _1); - if(!isDuplex) { - stream_placeholder = std::addressof(_outputStream); - } } + /// Start the DAQ. If it fails, everything is still nicely cleaned up and the + /// daq unique_ptr cleans up resources nicely. daq->start(inCallback, outCallback); // Move daq ptr to right place - *stream_placeholder = std::move(daq); + if (isInput) { + _inputStream = std::move(daq); + } else { + _outputStream = std::move(daq); + } } void StreamMgr::stopStream(const StreamType t) { + DEBUGTRACE_ENTER; checkRightThread(); - switch (t) { - case (StreamType::input): { + + if (t == StreamType::input) { if (!_inputStream) { throw rte("Input stream is not running"); } @@ -309,21 +318,18 @@ void StreamMgr::stopStream(const StreamType t) { for (auto &handler : _inDataHandlers) { handler->reset(nullptr); } - } break; - case (StreamType::output): { + } else { + /// t == output + + /// Kill input stream in case that one is a duplex stream if (_inputStream && _inputStream->duplexMode()) { _inputStream.reset(); } else { - if (!_outputStream) { throw rte("Output stream is not running"); } _outputStream.reset(); } // end else - } break; - default: - throw rte("BUG"); - break; } }