Skip to content

Engine → ECS → world.h

world

Syntax

class CONJURE_API world

Constructors

AccessSignatureDescription
publicexplicit world( const component_registry &registry, world_desc desc = {});

Important

Copy constructor and assignment has been deleted. Move constructors have been deleted. Move assignment has been deleted.

Destructors

AccessSignatureDescription
public~world();Destroys every entity without firing observers and returns every chunk.

Variables

AccessNameTypeDefaultDescription
privateFLUSH_INLINE_PAYLOADSstatic constexpr usize4Payload records a flush plan entry stores inline: a spawn with up to this many add/set payloads costs the plan no heap allocation.
privatemAliveCountu320
privatemAllocatorstd::unique_ptr<entity_allocator>{}
privatemArchetypeByMaskstd::unordered_map<component_mask, archetype *, component_mask::hasher>{}
privatemArchetypePointersstd::vector<archetype *>{}
privatemArchetypesstd::vector<std::unique_ptr<archetype>>{}
privatemChunkPoolstd::unique_ptr<chunk_pool>{}
privatemDeferredstd::unique_ptr<command_buffer>{}
privatemDeferredFlushingstd::unique_ptr<command_buffer>{}
privatemEmptyArchetypearchetype *nullptr
privatemFlushDestroyedstd::vector<u64>{}
privatemFlushPlanstd::vector<flush_entry>{}
privatemFlushSlotsstd::vector<flush_slot>{}
privatemFlushStampu320
privatemInObserverboolfalse
privatemLockDepthstd::atomic<i32>0
privatemNamestd::string{}
privatemObserversstd::unique_ptr<observer_table>{}
privatemOwnerThreadstd::thread::id{}
privatemPlacedScratchstd::vector<entity>{}
privatemRecordsstd::vector<entity_record>{}
privatemRegistryconst component_registry *nullptr
privatemStructuralDepthu320
privatemVersionstd::atomic<u64>1

Functions

Public

add

template <typename ComponentType> [[nodiscard]] core::result<ComponentType *> add(
    entity target,
    ComponentType value = {});

Adds a component with an initial value and fires ADDED observers once the value is in place. Returns the stored component: nullptr for a tag, and also when an ADDED observer's deferred work (drained before add returns) removed the component again or destroyed the entity; check the pointer when observers may do that. ENTITY_NOT_ALIVE, COMPONENT_ALREADY_PRESENT, UNKNOWN_COMPONENT, structural errors.

add_raw

[[nodiscard]] core::result<void *> add_raw(
    entity target,
    component_id id,
    std::span<const std::byte> initial = {});

Adds a component from bytes (ops.copy or memcpy; empty = default value) and returns the stored data: nullptr for a tag, and also when an ADDED observer's deferred work removed the component again or destroyed the entity, as with add(). Same errors as add().

archetypes

[[nodiscard]] std::span<archetype *const> archetypes() const noexcept;

Every archetype in creation order. Pointers stay valid for the world's lifetime.

bump_version

u64 bump_version() noexcept;

Increments the version and returns the new value. Any thread.

clear

void clear();

