rapidyaml  0.8.0
parse and emit YAML, and do it fast
Define to_chars to write scalar types

To serialize user scalar types, implement the appropriate function to_chars (see also to_chars: generalized chars to value): More...

Functions

template<class T >
size_t sample::to_chars (ryml::substr buf, vec2< T > v)
 
template<class T >
size_t sample::to_chars (ryml::substr buf, vec3< T > v)
 
template<class T >
size_t sample::to_chars (ryml::substr buf, vec4< T > v)
 
template<class T >
size_t sample::to_chars (ryml::substr buf, emit_only_vec2< T > v)
 
template<class T >
size_t sample::to_chars (ryml::substr buf, emit_only_vec3< T > v)
 
template<class T >
size_t sample::to_chars (ryml::substr buf, emit_only_vec4< T > v)
 

Detailed Description

To serialize user scalar types, implement the appropriate function to_chars (see also to_chars: generalized chars to value):

// any of these can be used:
size_t to_chars(substr buf, T const& v);
size_t to_chars(substr buf, T v); // this also works, and is good when the type is small
size_t to_chars(ryml::substr buf, vec2< T > v)

See the sample sample_user_scalar_types() for an example usage.

Your implementation of to_chars must format v to the given string view + return the number of characters written into it. The view size (buf.len) must be strictly respected. Return the number of characters that need to be written for the value to be completely serialized in the string. So if the return value is larger than buf.len, ryml will know that the buffer resize the buffer and call this again with a larger buffer of the correct size.

In your implementation, you may be interested in using the formatting facilities in Format utilities and Charconv utilities; refer to their documentation for further details. But this is not mandatory, and anything can be used, provided that the implemented to_chars() fulfills its contract, described above.

Warning
Because of C++'s ADL rules, it is required to overload these functions in the namespace of the type you're serializing (or in the c4 namespace, or in the c4::yml namespace). [Here's an example of an issue where failing to do this was causing problems in some platforms](https://github.com/biojppm/rapidyaml/issues/424)
Note
Please take note of the following pitfall when using serialization functions: you may have to include the header with your to_chars() implementation before any other headers that use functions from it. See the include order at the top of this source file. This constraint also applies to the conversion functions for your types; just like with the STL's headers, they should be included prior to ryml's headers. Lately, some effort was directed to provide forward declarations to alleviate this problem, but it may still occur.
See also
string.hpp
string_view.hpp

Function Documentation

◆ to_chars() [1/6]

template<class T >
size_t sample::to_chars ( ryml::substr  buf,
vec2< T >  v 
)

Definition at line 3443 of file quickstart.cpp.

3443 { return ryml::format(buf, "({},{})", v.x, v.y); }
size_t format(substr buf, csubstr fmt, Arg const &a, Args const &...more)
using a format string, serialize the arguments into the given fixed-size buffer.
Definition: format.hpp:816

References c4::format(), sample::vec2< T >::x, and sample::vec2< T >::y.

Referenced by c4::yml::Tree::to_arena(), and c4::yml::to_chars_float().

◆ to_chars() [2/6]

template<class T >
size_t sample::to_chars ( ryml::substr  buf,
vec3< T >  v 
)

Definition at line 3444 of file quickstart.cpp.

3444 { return ryml::format(buf, "({},{},{})", v.x, v.y, v.z); }

References c4::format(), sample::vec3< T >::x, sample::vec3< T >::y, and sample::vec3< T >::z.

◆ to_chars() [3/6]

template<class T >
size_t sample::to_chars ( ryml::substr  buf,
vec4< T >  v 
)

Definition at line 3445 of file quickstart.cpp.

3445 { return ryml::format(buf, "({},{},{},{})", v.x, v.y, v.z, v.w); }

References c4::format(), sample::vec4< T >::w, sample::vec4< T >::x, sample::vec4< T >::y, and sample::vec4< T >::z.

◆ to_chars() [4/6]

template<class T >
size_t sample::to_chars ( ryml::substr  buf,
emit_only_vec2< T >  v 
)

Definition at line 3447 of file quickstart.cpp.

3447 { return ryml::format(buf, "({},{})", v.x, v.y); }

References c4::format(), sample::emit_only_vec2< T >::x, and sample::emit_only_vec2< T >::y.

◆ to_chars() [5/6]

template<class T >
size_t sample::to_chars ( ryml::substr  buf,
emit_only_vec3< T >  v 
)

Definition at line 3448 of file quickstart.cpp.

3448 { return ryml::format(buf, "({},{},{})", v.x, v.y, v.z); }

References c4::format(), sample::emit_only_vec3< T >::x, sample::emit_only_vec3< T >::y, and sample::emit_only_vec3< T >::z.

◆ to_chars() [6/6]

template<class T >
size_t sample::to_chars ( ryml::substr  buf,
emit_only_vec4< T >  v 
)

Definition at line 3449 of file quickstart.cpp.

3449 { return ryml::format(buf, "({},{},{},{})", v.x, v.y, v.z, v.w); }

References c4::format(), sample::emit_only_vec4< T >::w, sample::emit_only_vec4< T >::x, sample::emit_only_vec4< T >::y, and sample::emit_only_vec4< T >::z.