added BufferView

This commit is contained in:
2025-10-27 17:58:03 +01:00
parent 75c47de5c5
commit 9fedd040b3
2 changed files with 84 additions and 8 deletions
+65
View File
@@ -0,0 +1,65 @@
/*
* jayDSP, A cross-platform Digital Signal Processing library written in modern C++.
* Copyright (C) 2025 Jens Ahrensfeld, All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* File: dsp_ringbuffer.hpp
* Author Jens Ahrensfeld on 26.10.2025.
*/
#ifndef DSP_BUFFER_VIEW_H
#define DSP_BUFFER_VIEW_H
#include "buffer.hpp"
namespace dsp {
template <typename T>
class BufferView
{
Buffer<T> &m_buffer;
uint m_offset;
public:
BufferView(Buffer<T> &buffer, int offset)
: m_buffer(buffer)
, m_offset(offset)
{
}
T & operator[](int offset)
{
return m_buffer[m_offset + offset];
}
const T & operator[](int offset) const
{
return m_buffer[m_offset + offset];
}
size_t size()
{
return m_buffer.size();
}
};
}
#endif // DSP_BUFFER_VIEW_H
+19 -8
View File
@@ -1,16 +1,22 @@
#include <algorithm>
#include <delay/buffer.hpp> #include <delay/buffer.hpp>
#include <delay/buffer_view.hpp>
#include <cstdio> #include <cstdio>
#include <ranges>
#include <iostream> #include <iostream>
void print(std::ranges::input_range auto&& range) template<typename T>
void print(dsp::BufferView<T> &v, size_t size)
{ {
for(const auto&i : range) // for(const auto&i : v)
// {
// std::cout << i << std::endl;
// }
std::cout << "----------------------------------------------" << std::endl;
for (int i=0; i < size; i++)
{ {
std::cout << i << std::endl; std::cout << v[i] << std::endl;
} }
} }
int main() int main()
@@ -29,9 +35,14 @@ int main()
printf("i=%d: v=%d\n", i, v); printf("i=%d: v=%d\n", i, v);
} }
std::vector<int> numbers{1, 2, 3, 4, 5}; auto v1 = dsp::BufferView<int>(buffer, 0);
auto v{std::views::take(numbers, 3)}; auto v2 = dsp::BufferView<int>(buffer, 5);
print<>(v);
buffer.reset();
v1[5] = 55;
print(v1, 10);
v2[2] = 77;
print(v2,10);
return 0; return 0;
} }