Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Attaching a preallocated buffer to an existing std::vector.

Reply
Thread Tools

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

 
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      06-19-2008
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
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      06-19-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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 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
 
Reply With Quote
 
 
 
 
joseph cook
Guest
Posts: n/a
 
      06-19-2008
On Jun 18, 9:52*pm, "(E-Mail Removed)"
<(E-Mail Removed)> 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 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Existing Dll - using Functions from an existing dll Tristin.Colby@gmail.com Ruby 0 02-05-2008 07:38 PM
Why no existing Java type to existing XML schema binding support? nrm Java 3 04-10-2006 04:52 PM
access to preallocated block of memory? Greg Copeland Python 14 12-16-2005 06:53 PM
Attaching basic_string to preallocated buffer Alvin777 C++ 2 02-21-2004 11:11 AM
attaching & detaching buffer to standard string ? Attila Feher C++ 2 09-23-2003 01:43 PM



Advertisments