I have a stream object which provide GetBuffer()
and GetBufferSize()
methods. The GetBuffer
method returns a raw uint8_t
pointer. I want to pass (by value) this buffer to another object which expects a shared_array<uint8_t>
. I'm using boost, (which I am fairly new to), and this is what I came up with.
// relevant protos for a and b
void BClass::SetData(shared_array<uint8_t> data, size_t data_len);
uint8_t* AClass::GetBuffer(void);
size_t AClass::GetBufferSize(void);
AClass a;
BClass b;
shared_array<uint8_t> data = shared_array<uint8_t>(new uint8_t[a.GetBufferSize()]);
memcpy(data.get(), a.GetBuffer(), a.GetBufferSize());
b.SetData(data, a.GetBufferSize());
It feels like there should be something similar to boost's make_shared that could clean this up. Am I missing something obvious?
In Boost 1.53
we've got make_shared
for arrays. So your code could look like this:
shared_ptr<uint8_t[]> data = make_shared<uint8_t[]>(a.GetBufferSize());
Of course, then you'd need to change BClass::SetData
signature, to accept shared_ptr
instead of shared_array
.