2

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?

benf
  • 915
  • 1
  • 9
  • 28

1 Answers1

0

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.

Igor R.
  • 14,716
  • 2
  • 49
  • 83
  • That removes the call to new, but the `memcpy` still needs to be done separately. Also, isn't arrays in shared_ptr new in 1.53? IIRC, we're using Boost 1.33 – benf Apr 03 '13 at 14:50
  • @benf Yes, it's a new feature in boost1.53. As for `memcpy`, you can use `std::copy`, but it doesn't matter in this case. – Igor R. Apr 03 '13 at 14:58