Example usage of uint8_t* buffer?
Net5F opened this issue · 5 comments
Hello, I'm having trouble figuring out how to use a raw uint8_t* buffer for serialization/deserialization. Are there any examples of how to do this?
This might be helpful. You basically need to specialize ContainerTraits
and BufferAdapterTraits
in bitsery/traits/core/traits.h
in order to use a type as buffer. T*
and T[N]
is already specialized, so it should work...
Hah, I must've been doing something weird cause it wasn't working, but it's actually really straightforward. It's working now, thanks!
Sorry, reopening this. I realized my issue is trying to use an OutputBufferAdapter<uint8_t*>
. In the example, a uint8_t* is used for the InputBufferAdapter
, but a fixed-size array type is used for the output. Is it possible to use a pointer, potentially passing in the buffer size separately? (I'm guessing it's used to detect a buffer overflow?)
Sorry for being that late to respond, but I guess you'll need to create your custom buffer type...
Minimal example could look like this
// your custom buffer type
struct MyBuffer {
uint8_t *buf;
size_t size;
// begin/end is required to get an iterator
uint8_t* begin() const
{
return buf;
}
uint8_t* end() const
{
return buf + size;
}
};
// required, by bitsery, so this type could be used as a proper container
template <>
struct bitsery::traits::ContainerTraits<MyBuffer> {
using TValue = uint8_t;
static constexpr bool isResizable = false;
static constexpr bool isContiguous = true;
static void resize(MyBuffer& , size_t ) {
}
static size_t size(const MyBuffer& buf) {
return buf.size;
}
};
// required to use container as buffer
template <>
struct bitsery::traits::BufferAdapterTraits<MyBuffer> {
using TIterator = uint8_t*;
using TConstIterator = const uint8_t*;
using TValue = uint8_t;
};
That worked perfectly, thanks again!