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

Abstract base for entity components. More...

#include <entitygen.hpp>

Inheritance diagram for dasmig::component:
dasmig::callback_component< T, GenFn, Formatter > dasmig::choice_component< T, Formatter > dasmig::constant_component< T, Formatter > dasmig::field_component< T > dasmig::range_component< T, Formatter > dasmig::weighted_choice_component< T, Formatter >

Public Member Functions

 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.
 
virtual std::wstring key () const =0
 Unique key identifying this component (e.g. "name", "age").
 
virtual std::any generate (const generation_context &ctx) const =0
 Generate a random value for this component.
 
virtual std::wstring to_string (const std::any &value) const =0
 Convert a generated value to a displayable string.
 
virtual json_value to_json (const std::any &value) const
 Convert a generated value to JSON, for entity::to_json().
 
virtual std::wstring label () const
 Display name of this component, used by entity::to_string().
 
virtual std::vector< std::wstring > depends_on () const
 Keys of the components this one reads from the context.
 
virtual std::uint64_t data_hash () const
 Hash of the data this component draws from, for replay.
 
virtual double weight () const
 Inclusion weight for this component (0.0 to 1.0).
 
virtual bool validate (const std::any &value) const
 Validate a generated value.
 
virtual bool should_generate (const generation_context &ctx) const
 Decide whether this component should be generated.
 

Static Protected Member Functions

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

Abstract base for entity components.

Implement this interface to define custom components that the entity generator can produce. Each component has a unique key, generates typed random values, and converts them to display strings.

See also
generation_context, eg::add()

Definition at line 293 of file entitygen.hpp.

Member Function Documentation

◆ data_hash()

virtual std::uint64_t dasmig::component::data_hash ( ) const
inlinevirtual

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 in dasmig::field_component< T >.

Definition at line 359 of file entitygen.hpp.

◆ default_to_json()

static json_value dasmig::component::default_to_json ( const std::any &  value,
const std::wstring &  display 
)
inlinestaticprotected

Default JSON conversion used by to_json().

Parameters
valueThe value to convert.
displayIts to_string() form, used for unrecognized types.
Returns
The JSON value.

Definition at line 453 of file entitygen.hpp.

◆ default_to_string()

static std::wstring dasmig::component::default_to_string ( const std::any &  value)
inlinestaticprotected

Default conversion covering common standard types.

Handles std::wstring, std::string (UTF-8), bool, float, double and the integer types int, long, long long and their unsigned forms. Returns "[?]" for unrecognized types.

Parameters
valueThe value to convert.
Returns
A wide-string representation.

Definition at line 399 of file entitygen.hpp.

◆ depends_on()

virtual std::vector< std::wstring > dasmig::component::depends_on ( ) const
inlinevirtual

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 in dasmig::field_component< T >.

Definition at line 351 of file entitygen.hpp.

◆ generate()

virtual std::any dasmig::component::generate ( const generation_context &  ctx) const
pure virtual

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.

Implemented in dasmig::constant_component< T, Formatter >, dasmig::choice_component< T, Formatter >, dasmig::range_component< T, Formatter >, dasmig::callback_component< T, GenFn, Formatter >, dasmig::field_component< T >, and dasmig::weighted_choice_component< T, Formatter >.

◆ key()

virtual std::wstring dasmig::component::key ( ) const
pure virtual

◆ label()

virtual std::wstring dasmig::component::label ( ) const
inlinevirtual

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 in dasmig::field_component< T >.

Definition at line 341 of file entitygen.hpp.

◆ should_generate()

virtual bool dasmig::component::should_generate ( const generation_context &  ctx) const
inlinevirtual

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 in dasmig::field_component< T >.

Definition at line 385 of file entitygen.hpp.

◆ to_json()

virtual json_value dasmig::component::to_json ( const std::any &  value) const
inlinevirtual

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 in dasmig::field_component< T >.

Definition at line 331 of file entitygen.hpp.

◆ to_string()

virtual std::wstring dasmig::component::to_string ( const std::any &  value) const
pure virtual

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

Implemented in dasmig::constant_component< T, Formatter >, dasmig::choice_component< T, Formatter >, dasmig::range_component< T, Formatter >, dasmig::callback_component< T, GenFn, Formatter >, dasmig::field_component< T >, and dasmig::weighted_choice_component< T, Formatter >.

◆ validate()

virtual bool dasmig::component::validate ( const std::any &  value) const
inlinevirtual

Validate a generated value.

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

Reimplemented in dasmig::field_component< T >.

Definition at line 374 of file entitygen.hpp.

◆ weight()

virtual double dasmig::component::weight ( ) const
inlinevirtual

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 in dasmig::field_component< T >.

Definition at line 368 of file entitygen.hpp.


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