473,473 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 4107
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
18
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that...
5
by: Eric Lilja | last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope it's readable at least): #include <algorithm> #include <iostream> #include <vector> class Row { public:
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
56
by: Peter Olcott | last post by:
I am trying to refer to the same std::vector in a class by two different names, I tried a union, and I tried a reference, I can't seem to get the syntax right. Can anyone please help? Thanks
10
by: Jim Langston | last post by:
Someone is working on some code, and during the iteration of a vector may delete an element and push_back a new one. I was thinking that this may invalidate the iteration iterator, but in my test...
13
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.