Skip to content

Commit

Permalink
C++17 std::optional will not support optional references. Remove the …
Browse files Browse the repository at this point in the history
…use of optional references in the ECS as a result.
  • Loading branch information
thebracket committed Jan 23, 2017
1 parent 0d9ca6f commit 572e4bc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
4 changes: 2 additions & 2 deletions examples/ex10/main.cpp
Expand Up @@ -62,10 +62,10 @@ int left_x, right_x, top_y, bottom_y;

struct actor_moved_message : base_message_t {
actor_moved_message() {}
actor_moved_message(boost::optional<entity_t &> ACTOR, const int fx, const int fy, const int dx, const int dy) :
actor_moved_message(entity_t * ACTOR, const int fx, const int fy, const int dx, const int dy) :
mover(ACTOR), from_x(fx), from_y(fy), destination_x(dx), destination_y(dy) {}

boost::optional<entity_t &> mover;
entity_t * mover;
int from_x, from_y, destination_x, destination_y;
};

Expand Down
16 changes: 8 additions & 8 deletions rltk/ecs.cpp
Expand Up @@ -19,28 +19,28 @@ void unset_component_mask(const std::size_t id, const std::size_t family_id, boo
}
}

boost::optional<entity_t&> entity(const std::size_t id) noexcept {
boost::optional<entity_t&> result;
entity_t * entity(const std::size_t id) noexcept {
entity_t * result = nullptr;
auto finder = entity_store.find(id);
if (finder == entity_store.end()) return result;
if (finder->second.deleted) return result;
result = finder->second;
result = &finder->second;
return result;
}

boost::optional<entity_t&> create_entity() {
entity_t new_entity;
entity_t * create_entity() {
entity_t new_entity;
while (entity_store.find(new_entity.id) != entity_store.end()) {
++entity_t::entity_counter;
new_entity.id = entity_t::entity_counter;
}
//std::cout << "New Entity ID#: " << new_entity.id << "\n";

entity_store.emplace(new_entity.id, new_entity);
return entity(new_entity.id);
return entity(new_entity.id);
}

boost::optional<entity_t&> create_entity(const std::size_t new_id) {
entity_t * create_entity(const std::size_t new_id) {
entity_t new_entity(new_id);
if (entity_store.find(new_entity.id) != entity_store.end()) {
throw std::runtime_error("WARNING: Duplicate entity ID. Odd things will happen\n");
Expand Down Expand Up @@ -200,4 +200,4 @@ std::string ecs_profile_dump() {
return ss.str();
}

}
}
13 changes: 6 additions & 7 deletions rltk/ecs.hpp
Expand Up @@ -15,7 +15,6 @@
#include <queue>
#include <future>
#include <mutex>
#include <boost/optional.hpp>
#include <typeinfo>
#include <atomic>
#include "serialization_utils.hpp"
Expand Down Expand Up @@ -259,16 +258,16 @@ struct entity_t {
* Find a component of the specified type that belongs to the entity.
*/
template <class C>
inline boost::optional<C&> component() noexcept {
boost::optional<C&> result;
inline C * component() noexcept {
C * result = nullptr;
if (deleted) return result;

C empty_component;
component_t<C> temp(empty_component);
if (!component_mask.test(temp.family_id)) return result;
for (component_t<C> &component : static_cast<component_store_t<component_t<C>> *>(component_store[temp.family_id].get())->components) {
if (component.entity_id == id) {
result = component.data;
result = &component.data;
return result;
}
}
Expand All @@ -290,18 +289,18 @@ void unset_component_mask(const std::size_t id, const std::size_t family_id, boo
* entity(ID) is used to reference an entity. So you can, for example, do:
* entity(12)->component<position_component>()->x = 3;
*/
boost::optional<entity_t&> entity(const std::size_t id) noexcept;
entity_t * entity(const std::size_t id) noexcept;

/*
* Creates an entity with a new ID #. Returns a pointer to the entity, to enable
* call chaining. For example create_entity()->assign(foo)->assign(bar)
*/
boost::optional<entity_t&> create_entity();
entity_t * create_entity();

/*
* Creates an entity with a specified ID #. You generally only do this during loading.
*/
boost::optional<entity_t&> create_entity(const std::size_t new_id);
entity_t * create_entity(const std::size_t new_id);

/*
* Finds all entities that have a component of the type specified, and returns a
Expand Down

0 comments on commit 572e4bc

Please sign in to comment.