Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use shared_ptr in &Engine::AddWorld() fn #30

Merged
merged 6 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions tpe/lib/src/Engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ Engine::Engine()
/////////////////////////////////////////////////
Entity &Engine::AddWorld()
{
World world;
std::size_t worldId = world.GetId();
const auto [it, success] = this->worlds.insert({worldId, world});
return it->second;
auto world = std::make_shared<World>();
const auto [it, success] = this->worlds.insert({world->GetId(), world});
return *it->second;
}

/////////////////////////////////////////////////
Expand All @@ -46,7 +45,7 @@ std::size_t Engine::GetWorldCount() const
}

/////////////////////////////////////////////////
std::map<std::size_t, Entity> Engine::GetWorlds() const
std::map<std::size_t, std::shared_ptr<Entity>> Engine::GetWorlds() const
{
return this->worlds;
}
Expand All @@ -57,7 +56,7 @@ Entity &Engine::GetWorldById(std::size_t _worldId)
auto it = this->worlds.find(_worldId);
if (it != this->worlds.end())
{
return it->second;
return *it->second;
}
return Entity::kNullEntity;
}
Expand Down
6 changes: 4 additions & 2 deletions tpe/lib/src/Engine.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#ifndef IGNITION_PHYSICS_TPE_LIB_SRC_ENGINE_HH_
#define IGNITION_PHYSICS_TPE_LIB_SRC_ENGINE_HH_

#include <memory>

#include <ignition/utilities/SuppressWarning.hh>

#include "ignition/physics/tpelib/Export.hh"
Expand Down Expand Up @@ -53,15 +55,15 @@ class IGNITION_PHYSICS_TPELIB_VISIBLE Engine

/// \brief Get all worlds in engine
/// \return a map of id -> world
public: std::map<std::size_t, Entity> GetWorlds() const;
public: std::map<std::size_t, std::shared_ptr<Entity>> GetWorlds() const;

/// \brief Remove World from engine
/// \return true/false if world is removed/not
public: bool RemoveWorldById(std::size_t _worldId);

IGN_UTILS_WARN_IGNORE__DLL_INTERFACE_MISSING
/// \brief World entities in engine
protected: std::map<std::size_t, Entity> worlds;
protected: std::map<std::size_t, std::shared_ptr<Entity>> worlds;
IGN_UTILS_WARN_RESUME__DLL_INTERFACE_MISSING
};

Expand Down