Skip to content

Commit

Permalink
Fix removeComponent return type
Browse files Browse the repository at this point in the history
Add [[nodiscard]] to relevant functions
  • Loading branch information
Tiphaine LAURENT committed Aug 5, 2019
1 parent a680df8 commit 8307e0d
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 58 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@
.idea/
build/
test/
out/
bin/*
5 changes: 2 additions & 3 deletions ECS/src/Component/Component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#ifndef ECS_COMPONENT_HPP
#define ECS_COMPONENT_HPP

# include <ostream>
# include "IComponent.hpp"

namespace ecs
Expand Down Expand Up @@ -43,12 +42,12 @@ namespace ecs
Component &operator=(Component &&) noexcept = default;

public:
static const ComponentID getComponentTypeCount()
static ComponentID getComponentTypeCount()
{
return _componentTypeCount;
}

const ComponentTypeID getComponentTypeID() const override
[[nodiscard]] ComponentTypeID getComponentTypeID() const override
{
return _componentTypeID;
}
Expand Down
11 changes: 6 additions & 5 deletions ECS/src/Component/ComponentContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace ecs
ComponentContainer &operator=(ComponentContainer &&) noexcept = delete;

public:
const char *getComponentContainerTypeName() const override
[[nodiscard]] const char *getComponentContainerTypeName() const override
{
static const auto componentTypeName{typeid(C).name()};

Expand All @@ -66,7 +66,7 @@ namespace ecs
_components.push_back(std::move(component));
return _components.back().get();
}
C *getComponent(EntityID entityID)
[[nodiscard]] C *getComponent(EntityID entityID)
{
for (auto &component : _components) {
if (component->getOwner() == entityID) {
Expand All @@ -75,7 +75,7 @@ namespace ecs
}
return nullptr;
}
std::vector<C *const> getComponents(EntityID entityID)
[[nodiscard]] std::vector<C *const> getComponents(EntityID entityID)
{
auto components = std::vector<C *const>{};

Expand All @@ -86,7 +86,8 @@ namespace ecs
}
return components;
}
const ComponentStorage<C> getComponents(EntityID entityID) const
[[nodiscard]] ComponentStorage<C> getComponents(EntityID entityID)
const
{
auto components = std::vector<C *const>{};

Expand Down Expand Up @@ -130,7 +131,7 @@ namespace ecs
return _components.end();
}

ComponentStorage<C> &getComponents()
[[nodiscard]] ComponentStorage<C> &getComponents()
{
return _components;
}
Expand Down
8 changes: 4 additions & 4 deletions ECS/src/Component/ComponentManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ namespace ecs
);
}
template <class C>
static C *getComponent(EntityID entityID)
[[nodiscard]] static C *getComponent(EntityID entityID)
{
auto &container = getComponentContainer<C>();

return container.getComponent(entityID);
}
template <class C>
static std::vector<C *> getComponents(EntityID entityID)
[[nodiscard]] static std::vector<C *> getComponents(EntityID entityID)
{
auto &container = getComponentContainer<C>();

Expand All @@ -99,7 +99,7 @@ namespace ecs
return getComponentContainer<C>().end();
}

size_t getContainerCount() const
[[nodiscard]] size_t getContainerCount() const
{
return _containers.size();
}
Expand All @@ -117,7 +117,7 @@ namespace ecs
}

template <class C>
ComponentContainer<C> *getContainer()
[[nodiscard]] ComponentContainer<C> *getContainer()
{
const auto componentTypeID = C::_componentTypeID;
auto it = _containers.find(componentTypeID);
Expand Down
18 changes: 9 additions & 9 deletions ECS/src/Component/IComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ namespace ecs
}
}

const ComponentID IComponent::getComponentCount()
IComponent::~IComponent()
{
return _componentCount;
_freeID.push_back(_componentID);
--_componentCount;
}

const ComponentID IComponent::getComponentID() const
ComponentID IComponent::getComponentCount()
{
return _componentID;
return _componentCount;
}

IComponent::~IComponent()
ComponentID IComponent::getComponentID() const
{
_freeID.push_back(_componentID);
--_componentCount;
return _componentID;
}

const EntityID IComponent::getOwner() const
EntityID IComponent::getOwner() const
{
return _owner;
}
Expand All @@ -51,7 +51,7 @@ namespace ecs
return *this;
}

const bool IComponent::isActive() const
bool IComponent::isActive() const
{
return _active;
}
Expand Down
10 changes: 5 additions & 5 deletions ECS/src/Component/IComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ namespace ecs
IComponent &operator=(IComponent &&) = default;

public:
static const ComponentID getComponentCount();
[[nodiscard]] static ComponentID getComponentCount();

const ComponentID getComponentID() const;
virtual const ComponentTypeID getComponentTypeID() const = 0;
[[nodiscard]] ComponentID getComponentID() const;
[[nodiscard]] virtual ComponentTypeID getComponentTypeID() const = 0;

IComponent &setOwner(EntityID entityID);
const EntityID getOwner() const;
[[nodiscard]] EntityID getOwner() const;

IComponent &setActive(bool state);
const bool isActive() const;
[[nodiscard]] bool isActive() const;

private:
};
Expand Down
2 changes: 1 addition & 1 deletion ECS/src/Component/IComponentContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace ecs
IComponentContainer &operator=(IComponentContainer &&) = delete;

public:
virtual const char *getComponentContainerTypeName() const = 0;
[[nodiscard]] virtual const char *getComponentContainerTypeName() const = 0;

virtual void removeComponent(EntityID entityID) = 0;

Expand Down
2 changes: 1 addition & 1 deletion ECS/src/Entity/Entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace ecs
Entity &operator=(Entity &&) noexcept = default;

public:
const EntityTypeID getEntityTypeID() const override
[[nodiscard]] EntityTypeID getEntityTypeID() const override
{
return _entityTypeID;
}
Expand Down
8 changes: 4 additions & 4 deletions ECS/src/Entity/EntityContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace ecs
EntityContainer &operator=(EntityContainer &&) noexcept = delete;

public:
const char *getEntityContainerTypeName() const override
[[nodiscard]] const char *getEntityContainerTypeName() const override
{
static const char *entityTypeName{typeid(E).name()};

Expand All @@ -65,15 +65,15 @@ namespace ecs
_entities[entityID] = std::move(entity);
return getEntityById(entityID);
}
E &getEntityById(EntityID entityID)
[[nodiscard]] E &getEntityById(EntityID entityID)
{
return *_entities[entityID].get();
}
EntityMap<E> &getEntities()
[[nodiscard]] EntityMap<E> &getEntities()
{
return _entities;
}
const EntityMap<E> &getEntities() const
[[nodiscard]] const EntityMap<E> &getEntities() const
{
return _entities;
}
Expand Down
6 changes: 3 additions & 3 deletions ECS/src/Entity/EntityManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace ecs
public:
static EntityManager &getInstance();
template <class E>
constexpr static EntityContainer<E> &getEntityContainer()
[[nodiscard]] constexpr static EntityContainer<E> &getEntityContainer()
{
static_assert(
std::is_base_of<IEntity, E>::value,
Expand Down Expand Up @@ -70,14 +70,14 @@ namespace ecs
return container.createEntity(std::forward(args)...);
}
template <class E>
static E &getEntityById(EntityID entityID)
[[nodiscard]] static E &getEntityById(EntityID entityID)
{
EntityContainer<E> &container = getEntityContainer<E>();

return container.getEntityById(entityID);
}
template <class E>
static EntityMap<E> &getComponents(EntityID entityID)
[[nodiscard]] static EntityMap<E> &getComponents(EntityID entityID)
{
EntityContainer<E> &container = getEntityContainer<E>();

Expand Down
8 changes: 4 additions & 4 deletions ECS/src/Entity/IEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace ecs
std::vector<EntityID> IEntity::_freeID;
EntityID IEntity::_entityCount = 0;

const EntityTypeID IEntity::getEntityTypeID() const
EntityTypeID IEntity::getEntityTypeID() const
{
return _entityTypeID;
}
Expand All @@ -29,7 +29,7 @@ namespace ecs
}
}

const EntityID IEntity::getEntityID() const
EntityID IEntity::getEntityID() const
{
return _entityID;
}
Expand All @@ -40,12 +40,12 @@ namespace ecs
return *this;
}

const bool IEntity::isActive() const
bool IEntity::isActive() const
{
return _active;
}

const EntityID IEntity::getEntityCount()
EntityID IEntity::getEntityCount()
{
return _entityCount;
}
Expand Down
16 changes: 8 additions & 8 deletions ECS/src/Entity/IEntity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ namespace ecs
IEntity &operator=(IEntity &&) noexcept = default;

public:
static const EntityID getEntityCount();
[[nodiscard]] static EntityID getEntityCount();

const EntityID getEntityID() const;
virtual const EntityTypeID getEntityTypeID() const = 0;
[[nodiscard]] EntityID getEntityID() const;
[[nodiscard]] virtual EntityTypeID getEntityTypeID() const = 0;

IEntity &setActive(bool state);
const bool isActive() const;
[[nodiscard]] bool isActive() const;

template <class C, class ...ARGS>
C *addComponent(ARGS &&... args)
Expand All @@ -66,19 +66,19 @@ namespace ecs
);
}
template <class C>
C *getComponent() const
[[nodiscard]] C *getComponent() const
{
return ComponentManager::getComponent<C>(_entityID);
}
template <class C>
std::vector<C *const> getComponents() const
[[nodiscard]] std::vector<C *const> getComponents() const
{
return ComponentManager::getComponents<C>(_entityID);
}
template <class C>
IEntity &removeComponent()
void removeComponent()
{
return ComponentManager::removeComponent<C>(_entityID);
ComponentManager::removeComponent<C>(_entityID);
}

private:
Expand Down
2 changes: 1 addition & 1 deletion ECS/src/Entity/IEntityContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace ecs
IEntityContainer &operator=(IEntityContainer &&) = delete;

public:
virtual const char *getEntityContainerTypeName() const = 0;
[[nodiscard]] virtual const char *getEntityContainerTypeName() const = 0;

virtual void destroyEntity(EntityID) = 0;

Expand Down
12 changes: 6 additions & 6 deletions ECS/src/System/ISystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ namespace ecs
ISystem &operator=(ISystem &&other) noexcept = delete;

public:
virtual const SystemTypeID getSystemTypeID() const = 0;
virtual const char *getSystemTypeName() const = 0;
[[nodiscard]] virtual SystemTypeID getSystemTypeID() const = 0;
[[nodiscard]] virtual const char *getSystemTypeName() const = 0;

virtual void preUpdate() = 0;
virtual void update() = 0;
virtual void postUpdate() = 0;

bool isEnable() const
[[nodiscard]] bool isEnable() const
{
return _enabled;
}
Expand All @@ -76,11 +76,11 @@ namespace ecs
_enabled = false;
}

float getUpdateInterval() const
[[nodiscard]] float getUpdateInterval() const
{
return _updateInterval;
}
float getTimeSinceLastUpdate() const
[[nodiscard]] float getTimeSinceLastUpdate() const
{
return _timeSinceLastUpdate;
}
Expand All @@ -89,7 +89,7 @@ namespace ecs
_updateInterval = interval;
}

SystemPriority getPriority() const
[[nodiscard]] SystemPriority getPriority() const
{
return _priority;
}
Expand Down
4 changes: 2 additions & 2 deletions ECS/src/System/System.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ namespace ecs
System &operator=(System &&other) = delete;

public:
const SystemTypeID getSystemTypeID() const override
[[nodiscard]] SystemTypeID getSystemTypeID() const override
{
return _systemTypeID;
}
const char *getSystemTypeName() const override
[[nodiscard]] const char *getSystemTypeName() const override
{
static const char *systemTypeName{typeid(S).name()};

Expand Down
Binary file modified bin/tests.ilk
Binary file not shown.
Binary file modified bin/tests.pdb
Binary file not shown.
9 changes: 7 additions & 2 deletions tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ TEST_CASE("Basic creation", "creation")
REQUIRE(ecs::ComponentManager::getInstance().getContainerCount() == 2);
REQUIRE(component->getComponentCount() == 2);
REQUIRE(component->getComponentTypeCount() == 1);
REQUIRE(component->getComponentID() == 1);
REQUIRE(component->getComponentTypeID() == 1);
REQUIRE(component->getComponentID() == 0);
REQUIRE(component->getComponentTypeID() == 0);

REQUIRE(component2->getComponentCount() == 2);
REQUIRE(component2->getComponentTypeCount() == 1);
REQUIRE(component2->getComponentID() == 1);
REQUIRE(component2->getComponentTypeID() == 1);
REQUIRE(!component2->setActive(false).isActive());

entity.removeComponent<MyComponent2>();
Expand Down

0 comments on commit 8307e0d

Please sign in to comment.