# HG changeset patch # User Hasan Yavuz ÖZDERYA # Date 2017-12-09 14:59:50 # Node ID f8c253a6ab5f4588dfbf6cca221bfb3546a0cfcc # Parent 4d01ee44e4e516107f24d1a485249098267a943e added frame buffer abstract class definitions and index buffer for xdata diff --git a/src/framebuffer2.h b/src/framebuffer2.h new file mode 100644 --- /dev/null +++ b/src/framebuffer2.h @@ -0,0 +1,56 @@ +/* + Copyright © 2017 Hasan Yavuz Özderya + + This file is part of serialplot. + + serialplot 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. + + serialplot 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 serialplot. If not, see . +*/ + +// IMPORTANT NOTE: this file will be renamed to "framebuffer.h" when +// stream work is complete + +#ifndef FRAMEBUFFER_H +#define FRAMEBUFFER_H + +struct Range +{ + double min, max; +}; + +/// Abstract base class for all frame buffers. +class FrameBuffer +{ +public: + virtual unsigned size() const = 0; + virtual double sample(unsigned i) const = 0; + virtual Range limits() const = 0; +}; + +/// Common base class for index and writable frame buffers +class ResizableBuffer : public FrameBuffer +{ + /// Resize buffer + virtual void resize(unsigned n) = 0; +}; + +/// Abstract base class for writable frame buffers +class WFrameBuffer : public ResizableBuffer +{ + /// Add samples to the buffer + virtual void addSamples(double* samples, unsigned n) = 0; + /// Reset all data to 0 + virtual void clear() = 0; +}; + +#endif // FRAMEBUFFER_H diff --git a/src/indexbuffer.cpp b/src/indexbuffer.cpp new file mode 100644 --- /dev/null +++ b/src/indexbuffer.cpp @@ -0,0 +1,49 @@ +/* + Copyright © 2017 Hasan Yavuz Özderya + + This file is part of serialplot. + + serialplot 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. + + serialplot 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 serialplot. If not, see . +*/ + +#include + +#include "indexbuffer.h" + +IndexBuffer::IndexBuffer(unsigned n) +{ + _size = n; +} + +unsigned IndexBuffer::size() const +{ + return _size; +} + +void IndexBuffer::resize(unsigned n) +{ + _size = n; +} + +double IndexBuffer::sample(unsigned i) const +{ + Q_ASSERT(i < _size); + + return i; +} + +Range IndexBuffer::limits() const +{ + return Range{0, _size-1.}; +} diff --git a/src/indexbuffer.h b/src/indexbuffer.h new file mode 100644 --- /dev/null +++ b/src/indexbuffer.h @@ -0,0 +1,42 @@ +/* + Copyright © 2017 Hasan Yavuz Özderya + + This file is part of serialplot. + + serialplot 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. + + serialplot 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 serialplot. If not, see . +*/ + +#ifndef INDEXBUFFER_H +#define INDEXBUFFER_H + +// IMPORTANT TODO: rename to "framebuffer.h" when stream work is done. +#include "framebuffer2.h" + +/// A simple frame buffer that simply returns requested index as +/// value. +class IndexBuffer : public ResizableBuffer +{ +public: + IndexBuffer(unsigned n); + + unsigned size() const; + double sample(unsigned i) const; + Range limits() const; + void resize(unsigned n); + +private: + unsigned _size; +}; + +#endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -27,6 +27,7 @@ add_executable(Test EXCLUDE_FROM_ALL ../src/samplepack.cpp ../src/sink.cpp ../src/source.cpp + ../src/indexbuffer.cpp ) add_test(NAME test1 COMMAND Test) qt5_use_modules(Test Widgets) diff --git a/tests/test.cpp b/tests/test.cpp --- a/tests/test.cpp +++ b/tests/test.cpp @@ -22,6 +22,7 @@ #include "samplepack.h" #include "source.h" +#include "indexbuffer.h" TEST_CASE("samplepack with no X", "[memory]") { @@ -180,3 +181,24 @@ TEST_CASE("source", "[memory, stream]") source._feed(pack); REQUIRE(sink.totalFed == 100); } + +TEST_CASE("IndexBuffer", "[memory, buffer]") +{ + IndexBuffer buf(10); + + REQUIRE(buf.size() == 10); + for (unsigned i = 0; i < 10; i++) + { + REQUIRE(buf.sample(i) == i); + } + auto l = buf.limits(); + REQUIRE(l.min == 0); + REQUIRE(l.max == 9); + + buf.resize(20); + REQUIRE(buf.size() == 20); + REQUIRE(buf.sample(15) == 15); + l = buf.limits(); + REQUIRE(l.min == 0); + REQUIRE(l.max == 19); +}