Changeset - 9a3e4ca7c84f
[Not reviewed]
stream
0 5 0
Hasan Yavuz Ă–ZDERYA - 8 years ago 2018-05-26 15:43:58
hy@ozderya.net
disconnect previous source in `connectSink` function
5 files changed with 15 insertions and 13 deletions:
0 comments (0 inline, 0 general)
src/mainwindow.cpp
Show inline comments
 
@@ -339,30 +339,24 @@ void MainWindow::setupAboutDialog()
 
    uiAboutDialog.lbAbout->setText(aboutText);
 
}
 

	
 
void MainWindow::onPortToggled(bool open)
 
{
 
    // make sure demo mode is disabled
 
    if (open && isDemoRunning()) enableDemo(false);
 
    ui->actionDemoMode->setEnabled(!open);
 
}
 

	
 
void MainWindow::setStreamSource(Source* source)
 
{
 
    // disconnect previous source
 
    auto currentSource = stream.connectedSource();
 
    if (currentSource != nullptr)
 
    {
 
        currentSource->disconnect((Sink*) &stream);
 
    }
 
    // connect to new source
 
    source->connectSink(&stream);
 
}
 

	
 
void MainWindow::clearPlot()
 
{
 
    stream.clear();
 
    plotMan->replot();
 
}
 

	
 
void MainWindow::onNumOfSamplesChanged(int value)
 
{
src/sink.cpp
Show inline comments
 
@@ -44,25 +44,25 @@ void Sink::feedIn(const SamplePack& data
 
}
 

	
 
void Sink::setNumChannels(unsigned nc, bool x)
 
{
 
    _numChannels = nc;
 
    _hasX = x;
 
    for (auto sink : followers)
 
    {
 
        sink->setNumChannels(nc, x);
 
    }
 
}
 

	
 
void Sink::setSource(const Source* s)
 
void Sink::setSource(Source* s)
 
{
 
    Q_ASSERT((source == nullptr) != (s == nullptr));
 
    source = s;
 
}
 

	
 
const Source* Sink::connectedSource() const
 
{
 
    return source;
 
}
 

	
 
Source* Sink::connectedSource()
 
{
src/sink.h
Show inline comments
 
@@ -44,28 +44,30 @@ public:
 
    Source* connectedSource();
 

	
 
protected:
 
    /// Entry point for incoming data. Re-implementations should
 
    /// call this function to feed followers.
 
    virtual void feedIn(const SamplePack& data);
 

	
 
    /// Is set by connected source. Re-implementations should call
 
    /// this function to update followers.
 
    virtual void setNumChannels(unsigned nc, bool x);
 

	
 
    /// Set by the connected source when its connected. When
 
    /// disconnecting it's set to `NULL`.
 
    /// disconnecting it's set to `nullptr`.
 
    ///
 
    /// @note Previous source is disconnected.
 
    ///
 
    /// @important Trying to connect a source while its already
 
    /// connected is an error.
 
    void setSource(const Source* s);
 
    void setSource(Source* s);
 

	
 
    friend Source;
 

	
 
private:
 
    QList<Sink*> followers;
 
    const Source* source = nullptr;   ///< source that this sink is connected to
 
    Source* source = nullptr;   ///< source that this sink is connected to
 
    bool _hasX;
 
    unsigned _numChannels;
 
};
 

	
 
#endif // SINK_H
src/source.cpp
Show inline comments
 
@@ -23,25 +23,30 @@
 

	
 
Source::~Source()
 
{
 
    for (auto sink : sinks)
 
    {
 
        sink->setSource(nullptr);
 
    }
 
}
 

	
 
void Source::connectSink(Sink* sink)
 
{
 
    Q_ASSERT(!sinks.contains(sink));
 
    Q_ASSERT(sink->connectedSource() == nullptr);
 

	
 
    auto prevSource = sink->connectedSource();
 
    if (prevSource != nullptr)
 
    {
 
        prevSource->disconnect(sink);
 
    }
 

	
 
    sinks.append(sink);
 
    sink->setSource(this);
 
    sink->setNumChannels(numChannels(), hasX());
 
}
 

	
 
void Source::disconnect(Sink* sink)
 
{
 
    Q_ASSERT(sinks.contains(sink));
 
    Q_ASSERT(sink->connectedSource() == this);
 

	
 
    sink->setSource(nullptr);
src/source.h
Show inline comments
 
@@ -28,26 +28,27 @@
 
class Source
 
{
 
public:
 
    /// Virtual destructor. Must be called by implementors to notify sinks.
 
    virtual ~Source();
 

	
 
    /// Returns true if source has X data
 
    virtual bool hasX() const = 0;
 

	
 
    /// Returns number of channels
 
    virtual unsigned numChannels() const = 0;
 

	
 
    /// Connects a sink to this source. Trying to connect an already
 
    /// connected sink is an error.
 
    /// Connects a sink to this source.
 
    ///
 
    /// If `Sink` is already connected to a source, it's disconnected first.
 
    void connectSink(Sink* sink);
 

	
 
    /// Disconnects an already connected sink. Trying to disconnect an
 
    /// unconnected sink is an error.
 
    void disconnect(Sink* sink);
 

	
 
    /// Disconnects all connected sinks.
 
    void disconnectSinks();
 

	
 
protected:
 
    /// Feeds "in" given data to connected sinks
 
    virtual void feedOut(const SamplePack& data) const;
0 comments (0 inline, 0 general)