Function emplace_or_replace

Synopsis

#include <src/entt/entity/registry.hpp>

template <typename Component, typename... Args>
decltype(auto) emplace_or_replace(const entity_type entity, Args &&... args)

Description

Assigns or replaces the given component for an entity.

Equivalent to the following snippet (pseudocode):

auto &component = registry.has<Component>(entity) ? registry.replace<Component>(entity, args...) : registry.emplace<Component>(entity, args...);

Prefer this function anyway because it has slightly better performance.

Warning
Attempting to use an invalid entity results in undefined behavior.
Template Parameters

Component - Type of component to assign or replace.

Args - Types of arguments to use to construct the component.

Parameters

entity - A valid entity identifier.

args - Parameters to use to initialize the component.

Return
A reference to the newly created component.

Source

Lines 622-630 in src/entt/entity/registry.hpp.

template<typename Component, typename... Args>
decltype(auto) emplace_or_replace(const entity_type entity, Args &&... args) {
    ENTT_ASSERT(valid(entity));
    auto &cpool = assure<Component>();
    return cpool.contains(entity)
        ? cpool.patch(*this, entity, [&args...](auto &curr) { curr = Component{std::forward<Args>(args)...}; })
        : cpool.emplace(*this, entity, std::forward<Args>(args)...);
}





Add Discussion

Log in to comment