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

Discussion in 'C++' started by jason.cipriani@gmail.com, Jun 19, 2008.

  1. Guest

    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
     
    , Jun 19, 2008
    #1
    1. Advertising

  2. Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Jun 19, 2008
    #2
    1. Advertising

  3. joseph cook Guest

    On Jun 18, 9:52 pm, ""
    <> 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
     
    joseph cook, Jun 19, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alvin777
    Replies:
    2
    Views:
    438
    John Harrison
    Feb 21, 2004
  2. Anonymous
    Replies:
    20
    Views:
    4,344
    Pete Becker
    Mar 30, 2005
  3. Greg Copeland

    access to preallocated block of memory?

    Greg Copeland, Dec 14, 2005, in forum: Python
    Replies:
    14
    Views:
    591
    Greg Copeland
    Dec 16, 2005
  4. Jason Heyes
    Replies:
    8
    Views:
    737
    Andrew Koenig
    Jan 15, 2006
  5. Rune Allnor
    Replies:
    4
    Views:
    961
    Rune Allnor
    Dec 11, 2008
Loading...

Share This Page