Skip to content

API muesli projector_codec

Felix Jones edited this page Jan 15, 2026 · 1 revision

muesli/projector_codec

Defined in header <muesli/projector_codec>

projector_codec

template<Owner, Projector>
struct projector_codec : fluent::optionable<projector_codec<Owner, FieldCodec, Projector>>,
                         fluent::constrainable<projector_codec<Owner, FieldCodec, Projector>>,
                         fluent::transformable<projector_codec<Owner, FieldCodec, Projector>>,
                         fluent::constructable<projector_codec<Owner, FieldCodec, Projector>>

Codec for projecting values from an owner type via custom lambda.

The projector_codec allows encoding a derived/projected value from an owner object using a custom projection function, enabling flexible value extraction patterns.

Template Parameters

Parameter Description
Owner The type containing the value to project
FieldCodec The codec for the projected field type
Projector Function taking const Owner& and returning the projected value

Member Types

owner_type

using owner_type = Owner

The owner type containing the value

projector_type

using projector_type = Projector

The projector function type

value_type

using value_type = std::invoke_result_t<Projector, const Owner&>

Value type is the result of invoking the projector

next_codec_type

using next_codec_type = std::decay_t<decltype(std::declval<FieldCodec>().next_codec())>

Next codec in the encoding chain

encoded_type

using encoded_type = typename next_codec_type::value_type

Encoded type from the next codec

Member Functions

move

Constructs a projector_codec with field codec and projector function

constexpr projector_codec(const FieldCodec& fc, Projector proj) noexcept : field_codec(fc), projector(std::move(proj))

Example

struct Rectangle { int width, height; };
auto area_codec = projector_codec<Rectangle>(
    int_codec,
    [](const Rectangle& r) { return r.width * r.height; }
);

move

using owner_type = Owner; /** @brief The projector function type */ using projector_type = Projector; /** @brief Value type is the result of invoking the projector */ using value_type = std::invoke_result_t<Projector, const Owner&>; /** @brief Next codec in the encoding chain */ using next_codec_type = std::decay_t<decltype(std::declval<FieldCodec>().next_codec())>; /** @brief Encoded type from the next codec */ using encoded_type = typename next_codec_type::value_type; /** @brief Codec for encoding/decoding the projected field */ FieldCodec field_codec; /** @brief Function to project values from owner objects */ Projector projector; /** @brief Constructs a projector_codec with field codec and projector function */ constexpr projector_codec(const FieldCodec& fc, Projector proj) noexcept : field_codec(fc), projector(std::move(proj))

The owner type containing the value


next_codec

constexpr next_codec_type next_codec() const noexcept

Returns the next codec in the encoding chain


encode

constexpr encoded_type encode(const value_type& value) const noexcept

Encodes a projected value


decode

constexpr value_type decode(const auto& value) const noexcept

Decodes to the projected value type


project

constexpr value_type project(const owner_type& obj) const noexcept

Projects a value from an owner object using the projector function


projector_of

template<typename Owner, Codec ElementCodec, typename Projector> constexpr auto projector_of(const ElementCodec& elem, Projector projector)

Helper function to create a projector_codec

Template Parameters

Parameter Description
Owner The type containing the value to project

Parameters

  • elem — The codec for the projected value
  • projector — Function to extract the value from the owner

Returns: A projector_codec wrapping the element codec


Clone this wiki locally