I'd recommend going with Guillaume's suggestion with a bit of modifications. Currently it won't work because the vectors would be freed when "Foo Test" gets out of scope because of automatic destruction. Making the allocation and de-allocation of the vector manual should do the trick:
struct Foo{
std::vector<double> *A;
std::vector<double> *B;
bool C;
bool D;
void init()
{
A = new std::vector<double>;
B = new std::vector<double>;
}
void destroy()
{
delete A;
delete B;
}
};
...
Foo Test;
//Allocate the vectors
Test.init();
//Set some values
Test.A->pushback(12);
Test.B->pushback(24);
Test.B->pushback(32);
Test.C->pushback(true);
Test.D->pushback(false);
myMap.PutItemValue( i, (unsigned char*)&Test, sizeof( Foo )) ;
// Get them back
const unsigned char* pInternalData = NULL ;
UINT cntData = 0 ;
myMap.GetItemValue( 0, pInternalData, cntData ) ;
Foo *pData = (Foo*) pInternalData ;
if(pData)
{
Application().LogMessage("nb "+CString(pData->A->size()));
}
// Free the vectors
pData->destroy();
As a rule of thumb, be careful when putting structs with non-trivial copy
constructors (such as stl containers) into UserDataMap as it treats your
UserData as raw memory and thus won't follow C++ copy semantics.
From: [email protected]
[mailto:[email protected]] On Behalf Of Ahmidou Lyazidi
Sent: Wednesday, June 12, 2013 4:41 PM
To: [email protected]
Subject: Re: [C++] Store a structure of vector in a UserData
Thanks for the example, I'll try tonight!
I also started to mockup a simple version, and surprise, it's working......
I'll keep you informed if I find where the problem is.
Cheers
-----------------------------------------------
Ahmidou Lyazidi
Director | TD | CG artist
http://vimeo.com/ahmidou/videos
http://www.cappuccino-films.com
2013/6/10 Marc-Andre Belzile
<[email protected]<mailto:[email protected]>>
1) Assigning buffer to a std::vector (copy data)
Std::vector v(buffer,buffersize);
2) std::vector wrapper (untested)
struct Wrapper : public std::vector
{
Wrapper( double* buffer,size_t size )
{
// initialize stl internals with buffer
this->_M_impl _M_start = buffer;
this->_M_impl _M_finish = this->_M_impl _M_end_of_storage = buffer + size;
}
~Wrapper()
{
// nulls out internals to avoid deallocation from stl
this->_M_impl _M_start = this->_M_impl _M_finish =
this->_M_impl _M_end_of_storage = NULL;
}
};
I think boost have already a similar class, but boost is a big hammer for such
a tiny nail!
-mab
From:
[email protected]<mailto:[email protected]>
[mailto:[email protected]<mailto:[email protected]>]
On Behalf Of Guillaume Laforge
Sent: Monday, June 10, 2013 8:53 AM
To: [email protected]<mailto:[email protected]>
Subject: Re: [C++] Store a structure of vector in a UserData
Hi Ahmidou,
I'm often storing just a pointer to an array/buffer/vector to pass any struct
to a User Data and it is working fine.
It is hard to help you without seeing more chunks of your code :).
Maybe you could try to repro the issue on a simpler code that you could share
here ?
Cheers,
Guillaume
On Mon, Jun 10, 2013 at 4:33 AM, Ahmidou Lyazidi
<[email protected]<mailto:[email protected]><mailto:[email protected]<mailto:[email protected]>>>
wrote:
Thanks everyone, unfortunatly I'm still stuck.....
@ Stephane, this is what I'm doing, but it's not working:
sizeof( test.A ) + sizeof( double ) * test.A.size() + sizeof( test.B ) +
sizeof( double ) * test.B.size() + sizeof( bool ) * 2
@ Guillame
Thanks, I tryed your example but I'm probably doing it the wrong way:
Foo Test;
//Set some values
Test.A->pushback(12);
Test.B->pushback(24);
Test.B->pushback(32);
Test.C->pushback(true);
Test.D->pushback(false);
myMap.PutItemValue( i, (unsigned char*)&Test, sizeof( Foo )) ;
// Get them back
const unsigned char* pInternalData = NULL ;
UINT cntData = 0 ;
myMap.GetItemValue( 0, pInternalData, cntData ) ;
Foo *pData = (Foo*) pInternalData ;
if(pData)
{
Application().LogMessage("nb "+CString(pData->A->size()));
}
this return empty vector....
@MAB
Could you point me to some ressources on the web about this?
Thanks!!
-----------------------------------------------
Ahmidou Lyazidi
Director | TD | CG artist
http://vimeo.com/ahmidou/videos
http://www.cappuccino-films.com
2013/6/10 Marc-Andre Belzile
<[email protected]<mailto:[email protected]><mailto:[email protected]<mailto:[email protected]>>>
Alternatively, you could store C++ buffers in your struct instead of
std::vector objects. Then if you need to access your data with stl, just assign
each buffer to an std::vector out from these buffers.
If you can't afford the extra copy performed by std::vector constructor, you'll
need to implement your own wrapper class deriving from std::vector that nulls
out the internal container upon destruction.
This is required to avoid std::vector to deallocate your buffer memory.
Of course I haven't tested this solution yet but it should work. :)
-mab
From:
[email protected]<mailto:[email protected]><mailto:[email protected]<mailto:[email protected]>>
[mailto:[email protected]<mailto:[email protected]><mailto:[email protected]<mailto:[email protected]>>]
On Behalf Of Guillaume Laforge
Sent: Sunday, June 09, 2013 3:29 PM
To:
[email protected]<mailto:[email protected]><mailto:[email protected]<mailto:[email protected]>>
Subject: Re: [C++] Store a structure of vector in a UserData
Well, if you need the exact number of bytes, you will need to take into account
the size of std::vector objects I think :).
On Sun, Jun 9, 2013 at 10:07 AM, Stephan Woermann
<[email protected]<mailto:[email protected]><mailto:[email protected]<mailto:[email protected]>><mailto:[email protected]<mailto:[email protected]><mailto:[email protected]<mailto:[email protected]>>>>
wrote:
To get the total size of the struct, this should work:
Foo test;
sizeof( double ) * test.A.size() + sizeof( double ) * test.B.size() + sizeof(
bool ) * 2
Stephan
2013/6/9 Guillaume Laforge
<[email protected]<mailto:[email protected]><mailto:[email protected]<mailto:[email protected]>><mailto:[email protected]<mailto:[email protected]><mailto:[email protected]<mailto:[email protected]>>>>
Hi Ahmidou :),
You could try to use pointers to std::vector. This way you will be able to
access those vector and get the double values correctly.
But you must handle the allocation/deallocation of those vectors by yourself:
struct Foo{
std::vector<double> *A;
std::vector<double> *B;
bool C;
bool D;
Foo()
{
A = new std::vector<double>;
B = new std::vector<double>;
}
~Foo()
{
delete A;
delete B;
}
};
Hope this help,
Guillaume
On Sun, Jun 9, 2013 at 8:07 AM, Ahmidou Lyazidi
<[email protected]<mailto:[email protected]><mailto:[email protected]<mailto:[email protected]>><mailto:[email protected]<mailto:[email protected]><mailto:[email protected]<mailto:[email protected]>>>>
wrote:
Hi List,
Is it possible to store this kind of struct in a UserData (map or blob):
struct Foo{
std::vector<double> A;
std::vector<double> B;
bool C;
bool D;
};
I can pull out the structure, and the vectors have the good number of
item...but they are empty, the values are gone
I'm not sure, but I think it's lost because of the size parameter in
UserDataMap.PutItemValue
I tried to set the real size ( sizeof(vector)+ sizeof(double)*vector::size() )
but this gave me some crazy results.
Any idea?
Thanks
-----------------------------------------------
Ahmidou Lyazidi
Director | TD | CG artist
http://vimeo.com/ahmidou/videos
http://www.cappuccino-films.com
<<attachment: winmail.dat>>

