| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Hakan" |
| Date: |
23 Nov 2003 12:20:21 AM |
| Object: |
String Access |
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.
.
|
|
| User: "=?iso-8859-1?Q?Juli=E1n?= Albo" |
|
| Title: Re: String Access |
23 Nov 2003 04:41:05 AM |
|
|
Hakan escribi=F3:
If none exist I have to work around it by getting the length of the buf=
fer
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.
.
|
|
|
|
| User: "Gianni Mariani" |
|
| Title: Re: String Access |
23 Nov 2003 12:54:37 AM |
|
|
Hakan wrote:
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 );
}
.
|
|
|
| User: "Hakan" |
|
| Title: Re: String Access |
23 Nov 2003 10:07:13 PM |
|
|
Thanks, worked great!
On 23 Nov 2003 06:54:37 GMT, Gianni Mariani wrote:
Hakan wrote:
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 );
}
.
|
|
|
| User: "Gianni Mariani" |
|
| Title: Re: String Access |
24 Nov 2003 01:31:22 AM |
|
|
Hakan wrote:
Thanks, worked great!
On 23 Nov 2003 06:54:37 GMT, Gianni Mariani wrote:
Hakan wrote:
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.
.
|
|
|
|
|
|

|
Related Articles |
|
|