Context passed to components during generation.
More...
#include <entitygen.hpp>
|
| 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.
|
| |
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.
◆ 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
-
| T | The 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_key | The component key to retrieve. |
- Returns
- The value cast to
T.
- Exceptions
-
| std::out_of_range | If 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_cast | If 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
-
| T | The expected type of the stored value. |
- Parameters
-
| component_key | The component key to retrieve. |
| fallback | Returned when no value was generated. |
- Returns
- The value, or
fallback.
- Exceptions
-
| std::bad_any_cast | If 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_key | The 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 |
◆ input()
template<typename T >
| T dasmig::generation_context::input |
( |
std::string_view |
name | ) |
const |
|
inline |
A per-call input from the request.
- Template Parameters
-
| T | One of bool, std::int64_t, double, std::string: the type the input was given with. |
- Parameters
-
- Returns
- The value.
- Exceptions
-
| std::invalid_argument | If 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
-
- Parameters
-
| name | The input name. |
| fallback | Returned when the input was not supplied. |
- Returns
- The value, or
fallback.
- Exceptions
-
| std::invalid_argument | If 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
-
| T | The expected type of the stored value. |
- Parameters
-
| component_key | The component key to retrieve. |
- Returns
- The value, or an empty optional if none was generated.
- Exceptions
-
| std::bad_any_cast | If T does not match the stored type. |
Definition at line 182 of file entitygen.hpp.
◆ eg
The documentation for this class was generated from the following file: