diff --git a/CMakeLists.txt b/CMakeLists.txt index d664df3..ca6c214 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,7 @@ list(APPEND inspectrum_sources plot.cpp plotview.cpp samplebuffer.cpp + samplesource.cpp spectrogramcontrols.cpp spectrogramplot.cpp traceplot.cpp diff --git a/samplebuffer.cpp b/samplebuffer.cpp index a34a2f8..b4c6f65 100644 --- a/samplebuffer.cpp +++ b/samplebuffer.cpp @@ -19,6 +19,12 @@ #include "samplebuffer.h" +template +SampleBuffer::SampleBuffer(SampleSource *src) : src(src) +{ + src->subscribe(this); +} + template std::unique_ptr SampleBuffer::getSamples(off_t start, off_t length) { @@ -28,5 +34,11 @@ std::unique_ptr SampleBuffer::getSamples(off_t start, off_t l return dest; } +template +void SampleBuffer::invalidEvent() +{ + SampleSource::invalidate(); +} + template class SampleBuffer, std::complex>; template class SampleBuffer, float>; diff --git a/samplebuffer.h b/samplebuffer.h index 93fc135..511d14a 100644 --- a/samplebuffer.h +++ b/samplebuffer.h @@ -24,13 +24,14 @@ #include "samplesource.h" template -class SampleBuffer : public SampleSource +class SampleBuffer : public SampleSource, public Subscriber { private: SampleSource *src; public: - SampleBuffer(SampleSource *src) : src(src) {}; + SampleBuffer(SampleSource *src); + void invalidEvent(); virtual std::unique_ptr getSamples(off_t start, off_t length); virtual void work(void *input, void *output, int count) = 0; virtual off_t count() { diff --git a/samplesource.cpp b/samplesource.cpp new file mode 100644 index 0000000..123a203 --- /dev/null +++ b/samplesource.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2015, Mike Walters + * + * This file is part of inspectrum. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "samplesource.h" + +template +void SampleSource::subscribe(Subscriber *subscriber) +{ + subscribers.insert(subscriber); +} + +template +void SampleSource::invalidate() +{ + for (auto subscriber : subscribers) { + subscriber->invalidateEvent(); + } +} + +template +void SampleSource::unsubscribe(Subscriber *subscriber) +{ + subscribers.erase(subscriber); +} + +template class SampleSource>; +template class SampleSource; diff --git a/samplesource.h b/samplesource.h index 22d5bbd..7035218 100644 --- a/samplesource.h +++ b/samplesource.h @@ -21,7 +21,9 @@ #include #include +#include #include "abstractsamplesource.h" +#include "subscriber.h" template class SampleSource : public AbstractSampleSource @@ -31,5 +33,14 @@ public: virtual ~SampleSource() {}; virtual std::unique_ptr getSamples(off_t start, off_t length) = 0; + virtual void invalidateEvent() { }; virtual off_t count() = 0; + void subscribe(Subscriber *subscriber); + void unsubscribe(Subscriber *subscriber); + +protected: + virtual void invalidate(); + +private: + std::set subscribers; }; diff --git a/subscriber.h b/subscriber.h new file mode 100644 index 0000000..bbf4c67 --- /dev/null +++ b/subscriber.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2016, Mike Walters + * + * This file is part of inspectrum. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + + +class Subscriber +{ +public: + virtual void invalidateEvent() = 0; +};