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

Component that picks from a list of values using per-option weights. More...

#include <entitygen.hpp>

Inheritance diagram for dasmig::weighted_choice_component< T, Formatter >:
dasmig::component

Classes

struct  option
 A value–weight pair for weighted selection. More...
 

Public Member Functions

 weighted_choice_component (std::wstring key, std::vector< option > options, Formatter fmt={})
 Construct a weighted choice component.
 
std::wstring key () const override
 Unique key identifying this component (e.g. "name", "age").
 
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.
 
- 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.
 
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.
 

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, typename Formatter = use_default_formatter>
class dasmig::weighted_choice_component< T, Formatter >

Component that picks from a list of values using per-option weights.

Weights are relative (they don't need to sum to 1.0). A weight of 0 means the option is never selected. The choice is dasmig-core's weighted choice (the same algorithm as core::weighted_table), built once.

Template Parameters
TThe value type.
FormatterOptional callable for custom string conversion.

Definition at line 824 of file entitygen.hpp.

Constructor & Destructor Documentation

◆ weighted_choice_component()

template<typename T , typename Formatter = use_default_formatter>
dasmig::weighted_choice_component< T, Formatter >::weighted_choice_component ( std::wstring  key,
std::vector< option >  options,
Formatter  fmt = {} 
)
inlineexplicit

Construct a weighted choice component.

Parameters
keyUnique component key.
optionsNon-empty list of value–weight pairs. At least one must have a positive weight.
fmtOptional custom formatter.
Exceptions
std::invalid_argumentIf options is empty, a weight is negative or not finite, or all weights are zero.

Definition at line 841 of file entitygen.hpp.

Member Function Documentation

◆ generate()

template<typename T , typename Formatter = use_default_formatter>
std::any dasmig::weighted_choice_component< T, Formatter >::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 851 of file entitygen.hpp.

◆ key()

template<typename T , typename Formatter = use_default_formatter>
std::wstring dasmig::weighted_choice_component< T, Formatter >::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 849 of file entitygen.hpp.

◆ to_string()

template<typename T , typename Formatter = use_default_formatter>
std::wstring dasmig::weighted_choice_component< T, Formatter >::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 857 of file entitygen.hpp.


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