Destroys every entity (firing observers) and returns every chunk, the parked chunk metadata and the structural scratch (flush plan, placement undo list, the deferred buffers' room) to the heap. Archetypes, observers and the id space are kept: destroyed ids come back with a bumped generation, so handles into the cleared world stay stale and ids reserved by command buffers stay valid. Owner thread, unlocked.

create_entities

[[nodiscard]] core::result<void> create_entities(
    const component_mask &mask,
    std::span<entity> out);

Creates out.size() entities of one archetype chunk by chunk, writes their ids and fires ADDED observers for each, which see the default values. create_entities(mask, out, {}).

[[nodiscard]] core::result<void> create_entities(
    const component_mask &mask,
    std::span<entity> out,
    std::span<const component_init> inits);

Like create_entities(mask, out), but copies each init into the new rows (the registered copy, a memcpy for a trivial type) after placement and before the ADDED observers fire, so the observers see the values, as with add(). Inits apply in order, so a later init of the same id wins. Every new column is stamped as written by this call, the copied ones included. The caller's values must cover out.size() entities at each stride; a non-zero stride below the component size overlaps the values, which is the caller's choice. UNKNOWN_COMPONENT, before anything is placed, when an init id is unregistered or not in mask (also for an empty out); otherwise the errors of create_entity, and a failed call creates no entity.

create_entity

[[nodiscard]] core::result<entity> create_entity(
    const component_mask &mask = {});

Creates one entity in the archetype of mask (default-constructed components) and fires ADDED observers. ARCHETYPE_TOO_LARGE, OUT_OF_MEMORY, ENTITY_LIMIT_REACHED, STRUCTURE_LOCKED, WRONG_THREAD, UNKNOWN_COMPONENT.

create_query

[[nodiscard]] query create_query(
    const query_desc &desc);

An untyped query.

destroy_entity

[[nodiscard]] core::result<void> destroy_entity(
    entity target);

Fires REMOVED and destroyed observers, destroys the components and frees the id. The deferred buffer observers filled is drained before returning. ENTITY_NOT_ALIVE for a dead or stale entity. Never cascades.

entity_count

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

Number of alive entities.

flush

[[nodiscard]] flush_report flush(
    command_buffer &commands);

Applies a command buffer: one archetype move per touched entity, destroys last, observers for the mask delta only. Commands on dead or stale entities are skipped and counted. A destroy of the root an instantiation of the same buffer returned cancels that copy: the archive is not placed for it, its ids are handed back with a bumped generation and the root never becomes alive, as a reserved id the buffer destroys. The buffer is empty afterwards. A structural violation leaves the buffer untouched and reports nothing applied.

get

template <typename ComponentType> [[nodiscard]] ComponentType *get(
    entity target) noexcept;

The component; a non-const T is for writing and stamps its chunk column, a const T never stamps. nullptr when dead or absent.

template <typename ComponentType> [[nodiscard]] const ComponentType *get(
    entity target) const noexcept;

The component for reading. nullptr when dead or absent. Never stamps.

get_raw

[[nodiscard]] void *get_raw(
    entity target,
    component_id id,
    bool forWrite) noexcept;

Component data by id; forWrite stamps the column. nullptr when dead, absent or a tag.

[[nodiscard]] const void *get_raw(
    entity target,
    component_id id) const noexcept;

Read-only component data by id. nullptr when dead, absent or a tag. Safe from grain jobs while the world is locked (an iteration_scope, a system stage, parallel_for_chunks): no structural change can move a row meanwhile, so a job may read another entity's component this way (the scene's transform pass reads parent columns through it) as long as no grain is writing that column at the same time.

handle

[[nodiscard]] entity_handle handle(
    entity target) noexcept;

An OOP handle to an entity (entity_handle.h). Defined in entity_handle.h.

has

template <typename ComponentType> [[nodiscard]] bool has(
    entity target) const noexcept;

True when the entity is alive and holds the component.

has_raw

[[nodiscard]] bool has_raw(
    entity target,
    component_id id) const noexcept;

True when the entity is alive and its archetype holds id.

instance_count

[[nodiscard]] u32 instance_count(
    component_id id) const noexcept;

Number of alive entities holding id.

instantiate

[[nodiscard]] core::result<entity> instantiate(
    const std::shared_ptr<const entity_archive> &archive,
    command_buffer *commands = nullptr);

Instantiates one copy of an archive and returns its root (entry 0). With a command buffer the copy is recorded and applied at flush; the root id is still returned now. The buffer must target this world (INVALID_ARGUMENT otherwise, as instantiate_batch).

instantiate_batch

[[nodiscard]] core::result<void> instantiate_batch(
    const std::shared_ptr<const entity_archive> &archive,
    std::span<entity> outRoots,
    command_buffer *commands = nullptr,
    std::vector<entity> *outEntities = nullptr);

Instantiates outRoots.size() copies of an archive, placing entities directly in their final archetypes and rewiring internal entity references. ARCHIVE_CORRUPT before anything is created when the archive does not validate against this world's registry. When given, outEntities receives every created id, copy-major in entry order (copies x entity_count() entries, entries without components included); with a command buffer those are the ids reserved for the flush. After a failed call outEntities is empty. INVALID_ARGUMENT (asserted in Debug and Development) when the command buffer targets another world; nothing is reserved then.

is_alive

[[nodiscard]] bool is_alive(
    entity target) const noexcept;

True when the entity is alive with this generation. Any thread while locked.

is_locked

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

True while at least one iteration_scope is alive.

lock_iteration

[[nodiscard]] iteration_scope lock_iteration() noexcept;

Locks the world for iteration until the returned scope dies. Any thread.

mask_of

[[nodiscard]] component_mask mask_of(
    entity target) const noexcept;

The component set of an entity; empty for a dead one.

name

[[nodiscard]] const std::string &name() const noexcept;

The world's name from world_desc.

observe

[[nodiscard]] observer_id observe(
    component_id id,
    component_event event,
    component_observer_fn fn);

Registers a component observer. Observers run on the owner thread inside the structural operation and may only enqueue into the deferred buffer they receive. Off the owner thread, an unregistered id, an event past REMOVED or an empty callable asserts and returns an invalid observer_id.

