Entity Generator 4.2.0
Composable, deterministic entity generation for C++23
Loading...
Searching...
No Matches
dasmig::field_component< T > Class Template Referencefinal

A component built from a callable, with every optional hook set by a chained call; see field(). More...

#include <entitygen.hpp>

Inheritance diagram for dasmig::field_component< T >:
dasmig::component

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().
 

Detailed Description

template<typename T>
class dasmig::field_component< T >

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():

gen.add(dasmig::field(L"identity.age", [](const dasmig::generation_context& ctx) {
return ctx.get<birth>(L"birth").age;
})
.label(L"Age")
.depends_on({L"birth"}));
std::wstring label() const override
Display name of this component, used by entity::to_string().
Context passed to components during generation.
T get(const std::wstring &component_key) const
Typed retrieval of an already-generated component value.
Template Parameters
TThe value type the callable returns.

Definition at line 691 of file entitygen.hpp.

Member Typedef Documentation

◆ generator

template<typename T >
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.

Constructor & Destructor Documentation

◆ field_component()

template<typename T >
dasmig::field_component< T >::field_component ( std::wstring  key,
generator  fn 
)
inline

Construct a field.

Parameters
keyUnique component key (dots make a path in the JSON).
fnCallable invoked during generation.

Definition at line 700 of file entitygen.hpp.

Member Function Documentation

◆ data_hash() [1/2]

template<typename T >
std::uint64_t dasmig::field_component< T >::data_hash ( ) const
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.

Returns
The hash; 0 (no data) by default.

Reimplemented from dasmig::component.

Definition at line 762 of file entitygen.hpp.

◆ data_hash() [2/2]

template<typename T >
field_component dasmig::field_component< T >::data_hash ( std::uint64_t  hash) &&
inline

Set the hash of the data the field draws from; see component::data_hash().

Definition at line 753 of file entitygen.hpp.

◆ depends_on() [1/2]

template<typename T >
std::vector< std::wstring > dasmig::field_component< T >::depends_on ( ) const
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.

Returns
The dependency keys; empty by default.

Reimplemented from dasmig::component.

Definition at line 761 of file entitygen.hpp.

◆ depends_on() [2/2]

template<typename T >
field_component dasmig::field_component< T >::depends_on ( std::vector< std::wstring >  keys) &&
inline

Declare the components this one reads; see component::depends_on().

Definition at line 710 of file entitygen.hpp.

◆ format()

template<typename T >
field_component dasmig::field_component< T >::format ( std::function< std::wstring(const T &)>  fn) &&
inline

Set how the value is shown by to_string().

Definition at line 717 of file entitygen.hpp.

◆ generate()

template<typename T >
std::any dasmig::field_component< T >::generate ( const generation_context &  ctx) const
inlineoverridevirtual

Generate a random value for this component.

Parameters
ctxContext providing access to previously generated values and a seeded random engine.
Returns
The generated value wrapped in std::any.

Implements dasmig::component.

Definition at line 765 of file entitygen.hpp.

◆ json()

template<typename T >
field_component dasmig::field_component< T >::json ( std::function< json_value(const T &)>  fn) &&
inline

Set how the value is written by to_json().

Definition at line 724 of file entitygen.hpp.

◆ key()

template<typename T >
std::wstring dasmig::field_component< T >::key ( ) const
inlineoverridevirtual

Unique key identifying this component (e.g. "name", "age").

Returns
The component key as a wide string.

Implements dasmig::component.

Definition at line 759 of file entitygen.hpp.

◆ label() [1/2]

template<typename T >
std::wstring dasmig::field_component< T >::label ( ) const
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":..}}.

Returns
The label; defaults to the key.

Reimplemented from dasmig::component.

Definition at line 760 of file entitygen.hpp.

◆ label() [2/2]

template<typename T >
field_component dasmig::field_component< T >::label ( std::wstring  text) &&
inline

Set the display label (default: the key).

Definition at line 703 of file entitygen.hpp.

◆ should_generate()

template<typename T >
bool dasmig::field_component< T >::should_generate ( const generation_context &  ctx) const
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.

Parameters
ctxContext with previously generated component values.
Returns
true to generate, false to skip.

Reimplemented from dasmig::component.

Definition at line 786 of file entitygen.hpp.

◆ to_json()

template<typename T >
json_value dasmig::field_component< T >::to_json ( const std::any &  value) const
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.

Parameters
valueThe value previously returned by generate().
Returns
The JSON value.

Reimplemented from dasmig::component.

Definition at line 775 of file entitygen.hpp.

◆ to_string()

template<typename T >
std::wstring dasmig::field_component< T >::to_string ( const std::any &  value) const
inlineoverridevirtual

Convert a generated value to a displayable string.

Parameters
valueThe value previously returned by generate().
Returns
A human-readable wide-string representation.
See also
default_to_string()

Implements dasmig::component.

Definition at line 770 of file entitygen.hpp.

◆ validate() [1/2]

template<typename T >
bool dasmig::field_component< T >::validate ( const std::any &  value) const
inlineoverridevirtual

Validate a generated value.

Parameters
valueThe value to validate.
Returns
true to accept, false to retry generation.
See also
eg::max_retries()

Reimplemented from dasmig::component.

Definition at line 781 of file entitygen.hpp.

◆ validate() [2/2]

template<typename T >
field_component dasmig::field_component< T >::validate ( std::function< bool(const T &)>  fn) &&
inline

Accept only values the callable accepts; see component::validate().

Definition at line 746 of file entitygen.hpp.

◆ weight() [1/2]

template<typename T >
double dasmig::field_component< T >::weight ( ) const
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().

Returns
The inclusion probability.
See also
eg::weight()

Reimplemented from dasmig::component.

Definition at line 763 of file entitygen.hpp.

◆ weight() [2/2]

template<typename T >
field_component dasmig::field_component< T >::weight ( double  inclusion) &&
inline

Set the inclusion weight (0.0 to 1.0); see component::weight().

Definition at line 731 of file entitygen.hpp.

◆ when()

template<typename T >
field_component dasmig::field_component< T >::when ( std::function< bool(const generation_context &)>  fn) &&
inline

Generate the field only when the callable returns true; see component::should_generate().

Definition at line 739 of file entitygen.hpp.


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