From 2cafb49ffebc9cc9b5e91afff2d287b4a716536f Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Sun, 7 Jun 2020 00:51:45 +0200 Subject: [PATCH] entity: [[nodiscard]] (close #501) --- src/entt/entity/actor.hpp | 18 +++--- src/entt/entity/entity.hpp | 14 ++--- src/entt/entity/group.hpp | 60 +++++++++---------- src/entt/entity/helper.hpp | 4 +- src/entt/entity/observer.hpp | 10 ++-- src/entt/entity/registry.hpp | 91 ++++++++++++++-------------- src/entt/entity/runtime_view.hpp | 22 +++---- src/entt/entity/snapshot.hpp | 4 +- src/entt/entity/sparse_set.hpp | 44 +++++++------- src/entt/entity/storage.hpp | 42 ++++++------- src/entt/entity/view.hpp | 64 ++++++++++---------- test/benchmark/benchmark.cpp | 100 +++++++++++++++---------------- test/entt/entity/group.cpp | 8 +-- test/entt/entity/registry.cpp | 3 +- 14 files changed, 243 insertions(+), 241 deletions(-) diff --git a/src/entt/entity/actor.hpp b/src/entt/entity/actor.hpp index 495bfd989..dabf9bf3c 100644 --- a/src/entt/entity/actor.hpp +++ b/src/entt/entity/actor.hpp @@ -129,7 +129,7 @@ struct basic_actor { * @return True if the actor has all the components, false otherwise. */ template - bool has() const { + [[nodiscard]] bool has() const { return reg->template has(entt); } @@ -139,13 +139,13 @@ struct basic_actor { * @return References to the components owned by the actor. */ template - decltype(auto) get() const { + [[nodiscard]] decltype(auto) get() const { return std::as_const(*reg).template get(entt); } /*! @copydoc get */ template - decltype(auto) get() { + [[nodiscard]] decltype(auto) get() { return reg->template get(entt); } @@ -155,13 +155,13 @@ struct basic_actor { * @return Pointers to the components owned by the actor. */ template - auto try_get() const { + [[nodiscard]] auto try_get() const { return std::as_const(*reg).template try_get(entt); } /*! @copydoc try_get */ template - auto try_get() { + [[nodiscard]] auto try_get() { return reg->template try_get(entt); } @@ -169,12 +169,12 @@ struct basic_actor { * @brief Returns a reference to the underlying registry. * @return A reference to the underlying registry. */ - const registry_type & backend() const ENTT_NOEXCEPT { + [[nodiscard]] const registry_type & backend() const ENTT_NOEXCEPT { return *reg; } /*! @copydoc backend */ - registry_type & backend() ENTT_NOEXCEPT { + [[nodiscard]] registry_type & backend() ENTT_NOEXCEPT { return const_cast(std::as_const(*this).backend()); } @@ -182,7 +182,7 @@ struct basic_actor { * @brief Returns the entity associated with an actor. * @return The entity associated with the actor. */ - entity_type entity() const ENTT_NOEXCEPT { + [[nodiscard]] entity_type entity() const ENTT_NOEXCEPT { return entt; } @@ -190,7 +190,7 @@ struct basic_actor { * @brief Checks if an actor refers to a valid entity or not. * @return True if the actor refers to a valid entity, false otherwise. */ - explicit operator bool() const { + [[nodiscard]] explicit operator bool() const { return reg && reg->valid(entt); } diff --git a/src/entt/entity/entity.hpp b/src/entt/entity/entity.hpp index de7b539dc..d2e029c7b 100644 --- a/src/entt/entity/entity.hpp +++ b/src/entt/entity/entity.hpp @@ -115,38 +115,38 @@ class null { public: template - constexpr operator Entity() const ENTT_NOEXCEPT { + [[nodiscard]] constexpr operator Entity() const ENTT_NOEXCEPT { return Entity{traits_type::entity_mask}; } - constexpr bool operator==(null) const ENTT_NOEXCEPT { + [[nodiscard]] constexpr bool operator==(null) const ENTT_NOEXCEPT { return true; } - constexpr bool operator!=(null) const ENTT_NOEXCEPT { + [[nodiscard]] constexpr bool operator!=(null) const ENTT_NOEXCEPT { return false; } template - constexpr bool operator==(const Entity entity) const ENTT_NOEXCEPT { + [[nodiscard]] constexpr bool operator==(const Entity entity) const ENTT_NOEXCEPT { return (to_integral(entity) & traits_type::entity_mask) == to_integral(static_cast(*this)); } template - constexpr bool operator!=(const Entity entity) const ENTT_NOEXCEPT { + [[nodiscard]] constexpr bool operator!=(const Entity entity) const ENTT_NOEXCEPT { return !(entity == *this); } }; template -constexpr bool operator==(const Entity entity, null other) ENTT_NOEXCEPT { +[[nodiscard]] constexpr bool operator==(const Entity entity, null other) ENTT_NOEXCEPT { return other.operator==(entity); } template -constexpr bool operator!=(const Entity entity, null other) ENTT_NOEXCEPT { +[[nodiscard]] constexpr bool operator!=(const Entity entity, null other) ENTT_NOEXCEPT { return !(other == entity); } diff --git a/src/entt/entity/group.hpp b/src/entt/entity/group.hpp index 9107550fe..91522eecb 100644 --- a/src/entt/entity/group.hpp +++ b/src/entt/entity/group.hpp @@ -110,7 +110,7 @@ public: * @return Number of existing components of the given type. */ template - size_type size() const ENTT_NOEXCEPT { + [[nodiscard]] size_type size() const ENTT_NOEXCEPT { return std::get *>(pools)->size(); } @@ -118,7 +118,7 @@ public: * @brief Returns the number of entities that have the given components. * @return Number of entities that have the given components. */ - size_type size() const ENTT_NOEXCEPT { + [[nodiscard]] size_type size() const ENTT_NOEXCEPT { return handler->size(); } @@ -127,7 +127,7 @@ public: * allocated space for. * @return Capacity of the group. */ - size_type capacity() const ENTT_NOEXCEPT { + [[nodiscard]] size_type capacity() const ENTT_NOEXCEPT { return handler->capacity(); } @@ -142,7 +142,7 @@ public: * @return True if the group or the pools are empty, false otherwise. */ template - bool empty() const ENTT_NOEXCEPT { + [[nodiscard]] bool empty() const ENTT_NOEXCEPT { if constexpr(sizeof...(Component) == 0) { return handler->empty(); } else { @@ -165,7 +165,7 @@ public: * @return A pointer to the array of components. */ template - Component * raw() const ENTT_NOEXCEPT { + [[nodiscard]] Component * raw() const ENTT_NOEXCEPT { return std::get *>(pools)->raw(); } @@ -184,7 +184,7 @@ public: * @return A pointer to the array of entities. */ template - const entity_type * data() const ENTT_NOEXCEPT { + [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT { return std::get *>(pools)->data(); } @@ -200,7 +200,7 @@ public: * * @return A pointer to the array of entities. */ - const entity_type * data() const ENTT_NOEXCEPT { + [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT { return handler->data(); } @@ -218,7 +218,7 @@ public: * * @return An iterator to the first entity that has the given components. */ - iterator begin() const ENTT_NOEXCEPT { + [[nodiscard]] iterator begin() const ENTT_NOEXCEPT { return handler->begin(); } @@ -237,7 +237,7 @@ public: * @return An iterator to the entity following the last entity that has the * given components. */ - iterator end() const ENTT_NOEXCEPT { + [[nodiscard]] iterator end() const ENTT_NOEXCEPT { return handler->end(); } @@ -246,7 +246,7 @@ public: * @return The first entity that has the given components if one exists, the * null entity otherwise. */ - entity_type front() const { + [[nodiscard]] entity_type front() const { const auto it = begin(); return it != end() ? *it : null; } @@ -256,7 +256,7 @@ public: * @return The last entity that has the given components if one exists, the * null entity otherwise. */ - entity_type back() const { + [[nodiscard]] entity_type back() const { const auto it = std::make_reverse_iterator(end()); return it != std::make_reverse_iterator(begin()) ? *it : null; } @@ -267,7 +267,7 @@ public: * @return An iterator to the given entity if it's found, past the end * iterator otherwise. */ - iterator find(const entity_type entt) const { + [[nodiscard]] iterator find(const entity_type entt) const { const auto it = handler->find(entt); return it != end() && *it == entt ? it : end(); } @@ -277,7 +277,7 @@ public: * @param pos Position of the element to return. * @return The identifier that occupies the given position. */ - entity_type operator[](const size_type pos) const { + [[nodiscard]] entity_type operator[](const size_type pos) const { return begin()[pos]; } @@ -286,7 +286,7 @@ public: * @param entt A valid entity identifier. * @return True if the group contains the given entity, false otherwise. */ - bool contains(const entity_type entt) const { + [[nodiscard]] bool contains(const entity_type entt) const { return handler->contains(entt); } @@ -308,7 +308,7 @@ public: * @return The components assigned to the entity. */ template - decltype(auto) get([[maybe_unused]] const entity_type entt) const { + [[nodiscard]] decltype(auto) get([[maybe_unused]] const entity_type entt) const { ENTT_ASSERT(contains(entt)); if constexpr(sizeof...(Component) == 1) { @@ -531,7 +531,7 @@ public: * @return Number of existing components of the given type. */ template - size_type size() const ENTT_NOEXCEPT { + [[nodiscard]] size_type size() const ENTT_NOEXCEPT { return std::get *>(pools)->size(); } @@ -539,7 +539,7 @@ public: * @brief Returns the number of entities that have the given components. * @return Number of entities that have the given components. */ - size_type size() const ENTT_NOEXCEPT { + [[nodiscard]] size_type size() const ENTT_NOEXCEPT { return *length; } @@ -549,7 +549,7 @@ public: * @return True if the group or the pools are empty, false otherwise. */ template - bool empty() const ENTT_NOEXCEPT { + [[nodiscard]] bool empty() const ENTT_NOEXCEPT { if constexpr(sizeof...(Component) == 0) { return !*length; } else { @@ -575,7 +575,7 @@ public: * @return A pointer to the array of components. */ template - Component * raw() const ENTT_NOEXCEPT { + [[nodiscard]] Component * raw() const ENTT_NOEXCEPT { return std::get *>(pools)->raw(); } @@ -597,7 +597,7 @@ public: * @return A pointer to the array of entities. */ template - const entity_type * data() const ENTT_NOEXCEPT { + [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT { return std::get *>(pools)->data(); } @@ -613,7 +613,7 @@ public: * * @return A pointer to the array of entities. */ - const entity_type * data() const ENTT_NOEXCEPT { + [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT { return std::get<0>(pools)->data(); } @@ -631,7 +631,7 @@ public: * * @return An iterator to the first entity that has the given components. */ - iterator begin() const ENTT_NOEXCEPT { + [[nodiscard]] iterator begin() const ENTT_NOEXCEPT { return std::get<0>(pools)->sparse_set::end() - *length; } @@ -650,7 +650,7 @@ public: * @return An iterator to the entity following the last entity that has the * given components. */ - iterator end() const ENTT_NOEXCEPT { + [[nodiscard]] iterator end() const ENTT_NOEXCEPT { return std::get<0>(pools)->sparse_set::end(); } @@ -659,7 +659,7 @@ public: * @return The first entity that has the given components if one exists, the * null entity otherwise. */ - entity_type front() const { + [[nodiscard]] entity_type front() const { const auto it = begin(); return it != end() ? *it : null; } @@ -669,7 +669,7 @@ public: * @return The last entity that has the given components if one exists, the * null entity otherwise. */ - entity_type back() const { + [[nodiscard]] entity_type back() const { const auto it = std::make_reverse_iterator(end()); return it != std::make_reverse_iterator(begin()) ? *it : null; } @@ -680,7 +680,7 @@ public: * @return An iterator to the given entity if it's found, past the end * iterator otherwise. */ - iterator find(const entity_type entt) const { + [[nodiscard]] iterator find(const entity_type entt) const { const auto it = std::get<0>(pools)->find(entt); return it != end() && it >= begin() && *it == entt ? it : end(); } @@ -690,7 +690,7 @@ public: * @param pos Position of the element to return. * @return The identifier that occupies the given position. */ - entity_type operator[](const size_type pos) const { + [[nodiscard]] entity_type operator[](const size_type pos) const { return begin()[pos]; } @@ -699,7 +699,7 @@ public: * @param entt A valid entity identifier. * @return True if the group contains the given entity, false otherwise. */ - bool contains(const entity_type entt) const { + [[nodiscard]] bool contains(const entity_type entt) const { return std::get<0>(pools)->contains(entt) && (std::get<0>(pools)->index(entt) < (*length)); } @@ -721,7 +721,7 @@ public: * @return The components assigned to the entity. */ template - decltype(auto) get([[maybe_unused]] const entity_type entt) const { + [[nodiscard]] decltype(auto) get([[maybe_unused]] const entity_type entt) const { ENTT_ASSERT(contains(entt)); if constexpr(sizeof...(Component) == 1) { @@ -764,7 +764,7 @@ public: * @brief Checks whether the group can be sorted. * @return True if the group can be sorted, false otherwise. */ - bool sortable() const ENTT_NOEXCEPT { + [[nodiscard]] bool sortable() const ENTT_NOEXCEPT { constexpr auto size = sizeof...(Owned) + sizeof...(Get) + sizeof...(Exclude); return *super == size; } diff --git a/src/entt/entity/helper.hpp b/src/entt/entity/helper.hpp index 49bfb0c7f..3e2aca204 100644 --- a/src/entt/entity/helper.hpp +++ b/src/entt/entity/helper.hpp @@ -36,7 +36,7 @@ struct as_view { * @return A newly created view. */ template - operator entt::basic_view() const { + [[nodiscard]] operator entt::basic_view() const { return reg.template view(Exclude{}); } @@ -86,7 +86,7 @@ struct as_group { * @return A newly created group. */ template - operator entt::basic_group() const { + [[nodiscard]] operator entt::basic_group() const { return reg.template group(Get{}, Exclude{}); } diff --git a/src/entt/entity/observer.hpp b/src/entt/entity/observer.hpp index e9e3ba73b..fe91c1aeb 100644 --- a/src/entt/entity/observer.hpp +++ b/src/entt/entity/observer.hpp @@ -334,7 +334,7 @@ public: * @brief Returns the number of elements in an observer. * @return Number of elements. */ - size_type size() const ENTT_NOEXCEPT { + [[nodiscard]] size_type size() const ENTT_NOEXCEPT { return view.size(); } @@ -342,7 +342,7 @@ public: * @brief Checks whether an observer is empty. * @return True if the observer is empty, false otherwise. */ - bool empty() const ENTT_NOEXCEPT { + [[nodiscard]] bool empty() const ENTT_NOEXCEPT { return view.empty(); } @@ -358,7 +358,7 @@ public: * * @return A pointer to the array of entities. */ - const entity_type * data() const ENTT_NOEXCEPT { + [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT { return view.data(); } @@ -370,7 +370,7 @@ public: * * @return An iterator to the first entity of the observer. */ - iterator begin() const ENTT_NOEXCEPT { + [[nodiscard]] iterator begin() const ENTT_NOEXCEPT { return view.sparse_set::begin(); } @@ -384,7 +384,7 @@ public: * @return An iterator to the entity following the last entity of the * observer. */ - iterator end() const ENTT_NOEXCEPT { + [[nodiscard]] iterator end() const ENTT_NOEXCEPT { return view.sparse_set::end(); } diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index e85552042..7af8193d6 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -48,15 +48,15 @@ class basic_registry { static_assert(std::is_same_v>, "Invalid component type"); std::size_t super{}; - auto on_construct() ENTT_NOEXCEPT { + [[nodiscard]] auto on_construct() ENTT_NOEXCEPT { return sink{construction}; } - auto on_update() ENTT_NOEXCEPT { + [[nodiscard]] auto on_update() ENTT_NOEXCEPT { return sink{update}; } - auto on_destroy() ENTT_NOEXCEPT { + [[nodiscard]] auto on_destroy() ENTT_NOEXCEPT { return sink{destruction}; } @@ -180,7 +180,7 @@ class basic_registry { }; template - const pool_handler & assure() const { + [[nodiscard]] const pool_handler & assure() const { const sparse_set *cpool; if constexpr(ENTT_FAST_PATH(has_type_index_v)) { @@ -217,7 +217,7 @@ class basic_registry { } template - pool_handler & assure() { + [[nodiscard]] pool_handler & assure() { return const_cast &>(std::as_const(*this).template assure()); } @@ -244,7 +244,8 @@ public: */ template void prepare() { - assure(); + // suppress the warning due to the [[nodiscard]] attribute + static_cast(assure()); } /** @@ -253,7 +254,7 @@ public: * @return Number of existing components of the given type. */ template - size_type size() const { + [[nodiscard]] size_type size() const { return assure().size(); } @@ -261,7 +262,7 @@ public: * @brief Returns the number of entities created so far. * @return Number of entities created so far. */ - size_type size() const ENTT_NOEXCEPT { + [[nodiscard]] size_type size() const ENTT_NOEXCEPT { return entities.size(); } @@ -269,7 +270,7 @@ public: * @brief Returns the number of entities still in use. * @return Number of entities still in use. */ - size_type alive() const { + [[nodiscard]] size_type alive() const { auto sz = entities.size(); auto curr = destroyed; @@ -308,7 +309,7 @@ public: * @return Capacity of the pool of the given component. */ template - size_type capacity() const { + [[nodiscard]] size_type capacity() const { return assure().capacity(); } @@ -317,7 +318,7 @@ public: * allocated space for. * @return Capacity of the registry. */ - size_type capacity() const ENTT_NOEXCEPT { + [[nodiscard]] size_type capacity() const ENTT_NOEXCEPT { return entities.capacity(); } @@ -343,7 +344,7 @@ public: * empty, false otherwise. */ template - bool empty() const { + [[nodiscard]] bool empty() const { if constexpr(sizeof...(Component) == 0) { return !alive(); } else { @@ -369,13 +370,13 @@ public: * @return A pointer to the array of components of the given type. */ template - const Component * raw() const { + [[nodiscard]] const Component * raw() const { return assure().raw(); } /*! @copydoc raw */ template - Component * raw() { + [[nodiscard]] Component * raw() { return const_cast(std::as_const(*this).template raw()); } @@ -393,7 +394,7 @@ public: * @return A pointer to the array of entities. */ template - const entity_type * data() const { + [[nodiscard]] const entity_type * data() const { return assure().data(); } @@ -409,7 +410,7 @@ public: * * @return A pointer to the array of entities. */ - const entity_type * data() const ENTT_NOEXCEPT { + [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT { return entities.data(); } @@ -418,7 +419,7 @@ public: * @param entity An entity identifier, either valid or not. * @return True if the identifier is valid, false otherwise. */ - bool valid(const entity_type entity) const { + [[nodiscard]] bool valid(const entity_type entity) const { const auto pos = size_type(to_integral(entity) & traits_type::entity_mask); return (pos < entities.size() && entities[pos] == entity); } @@ -428,7 +429,7 @@ public: * @param entity An entity identifier, either valid or not. * @return The entity identifier without the version. */ - static entity_type entity(const entity_type entity) ENTT_NOEXCEPT { + [[nodiscard]] static entity_type entity(const entity_type entity) ENTT_NOEXCEPT { return entity_type{to_integral(entity) & traits_type::entity_mask}; } @@ -437,7 +438,7 @@ public: * @param entity An entity identifier, either valid or not. * @return The version stored along with the given entity identifier. */ - static version_type version(const entity_type entity) ENTT_NOEXCEPT { + [[nodiscard]] static version_type version(const entity_type entity) ENTT_NOEXCEPT { return version_type(to_integral(entity) >> traits_type::entity_shift); } @@ -454,7 +455,7 @@ public: * @param entity A valid entity identifier. * @return Actual version for the given entity identifier. */ - version_type current(const entity_type entity) const { + [[nodiscard]] version_type current(const entity_type entity) const { const auto pos = size_type(to_integral(entity) & traits_type::entity_mask); ENTT_ASSERT(pos < entities.size()); return version_type(to_integral(entities[pos]) >> traits_type::entity_shift); @@ -498,7 +499,7 @@ public: * @param hint A desired entity identifier. * @return A valid entity identifier. */ - entity_type create(const entity_type hint) { + [[nodiscard]] entity_type create(const entity_type hint) { ENTT_ASSERT(hint != null); entity_type entt; @@ -871,7 +872,7 @@ public: * @return True if the entity has all the components, false otherwise. */ template - bool has(const entity_type entity) const { + [[nodiscard]] bool has(const entity_type entity) const { ENTT_ASSERT(valid(entity)); return (assure().contains(entity) && ...); } @@ -890,7 +891,7 @@ public: * false otherwise. */ template - bool any(const entity_type entity) const { + [[nodiscard]] bool any(const entity_type entity) const { ENTT_ASSERT(valid(entity)); return (assure().contains(entity) || ...); } @@ -910,7 +911,7 @@ public: * @return References to the components owned by the entity. */ template - decltype(auto) get([[maybe_unused]] const entity_type entity) const { + [[nodiscard]] decltype(auto) get([[maybe_unused]] const entity_type entity) const { ENTT_ASSERT(valid(entity)); if constexpr(sizeof...(Component) == 1) { @@ -922,7 +923,7 @@ public: /*! @copydoc get */ template - decltype(auto) get([[maybe_unused]] const entity_type entity) { + [[nodiscard]] decltype(auto) get([[maybe_unused]] const entity_type entity) { ENTT_ASSERT(valid(entity)); if constexpr(sizeof...(Component) == 1) { @@ -957,7 +958,7 @@ public: * @return Reference to the component owned by the entity. */ template - decltype(auto) get_or_emplace(const entity_type entity, Args &&... args) { + [[nodiscard]] decltype(auto) get_or_emplace(const entity_type entity, Args &&... args) { ENTT_ASSERT(valid(entity)); auto &cpool = assure(); return cpool.contains(entity) ? cpool.get(entity) : cpool.emplace(*this, entity, std::forward(args)...); @@ -976,7 +977,7 @@ public: * @return Pointers to the components owned by the entity. */ template - auto try_get([[maybe_unused]] const entity_type entity) const { + [[nodiscard]] auto try_get([[maybe_unused]] const entity_type entity) const { ENTT_ASSERT(valid(entity)); if constexpr(sizeof...(Component) == 1) { @@ -988,7 +989,7 @@ public: /*! @copydoc try_get */ template - auto try_get([[maybe_unused]] const entity_type entity) { + [[nodiscard]] auto try_get([[maybe_unused]] const entity_type entity) { ENTT_ASSERT(valid(entity)); if constexpr(sizeof...(Component) == 1) { @@ -1051,7 +1052,7 @@ public: * @param entity A valid entity identifier. * @return True if the entity has no components assigned, false otherwise. */ - bool orphan(const entity_type entity) const { + [[nodiscard]] bool orphan(const entity_type entity) const { ENTT_ASSERT(valid(entity)); return std::none_of(pools.cbegin(), pools.cend(), [entity](auto &&pdata) { return pdata.pool && pdata.pool->contains(entity); }); } @@ -1104,7 +1105,7 @@ public: * @return A temporary sink object. */ template - auto on_construct() { + [[nodiscard]] auto on_construct() { return assure().on_construct(); } @@ -1129,7 +1130,7 @@ public: * @return A temporary sink object. */ template - auto on_update() { + [[nodiscard]] auto on_update() { return assure().on_update(); } @@ -1156,7 +1157,7 @@ public: * @return A temporary sink object. */ template - auto on_destroy() { + [[nodiscard]] auto on_destroy() { return assure().on_destroy(); } @@ -1289,14 +1290,14 @@ public: * @return A newly created view. */ template - entt::basic_view, Component...> view(exclude_t = {}) const { + [[nodiscard]] entt::basic_view, Component...> view(exclude_t = {}) const { static_assert(sizeof...(Component) > 0, "Exclusion-only views are not supported"); return { assure>()..., assure()... }; } /*! @copydoc view */ template - entt::basic_view, Component...> view(exclude_t = {}) { + [[nodiscard]] entt::basic_view, Component...> view(exclude_t = {}) { static_assert(sizeof...(Component) > 0, "Exclusion-only views are not supported"); return { assure>()..., assure()... }; } @@ -1308,7 +1309,7 @@ public: * otherwise. */ template - bool sortable() const { + [[nodiscard]] bool sortable() const { return !(assure().super || ...); } @@ -1340,7 +1341,7 @@ public: * @return A newly created group. */ template - entt::basic_group, get_t, Owned...> group(get_t, exclude_t = {}) { + [[nodiscard]] entt::basic_group, get_t, Owned...> group(get_t, exclude_t = {}) { static_assert(sizeof...(Owned) + sizeof...(Get) > 0, "Exclusion-only views are not supported"); static_assert(sizeof...(Owned) + sizeof...(Get) + sizeof...(Exclude) > 1, "Single component groups are not allowed"); @@ -1436,7 +1437,7 @@ public: * @return A newly created group. */ template - entt::basic_group, get_t, Owned...> group(get_t, exclude_t = {}) const { + [[nodiscard]] entt::basic_group, get_t, Owned...> group(get_t, exclude_t = {}) const { static_assert(std::conjunction_v..., std::is_const...>, "Invalid non-const type"); return const_cast(this)->group(entt::get, exclude); } @@ -1451,7 +1452,7 @@ public: * @return A newly created group. */ template - entt::basic_group, get_t<>, Owned...> group(exclude_t = {}) { + [[nodiscard]] entt::basic_group, get_t<>, Owned...> group(exclude_t = {}) { return group(entt::get<>, exclude); } @@ -1465,7 +1466,7 @@ public: * @return A newly created group. */ template - entt::basic_group, get_t<>, Owned...> group(exclude_t = {}) const { + [[nodiscard]] entt::basic_group, get_t<>, Owned...> group(exclude_t = {}) const { static_assert(std::conjunction_v...>, "Invalid non-const type"); return const_cast(this)->group(exclude); } @@ -1491,7 +1492,7 @@ public: * @return A newly created runtime view. */ template - entt::basic_runtime_view runtime_view(It first, It last) const { + [[nodiscard]] entt::basic_runtime_view runtime_view(It first, It last) const { std::vector *> selected(std::distance(first, last)); std::transform(first, last, selected.begin(), [this](const auto ctype) { @@ -1602,7 +1603,7 @@ public: * @return A reference to the object in the context of the registry. */ template - Type & ctx_or_set(Args &&... args) { + [[nodiscard]] Type & ctx_or_set(Args &&... args) { auto *value = try_ctx(); return value ? *value : set(std::forward(args)...); } @@ -1614,14 +1615,14 @@ public: * registry, a null pointer otherwise. */ template - const Type * try_ctx() const { + [[nodiscard]] const Type * try_ctx() const { auto it = std::find_if(vars.cbegin(), vars.cend(), [](auto &&var) { return var.type_id == type_info::id(); }); return it == vars.cend() ? nullptr : static_cast(it->value.get()); } /*! @copydoc try_ctx */ template - Type * try_ctx() { + [[nodiscard]] Type * try_ctx() { return const_cast(std::as_const(*this).template try_ctx()); } @@ -1638,7 +1639,7 @@ public: * @return A valid reference to the object in the context of the registry. */ template - const Type & ctx() const { + [[nodiscard]] const Type & ctx() const { const auto *instance = try_ctx(); ENTT_ASSERT(instance); return *instance; @@ -1646,7 +1647,7 @@ public: /*! @copydoc ctx */ template - Type & ctx() { + [[nodiscard]] Type & ctx() { return const_cast(std::as_const(*this).template ctx()); } diff --git a/src/entt/entity/runtime_view.hpp b/src/entt/entity/runtime_view.hpp index d5626768b..874e79457 100644 --- a/src/entt/entity/runtime_view.hpp +++ b/src/entt/entity/runtime_view.hpp @@ -74,7 +74,7 @@ class basic_runtime_view { } } - bool valid() const { + [[nodiscard]] bool valid() const { return std::all_of(pools->begin()++, pools->end(), [entt = *it](const auto *curr) { return curr->contains(entt); }); @@ -109,19 +109,19 @@ class basic_runtime_view { return operator--(), orig; } - bool operator==(const view_iterator &other) const ENTT_NOEXCEPT { + [[nodiscard]] bool operator==(const view_iterator &other) const ENTT_NOEXCEPT { return other.it == it; } - bool operator!=(const view_iterator &other) const ENTT_NOEXCEPT { + [[nodiscard]] bool operator!=(const view_iterator &other) const ENTT_NOEXCEPT { return !(*this == other); } - pointer operator->() const { + [[nodiscard]] pointer operator->() const { return it.operator->(); } - reference operator*() const { + [[nodiscard]] reference operator*() const { return *operator->(); } @@ -141,7 +141,7 @@ class basic_runtime_view { std::rotate(pools.begin(), it, pools.end()); } - bool valid() const { + [[nodiscard]] bool valid() const { return !pools.empty() && pools.front(); } @@ -157,7 +157,7 @@ public: * @brief Estimates the number of entities that have the given components. * @return Estimated number of entities that have the given components. */ - size_type size() const { + [[nodiscard]] size_type size() const { return valid() ? pools.front()->size() : size_type{}; } @@ -165,7 +165,7 @@ public: * @brief Checks if the view is definitely empty. * @return True if the view is definitely empty, false otherwise. */ - bool empty() const { + [[nodiscard]] bool empty() const { return !valid() || pools.front()->empty(); } @@ -183,7 +183,7 @@ public: * * @return An iterator to the first entity that has the given components. */ - iterator begin() const { + [[nodiscard]] iterator begin() const { iterator it{}; if(valid()) { @@ -208,7 +208,7 @@ public: * @return An iterator to the entity following the last entity that has the * given components. */ - iterator end() const { + [[nodiscard]] iterator end() const { iterator it{}; if(valid()) { @@ -223,7 +223,7 @@ public: * @param entt A valid entity identifier. * @return True if the view contains the given entity, false otherwise. */ - bool contains(const entity_type entt) const { + [[nodiscard]] bool contains(const entity_type entt) const { return valid() && std::all_of(pools.cbegin(), pools.cend(), [entt](const auto *view) { return view->find(entt) != view->end(); }); diff --git a/src/entt/entity/snapshot.hpp b/src/entt/entity/snapshot.hpp index 37465c40e..810253678 100644 --- a/src/entt/entity/snapshot.hpp +++ b/src/entt/entity/snapshot.hpp @@ -533,7 +533,7 @@ public: * @param entt An entity identifier. * @return True if `entity` is managed by the loader, false otherwise. */ - bool contains(entity_type entt) const ENTT_NOEXCEPT { + [[nodiscard]] bool contains(entity_type entt) const ENTT_NOEXCEPT { return (remloc.find(entt) != remloc.cend()); } @@ -542,7 +542,7 @@ public: * @param entt An entity identifier. * @return The local identifier if any, the null entity otherwise. */ - entity_type map(entity_type entt) const ENTT_NOEXCEPT { + [[nodiscard]] entity_type map(entity_type entt) const ENTT_NOEXCEPT { const auto it = remloc.find(entt); entity_type other = null; diff --git a/src/entt/entity/sparse_set.hpp b/src/entt/entity/sparse_set.hpp index 721247455..d92442e87 100644 --- a/src/entt/entity/sparse_set.hpp +++ b/src/entt/entity/sparse_set.hpp @@ -106,41 +106,41 @@ class sparse_set { return other.index - index; } - reference operator[](const difference_type value) const { + [[nodiscard]] reference operator[](const difference_type value) const { const auto pos = size_type(index-value-1); return (*packed)[pos]; } - bool operator==(const sparse_set_iterator &other) const ENTT_NOEXCEPT { + [[nodiscard]] bool operator==(const sparse_set_iterator &other) const ENTT_NOEXCEPT { return other.index == index; } - bool operator!=(const sparse_set_iterator &other) const ENTT_NOEXCEPT { + [[nodiscard]] bool operator!=(const sparse_set_iterator &other) const ENTT_NOEXCEPT { return !(*this == other); } - bool operator<(const sparse_set_iterator &other) const ENTT_NOEXCEPT { + [[nodiscard]] bool operator<(const sparse_set_iterator &other) const ENTT_NOEXCEPT { return index > other.index; } - bool operator>(const sparse_set_iterator &other) const ENTT_NOEXCEPT { + [[nodiscard]] bool operator>(const sparse_set_iterator &other) const ENTT_NOEXCEPT { return index < other.index; } - bool operator<=(const sparse_set_iterator &other) const ENTT_NOEXCEPT { + [[nodiscard]] bool operator<=(const sparse_set_iterator &other) const ENTT_NOEXCEPT { return !(*this > other); } - bool operator>=(const sparse_set_iterator &other) const ENTT_NOEXCEPT { + [[nodiscard]] bool operator>=(const sparse_set_iterator &other) const ENTT_NOEXCEPT { return !(*this < other); } - pointer operator->() const { + [[nodiscard]] pointer operator->() const { const auto pos = size_type(index-1); return &(*packed)[pos]; } - reference operator*() const { + [[nodiscard]] reference operator*() const { return *operator->(); } @@ -149,15 +149,15 @@ class sparse_set { index_type index; }; - auto page(const Entity entt) const ENTT_NOEXCEPT { + [[nodiscard]] auto page(const Entity entt) const ENTT_NOEXCEPT { return std::size_t{(to_integral(entt) & traits_type::entity_mask) / entt_per_page}; } - auto offset(const Entity entt) const ENTT_NOEXCEPT { + [[nodiscard]] auto offset(const Entity entt) const ENTT_NOEXCEPT { return std::size_t{to_integral(entt) & (entt_per_page - 1)}; } - page_type & assure(const std::size_t pos) { + [[nodiscard]] page_type & assure(const std::size_t pos) { if(!(pos < sparse.size())) { sparse.resize(pos+1); } @@ -210,7 +210,7 @@ public: * allocated space for. * @return Capacity of the sparse set. */ - size_type capacity() const ENTT_NOEXCEPT { + [[nodiscard]] size_type capacity() const ENTT_NOEXCEPT { return packed.capacity(); } @@ -235,7 +235,7 @@ public: * * @return Extent of the sparse set. */ - size_type extent() const ENTT_NOEXCEPT { + [[nodiscard]] size_type extent() const ENTT_NOEXCEPT { return sparse.size() * entt_per_page; } @@ -249,7 +249,7 @@ public: * * @return Number of elements. */ - size_type size() const ENTT_NOEXCEPT { + [[nodiscard]] size_type size() const ENTT_NOEXCEPT { return packed.size(); } @@ -257,7 +257,7 @@ public: * @brief Checks whether a sparse set is empty. * @return True if the sparse set is empty, false otherwise. */ - bool empty() const ENTT_NOEXCEPT { + [[nodiscard]] bool empty() const ENTT_NOEXCEPT { return packed.empty(); } @@ -273,7 +273,7 @@ public: * * @return A pointer to the internal packed array. */ - const entity_type * data() const ENTT_NOEXCEPT { + [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT { return packed.data(); } @@ -290,7 +290,7 @@ public: * * @return An iterator to the first entity of the internal packed array. */ - iterator begin() const ENTT_NOEXCEPT { + [[nodiscard]] iterator begin() const ENTT_NOEXCEPT { const typename traits_type::difference_type pos = packed.size(); return iterator{packed, pos}; } @@ -309,7 +309,7 @@ public: * @return An iterator to the element following the last entity of the * internal packed array. */ - iterator end() const ENTT_NOEXCEPT { + [[nodiscard]] iterator end() const ENTT_NOEXCEPT { return iterator{packed, {}}; } @@ -319,7 +319,7 @@ public: * @return An iterator to the given entity if it's found, past the end * iterator otherwise. */ - iterator find(const entity_type entt) const { + [[nodiscard]] iterator find(const entity_type entt) const { return contains(entt) ? --(end() - index(entt)) : end(); } @@ -328,7 +328,7 @@ public: * @param entt A valid entity identifier. * @return True if the sparse set contains the entity, false otherwise. */ - bool contains(const entity_type entt) const { + [[nodiscard]] bool contains(const entity_type entt) const { const auto curr = page(entt); // testing against null permits to avoid accessing the packed array return (curr < sparse.size() && sparse[curr] && sparse[curr][offset(entt)] != null); @@ -346,7 +346,7 @@ public: * @param entt A valid entity identifier. * @return The position of the entity in the sparse set. */ - size_type index(const entity_type entt) const { + [[nodiscard]] size_type index(const entity_type entt) const { ENTT_ASSERT(contains(entt)); return size_type(sparse[page(entt)][offset(entt)]); } diff --git a/src/entt/entity/storage.hpp b/src/entt/entity/storage.hpp index e2155f32c..29622013d 100644 --- a/src/entt/entity/storage.hpp +++ b/src/entt/entity/storage.hpp @@ -111,41 +111,41 @@ class storage: public sparse_set { return other.index - index; } - reference operator[](const difference_type value) const ENTT_NOEXCEPT { + [[nodiscard]] reference operator[](const difference_type value) const ENTT_NOEXCEPT { const auto pos = size_type(index-value-1); return (*instances)[pos]; } - bool operator==(const storage_iterator &other) const ENTT_NOEXCEPT { + [[nodiscard]] bool operator==(const storage_iterator &other) const ENTT_NOEXCEPT { return other.index == index; } - bool operator!=(const storage_iterator &other) const ENTT_NOEXCEPT { + [[nodiscard]] bool operator!=(const storage_iterator &other) const ENTT_NOEXCEPT { return !(*this == other); } - bool operator<(const storage_iterator &other) const ENTT_NOEXCEPT { + [[nodiscard]] bool operator<(const storage_iterator &other) const ENTT_NOEXCEPT { return index > other.index; } - bool operator>(const storage_iterator &other) const ENTT_NOEXCEPT { + [[nodiscard]] bool operator>(const storage_iterator &other) const ENTT_NOEXCEPT { return index < other.index; } - bool operator<=(const storage_iterator &other) const ENTT_NOEXCEPT { + [[nodiscard]] bool operator<=(const storage_iterator &other) const ENTT_NOEXCEPT { return !(*this > other); } - bool operator>=(const storage_iterator &other) const ENTT_NOEXCEPT { + [[nodiscard]] bool operator>=(const storage_iterator &other) const ENTT_NOEXCEPT { return !(*this < other); } - pointer operator->() const ENTT_NOEXCEPT { + [[nodiscard]] pointer operator->() const ENTT_NOEXCEPT { const auto pos = size_type(index-1); return &(*instances)[pos]; } - reference operator*() const ENTT_NOEXCEPT { + [[nodiscard]] reference operator*() const ENTT_NOEXCEPT { return *operator->(); } @@ -197,12 +197,12 @@ public: * * @return A pointer to the array of objects. */ - const object_type * raw() const ENTT_NOEXCEPT { + [[nodiscard]] const object_type * raw() const ENTT_NOEXCEPT { return instances.data(); } /*! @copydoc raw */ - object_type * raw() ENTT_NOEXCEPT { + [[nodiscard]] object_type * raw() ENTT_NOEXCEPT { return const_cast(std::as_const(*this).raw()); } @@ -218,18 +218,18 @@ public: * * @return An iterator to the first instance of the given type. */ - const_iterator cbegin() const ENTT_NOEXCEPT { + [[nodiscard]] const_iterator cbegin() const ENTT_NOEXCEPT { const typename traits_type::difference_type pos = underlying_type::size(); return const_iterator{instances, pos}; } /*! @copydoc cbegin */ - const_iterator begin() const ENTT_NOEXCEPT { + [[nodiscard]] const_iterator begin() const ENTT_NOEXCEPT { return cbegin(); } /*! @copydoc begin */ - iterator begin() ENTT_NOEXCEPT { + [[nodiscard]] iterator begin() ENTT_NOEXCEPT { const typename traits_type::difference_type pos = underlying_type::size(); return iterator{instances, pos}; } @@ -248,17 +248,17 @@ public: * @return An iterator to the element following the last instance of the * given type. */ - const_iterator cend() const ENTT_NOEXCEPT { + [[nodiscard]] const_iterator cend() const ENTT_NOEXCEPT { return const_iterator{instances, {}}; } /*! @copydoc cend */ - const_iterator end() const ENTT_NOEXCEPT { + [[nodiscard]] const_iterator end() const ENTT_NOEXCEPT { return cend(); } /*! @copydoc end */ - iterator end() ENTT_NOEXCEPT { + [[nodiscard]] iterator end() ENTT_NOEXCEPT { return iterator{instances, {}}; } @@ -274,12 +274,12 @@ public: * @param entt A valid entity identifier. * @return The object associated with the entity. */ - const object_type & get(const entity_type entt) const { + [[nodiscard]] const object_type & get(const entity_type entt) const { return instances[underlying_type::index(entt)]; } /*! @copydoc get */ - object_type & get(const entity_type entt) { + [[nodiscard]] object_type & get(const entity_type entt) { return const_cast(std::as_const(*this).get(entt)); } @@ -288,12 +288,12 @@ public: * @param entt A valid entity identifier. * @return The object associated with the entity, if any. */ - const object_type * try_get(const entity_type entt) const { + [[nodiscard]] const object_type * try_get(const entity_type entt) const { return underlying_type::contains(entt) ? (instances.data() + underlying_type::index(entt)) : nullptr; } /*! @copydoc try_get */ - object_type * try_get(const entity_type entt) { + [[nodiscard]] object_type * try_get(const entity_type entt) { return const_cast(std::as_const(*this).try_get(entt)); } diff --git a/src/entt/entity/view.hpp b/src/entt/entity/view.hpp index 9180cf051..2f2674f9a 100644 --- a/src/entt/entity/view.hpp +++ b/src/entt/entity/view.hpp @@ -101,7 +101,7 @@ class basic_view, Component...> { } } - bool valid() const { + [[nodiscard]] bool valid() const { return std::all_of(unchecked.cbegin(), unchecked.cend(), [entt = *it](const sparse_set *curr) { return curr->contains(entt); }) && std::none_of(filter.cbegin(), filter.cend(), [entt = *it](const sparse_set *curr) { return curr->contains(entt); }); } @@ -135,19 +135,19 @@ class basic_view, Component...> { return operator--(), orig; } - bool operator==(const view_iterator &other) const ENTT_NOEXCEPT { + [[nodiscard]] bool operator==(const view_iterator &other) const ENTT_NOEXCEPT { return other.it == it; } - bool operator!=(const view_iterator &other) const ENTT_NOEXCEPT { + [[nodiscard]] bool operator!=(const view_iterator &other) const ENTT_NOEXCEPT { return !(*this == other); } - pointer operator->() const { + [[nodiscard]] pointer operator->() const { return it.operator->(); } - reference operator*() const { + [[nodiscard]] reference operator*() const { return *operator->(); } @@ -162,13 +162,13 @@ class basic_view, Component...> { : pools{&component..., &epool...} {} - const sparse_set & candidate() const ENTT_NOEXCEPT { + [[nodiscard]] const sparse_set & candidate() const ENTT_NOEXCEPT { return *std::min({ static_cast *>(std::get *>(pools))... }, [](const auto *lhs, const auto *rhs) { return lhs->size() < rhs->size(); }); } - unchecked_type unchecked(const sparse_set &view) const { + [[nodiscard]] unchecked_type unchecked(const sparse_set &view) const { std::size_t pos{}; unchecked_type other{}; ((std::get *>(pools) == &view ? nullptr : (other[pos++] = std::get *>(pools))), ...); @@ -176,7 +176,7 @@ class basic_view, Component...> { } template - decltype(auto) get([[maybe_unused]] component_iterator it, [[maybe_unused]] pool_type *cpool, [[maybe_unused]] const Entity entt) const { + [[nodiscard]] decltype(auto) get([[maybe_unused]] component_iterator it, [[maybe_unused]] pool_type *cpool, [[maybe_unused]] const Entity entt) const { if constexpr(std::is_same_v) { return *it; } else { @@ -230,7 +230,7 @@ public: * @return Number of existing components of the given type. */ template - size_type size() const ENTT_NOEXCEPT { + [[nodiscard]] size_type size() const ENTT_NOEXCEPT { return std::get *>(pools)->size(); } @@ -238,7 +238,7 @@ public: * @brief Estimates the number of entities iterated by the view. * @return Estimated number of entities iterated by the view. */ - size_type size() const ENTT_NOEXCEPT { + [[nodiscard]] size_type size() const ENTT_NOEXCEPT { return std::min({ std::get *>(pools)->size()... }); } @@ -253,7 +253,7 @@ public: * @return True if the view or the pools are empty, false otherwise. */ template - bool empty() const ENTT_NOEXCEPT { + [[nodiscard]] bool empty() const ENTT_NOEXCEPT { if constexpr(sizeof...(Comp) == 0) { return (std::get *>(pools)->empty() || ...); } else { @@ -276,7 +276,7 @@ public: * @return A pointer to the array of components. */ template - Comp * raw() const ENTT_NOEXCEPT { + [[nodiscard]] Comp * raw() const ENTT_NOEXCEPT { return std::get *>(pools)->raw(); } @@ -295,7 +295,7 @@ public: * @return A pointer to the array of entities. */ template - const entity_type * data() const ENTT_NOEXCEPT { + [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT { return std::get *>(pools)->data(); } @@ -313,7 +313,7 @@ public: * * @return An iterator to the first entity that has the given components. */ - iterator begin() const { + [[nodiscard]] iterator begin() const { const auto &view = candidate(); const filter_type ignore{std::get *>(pools)...}; return iterator{view, unchecked(view), ignore, view.begin()}; @@ -334,7 +334,7 @@ public: * @return An iterator to the entity following the last entity that has the * given components. */ - iterator end() const { + [[nodiscard]] iterator end() const { const auto &view = candidate(); const filter_type ignore{std::get *>(pools)...}; return iterator{view, unchecked(view), ignore, view.end()}; @@ -345,7 +345,7 @@ public: * @return The first entity that has the given components if one exists, the * null entity otherwise. */ - entity_type front() const { + [[nodiscard]] entity_type front() const { const auto it = begin(); return it != end() ? *it : null; } @@ -355,7 +355,7 @@ public: * @return The last entity that has the given components if one exists, the * null entity otherwise. */ - entity_type back() const { + [[nodiscard]] entity_type back() const { const auto it = std::make_reverse_iterator(end()); return it != std::make_reverse_iterator(begin()) ? *it : null; } @@ -366,7 +366,7 @@ public: * @return An iterator to the given entity if it's found, past the end * iterator otherwise. */ - iterator find(const entity_type entt) const { + [[nodiscard]] iterator find(const entity_type entt) const { const auto &view = candidate(); const filter_type ignore{std::get *>(pools)...}; iterator it{view, unchecked(view), ignore, view.find(entt)}; @@ -378,7 +378,7 @@ public: * @param entt A valid entity identifier. * @return True if the view contains the given entity, false otherwise. */ - bool contains(const entity_type entt) const { + [[nodiscard]] bool contains(const entity_type entt) const { return (std::get *>(pools)->contains(entt) && ...) && (!std::get *>(pools)->contains(entt) && ...); } @@ -401,7 +401,7 @@ public: * @return The components assigned to the entity. */ template - decltype(auto) get([[maybe_unused]] const entity_type entt) const { + [[nodiscard]] decltype(auto) get([[maybe_unused]] const entity_type entt) const { ENTT_ASSERT(contains(entt)); if constexpr(sizeof...(Comp) == 0) { @@ -525,7 +525,7 @@ public: * @brief Returns the number of entities that have the given component. * @return Number of entities that have the given component. */ - size_type size() const ENTT_NOEXCEPT { + [[nodiscard]] size_type size() const ENTT_NOEXCEPT { return pool->size(); } @@ -533,7 +533,7 @@ public: * @brief Checks whether a view is empty. * @return True if the view is empty, false otherwise. */ - bool empty() const ENTT_NOEXCEPT { + [[nodiscard]] bool empty() const ENTT_NOEXCEPT { return pool->empty(); } @@ -549,7 +549,7 @@ public: * * @return A pointer to the array of components. */ - raw_type * raw() const ENTT_NOEXCEPT { + [[nodiscard]] raw_type * raw() const ENTT_NOEXCEPT { return pool->raw(); } @@ -565,7 +565,7 @@ public: * * @return A pointer to the array of entities. */ - const entity_type * data() const ENTT_NOEXCEPT { + [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT { return pool->data(); } @@ -583,7 +583,7 @@ public: * * @return An iterator to the first entity that has the given component. */ - iterator begin() const ENTT_NOEXCEPT { + [[nodiscard]] iterator begin() const ENTT_NOEXCEPT { return pool->sparse_set::begin(); } @@ -602,7 +602,7 @@ public: * @return An iterator to the entity following the last entity that has the * given component. */ - iterator end() const ENTT_NOEXCEPT { + [[nodiscard]] iterator end() const ENTT_NOEXCEPT { return pool->sparse_set::end(); } @@ -611,7 +611,7 @@ public: * @return The first entity that has the given component if one exists, the * null entity otherwise. */ - entity_type front() const { + [[nodiscard]] entity_type front() const { const auto it = begin(); return it != end() ? *it : null; } @@ -621,7 +621,7 @@ public: * @return The last entity that has the given component if one exists, the * null entity otherwise. */ - entity_type back() const { + [[nodiscard]] entity_type back() const { const auto it = std::make_reverse_iterator(end()); return it != std::make_reverse_iterator(begin()) ? *it : null; } @@ -632,7 +632,7 @@ public: * @return An iterator to the given entity if it's found, past the end * iterator otherwise. */ - iterator find(const entity_type entt) const { + [[nodiscard]] iterator find(const entity_type entt) const { const auto it = pool->find(entt); return it != end() && *it == entt ? it : end(); } @@ -642,7 +642,7 @@ public: * @param pos Position of the element to return. * @return The identifier that occupies the given position. */ - entity_type operator[](const size_type pos) const { + [[nodiscard]] entity_type operator[](const size_type pos) const { return begin()[pos]; } @@ -651,7 +651,7 @@ public: * @param entt A valid entity identifier. * @return True if the view contains the given entity, false otherwise. */ - bool contains(const entity_type entt) const { + [[nodiscard]] bool contains(const entity_type entt) const { return pool->contains(entt); } @@ -671,7 +671,7 @@ public: * @return The component assigned to the entity. */ template - decltype(auto) get(const entity_type entt) const { + [[nodiscard]] decltype(auto) get(const entity_type entt) const { static_assert(std::is_same_v, "Invalid component type"); ENTT_ASSERT(contains(entt)); return pool->get(entt); diff --git a/test/benchmark/benchmark.cpp b/test/benchmark/benchmark.cpp index 45784e2f8..44e3b04ad 100644 --- a/test/benchmark/benchmark.cpp +++ b/test/benchmark/benchmark.cpp @@ -148,7 +148,7 @@ TEST(Benchmark, IterateSingleComponent1M) { registry.emplace(entity); } - auto test = [®istry](auto func) { + auto test = [&](auto func) { timer timer; registry.view().each(func); timer.elapsed(); @@ -169,7 +169,7 @@ TEST(Benchmark, IterateSingleComponentRuntime1M) { registry.emplace(entity); } - auto test = [®istry](auto func) { + auto test = [&](auto func) { ENTT_ID_TYPE types[] = { entt::type_info::id() }; timer timer; @@ -193,7 +193,7 @@ TEST(Benchmark, IterateTwoComponents1M) { registry.emplace(entity); } - auto test = [®istry](auto func) { + auto test = [&](auto func) { timer timer; registry.view().each(func); timer.elapsed(); @@ -218,7 +218,7 @@ TEST(Benchmark, IterateTwoComponents1MHalf) { } } - auto test = [®istry](auto func) { + auto test = [&](auto func) { timer timer; registry.view().each(func); timer.elapsed(); @@ -243,7 +243,7 @@ TEST(Benchmark, IterateTwoComponents1MOne) { } } - auto test = [®istry](auto func) { + auto test = [&](auto func) { timer timer; registry.view().each(func); timer.elapsed(); @@ -256,7 +256,7 @@ TEST(Benchmark, IterateTwoComponents1MOne) { TEST(Benchmark, IterateTwoComponentsNonOwningGroup1M) { entt::registry registry; - registry.group<>(entt::get); + const auto group = registry.group<>(entt::get); std::cout << "Iterating over 1000000 entities, two components, non owning group" << std::endl; @@ -266,9 +266,9 @@ TEST(Benchmark, IterateTwoComponentsNonOwningGroup1M) { registry.emplace(entity); } - auto test = [®istry](auto func) { + auto test = [&](auto func) { timer timer; - registry.group<>(entt::get).each(func); + group.each(func); timer.elapsed(); }; @@ -279,7 +279,7 @@ TEST(Benchmark, IterateTwoComponentsNonOwningGroup1M) { TEST(Benchmark, IterateTwoComponentsFullOwningGroup1M) { entt::registry registry; - registry.group(); + const auto group = registry.group(); std::cout << "Iterating over 1000000 entities, two components, full owning group" << std::endl; @@ -289,9 +289,9 @@ TEST(Benchmark, IterateTwoComponentsFullOwningGroup1M) { registry.emplace(entity); } - auto test = [®istry](auto func) { + auto test = [&](auto func) { timer timer; - registry.group().each(func); + group.each(func); timer.elapsed(); }; @@ -302,7 +302,7 @@ TEST(Benchmark, IterateTwoComponentsFullOwningGroup1M) { TEST(Benchmark, IterateTwoComponentsPartialOwningGroup1M) { entt::registry registry; - registry.group(entt::get); + const auto group = registry.group(entt::get); std::cout << "Iterating over 1000000 entities, two components, partial owning group" << std::endl; @@ -312,9 +312,9 @@ TEST(Benchmark, IterateTwoComponentsPartialOwningGroup1M) { registry.emplace(entity); } - auto test = [®istry](auto func) { + auto test = [&](auto func) { timer timer; - registry.group(entt::get).each(func); + group.each(func); timer.elapsed(); }; @@ -334,7 +334,7 @@ TEST(Benchmark, IterateTwoComponentsRuntime1M) { registry.emplace(entity); } - auto test = [®istry](auto func) { + auto test = [&](auto func) { ENTT_ID_TYPE types[] = { entt::type_info::id(), entt::type_info::id() @@ -365,7 +365,7 @@ TEST(Benchmark, IterateTwoComponentsRuntime1MHalf) { } } - auto test = [®istry](auto func) { + auto test = [&](auto func) { ENTT_ID_TYPE types[] = { entt::type_info::id(), entt::type_info::id() @@ -396,7 +396,7 @@ TEST(Benchmark, IterateTwoComponentsRuntime1MOne) { } } - auto test = [®istry](auto func) { + auto test = [&](auto func) { ENTT_ID_TYPE types[] = { entt::type_info::id(), entt::type_info::id() @@ -425,7 +425,7 @@ TEST(Benchmark, IterateThreeComponents1M) { registry.emplace>(entity); } - auto test = [®istry](auto func) { + auto test = [&](auto func) { timer timer; registry.view>().each(func); timer.elapsed(); @@ -451,7 +451,7 @@ TEST(Benchmark, IterateThreeComponents1MHalf) { } } - auto test = [®istry](auto func) { + auto test = [&](auto func) { timer timer; registry.view>().each(func); timer.elapsed(); @@ -477,7 +477,7 @@ TEST(Benchmark, IterateThreeComponents1MOne) { } } - auto test = [®istry](auto func) { + auto test = [&](auto func) { timer timer; registry.view>().each(func); timer.elapsed(); @@ -490,7 +490,7 @@ TEST(Benchmark, IterateThreeComponents1MOne) { TEST(Benchmark, IterateThreeComponentsNonOwningGroup1M) { entt::registry registry; - registry.group<>(entt::get>); + const auto group = registry.group<>(entt::get>); std::cout << "Iterating over 1000000 entities, three components, non owning group" << std::endl; @@ -501,9 +501,9 @@ TEST(Benchmark, IterateThreeComponentsNonOwningGroup1M) { registry.emplace>(entity); } - auto test = [®istry](auto func) { + auto test = [&](auto func) { timer timer; - registry.group<>(entt::get>).each(func); + group.each(func); timer.elapsed(); }; @@ -514,7 +514,7 @@ TEST(Benchmark, IterateThreeComponentsNonOwningGroup1M) { TEST(Benchmark, IterateThreeComponentsFullOwningGroup1M) { entt::registry registry; - registry.group>(); + const auto group = registry.group>(); std::cout << "Iterating over 1000000 entities, three components, full owning group" << std::endl; @@ -525,9 +525,9 @@ TEST(Benchmark, IterateThreeComponentsFullOwningGroup1M) { registry.emplace>(entity); } - auto test = [®istry](auto func) { + auto test = [&](auto func) { timer timer; - registry.group>().each(func); + group.each(func); timer.elapsed(); }; @@ -538,7 +538,7 @@ TEST(Benchmark, IterateThreeComponentsFullOwningGroup1M) { TEST(Benchmark, IterateThreeComponentsPartialOwningGroup1M) { entt::registry registry; - registry.group(entt::get>); + const auto group = registry.group(entt::get>); std::cout << "Iterating over 1000000 entities, three components, partial owning group" << std::endl; @@ -549,9 +549,9 @@ TEST(Benchmark, IterateThreeComponentsPartialOwningGroup1M) { registry.emplace>(entity); } - auto test = [®istry](auto func) { + auto test = [&](auto func) { timer timer; - registry.group(entt::get>).each(func); + group.each(func); timer.elapsed(); }; @@ -572,7 +572,7 @@ TEST(Benchmark, IterateThreeComponentsRuntime1M) { registry.emplace>(entity); } - auto test = [®istry](auto func) { + auto test = [&](auto func) { ENTT_ID_TYPE types[] = { entt::type_info::id(), entt::type_info::id(), @@ -606,7 +606,7 @@ TEST(Benchmark, IterateThreeComponentsRuntime1MHalf) { } } - auto test = [®istry](auto func) { + auto test = [&](auto func) { ENTT_ID_TYPE types[] = { entt::type_info::id(), entt::type_info::id(), @@ -640,7 +640,7 @@ TEST(Benchmark, IterateThreeComponentsRuntime1MOne) { } } - auto test = [®istry](auto func) { + auto test = [&](auto func) { ENTT_ID_TYPE types[] = { entt::type_info::id(), entt::type_info::id(), @@ -673,7 +673,7 @@ TEST(Benchmark, IterateFiveComponents1M) { registry.emplace>(entity); } - auto test = [®istry](auto func) { + auto test = [&](auto func) { timer timer; registry.view, comp<1>, comp<2>>().each(func); timer.elapsed(); @@ -701,7 +701,7 @@ TEST(Benchmark, IterateFiveComponents1MHalf) { } } - auto test = [®istry](auto func) { + auto test = [&](auto func) { timer timer; registry.view, comp<1>, comp<2>>().each(func); timer.elapsed(); @@ -729,7 +729,7 @@ TEST(Benchmark, IterateFiveComponents1MOne) { } } - auto test = [®istry](auto func) { + auto test = [&](auto func) { timer timer; registry.view, comp<1>, comp<2>>().each(func); timer.elapsed(); @@ -742,7 +742,7 @@ TEST(Benchmark, IterateFiveComponents1MOne) { TEST(Benchmark, IterateFiveComponentsNonOwningGroup1M) { entt::registry registry; - registry.group<>(entt::get, comp<1>, comp<2>>); + const auto group = registry.group<>(entt::get, comp<1>, comp<2>>); std::cout << "Iterating over 1000000 entities, five components, non owning group" << std::endl; @@ -755,9 +755,9 @@ TEST(Benchmark, IterateFiveComponentsNonOwningGroup1M) { registry.emplace>(entity); } - auto test = [®istry](auto func) { + auto test = [&](auto func) { timer timer; - registry.group<>(entt::get, comp<1>, comp<2>>).each(func); + group.each(func); timer.elapsed(); }; @@ -768,7 +768,7 @@ TEST(Benchmark, IterateFiveComponentsNonOwningGroup1M) { TEST(Benchmark, IterateFiveComponentsFullOwningGroup1M) { entt::registry registry; - registry.group, comp<1>, comp<2>>(); + const auto group = registry.group, comp<1>, comp<2>>(); std::cout << "Iterating over 1000000 entities, five components, full owning group" << std::endl; @@ -781,9 +781,9 @@ TEST(Benchmark, IterateFiveComponentsFullOwningGroup1M) { registry.emplace>(entity); } - auto test = [®istry](auto func) { + auto test = [&](auto func) { timer timer; - registry.group, comp<1>, comp<2>>().each(func); + group.each(func); timer.elapsed(); }; @@ -794,7 +794,7 @@ TEST(Benchmark, IterateFiveComponentsFullOwningGroup1M) { TEST(Benchmark, IterateFiveComponentsPartialFourOfFiveOwningGroup1M) { entt::registry registry; - registry.group, comp<1>>(entt::get>); + const auto group = registry.group, comp<1>>(entt::get>); std::cout << "Iterating over 1000000 entities, five components, partial (4 of 5) owning group" << std::endl; @@ -807,9 +807,9 @@ TEST(Benchmark, IterateFiveComponentsPartialFourOfFiveOwningGroup1M) { registry.emplace>(entity); } - auto test = [®istry](auto func) { + auto test = [&](auto func) { timer timer; - registry.group, comp<1>>(entt::get>).each(func); + group.each(func); timer.elapsed(); }; @@ -820,7 +820,7 @@ TEST(Benchmark, IterateFiveComponentsPartialFourOfFiveOwningGroup1M) { TEST(Benchmark, IterateFiveComponentsPartialThreeOfFiveOwningGroup1M) { entt::registry registry; - registry.group>(entt::get, comp<2>>); + const auto group = registry.group>(entt::get, comp<2>>); std::cout << "Iterating over 1000000 entities, five components, partial (3 of 5) owning group" << std::endl; @@ -833,9 +833,9 @@ TEST(Benchmark, IterateFiveComponentsPartialThreeOfFiveOwningGroup1M) { registry.emplace>(entity); } - auto test = [®istry](auto func) { + auto test = [&](auto func) { timer timer; - registry.group>(entt::get, comp<2>>).each(func); + group.each(func); timer.elapsed(); }; @@ -858,7 +858,7 @@ TEST(Benchmark, IterateFiveComponentsRuntime1M) { registry.emplace>(entity); } - auto test = [®istry](auto func) { + auto test = [&](auto func) { ENTT_ID_TYPE types[] = { entt::type_info::id(), entt::type_info::id(), @@ -898,7 +898,7 @@ TEST(Benchmark, IterateFiveComponentsRuntime1MHalf) { } } - auto test = [®istry](auto func) { + auto test = [&](auto func) { ENTT_ID_TYPE types[] = { entt::type_info::id(), entt::type_info::id(), @@ -938,7 +938,7 @@ TEST(Benchmark, IterateFiveComponentsRuntime1MOne) { } } - auto test = [®istry](auto func) { + auto test = [&](auto func) { ENTT_ID_TYPE types[] = { entt::type_info::id(), entt::type_info::id(), diff --git a/test/entt/entity/group.cpp b/test/entt/entity/group.cpp index 79951e48b..49f239f98 100644 --- a/test/entt/entity/group.cpp +++ b/test/entt/entity/group.cpp @@ -511,12 +511,12 @@ TEST(NonOwningGroup, FrontBack) { TEST(NonOwningGroup, SignalRace) { entt::registry registry; registry.on_construct().connect<&entt::registry::emplace_or_replace>(); - registry.group(entt::get); + const auto group = registry.group(entt::get); auto entity = registry.create(); registry.emplace(entity); - ASSERT_EQ(registry.group(entt::get).size(), 1u); + ASSERT_EQ(group.size(), 1u); } TEST(OwningGroup, Functionalities) { @@ -1106,12 +1106,12 @@ TEST(OwningGroup, FrontBack) { TEST(OwningGroup, SignalRace) { entt::registry registry; registry.on_construct().connect<&entt::registry::emplace_or_replace>(); - registry.group(entt::get); + const auto group = registry.group(entt::get); auto entity = registry.create(); registry.emplace(entity); - ASSERT_EQ(registry.group(entt::get).size(), 1u); + ASSERT_EQ(group.size(), 1u); } TEST(OwningGroup, StableLateInitialization) { diff --git a/test/entt/entity/registry.cpp b/test/entt/entity/registry.cpp index 1e6a458f7..bf0ef5a0c 100644 --- a/test/entt/entity/registry.cpp +++ b/test/entt/entity/registry.cpp @@ -55,7 +55,8 @@ TEST(Registry, Context) { registry.set(); registry.set(); - registry.ctx_or_set(); + // suppress the warning due to the [[nodiscard]] attribute + static_cast(registry.ctx_or_set()); ASSERT_NE(registry.try_ctx(), nullptr); ASSERT_NE(registry.try_ctx(), nullptr);