Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problems with Entity Manager using std::unique_ptr  (Read 2721 times)

0 Members and 1 Guest are viewing this topic.

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Problems with Entity Manager using std::unique_ptr
« on: August 22, 2012, 10:59:24 pm »
Ok, I'm trying to create an entity manager for my engine, and I'm encountering a problem that I can't seem to figure out. Here's my code for EntityManager.hpp:

#ifndef ENTITYMANAGER_H
#define ENTITYMANAGER_H

#include <memory>
#include <vector>

class Entity;

class EntityManager {
public:
    void createEntity(std::unique_ptr<Entity>);
    void destroyEntity(std::unique_ptr<Entity>);
    void destroyAllEntities() { entities.clear(); }

private:
    std::vector<std::unique_ptr<Entity> > entities;
};

#endif // ENTITYMANAGER_H

And it's corresponding cpp file:

#include <memory>
#include <vector>
#include <algorithm>
#include <iostream>

#include "EntityManager.hpp"
#include "Entity.hpp"

void EntityManager::createEntity(std::unique_ptr<Entity> entity) {
    entities.push_back(entity);
}

void EntityManager::destroyEntity(std::unique_ptr<Entity> entity) {
    bool entityFound = binary_search(entities.begin(), entities.end(), entity);

    if (entityFound) {
        auto iter = find(entities.begin(), entities.end(), entity);
        entities.erase(iter, entities.end());
    }

    else
        std::cout << "Could not find entity to destroy!" << std::endl;
}

When I try to compile this, it gives me an error in actual c++ files like unique_ptr.h, etc. I can't really post the errors because it's reaaaaally long but what I do know is that the problem is due to me trying to push a unique_ptr into my entities vector. I have also done the same thing with raw pointers to my Entity class (Entity*) and it worked fine, but I don't like dealing with raw pointers because they can get really annoying sometimes (destroying them correctly, etc) and it's just easier to use smart pointers. I hope you guys can help me and thank you for your time  :D
Current Projects:
Technoport

jesyspa

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Problems with Entity Manager using std::unique_ptr
« Reply #1 on: August 22, 2012, 11:47:28 pm »
Due to the way std::unique_ptr works it is not possible to pass it by value.  You probably want to either pass the args to createEntity and then create the entity inside that, or pass an std::unique_ptr by rvalue reference and then use std::move together with emplace_back to put it into the vector.

By the way, I'm not quite sure how you intend to binary search over an unordered container...

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Re: Problems with Entity Manager using std::unique_ptr
« Reply #2 on: August 23, 2012, 02:20:14 am »
Thanks, I passed by rvalue and it works perfectly! I learned something new today, never having used rvalues before :P
Current Projects:
Technoport

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Problems with Entity Manager using std::unique_ptr
« Reply #3 on: August 26, 2012, 06:00:11 pm »
You actually don't need a rvalue parameter like std::unique_ptr<Entity>&&. Use a normal value parameter, i.e. std::unique_ptr<Entity>. Pass the argument with std::move(), if you transfer ownership from a named variable.
void EntityManager::createEntity(std::unique_ptr<Entity> entity)
{
    entities.push_back(std::move(entity));
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything