Storing a weak pointer in a Newton object

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Storing a weak pointer in a Newton object

Postby Leadwerks » Sun Oct 21, 2018 7:34 pm

In my new engine I am using smart pointers everywhere and doing a lot of multithreading. This time around, Newton is going to get its own thread to run on. :)

One issue I have is how to store a weak pointer in a Newton object? Ideally I want something like this:
Code: Select all
void NewtonApplyForceAndTorque(const NewtonBody* body, dFloat timestep, int threadIndex)
{
    void* data = NewtonBodyGetUserData(body);
    if (data != nullptr)
    {
        auto ptr = ((weak_ptr<PhysicsNode>)*)data;
        auto node = ptr->lock();
        if (node != nullptr)
        {
            // Do some stuff here...
        }
    }
}


Of course I do not want to use new to create a weak pointer! I guess I could create a global map but that seems bad to me:
Code: Select all
std::map<NewtonBody*, weak_ptr<PhysicsNode> > physicsnodes;

void NewtonApplyForceAndTorque(const NewtonBody* body, dFloat timestep, int threadIndex)
{
    auto node = physicsnodes[body].lock();
    if (node != nullptr)
    {
        // Do some stuff here...
    }
}


Any suggestions on how to handle this?
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm

Re: Storing a weak pointer in a Newton object

Postby Leadwerks » Sun Oct 21, 2018 8:08 pm

Actually, I think my physics thread-side objects should be created as pointers and only deleted when the threads sync. So nevermind.
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 13 guests

cron