Skip to content

Engine → ECS → command_buffer.h

command_buffer

Syntax

class CONJURE_API command_buffer

Constructors

AccessSignatureDescription
publicexplicit command_buffer( world &target);
publiccommand_buffer( command_buffer &&other) noexcept;

Important

Copy constructor and assignment has been deleted.

Destructors

AccessSignatureDescription
public~command_buffer();Discards pending commands with a WARN (payloads are destroyed) and returns unused ids. The ids of a pending instantiation are released with a bumped generation, as in clear().

Variables

AccessNameTypeDefaultDescription
privatemArenacore::memory::arena—
privatemCountu320
privatemFirstrecord *nullptr
privatemInstantiatesstd::vector<instantiate_entry>{}
privatemLastrecord *nullptr
privatemRegistryconst component_registry *nullptr
privatemReservedstd::array<entity, RESERVE_BLOCK>{}
privatemReservedCountu320
privatemWorldworld *nullptr
publicRESERVE_BLOCKstatic constexpr u3264Ids reserved per refill.

Functions

Public

add

template <typename ComponentType> void add(
    entity target,
    ComponentType value = {});

Records adding a component with an initial value (moved in). On an entity that already has it the value is applied like set(). An unregistered type asserts and records nothing.

add_raw

void add_raw(
    entity target,
    component_id id,
    std::span<const std::byte> payload = {});

Untyped add: the payload bytes are copy-constructed into the record (ops.copy or memcpy). An empty payload records no value: an entity that gains the component at flush gets its default value; one that holds it at flush (a remove_raw earlier in the same buffer included) keeps its current value, and no observer fires for it. Pass the default bytes to reset a held component.

append

void append(
    command_buffer &&other);

Moves every command of other behind this buffer's commands, in order. other keeps its reserved ids and is empty afterwards.

clear

void clear() noexcept;

Drops every command, destroying payloads that were not applied. The ids reserved by an instantiation that was never applied (cleared, or skipped by a failed flush) are released with a bumped generation, so the root instantiate() handed out stays dead and never aliases a later entity. That writes the world's record table, so clear a buffer holding an instantiation on the world's owner thread: off it, clear() asserts and leaves those ids reserved for good (the root still never aliases). Unused reserved ids are kept, and so is room for the records: a recording that outgrew the arena's 16 KiB block makes the buffer keep one block spanning the blocks that recording used (its bytes rounded up to whole 16 KiB blocks, like a vector's capacity: at most one block more than it needed), so repeating a recording of the same size allocates nothing after the next one. The room is kept for the buffer's lifetime unless shrink_to_fit() returns it.

command_count

[[nodiscard]] u32 command_count() const noexcept;

Number of recorded commands.

create_entity

[[nodiscard]] entity create_entity();

Reserves a real entity id that becomes alive at flush. The id may be used in later commands of any buffer of the same world and handed to scripts at once. Returns NULL_ENTITY (and logs) when the id space is exhausted.

destroy_entity

void destroy_entity(
    entity target);

Records the destruction of an entity. Wins over every other command on it in this buffer.

empty

[[nodiscard]] bool empty() const noexcept;

True when no command is recorded.

instantiate

[[nodiscard]] core::result<entity> instantiate(
    std::shared_ptr<const entity_archive> archive);

Records instantiating one copy of an archive at flush; ids are reserved now and the root is returned now. ARCHIVE_CORRUPT when the archive does not validate, ENTITY_LIMIT_REACHED when no ids are left.

remove

template <typename ComponentType> void remove(
    entity target);

Records removing a component. Cancels a pending add of the same component in this buffer. An unregistered type asserts and records nothing.

remove_raw

void remove_raw(
    entity target,
    component_id id);

Untyped remove.

reserved_count

[[nodiscard]] u32 reserved_count() const noexcept;

Ids reserved and not yet handed out by create_entity().

set

template <typename ComponentType> void set(
    entity target,
    ComponentType value);

Records replacing the value of a component the entity has at flush (a missing component is skipped and counted). An unregistered type asserts and records nothing.

set_raw

void set_raw(
    entity target,
    component_id id,
    std::span<const std::byte> payload);

Untyped set.

shrink_to_fit

void shrink_to_fit() noexcept;

Returns the room an earlier recording left behind to the heap: the arena's blocks and the capacity of the instantiation list. Only an empty buffer has room to give back; with commands pending the call changes nothing. Reserved ids are kept either way. The next recording acquires a fresh block, so call it when the buffer goes idle for good (a removed system, a cleared world), not between frames.

target

[[nodiscard]] world &target() const noexcept;

The world this buffer targets.

Private

destroy_payloads

void destroy_payloads() noexcept;

payload_size

template <typename ComponentType> [[nodiscard]] static constexpr u32 payload_size() noexcept;

push_instantiate

[[nodiscard]] core::result<std::span<const entity>> push_instantiate(
    std::shared_ptr<const entity_archive> archive,
    u32 copies);

push_plain_record

record *push_plain_record(
    opcode op,
    entity target,
    component_id id);

push_record

[[nodiscard]] void *push_record(
    opcode op,
    entity target,
    component_id id,
    u32 payloadSize,
    u32 alignment);

recycle_arena

void recycle_arena() noexcept;

refill_reserved

[[nodiscard]] bool refill_reserved();

return_reserved

void return_reserved() noexcept;

Operator Overloads

Public

operator=

command_buffer &operator=(
    command_buffer &&other) noexcept;