String Access

H

Hakan

I have an API call I need to pass a void* to to be filled by the API call --
since I want to return a std::string from the function I wrapped around the
API call I was hoping I could pass the std::string, one way or another, to
the API call. Well, neither operator[], nor, of course, data() are useable
for this. Perusing the documentation in Josutti's excellent book I find no
candidate function or operator -- is that correct or have I missed something?

If none exist I have to work around it by getting the length of the buffer
area needed, allocate a character array of that size, read the data,
construct a std::string from the buffer area and finally deallocate the
character array...

TIA.
 
G

Gianni Mariani

Hakan said:
I have an API call I need to pass a void* to to be filled by the API call --
since I want to return a std::string from the function I wrapped around the
API call I was hoping I could pass the std::string, one way or another, to
the API call. Well, neither operator[], nor, of course, data() are useable
for this. Perusing the documentation in Josutti's excellent book I find no
candidate function or operator -- is that correct or have I missed something?

If none exist I have to work around it by getting the length of the buffer
area needed, allocate a character array of that size, read the data,
construct a std::string from the buffer area and finally deallocate the
character array...


You give us little to go on.

Here is one idea.


#include <string>
#include <cassert>

void BustedApi( void *, int * length );

int const MAXIMUM_SIZE = 1024;

void MyWrapper( std::string & fill_me_in )
{

fill_me_in.resize( MAXIMUM_SIZE );

int length;

BustedApi( static_cast<void *>( &(fill_me_in[0]) ), & length );

assert( length <= MAXIMUM_SIZE );

fill_me_in.resize( length );

}
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Hakan escribió:
If none exist I have to work around it by getting the length of the buffer
area needed, allocate a character array of that size, read the data,
construct a std::string from the buffer area and finally deallocate the
character array...

None exist, the standard does not guarantee that the string are stored
in a continuous block of memory.

You can use a vector of char instead of manually allocated memory.
Current implementations use a block of memory, and a revision of the
standard will make mandatory this.

Regards.
 
H

Hakan

Thanks, worked great!

Hakan said:
I have an API call I need to pass a void* to to be filled by the API call --
since I want to return a std::string from the function I wrapped around the
API call I was hoping I could pass the std::string, one way or another, to
the API call. Well, neither operator[], nor, of course, data() are useable
for this. Perusing the documentation in Josutti's excellent book I find no
candidate function or operator -- is that correct or have I missed something?

If none exist I have to work around it by getting the length of the buffer
area needed, allocate a character array of that size, read the data,
construct a std::string from the buffer area and finally deallocate the
character array...


You give us little to go on.

Here is one idea.


#include <string>
#include <cassert>

void BustedApi( void *, int * length );

int const MAXIMUM_SIZE = 1024;

void MyWrapper( std::string & fill_me_in )
{

fill_me_in.resize( MAXIMUM_SIZE );

int length;

BustedApi( static_cast<void *>( &(fill_me_in[0]) ), & length );

assert( length <= MAXIMUM_SIZE );

fill_me_in.resize( length );

}
 
G

Gianni Mariani

Hakan said:
Thanks, worked great!

Hakan said:
I have an API call I need to pass a void* to to be filled by the API call --
since I want to return a std::string from the function I wrapped around the
API call I was hoping I could pass the std::string, one way or another, to
the API call. Well, neither operator[], nor, of course, data() are useable
for this. Perusing the documentation in Josutti's excellent book I find no
candidate function or operator -- is that correct or have I missed something?

If none exist I have to work around it by getting the length of the buffer
area needed, allocate a character array of that size, read the data,
construct a std::string from the buffer area and finally deallocate the
character array...


You give us little to go on.

Here is one idea.


#include <string>
#include <cassert>

void BustedApi( void *, int * length );

int const MAXIMUM_SIZE = 1024;

void MyWrapper( std::string & fill_me_in )
{

fill_me_in.resize( MAXIMUM_SIZE );

int length;

BustedApi( static_cast<void *>( &(fill_me_in[0]) ), & length );

assert( length <= MAXIMUM_SIZE );

fill_me_in.resize( length );

}

I believe that you need to read Julian Albo's post. I thought that the
upcoming "contiguous" requirement applied to strings as well as vectors.

In other words, this may not be portable across all compliant string
implementations - I have yet to meet one that this does not work for but
you'll find out soon enough.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top