rapidyaml  0.7.0
parse and emit YAML, and do it fast
Define from_chars to read scalar types

To deserialize user scalar types, implement the function bool from_chars(csubstr buf, T *val); see from_chars: generalized chars to value. More...

Functions

template<class T >
bool sample::from_chars (ryml::csubstr buf, vec2< T > *v)
 
template<class T >
bool sample::from_chars (ryml::csubstr buf, vec3< T > *v)
 
template<class T >
bool sample::from_chars (ryml::csubstr buf, vec4< T > *v)
 
template<class T >
bool sample::from_chars (ryml::csubstr buf, parse_only_vec2< T > *v)
 
template<class T >
bool sample::from_chars (ryml::csubstr buf, parse_only_vec3< T > *v)
 
template<class T >
bool sample::from_chars (ryml::csubstr buf, parse_only_vec4< T > *v)
 

Detailed Description

To deserialize user scalar types, implement the function bool from_chars(csubstr buf, T *val); see from_chars: generalized chars to value.

The implementation of from_chars must never read beyond the limit of the given buffer, and must return true/false to indicate success/failure in the deserialization. On failure, it is up to you whether the value is left unchanged; ryml itself does not care about the value when the deserialization failed.

In your implementation, you may be interested in using the reading 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 from_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 from_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.

Function Documentation

◆ from_chars() [1/6]

template<class T >
bool sample::from_chars ( ryml::csubstr  buf,
vec2< T > *  v 
)

Definition at line 3463 of file quickstart.cpp.

3463 { size_t ret = ryml::unformat(buf, "({},{})", v->x, v->y); return ret != ryml::yml::npos; }
size_t unformat(csubstr buf, csubstr fmt, Arg &a, Args &...more)
using a format string, deserialize the arguments from the given buffer.
Definition: format.hpp:855
@ npos
a null string position
Definition: common.hpp:266

References c4::yml::npos, c4::unformat(), sample::vec2< T >::x, and sample::vec2< T >::y.

Referenced by c4::yml::from_chars_float().

◆ from_chars() [2/6]

template<class T >
bool sample::from_chars ( ryml::csubstr  buf,
vec3< T > *  v 
)

Definition at line 3464 of file quickstart.cpp.

3464 { size_t ret = ryml::unformat(buf, "({},{},{})", v->x, v->y, v->z); return ret != ryml::yml::npos; }

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

◆ from_chars() [3/6]

template<class T >
bool sample::from_chars ( ryml::csubstr  buf,
vec4< T > *  v 
)

Definition at line 3465 of file quickstart.cpp.

3465 { size_t ret = ryml::unformat(buf, "({},{},{},{})", v->x, v->y, v->z, v->w); return ret != ryml::yml::npos; }

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

◆ from_chars() [4/6]

template<class T >
bool sample::from_chars ( ryml::csubstr  buf,
parse_only_vec2< T > *  v 
)

Definition at line 3467 of file quickstart.cpp.

3467 { size_t ret = ryml::unformat(buf, "({},{})", v->x, v->y); return ret != ryml::yml::npos; }

References c4::yml::npos, c4::unformat(), sample::parse_only_vec2< T >::x, and sample::parse_only_vec2< T >::y.

◆ from_chars() [5/6]

template<class T >
bool sample::from_chars ( ryml::csubstr  buf,
parse_only_vec3< T > *  v 
)

Definition at line 3468 of file quickstart.cpp.

3468 { size_t ret = ryml::unformat(buf, "({},{},{})", v->x, v->y, v->z); return ret != ryml::yml::npos; }

References c4::yml::npos, c4::unformat(), sample::parse_only_vec3< T >::x, sample::parse_only_vec3< T >::y, and sample::parse_only_vec3< T >::z.

◆ from_chars() [6/6]

template<class T >
bool sample::from_chars ( ryml::csubstr  buf,
parse_only_vec4< T > *  v 
)

Definition at line 3469 of file quickstart.cpp.

3469 { size_t ret = ryml::unformat(buf, "({},{},{},{})", v->x, v->y, v->z, v->w); return ret != ryml::yml::npos; }

References c4::yml::npos, c4::unformat(), sample::parse_only_vec4< T >::w, sample::parse_only_vec4< T >::x, sample::parse_only_vec4< T >::y, and sample::parse_only_vec4< T >::z.