quill
Classes | Static Public Member Functions | Static Public Attributes | List of all members
DeferredFormatCodec< T > Struct Template Reference

Provides serialization (codec) functionality for complex user-defined types. More...

#include <DeferredFormatCodec.h>

Static Public Member Functions

static size_t compute_encoded_size (detail::SizeCacheVector &, T const &) noexcept
 
template<typename Arg >
static void encode (std::byte *&buffer, detail::SizeCacheVector const &, uint32_t &, Arg &&arg)
 
static T decode_arg (std::byte *&buffer)
 
static void decode_and_store_arg (std::byte *&buffer, DynamicFormatArgStore *args_store)
 

Static Public Attributes

static constexpr bool use_memcpy
 

Detailed Description

template<typename T>
struct DeferredFormatCodec< T >

Provides serialization (codec) functionality for complex user-defined types.

This codec minimizes overhead on the hot-path by directly using std::memcpy or placement new to serialize objects into the SPSC buffer,

This approach avoids expensive string formatting on the hot path.

For a non trivially copyable types it requires valid copy constructor and move constructor.

Thread-Safety for non trivially copyable types: It is the user's responsibility to ensure that an non trivially copyable type remains thread-safe after being copied. For example, if the object contains a shared_ptr, ensure that its underlying value will not be modified.

Example usage:

class User
{
public:
User(std::string name, std::string surname, uint32_t age)
: name(std::move(name)), surname(std::move(surname)), age(age)
{
favorite_colors.push_back("red");
favorite_colors.push_back("blue");
favorite_colors.push_back("green");
};
std::string name;
std::string surname;
uint32_t age;
std::vector<std::string> favorite_colors;
};
template <>
struct fmtquill::formatter<User>
{
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
auto format(::User const& user, format_context& ctx) const
{
return fmtquill::format_to(ctx.out(), "Name: {}, Surname: {}, Age: {}, Favorite Colors: {}",
user.name, user.surname, user.age, user.favorite_colors);
}
};
template <>
struct quill::Codec<User> : quill::DeferredFormatCodec<User>
{
};
int main()
{
// ... init code
User user_1{"Super", "User", 1};
LOG_INFO(logger, "User is [{}]", user_1);
}

Member Data Documentation

◆ use_memcpy

template<typename T>
constexpr bool DeferredFormatCodec< T >::use_memcpy
static
Initial value:
=
std::conjunction_v<std::is_trivially_copyable<T>, std::is_default_constructible<T>>

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