observe_destroyed

[[nodiscard]] observer_id observe_destroyed(
    entity_observer_fn fn);

Registers an observer called before an entity is destroyed.

owner_thread

[[nodiscard]] std::thread::id owner_thread() const noexcept;

The thread structural operations must run on.

registry

[[nodiscard]] const component_registry &registry() const noexcept;

The registry this world was created on.

remove

template <typename ComponentType> [[nodiscard]] core::result<void> remove(
    entity target);

Removes a component after firing REMOVED observers. COMPONENT_NOT_PRESENT when absent.

remove_from_all

[[nodiscard]] core::result<void> remove_from_all(
    component_id id);

Removes id from every entity holding it (module unload, dynamic type redefinition).

remove_raw

[[nodiscard]] core::result<void> remove_raw(
    entity target,
    component_id id);

Removes a component by id.

set

template <typename ComponentType> [[nodiscard]] core::result<void> set(
    entity target,
    ComponentType value);

Replaces the value of an existing component and stamps its column. Not structural. ENTITY_NOT_ALIVE, COMPONENT_NOT_PRESENT.

set_owner_thread

[[nodiscard]] core::result<void> set_owner_thread(
    std::thread::id thread);

Hands ownership to another thread. Only while unlocked (STRUCTURE_LOCKED otherwise).

stats

[[nodiscard]] world_stats stats() const noexcept;

Size snapshot.

unobserve

void unobserve(
    observer_id id);

Removes an observer. Stale or unknown ids are ignored.

validate

[[nodiscard]] core::result<void> validate() const;

Full invariant check: records and rows agree, chunk counts sum to archetype counts, only the last chunk of an archetype is partial, free-list indices are FREE, every archetype mask maps back to itself, column offsets are aligned and inside the block. The error message names the first violated invariant.

version

[[nodiscard]] u64 version() const noexcept;

The current change-detection version (starts at 1, never decreases).

view

template <typename... ComponentTypes> [[nodiscard]] ecs::view<ComponentTypes...> view();

A typed view over this world (view.h).

Private

add_storage

[[nodiscard]] core::result<void *> add_storage(
    entity target,
    component_id id);

archetype_for

[[nodiscard]] core::result<archetype *> archetype_for(
    const component_mask &mask);

archetype_with

[[nodiscard]] core::result<archetype *> archetype_with(
    archetype &source,
    component_id id,
    bool added);

begin_structural

void begin_structural() noexcept;

cancel_destroyed_copies

void cancel_destroyed_copies(
    command_buffer::instantiate_entry &entry,
    std::span<const u64> destroyed);

check_structural

[[nodiscard]] core::result<void> check_structural() const;

construct_column_default

void construct_column_default(
    const component_info &info,
    void *destination,
    u32 count) noexcept;

destroy_alive

void destroy_alive(
    entity target,
    entity_record &record,
    bool fireObservers);

destruct_column

void destruct_column(
    const component_info &info,
    void *object,
    u32 count) noexcept;

discard_placed

void discard_placed(
    entity target);

drain_deferred

void drain_deferred();

end_structural

void end_structural();

ensure_records

[[nodiscard]] core::result<void> ensure_records(
    u32 highestIndex);

find_alive

[[nodiscard]] entity_record *find_alive(
    entity target) noexcept;
[[nodiscard]] const entity_record *find_alive(
    entity target) const noexcept;

finish_add

void finish_add(
    entity target,
    component_id id);

fire_added

void fire_added(
    entity target,
    const component_mask &added);

fire_destroyed

void fire_destroyed(
    entity target);

fire_removed

void fire_removed(
    entity target,
    const component_mask &removed);

flush_internal

[[nodiscard]] flush_report flush_internal(
    command_buffer &commands);

instantiate_now

[[nodiscard]] core::result<void> instantiate_now(
    const entity_archive &archive,
    std::span<const entity> ids,
    u32 copies);

is_reserved

[[nodiscard]] bool is_reserved(
    entity target) const noexcept;

move_entity

[[nodiscard]] core::result<void> move_entity(
    entity target,
    entity_record &record,
    archetype &destination);

place_rows

[[nodiscard]] core::result<void> place_rows(
    archetype &target,
    std::span<const entity> ids);

relayout_archetypes

void relayout_archetypes(
    component_id id);

release_reserved

void release_reserved(
    entity target);

remove_row

void remove_row(
    archetype &owner,
    u32 chunkIndex,
    u32 row);

write_inits

void write_inits(
    archetype &target,
    std::span<const entity> ids,
    std::span<const component_init> inits) noexcept;