![]() |
Entity Generator 4.2.0
Composable, deterministic entity generation for C++23
|
The entity generator — produces entities with configurable components. More...
#include <entitygen.hpp>
Public Member Functions | |
| eg ()=default | |
| Default constructor creates an empty generator. | |
| eg (const eg &)=delete | |
| Not copyable. | |
| eg & | operator= (const eg &)=delete |
| Not copyable. | |
| eg (eg &&) noexcept=default | |
| Move constructor. | |
| eg & | operator= (eg &&) noexcept=default |
| Move assignment. | |
| ~eg ()=default | |
| Default destructor. | |
Registration | |
| eg & | add (std::unique_ptr< component > comp) |
| Register a component. | |
| template<typename T > | |
| eg & | add (field_component< T > f) |
| Register a field built with field(). | |
| eg & | add_recipe (const std::filesystem::path &path) |
| Register the components a recipe declares. | |
| eg & | add_recipe_from_string (std::string_view text, std::string source="recipe") |
| Register the components a recipe in text declares. | |
| eg & | add (std::unique_ptr< component > comp, double weight_override) |
| Register a component with a weight override. | |
| eg & | weight (const std::wstring &component_key, double weight_value) |
| Set or update the weight override for a registered component. | |
| eg & | remove (const std::wstring &component_key) |
| Remove a registered component by key. | |
| bool | has (const std::wstring &component_key) const |
| Check if a component is registered by key. | |
| eg & | clear () |
| Remove all registered components, weight overrides, and groups. | |
| std::size_t | size () const |
| Return the number of registered components. | |
| std::vector< std::wstring > | component_keys () const |
| Return all registered component keys in registration order. | |
Validation | |
| eg & | max_retries (std::size_t retries) |
| Set the maximum number of retries for validation (default 10). | |
Observers | |
| eg & | add_observer (std::shared_ptr< generation_observer > obs) |
| Add an observer to receive generation lifecycle callbacks. | |
| eg & | remove_observer (const std::shared_ptr< generation_observer > &obs) |
| Remove a specific observer by identity. | |
| eg & | clear_observers () |
| Remove all observers. | |
Generation | |
| entity | generate (const entity_request &request) |
| Generate an entity using per-call options. | |
| entity | generate (const entity_request &request, std::uint64_t call_seed) const |
| Generate an entity using a request and a seed. | |
Batch Generation | |
| std::vector< entity > | generate_batch (const entity_request &request) |
| Generate request.count entities using per-call options. | |
| std::vector< entity > | generate_batch (const entity_request &request, std::uint64_t call_seed) const |
| Generate multiple entities using a seed. | |
| std::vector< entity > | generate_batch (const entity_request &request, const core::batch_options &options) |
| Generate a batch described by core::batch_options, from a seed drawn from the generator's engine. | |
| std::vector< entity > | generate_batch (const entity_request &request, const core::batch_options &options, std::uint64_t batch_seed) const |
| Generate a batch described by core::batch_options, deterministically. | |
| template<typename Sink > | |
| void | for_each_entity (const entity_request &request, const core::batch_options &options, std::uint64_t batch_seed, Sink &&sink) const |
| Stream a batch of entities to a sink without holding the batch. | |
| template<typename Sink > | |
| void | for_each_entity (const entity_request &request, const core::batch_options &options, Sink &&sink) |
| Stream a batch of entities with a batch seed drawn from the generator's engine. | |
Concurrent Batch Generation | |
| std::vector< entity > | generate_batch_async (const entity_request &request) |
| Generate multiple entities concurrently. | |
| std::vector< entity > | generate_batch_async (const entity_request &request, std::uint64_t call_seed) const |
| Generate multiple entities concurrently using a seed. | |
Component Groups | |
| eg & | add_group (const std::wstring &group_name, std::vector< std::wstring > component_keys) |
| Register a named group of component keys. | |
| eg & | remove_group (const std::wstring &group_name) |
| Remove a named group. | |
| bool | has_group (const std::wstring &group_name) const |
| Check if a named group exists. | |
| entity | generate_group (const entity_request &request) |
| Generate an entity using a named group. | |
| entity | generate_group (const entity_request &request, std::uint64_t call_seed) const |
| Generate an entity using a named group with a seed. | |
Static Public Member Functions | |
| static eg & | instance () |
| Access the global singleton instance. | |
The entity generator — produces entities with configurable components.
Components are registered by implementing the component interface and adding them to the generator. Components are generated in registration order, allowing later components to access earlier ones via context.
Definition at line 1253 of file entitygen.hpp.
|
inline |
Register a field built with field().
| T | The field's value type. |
| f | The field (moved). |
*this for chaining. | std::invalid_argument | As add(std::unique_ptr<component>). |
Definition at line 1315 of file entitygen.hpp.
Register a component.
Takes ownership of the component. If a component with the same key already exists, it will be replaced. Components are generated in the order they are registered.
| comp | The component to register (moved). |
*this for chaining. | std::invalid_argument | If the key has an empty path segment (a..b, .a, a.), or would be both a value and the parent of another key (a with a.b). |
Definition at line 1288 of file entitygen.hpp.
Register a component with a weight override.
The override takes precedence over the component's own weight() method.
| comp | The component to register (moved). |
| weight_override | Inclusion probability override (0.0–1.0). |
*this for chaining. Definition at line 1377 of file entitygen.hpp.
|
inline |
Register a named group of component keys.
| group_name | Name for the group. |
| component_keys | Keys in the group. |
*this for chaining. Definition at line 1691 of file entitygen.hpp.
|
inline |
Add an observer to receive generation lifecycle callbacks.
| obs | The observer (shared ownership). |
*this for chaining. Definition at line 1474 of file entitygen.hpp.
|
inline |
Register the components a recipe declares.
A recipe is a tab-separated file with a header line and one component per row, so a component that only draws from data is written as data instead of code (see add_recipe_from_string() for the columns). The components are the ones the code makes with field(): the same dependencies, ordering, weights and replay. An invalid row throws and adds nothing.
| path | The recipe file. |
*this for chaining. | core::tsv_error | If the file cannot be read or a row is invalid. |
| std::invalid_argument | As add(std::unique_ptr<component>). |
Definition at line 1333 of file entitygen.hpp.
|
inline |
Register the components a recipe in text declares.
Columns (the header names them, in any order):
| Column | Meaning | |
|---|---|---|
key | required | The component key; dots make a path in the JSON |
kind | required | constant, choice, weighted, int or real |
values | required | What the kind draws from, below |
label | optional | Display name; the key by default |
depends_on | optional | Keys this component follows, separated by ; |
weight | optional | Inclusion probability, 0 to 1; 1 by default |
The kinds: constant gives values as text; choice picks uniformly from a|b|c; weighted picks from a:3|b:1 (a value and its relative weight, split at the last colon); int draws an integer from lo..hi (both included); real draws a number from lo..hi (hi excluded). Text kinds give strings in the JSON, int and real give numbers. Items are not trimmed. A component with the key of an existing one replaces it. The recipe's rows are part of the entity's replay signature.
| text | UTF-8 recipe text. |
| source | Name used in errors. |
*this for chaining. | core::tsv_error | If the text is malformed or a row is invalid (an unknown kind, a bad range or weight, a repeated key). |
| std::invalid_argument | As add(std::unique_ptr<component>). |
Definition at line 1366 of file entitygen.hpp.
|
inline |
Remove all registered components, weight overrides, and groups.
*this for chaining. Definition at line 1429 of file entitygen.hpp.
|
inline |
|
inline |
Return all registered component keys in registration order.
Definition at line 1442 of file entitygen.hpp.
|
inline |
Stream a batch of entities with a batch seed drawn from the generator's engine.
| Sink | Callable taking (std::uint64_t, entity&). |
| request | Selection and entity validation options. |
| options | Count, first index, threads and uniqueness. |
| sink | Receives the entities. |
Definition at line 1639 of file entitygen.hpp.
|
inline |
Stream a batch of entities to a sink without holding the batch.
Generates the same entities as generate_batch(request, options,
batch_seed) and calls sink(index, entity&) in index order on the calling thread; index includes options.first.
| Sink | Callable taking (std::uint64_t, entity&); for example core::jsonl_writer. |
| request | Selection and entity validation options. |
| options | Count, first index, threads and uniqueness. |
| batch_seed | Seed of the whole batch. |
| sink | Receives the entities. |
Definition at line 1623 of file entitygen.hpp.
|
inline |
Generate an entity using per-call options.
Draws a call seed from the generator's engine.
| request | Selection and entity validation options. |
| std::runtime_error | If component or entity validation is exhausted. |
| std::invalid_argument | If both selection fields are set. |
| std::out_of_range | If the requested group does not exist. |
Definition at line 1509 of file entitygen.hpp.
|
inline |
Generate an entity using a request and a seed.
The same seed and components give the same entity on every compiler and platform. Each component draws from a stream split from the seed by its key, so adding or removing a component does not change the others.
| request | Selection and entity validation options. |
| call_seed | Seed for reproducible results; does not advance the internal engine. |
| std::runtime_error | If validation is exhausted. |
| std::invalid_argument | If both selection fields are set. |
| std::out_of_range | If the requested group does not exist. |
Definition at line 1527 of file entitygen.hpp.
|
inline |
Generate request.count entities using per-call options.
Draws one call seed from the generator's engine for the batch.
| request | Selection, batch count and entity validation options. |
| std::invalid_argument | If both selection fields are set. |
| std::out_of_range | If the requested group does not exist. |
| std::runtime_error | If component or entity validation is exhausted. |
Definition at line 1545 of file entitygen.hpp.
|
inline |
Generate a batch described by core::batch_options, from a seed drawn from the generator's engine.
Uses options.count (the request's count is ignored here; it stays for generate_batch(request) until the next major version).
| request | Selection and entity validation options. |
| options | Count, first index, threads and uniqueness. |
| std::invalid_argument | If both selection fields are set. |
| std::out_of_range | If the requested group does not exist. |
| std::runtime_error | If component or entity validation is exhausted. |
Definition at line 1581 of file entitygen.hpp.
|
inline |
Generate a batch described by core::batch_options, deterministically.
Entity i is exactly generate(request, core::item_seed(batch_seed, options.first + i)), for any options.threads. With more than one thread, components, observers and the request's validator (including captured state) must be thread-safe.
| request | Selection and entity validation options. |
| options | Count, first index, threads and uniqueness (see core::unique_rule, made for entity). |
| batch_seed | Seed of the whole batch. |
| std::invalid_argument | If both selection fields are set. |
| std::out_of_range | If the requested group does not exist. |
| std::runtime_error | If component or entity validation is exhausted, or a unique batch runs out of attempts. |
Definition at line 1602 of file entitygen.hpp.
|
inline |
Generate multiple entities using a seed.
Entity i uses core::item_seed(call_seed, i) as its call seed, so the result equals generate_batch_async(request, call_seed) and each entity replays with generate(request, e.seed()). Since 4.1.0 this is the same seed derivation as every other library's batch; before, entity i used the i-th value of core::rng{call_seed}.split("batch").
| request | Selection, batch count and entity validation options. |
| call_seed | Seed for reproducible results. |
| std::invalid_argument | If both selection fields are set. |
| std::out_of_range | If the requested group does not exist. |
| std::runtime_error | If component or entity validation is exhausted. |
Definition at line 1564 of file entitygen.hpp.
|
inline |
Generate multiple entities concurrently.
Same entities as generate_batch(request) would give from the same engine state; each entity is generated in its own async task. Results are returned in order. Components, observers and the request's validator (including captured state) must be thread-safe. In a WebAssembly build without threads (Emscripten without -pthread) the tasks run one after another, with the same results.
| request | Selection, batch count and entity validation options. |
| std::invalid_argument | If both selection fields are set. |
| std::out_of_range | If the requested group does not exist. |
| std::runtime_error | If component or entity validation is exhausted. |
Definition at line 1662 of file entitygen.hpp.
|
inline |
Generate multiple entities concurrently using a seed.
Gives the same entities as generate_batch(request, call_seed).
| request | Selection, batch count and entity validation options. |
| call_seed | Seed for reproducible results. |
| std::invalid_argument | If both selection fields are set. |
| std::out_of_range | If the requested group does not exist. |
| std::runtime_error | If component or entity validation is exhausted. |
Definition at line 1677 of file entitygen.hpp.
|
inline |
Generate an entity using a named group.
| request | Options with a registered group name. |
| std::out_of_range | If the group does not exist. |
| std::invalid_argument | If the group is absent or component_keys is set. |
| std::runtime_error | If component or entity validation is exhausted. |
Definition at line 1721 of file entitygen.hpp.
|
inline |
Generate an entity using a named group with a seed.
| request | Options with a registered group name. |
| call_seed | Seed for reproducible results. |
| std::out_of_range | If the group does not exist. |
| std::invalid_argument | If the group is absent or component_keys is set. |
| std::runtime_error | If component or entity validation is exhausted. |
Definition at line 1734 of file entitygen.hpp.
|
inline |
Check if a component is registered by key.
| component_key | The component key to look up. |
true if a component with the key exists. Definition at line 1420 of file entitygen.hpp.
|
inline |
Check if a named group exists.
| group_name | The group name to look up. |
true if the group exists. Definition at line 1710 of file entitygen.hpp.
|
inlinestatic |
Access the global singleton instance.
For multi-threaded use, prefer creating independent eg instances.
Definition at line 1269 of file entitygen.hpp.
|
inline |
Set the maximum number of retries for validation (default 10).
Applies to both component-level and entity-level validation.
| retries | The maximum retry count. |
*this for chaining. Definition at line 1460 of file entitygen.hpp.
|
inline |
Remove a registered component by key.
| component_key | The component key to remove. |
*this for chaining. Definition at line 1404 of file entitygen.hpp.
|
inline |
Remove a named group.
| group_name | The group to remove. |
*this for chaining. Definition at line 1701 of file entitygen.hpp.
|
inline |
Remove a specific observer by identity.
| obs | The observer to remove. |
*this for chaining. Definition at line 1483 of file entitygen.hpp.
|
inline |
Return the number of registered components.
Definition at line 1438 of file entitygen.hpp.
|
inline |
Set or update the weight override for a registered component.
| component_key | The component key. |
| weight_value | New inclusion probability (0.0–1.0). |
*this for chaining. | std::out_of_range | If the component is not registered. |
Definition at line 1390 of file entitygen.hpp.