Entity Generator 4.2.0
Composable, deterministic entity generation for C++23
Loading...
Searching...
No Matches
dasmig::generation_context Class Reference

Context passed to components during generation. More...

#include <entitygen.hpp>

Public Member Functions

bool has (const std::wstring &component_key) const
 Check if a component value has already been generated.
 
template<typename T >
T get (const std::wstring &component_key) const
 Typed retrieval of an already-generated component value.
 
template<typename T >
std::optional< T > try_get (const std::wstring &component_key) const
 Typed retrieval that tolerates a missing value.
 
template<typename T >
T get_or (const std::wstring &component_key, T fallback) const
 Typed retrieval with a fallback.
 
bool has_input (std::string_view name) const
 Check whether the request supplied an input.
 
template<typename T >
T input (std::string_view name) const
 A per-call input from the request.
 
template<typename T >
T input_or (std::string_view name, T fallback) const
 A per-call input, or a fallback when the request has none.
 
core::rng & random () const
 Access the random engine for this component.
 

Friends

class eg
 

Detailed Description

Context passed to components during generation.

Provides access to previously generated component values and a seeded random engine. Created and populated by the entity generator; not user-constructible.

See also
component::generate()

Definition at line 143 of file entitygen.hpp.

Member Function Documentation

◆ get()

template<typename T >
T dasmig::generation_context::get ( const std::wstring &  component_key) const
inline

Typed retrieval of an already-generated component value.

Template Parameters
TThe expected type of the stored value, or a const reference to it (get<const T&>) to read it without a copy; the reference is valid while the entity is being generated.
Parameters
component_keyThe component key to retrieve.
Returns
The value cast to T.
Exceptions
std::out_of_rangeIf no value for the key was generated (the message names the key); declare the dependency with component::depends_on() so a request pulls it in.
std::bad_any_castIf T does not match the stored type.

Definition at line 165 of file entitygen.hpp.

◆ get_or()

template<typename T >
T dasmig::generation_context::get_or ( const std::wstring &  component_key,
T  fallback 
) const
inline

Typed retrieval with a fallback.

Template Parameters
TThe expected type of the stored value.
Parameters
component_keyThe component key to retrieve.
fallbackReturned when no value was generated.
Returns
The value, or fallback.
Exceptions
std::bad_any_castIf T does not match the stored type.

Definition at line 196 of file entitygen.hpp.

◆ has()

bool dasmig::generation_context::has ( const std::wstring &  component_key) const
inline

Check if a component value has already been generated.

Parameters
component_keyThe component key to look up.
Returns
true if a value for the key exists in the context.

Definition at line 149 of file entitygen.hpp.

◆ has_input()

bool dasmig::generation_context::has_input ( std::string_view  name) const
inline

Check whether the request supplied an input.

Parameters
nameThe input name.
Returns
true if entity_request::inputs has the name.

Definition at line 205 of file entitygen.hpp.

◆ input()

template<typename T >
T dasmig::generation_context::input ( std::string_view  name) const
inline

A per-call input from the request.

Template Parameters
TOne of bool, std::int64_t, double, std::string: the type the input was given with.
Parameters
nameThe input name.
Returns
The value.
Exceptions
std::invalid_argumentIf the request has no such input, or it has another type (the message names the input).

Definition at line 218 of file entitygen.hpp.

◆ input_or()

template<typename T >
T dasmig::generation_context::input_or ( std::string_view  name,
T  fallback 
) const
inline

A per-call input, or a fallback when the request has none.

Template Parameters
TThe input type, as for input().
Parameters
nameThe input name.
fallbackReturned when the input was not supplied.
Returns
The value, or fallback.
Exceptions
std::invalid_argumentIf the input has another type.

Definition at line 240 of file entitygen.hpp.

◆ random()

core::rng & dasmig::generation_context::random ( ) const
inline

Access the random engine for this component.

Seeded with the component's seed (entity::seed(key)), so the same draws reproduce the value. Use it with the dasmig-core functions, which give the same results on every platform:

int age = dasmig::core::uniform_int(ctx.random(), 18, 65);
const auto& title = dasmig::core::pick(ctx.random(), titles);
bool lucky = dasmig::core::bernoulli(ctx.random(), 0.1);
Returns
A reference to the per-component seeded engine.

Definition at line 256 of file entitygen.hpp.

◆ try_get()

template<typename T >
std::optional< T > dasmig::generation_context::try_get ( const std::wstring &  component_key) const
inline

Typed retrieval that tolerates a missing value.

Template Parameters
TThe expected type of the stored value.
Parameters
component_keyThe component key to retrieve.
Returns
The value, or an empty optional if none was generated.
Exceptions
std::bad_any_castIf T does not match the stored type.

Definition at line 182 of file entitygen.hpp.

Friends And Related Symbol Documentation

◆ eg

friend class eg
friend

Definition at line 284 of file entitygen.hpp.


The documentation for this class was generated from the following file: