Register    Login    Forum    Search    FAQ

Board index » Everything




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Question
 Post Posted: December 7th, 2017, 1:45 am 
 

Joined: November 30th, 2013, 7:40 pm
Posts: 656
Location: Merville, British Columbia Canada
I think i know the relationship between smart pointers and scope. If a container (vector) holds a smart pointer then it is responsible for freeing it ? .. correct ? as in this example.
Code:
void EntityManager::Refresh()
{
   
   for (auto i(0u); i < maxGroups; i++)
   {
      auto& v(groups[i]);
      v.erase(std::remove_if(std::begin(v),std::end(v),
         [i](Entity* ent)
      {
         return !ent->Active() || !ent->HasGroup(i);
      }
      ), std::end(v));
   }
   
   entities.erase(std::remove_if(std::begin(entities), std::end(entities),
      [](const std::unique_ptr<Entity>& inEnt)
   {
      return !inEnt->Active();
   }), std::end(entities));
}

_________________
Curiosity killed the cat, satisfaction brought him back


Top 
 Profile  
Reply with quote  
 Post subject: Re: Question
 Post Posted: December 7th, 2017, 2:04 am 
 

Joined: November 30th, 2013, 7:40 pm
Posts: 656
Location: Merville, British Columbia Canada
I love my managers lol
groups hold entities with a particular component
ent->Add<Collider>();// add a collider component to the entity
ent->AddGroup(groupCollider);// add the entity to the group that holds colliders
i made the entity class have a static reference to EntityManager ie:: Entity::SetManager(m_EntityMgr.get()); now all entities have a reference err, pointer to the entity manager so ent->AddGroup(groupCollider) adds the entity to the group which the manager holds

_________________
Curiosity killed the cat, satisfaction brought him back


Top 
 Profile  
Reply with quote  
 Post subject: Re: Question
 Post Posted: December 8th, 2017, 12:23 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2970
Location: Oklahoma, United States
Yes, if you have a vector<unique_ptr<Entity>> and you erease an element from the vector, the unique_ptr at that index will have it's destructor called and the memory is freed for you, you do not need to call release or reset() for each removed element. Basically, what you have is fine.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 3 posts ] 

Board index » Everything


 
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 post attachments in this forum

Search for: