![]() |
Entity Generator 4.2.0
Composable, deterministic entity generation for C++23
|
A component built from a callable, with every optional hook set by a chained call; see field(). More...
#include <entitygen.hpp>
Public Types | |
| using | generator = std::function< T(const generation_context &)> |
| The callable that produces the value. | |
Public Member Functions | |
| field_component (std::wstring key, generator fn) | |
| Construct a field. | |
| field_component | label (std::wstring text) && |
| Set the display label (default: the key). | |
| field_component | depends_on (std::vector< std::wstring > keys) && |
| Declare the components this one reads; see component::depends_on(). | |
| field_component | format (std::function< std::wstring(const T &)> fn) && |
| Set how the value is shown by to_string(). | |
| field_component | json (std::function< json_value(const T &)> fn) && |
| Set how the value is written by to_json(). | |
| field_component | weight (double inclusion) && |
| Set the inclusion weight (0.0 to 1.0); see component::weight(). | |
| field_component | when (std::function< bool(const generation_context &)> fn) && |
| Generate the field only when the callable returns true; see component::should_generate(). | |
| field_component | validate (std::function< bool(const T &)> fn) && |
| Accept only values the callable accepts; see component::validate(). | |
| field_component | data_hash (std::uint64_t hash) && |
| Set the hash of the data the field draws from; see component::data_hash(). | |
| std::wstring | key () const override |
Unique key identifying this component (e.g. "name", "age"). | |
| std::wstring | label () const override |
| Display name of this component, used by entity::to_string(). | |
| std::vector< std::wstring > | depends_on () const override |
| Keys of the components this one reads from the context. | |
| std::uint64_t | data_hash () const override |
| Hash of the data this component draws from, for replay. | |
| double | weight () const override |
| Inclusion weight for this component (0.0 to 1.0). | |
| std::any | generate (const generation_context &ctx) const override |
| Generate a random value for this component. | |
| std::wstring | to_string (const std::any &value) const override |
| Convert a generated value to a displayable string. | |
| json_value | to_json (const std::any &value) const override |
| Convert a generated value to JSON, for entity::to_json(). | |
| bool | validate (const std::any &value) const override |
| Validate a generated value. | |
| bool | should_generate (const generation_context &ctx) const override |
| Decide whether this component should be generated. | |
Public Member Functions inherited from dasmig::component | |
| component ()=default | |
| Default constructor. | |
| component (const component &)=default | |
| Copyable. | |
| component & | operator= (const component &)=default |
| Copyable. | |
| component (component &&) noexcept=default | |
| Movable. | |
| component & | operator= (component &&) noexcept=default |
| Movable. | |
| virtual | ~component ()=default |
| Virtual destructor for proper cleanup of derived classes. | |
Additional Inherited Members | |
Static Protected Member Functions inherited from dasmig::component | |
| static std::wstring | default_to_string (const std::any &value) |
| Default conversion covering common standard types. | |
| static json_value | default_to_json (const std::any &value, const std::wstring &display) |
| Default JSON conversion used by to_json(). | |
A component built from a callable, with every optional hook set by a chained call; see field().
The setters work on a temporary and return it, so a field is written in one expression and passed to eg::add():
| T | The value type the callable returns. |
Definition at line 691 of file entitygen.hpp.
| using dasmig::field_component< T >::generator = std::function<T(const generation_context&)> |
The callable that produces the value.
Definition at line 695 of file entitygen.hpp.
|
inline |
Construct a field.
| key | Unique component key (dots make a path in the JSON). |
| fn | Callable invoked during generation. |
Definition at line 700 of file entitygen.hpp.
|
inlineoverridevirtual |
Hash of the data this component draws from, for replay.
A component that calls a generator with data returns that generator's data_hash(). entity::signature() combines the hashes of the components that ran, so a replay shows whether the data changed.
Reimplemented from dasmig::component.
Definition at line 762 of file entitygen.hpp.
|
inline |
Set the hash of the data the field draws from; see component::data_hash().
Definition at line 753 of file entitygen.hpp.
|
inlineoverridevirtual |
Keys of the components this one reads from the context.
A request that selects this component also generates its dependencies (transitively) and orders them before it, whatever the registration order. If a dependency is skipped (weight roll or should_generate()), this component is skipped too. A dependency that is not registered, or a cycle, makes generation throw.
Reimplemented from dasmig::component.
Definition at line 761 of file entitygen.hpp.
|
inline |
Declare the components this one reads; see component::depends_on().
Definition at line 710 of file entitygen.hpp.
|
inline |
Set how the value is shown by to_string().
Definition at line 717 of file entitygen.hpp.
|
inlineoverridevirtual |
Generate a random value for this component.
| ctx | Context providing access to previously generated values and a seeded random engine. |
std::any. Implements dasmig::component.
Definition at line 765 of file entitygen.hpp.
|
inline |
Set how the value is written by to_json().
Definition at line 724 of file entitygen.hpp.
|
inlineoverridevirtual |
Unique key identifying this component (e.g. "name", "age").
Implements dasmig::component.
Definition at line 759 of file entitygen.hpp.
|
inlineoverridevirtual |
Display name of this component, used by entity::to_string().
The key stays the identifier (lookups, seeds, JSON). A key with dots is a path: L"identity.age" is written as {"identity":{"age":..}}.
Reimplemented from dasmig::component.
Definition at line 760 of file entitygen.hpp.
|
inline |
Set the display label (default: the key).
Definition at line 703 of file entitygen.hpp.
|
inlineoverridevirtual |
Decide whether this component should be generated.
Unlike weight() (probabilistic), this is logic-driven. Return false to skip generation entirely based on the current context.
| ctx | Context with previously generated component values. |
true to generate, false to skip. Reimplemented from dasmig::component.
Definition at line 786 of file entitygen.hpp.
|
inlineoverridevirtual |
Convert a generated value to JSON, for entity::to_json().
The default writes bool as a boolean, integers as integers, double and float as numbers (non-finite ones as null), std::wstring and std::string as strings, and anything else as the string from to_string(). Override it to write a richer value: a json_value can hold arrays and objects.
| value | The value previously returned by generate(). |
Reimplemented from dasmig::component.
Definition at line 775 of file entitygen.hpp.
|
inlineoverridevirtual |
Convert a generated value to a displayable string.
| value | The value previously returned by generate(). |
Implements dasmig::component.
Definition at line 770 of file entitygen.hpp.
|
inlineoverridevirtual |
Validate a generated value.
| value | The value to validate. |
true to accept, false to retry generation. Reimplemented from dasmig::component.
Definition at line 781 of file entitygen.hpp.
|
inline |
Accept only values the callable accepts; see component::validate().
Definition at line 746 of file entitygen.hpp.
|
inlineoverridevirtual |
Inclusion weight for this component (0.0 to 1.0).
A value of 1.0 means always included; 0.5 means included roughly half the time. The generator may override this per-component at registration time via eg::add(comp, weight) or eg::weight().
Reimplemented from dasmig::component.
Definition at line 763 of file entitygen.hpp.
|
inline |
Set the inclusion weight (0.0 to 1.0); see component::weight().
Definition at line 731 of file entitygen.hpp.
|
inline |
Generate the field only when the callable returns true; see component::should_generate().
Definition at line 739 of file entitygen.hpp.