469,950 Members | 1,662 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

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

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 charbuffer_;
...
};

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 charbuffer_;
...
};

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
Jun 27 '08 #1
3 3798
ja************@gmail.com wrote:
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 charbuffer_;
...
};

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 charbuffer_;
...
};

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
Jun 27 '08 #2
On Jun 18, 9:52*pm, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.comwrote:
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 charbuffer_;
* ...

};

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 charbuffer_;
* ...

};

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
Jun 27 '08 #3
"ja************@gmail.com" <ja************@gmail.comwrote:
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 charbuffer_;
...
};

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 charbuffer_;
...
};

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?
Write a wrapper...

template < typename T >
class Buffer {
T* buffer;
int size;
public:
Buffer(T* buf, int len);
// other functions as necessary to make this a drop-in replacement
// for a std::vector.
};

Better would probably be if attachExternalBuffer accepted a
vector<unsigned char>&...
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by Janina Kramer | last post: by
8 posts views Thread by Ross A. Finlayson | last post: by
32 posts views Thread by zl2k | last post: by
56 posts views Thread by Peter Olcott | last post: by
10 posts views Thread by Jim Langston | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.