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
 
@@ -345,18 +345,12 @@ void MainWindow::onPortToggled(bool open
 
    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()
 
{
src/sink.cpp
Show inline comments
 
@@ -50,13 +50,13 @@ void Sink::setNumChannels(unsigned nc, b
 
    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
src/sink.h
Show inline comments
 
@@ -50,22 +50,24 @@ protected:
 

	
 
    /// 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
 
@@ -29,13 +29,18 @@ Source::~Source()
 
    }
 
}
 

	
 
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());
 
}
 

	
src/source.h
Show inline comments
 
@@ -34,14 +34,15 @@ public:
 
    /// 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);
 

	
0 comments (0 inline, 0 general)