 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
lotusny78@yahoo.com Guest
|
Posted: Wed Apr 27, 2005 8:49 am Post subject: vectors and legacy APIs: what next |
|
|
After you pass a vector to a legacy function, how do you use it if
other legacy functions expect a different type?
A Win32 example:
//The first call to DocumentProperties tells us the required
//buffer size in bytes
long bufferSizeNeeded =
DocumentProperties(0, hPrinter, printerName, 0, 0, 0);
vector<BYTE> resultDevMode(bufferSizeNeeded);
//Is this line OK??? Otherwise, how do we pass it back to API
PDEVMODE pResult = reinterpret_cast<PDEVMODE>(&resultDevMode[0]);
//DocumentProperties expects a PDEVMODE, but we used a vector
if(IDOK != DocumentProperties(0, hPrinter, printerName,
pResult, 0, DM_OUT_BUFFER))
throw(runtime_error("Can't get document properties"));
Thanks.
- Sean
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nickolay Merkin Guest
|
Posted: Thu Apr 28, 2005 12:32 pm Post subject: Re: vectors and legacy APIs: what next |
|
|
Just an idea (I never used it):
template<class T, class U>
T* reinterpret_bytes( vector<U>& vec )
{
assert( vec.size() * sizeof(U) >= sizeof(T) );
return (T*) &vec[0];
}
template<class T, class U>
const T* reinterpret_cbytes( const vector<U>& vec )
{
assert( vec.size() * sizeof(U) >= sizeof(T) );
return (const T*) &vec[0];
}
///////////////////////////////
PDEVMODE pResult = reinterpret_bytes<PDEVMODE>(resultDevMode);
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
unspammable@gmail.com Guest
|
Posted: Fri Apr 29, 2005 1:39 am Post subject: Re: vectors and legacy APIs: what next |
|
|
The line appears to be Ok. But if the only reaon you're using a vector
is that it manages its own memory you can use a smart pointer (like
auto_ptr) instead.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Sat Apr 30, 2005 3:58 pm Post subject: Re: vectors and legacy APIs: what next |
|
|
[email]unspammable (AT) gmail (DOT) com[/email] <unspammable (AT) gmail (DOT) com> wrote:
| Quote: | The line appears to be Ok. But if the only reaon you're using a vector
is that it manages its own memory you can use a smart pointer (like
auto_ptr) instead.
void test() |
{
auto_ptr does not suport arrays example this is won't work
std::auto_ptr<char> foo(new char[256]);
std::strcpy(foo.get(),"Hello World");
// ouch dtr of auto_ptr usse delete not delete [].
}
struct array_deleter
{
template <class T> void operator () (T *x) {delete [] x;}
};
boost::shared_ptr<T> p(new T[256],array_deleter());
void get_data(p.get());
is better solution but it too has about two longs and a deleter
overhead so it is easier to use std::vector and be done with it...
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Seungbeom Kim Guest
|
Posted: Sun May 01, 2005 11:05 am Post subject: Re: vectors and legacy APIs: what next |
|
|
Carl Barron wrote:
| Quote: |
void test()
{
auto_ptr does not suport arrays example this is won't work
std::auto_ptr<char> foo(new char[256]);
std::strcpy(foo.get(),"Hello World");
// ouch dtr of auto_ptr usse delete not delete [].
}
struct array_deleter
{
template <class T> void operator () (T *x) {delete [] x;}
};
boost::shared_ptr<T> p(new T[256],array_deleter());
void get_data(p.get());
is better solution but it too has about two longs and a deleter
overhead so it is easier to use std::vector and be done with it...
|
Why not use boost::shared_array which does that for you?
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|