Skip to content
RedRimmedBox edited this page Jan 4, 2025 · 11 revisions

Overview

The Query class group provides a flexible data query framework for the system. It has the following characteristics:

  • Cannot be instantiated directly: The constructor is defined as private.
  • Cannot be subclassed: Each class is marked with the final modifier, preventing inheritance.
  • Type safety: Generic parameters must conform to the Component protocol.
  • Flexible structure: Classes like Query2 and Query3 are available for handling multiple generic parameters, supporting up to 10 generic parameters.

Class Design

1. Query Class Structure

public final class Query<T: Component> {
    private init() {
        // Prevents direct instantiation
    }
}

2. Structure of Query2 and Beyond

To support multiple generic parameters, classes like Query2 and Query3 are provided. This allows queries to handle multiple data types flexibly.

/// A query for two `Component` types.
public final class Query2<T1: Component, T2: Component> {
    private init() {
        // Prevents direct instantiation
    }
}

Tip

Classes such as Query2 and beyond are auto-generated using Swift Macros. They are not directly defined in the codebase.

Usage

Using as Parameters in System Functions

Since Query classes cannot be instantiated directly, they are typically instantiated by the system and passed as parameters to functions as shown below:

Example: Using in System Functions

/// Example: A system function using a query for the `Position` component.
func exampleSystem(query: Query<Position>) {
    // Use the query to perform system logic
}

/// Example: A system function handling up to 10 components.
func exampleSystemWithMore(query: Query10<ComponentA, ComponentB, ComponentC, ComponentD, ComponentE, ComponentF, ComponentG, ComponentH, ComponentI, ComponentJ>) {
    // Supports up to 10 generic parameters
}

rrbox note

  • Filtered<Query, Condition> クラスについても説明したい
  • そういえば Query のデータにアクセスする方法(update メソッドと components メソッド)について書き忘れたな...(覚えていたら書きますmm)
Clone this wiki locally