-
Notifications
You must be signed in to change notification settings - Fork 0
API muesli transform_codec
Defined in header <muesli/transform_codec>
template<EncodeFn, DecodeFn, ValueType, InnerCodec>
struct transform_codec : fluent::optionable<transform_codec<InnerCodec, EncodeFn, DecodeFn, ValueType>>,
fluent::constrainable<transform_codec<InnerCodec, EncodeFn, DecodeFn, ValueType>>,
fluent::transformable<transform_codec<InnerCodec, EncodeFn, DecodeFn, ValueType>>,
fluent::projectable<transform_codec<InnerCodec, EncodeFn, DecodeFn, ValueType>>,
fluent::constructable<transform_codec<InnerCodec, EncodeFn, DecodeFn, ValueType>>,
fluent::nullableable<transform_codec<InnerCodec, EncodeFn, DecodeFn, ValueType>>Codec for transforming values during encoding/decoding.
The transform_codec wraps another codec and applies encode/decode functions to transform between the codec's native type and a different value type. Useful for type conversions, scaling, compression, encryption, or any custom transformation logic.
| Parameter | Description |
|---|---|
InnerCodec |
The wrapped codec |
EncodeFn |
Function applied during encoding (value_type -> encoded_type) |
DecodeFn |
Function applied during decoding (InnerCodec::value_type -> value_type) |
ValueType |
The external value type (inferred from DecodeFn) Common use cases: - Temperature conversion (Celsius to Fahrenheit) - Unit scaling (metres -> millimetres) - Data encoding (strings -> bytes) - Compression (large data -> compressed representation) - Encryption/decryption - Custom business logic transformations |
inner_value_type
using inner_value_type = typename InnerCodec::value_typeInner value type from the inner codec
value_type
using value_type = ValueTypeValue type after decode transformation
next_codec_type
using next_codec_type = std::decay_t<decltype(std::declval<InnerCodec>().next_codec())>Next codec type in the encoding chain
encoded_type
using encoded_type = typename next_codec_type::value_typeEncoded type from the next codec
Constructs a transform_codec with inner codec and transformation functions
constexpr transform_codec(const InnerCodec& c, EncodeFn enc, DecodeFn dec) : inner(c), encode_fn(std::move(enc)), decode_fn(std::move(dec))// Temperature codec: store as Celsius internally, work with Fahrenheit externally
auto temp_codec = transform_codec(
float_codec,
[](float fahrenheit) { return (fahrenheit - 32.0f) * 5.0f / 9.0f; }, // encode: F to C
[](float celsius) { return celsius * 9.0f / 5.0f + 32.0f; } // decode: C to F
);
float room_temp_f = 72.5f; // 72.5F
auto encoded = temp_codec.encode(room_temp_f); // Encodes as ~22.5C
auto decoded = temp_codec.decode(encoded); // Decodes back to 72.5F
// Scaling transformation
auto distance_mm = transform_codec(
int32_codec,
[](double meters) { return static_cast<int32_t>(meters * 1000); },
[](int32_t mm) { return mm / 1000.0; }
);
// String to bytes transformation
auto hex_codec = transform_codec(
string_codec,
[](std::span<const uint8_t> bytes) {
return bytes_to_hex(bytes); // custom function
},
[](const std::string& hex) {
return hex_to_bytes(hex); // custom function
}
);- transformable - Fluent API mixin for adding transformations
- transform_input - For encoding-only transformations
- transform_output - For decoding-only transformations
using inner_value_type = typename InnerCodec::value_type; /** @brief Value type after decode transformation */ using value_type = ValueType; /** @brief Next codec type in the encoding chain */ using next_codec_type = std::decay_t<decltype(std::declval<InnerCodec>().next_codec())>; /** @brief Encoded type from the next codec */ using encoded_type = typename next_codec_type::value_type; /** @brief Inner codec used after transformation */ InnerCodec inner; /** @brief Function to transform values before encoding */ EncodeFn encode_fn; /** @brief Function to transform values after decoding */ DecodeFn decode_fn; /** @brief Constructs a transform_codec with inner codec and transformation functions */ constexpr transform_codec(const InnerCodec& c, EncodeFn enc, DecodeFn dec) : inner(c), encode_fn(std::move(enc)), decode_fn(std::move(dec))Inner value type from the inner codec
constexpr next_codec_type next_codec() const noexceptReturns the next codec from the inner codec
template<typename Owner> constexpr value_type project(const Owner& obj) const noexcept requires requires(const InnerCodec& c, const Owner& o)Projects a value from owner and applies decode transformation
template<typename V> constexpr encoded_type encode(V&& value) constApplies encode transformation then encodes with inner codec
template<typename E> constexpr value_type decode(E&& encoded) constDecodes with inner codec then applies decode transformation
template<Codec InnerCodec, typename EncodeFn, typename DecodeFn> constexpr auto transform(const InnerCodec& inner, EncodeFn enc, DecodeFn dec)Creates a transform_codec that maps values during encoding/decoding
| Parameter | Description |
|---|---|
InnerCodec |
The codec for the transformed values |
EncodeFn |
Function type for transforming values before encoding |
DecodeFn |
Function type for transforming values after decoding |
-
inner— The inner codec to use after transformation -
enc— Function to transform values before encoding -
dec— Function to transform values after decoding
Returns: A transform_codec wrapping the inner codec