Changeset - 46213689acd3
[Not reviewed]
stream
0 4 0
Hasan Yavuz Ă–ZDERYA - 8 years ago 2017-12-19 10:13:34
hy@ozderya.net
sink stores pointer to connected source
4 files changed with 42 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/sink.cpp
Show inline comments
 
@@ -50,3 +50,14 @@ void Sink::setNumChannels(unsigned nc, b
 
        sink->setNumChannels(nc, x);
 
    }
 
}
 

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

	
 
const Source* Sink::connectedSource() const
 
{
 
    return source;
 
}
src/sink.h
Show inline comments
 
@@ -34,6 +34,8 @@ public:
 
    /// Disconnects a follower. Disconnecting an unconnected sink is
 
    /// an error.
 
    void disconnectFollower(Sink* sink);
 
    /// Returns the connected source. `NULL` if it's not connected.
 
    const Source* connectedSource() const;
 

	
 
protected:
 
    /// Entry point for incoming data. Re-implementations should
 
@@ -46,11 +48,18 @@ protected:
 
    virtual bool hasX() const = 0;
 
    /// Returns number of channels
 
    virtual unsigned numChannels() const = 0;
 
    /// Set by the connected source when its connected. When
 
    /// disconnecting it's set to `NULL`.
 
    ///
 
    /// @important Trying to connect a source while its already
 
    /// connected is an error.
 
    void setSource(const Source* s);
 

	
 
    friend Source;
 

	
 
private:
 
    QList<Sink*> followers;
 
    const Source* source = NULL;   ///< source that this sink is connected to
 
};
 

	
 
#endif // SINK_H
src/source.cpp
Show inline comments
 
@@ -24,15 +24,19 @@
 
void Source::connect(Sink* sink)
 
{
 
    Q_ASSERT(!sinks.contains(sink));
 
    Q_ASSERT(sink->connectedSource() == NULL);
 

	
 
    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(NULL);
 
    sinks.removeOne(sink);
 
}
 

	
tests/test.cpp
Show inline comments
 
@@ -123,6 +123,12 @@ TEST_CASE("sink", "[memory, stream]")
 
    REQUIRE(follower.hasX() == true);
 
}
 

	
 
TEST_CASE("sink must be created unconnected", "[memory, stream]")
 
{
 
    TestSink sink;
 
    REQUIRE(sink.connectedSource() == NULL);
 
}
 

	
 
class TestSource : public Source
 
{
 
public:
 
@@ -185,6 +191,18 @@ TEST_CASE("source", "[memory, stream]")
 
    REQUIRE(sink.totalFed == 100);
 
}
 

	
 
TEST_CASE("source must set/unset sink 'source'", "[memory, stream]")
 
{
 
    TestSink sink;
 
    TestSource source(3, false);
 

	
 
    source.connect(&sink);
 
    REQUIRE(sink.connectedSource() == &source);
 

	
 
    source.disconnect(&sink);
 
    REQUIRE(sink.connectedSource() == NULL);
 
}
 

	
 
TEST_CASE("IndexBuffer", "[memory, buffer]")
 
{
 
    IndexBuffer buf(10);
0 comments (0 inline, 0 general)