Archived

This forum has been archived. Please start a new discussion on GitHub.

Questions about sequence<byte> in C++?

Hi,

In CORBA C++, I'm used to dealing with sequence<octet>.
This maps to a CORBA::Octet buffer (which basically is a char buffer),
which you can manipulate quite
easily with get_buffer() and replace().

In Ice, a sequence<byte> maps to a vector<Ice::Byte>.

Is there a way to do the CORBA C++ equivalent of get_buffer() and
replace()?

To try thiings out in Ice, I made an interface::

sequence<byte> FrameData;

interface FrameOps
{

void sendFrame(FrameData f);
};

My client code looks like (eliminating all the Ice boilerplate to
get an object reference to a FrameOps object:

std::ifstream f;
f.open(argv[1], std::ios_base::binary);

if( f.fail() ) {
std::cout << "Failed" << std::endl;
return -1;
}

f.seekg(0, std::ios_base::end);
long len = f.tellg();
std::cout << "Pos: " << len << std::endl;
f.seekg(0, std::ios_base::beg);

Ice::Byte *buf = new Ice::Byte[len];
f.read(buf, len);

FrameData v(&buf[0], &buf[len]);
frame_ops->sendFrame(v);


Using this technique, I have to create
a separate Ice::Byte buffer, and then
copy that into a vector<Ice::Byte>
before calling the remote method.

Is there a more optimal way for me to do this, or is this the best way?

Thanks.

Comments

  • Re: Questions about sequence<byte> in C++?
    Originally posted by rodrigc
    Hi,

    Using this technique, I have to create
    a separate Ice::Byte buffer, and then
    copy that into a vector<Ice::Byte>
    before calling the remote method.

    Is there a more optimal way for me to do this, or is this the best way?

    Thanks.

    The latest version of the C++ standard guarantees that, for an STL vector, the elements are kept in contiguous memory. (Even before that standards update, all extant implementations maintained that guarantee.) So, you can just use the address of the first element of a vector as a buffer pointer and go for your life.

    Cheers,

    Michi.
  • Hi,

    OK, I simplified myt code and it works great!
    I changed it to:

    ==========================================================
    std::ifstream f;
    f.open(argv[1], std::ios_base::binary);

    if( f.fail() ) {
    std::cout << "Failed" << std::endl;
    return -1;
    }

    f.seekg(0, std::ios_base::end);
    long len = f.tellg();

    f.seekg(0, std::ios_base::beg);

    FrameData v(len);
    f.read(&v[0], len);
    frame_manip->sendFrame(v);

    frame_ops->sendFrame(v);

    ==========================================================

    Thanks!
  • Originally posted by rodrigc
    FrameData v(len);
    f.read(&v[0], len);
    frame_manip->sendFrame(v);
    
    frame_ops->sendFrame(v);
    

    Yep, that's it. Nice, isn't it? A sequence abstraction that guarantees contiguous memory is definitely nice for things such as binary data. No more mucking around with get_buffer() and set_buffer() and such :)

    Cheers,

    Michi.