Attaching a preallocated buffer to an existing std::vector.

J

jason.cipriani

I'm in a rather strange situation. I have a class that does some stuff
and uses a buffer to hold some data in; the details aren't important
but the buffer is an std::vector:

class Something {
...
private:
std::vector<unsigned char> buffer_;
...
};

The class is rather complex and has a lot of code that relies on
buffer_ being an std::vector. Now I'm in a position where I have to
add the ability for a client to use some external buffer. However, I
want to modify the minimum amount of code possible. So I want an
interface like this:

class Something {
...
void attachExternalBuffer (unsigned char *buf);
private:
std::vector<unsigned char> buffer_;
...
};

And I'd like to be able to use the supplied external buffer as the
"backing" for the buffer_ vector. I know for a fact that buffer_ is
never resize()'d so I don't have to worry about dealing with that. I
just want to be able to have the existing code in the class remain
unchanged, but modify the client-supplied buffer through "buffer_". Is
there some way I can make this work?

Thanks,
Jason
 
K

Kai-Uwe Bux

I'm in a rather strange situation. I have a class that does some stuff
and uses a buffer to hold some data in; the details aren't important
but the buffer is an std::vector:

class Something {
...
private:
std::vector<unsigned char> buffer_;
...
};

The class is rather complex and has a lot of code that relies on
buffer_ being an std::vector. Now I'm in a position where I have to
add the ability for a client to use some external buffer. However, I
want to modify the minimum amount of code possible. So I want an
interface like this:

class Something {
...
void attachExternalBuffer (unsigned char *buf);
private:
std::vector<unsigned char> buffer_;
...
};

And I'd like to be able to use the supplied external buffer as the
"backing" for the buffer_ vector. I know for a fact that buffer_ is
never resize()'d so I don't have to worry about dealing with that. I
just want to be able to have the existing code in the class remain
unchanged, but modify the client-supplied buffer through "buffer_". Is
there some way I can make this work?

You can use a custom allocator with the vector and pass an allocator object
when the vector is constructed. There is no way to change the region in
memory used by the vector at a later time. Thus, if attachExternalBuffer()
should be used after construction of buffer_, you would have to create a
new vector with appropriate contents and then you can swap it with buffer_.

BTW: it seems that you are headed down the wrong road design-wise.


Best

Kai-Uwe Bux
 
J

joseph cook

I'm in a rather strange situation. I have a class that does some stuff
and uses a buffer to hold some data in; the details aren't important
but the buffer is an std::vector:

class Something {
  ...
private:
  std::vector<unsigned char> buffer_;
  ...

};

The class is rather complex and has a lot of code that relies on
buffer_ being an std::vector. Now I'm in a position where I have to
add the ability for a client to use some external buffer. However, I
want to modify the minimum amount of code possible. So I want an
interface like this:

class Something {
  ...
  void attachExternalBuffer (unsigned char *buf);
private:
  std::vector<unsigned char> buffer_;
  ...

};

And I'd like to be able to use the supplied external buffer as the
"backing" for the buffer_ vector. I know for a fact that buffer_ is
never resize()'d so I don't have to worry about dealing with that. I
just want to be able to have the existing code in the class remain
unchanged, but modify the client-supplied buffer through "buffer_". Is
there some way I can make this work?

Minimum amount of code? One line:
std::copy(buf, buf+size,back_inserter(buffer_));

Of course, this leads to two copies of the data laying around, and the
time overhead of a copy...

Joe Cook
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top