# HG changeset patch # User Hasan Yavuz Ă–ZDERYA # Date 2017-12-19 10:13:34 # Node ID 46213689acd3a1869f9261e3d74d0ef86461c71f # Parent 90a919d06a7705a38d8c01391c7b7b3e6e9e67ba sink stores pointer to connected source diff --git a/src/sink.cpp b/src/sink.cpp --- a/src/sink.cpp +++ b/src/sink.cpp @@ -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; +} diff --git a/src/sink.h b/src/sink.h --- a/src/sink.h +++ b/src/sink.h @@ -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 followers; + const Source* source = NULL; ///< source that this sink is connected to }; #endif // SINK_H diff --git a/src/source.cpp b/src/source.cpp --- a/src/source.cpp +++ b/src/source.cpp @@ -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); } diff --git a/tests/test.cpp b/tests/test.cpp --- a/tests/test.cpp +++ b/tests/test.cpp @@ -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);