I don't think that putting the structure in the UserData map will work. The vector objects contain only a size (integer) and a pointer to where the array of values are located in the heap. So, if you want to save everything in the UserData, you would have to create a buffer large enough to fit both the size and all the values. Then, you'll have to manually copy the values from the vector to the buffer, and put the buffer itself in the UserData.

Take this with a grain of salt though, I haven't actually used UserData maps before.

Hope this helps,
-Mihail

On 10.6.2013 ?. 11:33 ?., Ahmidou Lyazidi 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]>>

    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]>] On Behalf Of
    Guillaume Laforge
    Sent: Sunday, June 09, 2013 3:29 PM
    To: [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]>>> 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]>>>
    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]>>> 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





Reply via email to