Skip to content

Commit 315577b

Browse files
committed
Add transparent access for internal buffer in LocalBuffer
1 parent d3312c6 commit 315577b

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

include/tscpp/util/LocalBuffer.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ template <class T = uint8_t, std::size_t EstSizeBound = 1024> class LocalBuffer
4242
T *data() const;
4343
std::size_t size() const;
4444

45+
T &operator[](std::size_t index);
46+
const T &operator[](std::size_t index) const;
47+
48+
operator T *();
49+
4550
private:
4651
T _buf[EstSizeBound];
4752
T *const _ptr;
@@ -69,4 +74,19 @@ LocalBuffer<T, S>::size() const
6974
return _size;
7075
}
7176

77+
template <class T, std::size_t S> inline T &LocalBuffer<T, S>::operator[](std::size_t index)
78+
{
79+
return data()[index];
80+
}
81+
82+
template <class T, std::size_t S> inline const T &LocalBuffer<T, S>::operator[](std::size_t index) const
83+
{
84+
return data()[index];
85+
}
86+
87+
template <class T, std::size_t S> inline LocalBuffer<T, S>::operator T *()
88+
{
89+
return data();
90+
}
91+
7292
} // namespace ts

src/tscpp/util/unit_tests/test_LocalBuffer.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,24 @@ TEST_CASE("LocalBuffer", "[libts][LocalBuffer]")
108108
CHECK(local_buffer.size() == 4096);
109109
}
110110
}
111+
112+
SECTION("Transparent access")
113+
{
114+
const size_t len = 1024;
115+
ts::LocalBuffer local_buffer1(len);
116+
ts::LocalBuffer local_buffer2(len);
117+
118+
memset(local_buffer1, 0xAA, len);
119+
local_buffer1[len - 1] = 0xBB;
120+
121+
CHECK(local_buffer1[0] == 0xAA);
122+
CHECK(local_buffer1[len - 1] == 0xBB);
123+
CHECK(local_buffer1.size() == 1024);
124+
125+
memcpy(local_buffer2, local_buffer1, local_buffer2.size());
126+
127+
CHECK(local_buffer2[0] == 0xAA);
128+
CHECK(local_buffer2[len - 1] == 0xBB);
129+
CHECK(local_buffer2.size() == 1024);
130+
}
111131
}

0 commit comments

Comments
 (0)