rapidyaml 0.14.0
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
Sample helpers

Helper utilities used in the sample. More...

Topics

 Serialize/deserialize scalar types
 Serialize/deserialize container types
 To serialize/deserialize container types to a tree, implement the appropriate functions:

Classes

struct  ErrorHandlerExample
 an example error handler, required for some of the quickstart examples. More...
struct  ScopedErrorHandlerExample
 Shows how to create a scoped error handler. More...
struct  GlobalAllocatorExample
struct  PerTreeMemoryExample
 an example for a per-tree memory allocator More...

Macros

#define CHECK(predicate)
 a quick'n'dirty assertion to verify a predicate

Functions

void ensure_callbacks ()
 set up default callbacks when ryml does not provide them (ie when RYML_NO_DEFAULT_CALLBACKS is defined)
ryml::Callbacks default_callbacks ()
 a bare-bones implementation of the callbacks
bool report_check (int line, const char *predicate, bool result)
void handle_args (int argc, const char *argv[])
int report_checks ()
template<class Fn>
bool ErrorHandlerExample::check_assertion_occurs (Fn &&fn)
 checking that an assertion occurs while calling fn.
template<class Fn>
bool ErrorHandlerExample::check_error_occurs (Fn &&fn)
 checking that an error occurs while calling fn
void ErrorHandlerExample::on_error_basic (ryml::csubstr msg, ryml::ErrorDataBasic const &errdata)
 this is where the callback implementation goes.
void ErrorHandlerExample::on_error_parse (ryml::csubstr msg, ryml::ErrorDataParse const &errdata)
 this is where the callback implementation goes.
void ErrorHandlerExample::on_error_visit (ryml::csubstr msg, ryml::ErrorDataVisit const &errdata)
 this is where the callback implementation goes.
static void ErrorHandlerExample::s_error_basic (ryml::csubstr msg, ryml::ErrorDataBasic const &errdata, void *this_)
 trampoline function to call the object's method
static void ErrorHandlerExample::s_error_parse (ryml::csubstr msg, ryml::ErrorDataParse const &errdata, void *this_)
 trampoline function to call the object's method
static void ErrorHandlerExample::s_error_visit (ryml::csubstr msg, ryml::ErrorDataVisit const &errdata, void *this_)
 trampoline function to call the object's method
ryml::Callbacks ErrorHandlerExample::callbacks ()
 a helper to create the Callbacks object for the custom error handler
void ErrorHandlerExample::check_enabled () const
 test that this handler is currently set
void ErrorHandlerExample::check_disabled () const
 test that this handler is currently not set

Variables

static std::jmp_buf s_jmp_env
static std::string s_jmp_msg

Detailed Description

Helper utilities used in the sample.

Macro Definition Documentation

◆ CHECK

#define CHECK ( predicate)
Value:
assert(predicate)

a quick'n'dirty assertion to verify a predicate

Definition at line 305 of file quickstart.cpp.

Function Documentation

◆ ensure_callbacks()

void ensure_callbacks ( )

set up default callbacks when ryml does not provide them (ie when RYML_NO_DEFAULT_CALLBACKS is defined)

Definition at line 6376 of file quickstart.cpp.

6377{
6378#ifdef RYML_NO_DEFAULT_CALLBACKS
6380#endif
6381}
void set_callbacks(Callbacks const &c)
set the global callbacks for the library; after a call to this function, these callbacks will be used...
Definition common.cpp:89
ryml::Callbacks default_callbacks()
a bare-bones implementation of the callbacks

◆ default_callbacks()

ryml::Callbacks default_callbacks ( )

a bare-bones implementation of the callbacks

Definition at line 6336 of file quickstart.cpp.

6337{
6338 return ryml::Callbacks{}
6339 .set_allocate([](size_t len, void* , void *){
6340 return malloc(len); // NOLINT
6341 })
6342 .set_free([](void* mem, size_t, void *){
6343 free(mem); // NOLINT
6344 })
6345 //
6346 // The error callbacks won't be called in this quickstart,
6347 // because no errors are expected. But we implement them here
6348 // to show how a bare-bones implementation looks like.
6349 //
6350 // For a different (more involved) implementation of the error
6351 // callbacks, see the implementation of ErrorHandlerExample
6352 // below.
6353 //
6354 // LCOV_EXCL_START
6355 .set_error_basic([](ryml::csubstr msg, ryml::ErrorDataBasic const& errdata, void *){
6356 ryml::err_basic_format(errdump, msg, errdata); // format the message, printing to stderr
6357 errend(); // print newline and flush
6358 abort(); // abort (must never return: abort, or exception or setjmp)
6359 })
6360 .set_error_parse([](ryml::csubstr msg, ryml::ErrorDataParse const& errdata, void *){
6361 ryml::err_parse_format(errdump, msg, errdata); // format the message, printing to out
6362 errend(); // print newline and flush
6363 abort(); // abort (must never return: abort, or exception or setjmp)
6364 })
6365 .set_error_visit([](ryml::csubstr msg, ryml::ErrorDataVisit const& errdata, void *){
6366 ryml::err_visit_format(errdump, msg, errdata); // format the message, printing to out
6367 errend(); // print newline and flush
6368 abort(); // abort (must never return: abort, or exception or setjmp)
6369 });
6370 // LCOV_EXCL_STOP
6371}
void err_visit_format(DumpFn &&dumpfn, csubstr msg, ErrorDataVisit const &errdata)
Given an error message and associated visit error data, format it fully as a visit error message.
void err_basic_format(DumpFn &&dumpfn, csubstr msg, ErrorDataBasic const &errdata)
Given an error message and associated basic error data, format it fully as a basic error message.
void err_parse_format(DumpFn &&dumpfn, csubstr msg, ErrorDataParse const &errdata)
Given an error message and associated parse error data, format it fully as a parse error message.
basic_substring< const char > csubstr
an immutable string view
Definition substr.hpp:2357
A c-style callbacks class to customize behavior on errors or allocation.
Definition common.hpp:546
Callbacks & set_allocate(pfn_allocate allocate=nullptr)
Set or reset the allocate callback.
Definition common.cpp:150
Data for a basic error.
Definition common.hpp:320
Data for a parse error.
Definition common.hpp:329
Data for a visit error.
Definition common.hpp:339

◆ report_check()

bool report_check ( int line,
const char * predicate,
bool result )

Definition at line 6282 of file quickstart.cpp.

6283{
6284 ++num_checks;
6285 const char *msg = predicate ? "OK! " : "OK!";
6286 if(!result)
6287 {
6288 ++num_failed_checks; // LCOV_EXCL_LINE
6289 msg = predicate ? "FAIL: " : "FAIL"; // LCOV_EXCL_LINE
6290 }
6291 if(!result || !quiet_mode)
6292 {
6293 std::cout << __FILE__ << ':' << line << ": " << msg << (predicate ? predicate : "") << std::endl;
6294 }
6295 return result;
6296}

◆ handle_args()

void handle_args ( int argc,
const char * argv[] )

Definition at line 6272 of file quickstart.cpp.

6273{
6274 auto arg_matches = [](const char *arg, const char *shortform, const char *longform) {
6275 return (0 == strcmp(arg, shortform) || 0 == strcmp(arg, longform));
6276 };
6277 for(int i = 1; i < argc; ++i)
6278 if(arg_matches(argv[i], "-q", "--quiet"))
6279 quiet_mode = true;
6280}

◆ report_checks()

int report_checks ( )

Definition at line 6299 of file quickstart.cpp.

6300{
6301 std::cout << "Completed " << num_checks << " checks." << std::endl;
6302 if(num_failed_checks)
6303 std::cout << "ERROR: " << num_failed_checks << '/' << num_checks << " checks failed." << std::endl;
6304 else
6305 std::cout << "SUCCESS!" << std::endl;
6306 return num_failed_checks;
6307}

◆ check_assertion_occurs()

template<class Fn>
bool ErrorHandlerExample::check_assertion_occurs ( Fn && fn)

checking that an assertion occurs while calling fn.

assertions are enabled if RYML_USE_ASSERT is defined.

Definition at line 6397 of file quickstart.cpp.

6398{
6399 #if RYML_USE_ASSERT
6400 return check_error_occurs(std::forward<Fn>(fn));
6401 #else
6402 (void)fn; // do nothing otherwise, as there would be undefined behavior
6403 return true;
6404 #endif
6405}
bool check_error_occurs(Fn &&fn)
checking that an error occurs while calling fn

◆ check_error_occurs()

template<class Fn>
bool ErrorHandlerExample::check_error_occurs ( Fn && fn)

checking that an error occurs while calling fn

Definition at line 6409 of file quickstart.cpp.

6410{
6411 saved_msg_short.clear();
6412 saved_msg_full.clear();
6414 saved_basic_loc = {};
6415 saved_parse_loc = {};
6416 saved_visit_tree = {};
6418 bool got_error = false;
6419 #ifdef C4_EXCEPTIONS
6420 try
6421 {
6422 std::forward<Fn>(fn)();
6423 }
6424 catch(std::exception const&)
6425 {
6426 got_error = true;
6427 }
6428 #else
6429 if(setjmp(s_jmp_env) == 0)
6430 {
6431 std::forward<Fn>(fn)();
6432 }
6433 else
6434 {
6435 got_error = true;
6436 }
6437 #endif
6438 return got_error;
6439}
static std::jmp_buf s_jmp_env
@ NONE
an index to none
Definition common.hpp:256
ryml::Location saved_basic_loc
std::string saved_msg_full
ryml::id_type saved_visit_id
std::string saved_msg_short
ryml::Tree const * saved_visit_tree
ryml::Location saved_parse_loc
std::string saved_msg_full_with_context

◆ on_error_basic()

void ErrorHandlerExample::on_error_basic ( ryml::csubstr msg,
ryml::ErrorDataBasic const & errdata )

this is where the callback implementation goes.

Remember that it must not return.

Definition at line 6457 of file quickstart.cpp.

6458{
6459 saved_msg_short.assign(msg.str, msg.len);
6460 // build a full error message with location
6462 saved_msg_full.append(s.str, s.len);
6463 }, msg, errdata);
6464 // Save the error params for subsequent testing in the quickstart.
6466 saved_basic_loc = errdata.location;
6467 stopexec(saved_msg_short);
6468}
size_t len
the length of the substring
Definition substr.hpp:218
C * str
a restricted pointer to the first character of the substring
Definition substr.hpp:216

◆ on_error_parse()

void ErrorHandlerExample::on_error_parse ( ryml::csubstr msg,
ryml::ErrorDataParse const & errdata )

this is where the callback implementation goes.

Remember that it must not return.

See also
ryml::format_location_context

Definition at line 6473 of file quickstart.cpp.

6474{
6475 saved_msg_short.assign(msg.str, msg.len);
6476 // build a full error message with location
6478 saved_msg_full.append(s.str, s.len);
6479 }, msg, errdata);
6480 // Save the error params for subsequent testing in the quickstart.
6481 //
6482 // To add the source context, the source buffer is required. If
6483 // the caller is interested in enriching the full message with the
6484 // source buffer context, he can ensure that the source buffer is
6485 // kept, and then arrange the handler to access it.
6486 // For now, we assign the full message without context:
6488 saved_basic_loc = errdata.cpploc;
6489 saved_parse_loc = errdata.ymlloc;
6490 stopexec(saved_msg_full);
6491}

◆ on_error_visit()

void ErrorHandlerExample::on_error_visit ( ryml::csubstr msg,
ryml::ErrorDataVisit const & errdata )

this is where the callback implementation goes.

Remember that it must not return.

Definition at line 6495 of file quickstart.cpp.

6496{
6497 saved_msg_short.assign(msg.str, msg.len);
6498 // build a full error message with location
6500 saved_msg_full.append(s.str, s.len);
6501 }, msg, errdata);
6502 // Save the error params for subsequent testing in the quickstart.
6503 //
6504 // To add the source context, the source buffer is required. If
6505 // the caller is interested in enriching the full message with the
6506 // source buffer context, he can ensure that the source buffer is
6507 // kept, and then arrange the handler to access it.
6508 // For now, we assign the full message without context:
6510 saved_basic_loc = errdata.cpploc;
6511 saved_visit_tree = errdata.tree;
6512 saved_visit_id = errdata.node;
6513 stopexec(saved_msg_full);
6514}

◆ s_error_basic()

void ErrorHandlerExample::s_error_basic ( ryml::csubstr msg,
ryml::ErrorDataBasic const & errdata,
void * this_ )
static

trampoline function to call the object's method

Definition at line 6517 of file quickstart.cpp.

6518{
6519 static_cast<ErrorHandlerExample*>(this_)->on_error_basic(msg, errdata);
6520}
void on_error_basic(ryml::csubstr msg, ryml::ErrorDataBasic const &errdata)
this is where the callback implementation goes.

◆ s_error_parse()

void ErrorHandlerExample::s_error_parse ( ryml::csubstr msg,
ryml::ErrorDataParse const & errdata,
void * this_ )
static

trampoline function to call the object's method

Definition at line 6522 of file quickstart.cpp.

6523{
6524 static_cast<ErrorHandlerExample*>(this_)->on_error_parse(msg, errdata);
6525}
void on_error_parse(ryml::csubstr msg, ryml::ErrorDataParse const &errdata)
this is where the callback implementation goes.

◆ s_error_visit()

void ErrorHandlerExample::s_error_visit ( ryml::csubstr msg,
ryml::ErrorDataVisit const & errdata,
void * this_ )
static

trampoline function to call the object's method

Definition at line 6527 of file quickstart.cpp.

6528{
6529 static_cast<ErrorHandlerExample*>(this_)->on_error_visit(msg, errdata);
6530}
void on_error_visit(ryml::csubstr msg, ryml::ErrorDataVisit const &errdata)
this is where the callback implementation goes.

◆ callbacks()

ryml::Callbacks ErrorHandlerExample::callbacks ( )

a helper to create the Callbacks object for the custom error handler

Definition at line 6536 of file quickstart.cpp.

6537{
6538 ryml::Callbacks copy = original_callbacks;
6539 copy.set_user_data(this)
6543 return copy;
6544}
static void s_error_basic(ryml::csubstr msg, ryml::ErrorDataBasic const &errdata, void *this_)
trampoline function to call the object's method
static void s_error_parse(ryml::csubstr msg, ryml::ErrorDataParse const &errdata, void *this_)
trampoline function to call the object's method
static void s_error_visit(ryml::csubstr msg, ryml::ErrorDataVisit const &errdata, void *this_)
trampoline function to call the object's method
ryml::Callbacks original_callbacks
Callbacks & set_error_visit(pfn_error_visit error_visit=nullptr)
Set or reset the error_visit callback.
Definition common.cpp:186
Callbacks & set_error_parse(pfn_error_parse error_parse=nullptr)
Set or reset the error_parse callback.
Definition common.cpp:177
Callbacks & set_error_basic(pfn_error_basic error_basic=nullptr)
Set or reset the error_basic callback.
Definition common.cpp:168
Callbacks & set_user_data(void *user_data)
Set the user data.
Definition common.cpp:144

◆ check_enabled()

void ErrorHandlerExample::check_enabled ( ) const

test that this handler is currently set

Definition at line 6547 of file quickstart.cpp.

6548{
6549 ryml::Callbacks const& current = ryml::get_callbacks();
6550 CHECK(current.m_error_basic == &s_error_basic);
6551 CHECK(current.m_error_parse == &s_error_parse);
6552 CHECK(current.m_error_visit == &s_error_visit);
6553 CHECK(current.m_allocate == original_callbacks.m_allocate);
6554 CHECK(current.m_free == original_callbacks.m_free);
6555}
Callbacks const & get_callbacks()
get the global callbacks
Definition common.cpp:94
#define CHECK(predicate)
a quick'n'dirty assertion to verify a predicate
pfn_error_basic m_error_basic
a pointer to a basic error handler function
Definition common.hpp:550
pfn_error_parse m_error_parse
a pointer to a parse error handler function
Definition common.hpp:551
pfn_allocate m_allocate
a pointer to an allocate handler function
Definition common.hpp:548
pfn_error_visit m_error_visit
a pointer to a visit error handler function
Definition common.hpp:552
pfn_free m_free
a pointer to a free handler function
Definition common.hpp:549

◆ check_disabled()

void ErrorHandlerExample::check_disabled ( ) const

test that this handler is currently not set

Definition at line 6557 of file quickstart.cpp.

6558{
6559 ryml::Callbacks const& current = ryml::get_callbacks();
6560 CHECK(current.m_error_basic != &s_error_basic);
6561 CHECK(current.m_error_parse != &s_error_parse);
6562 CHECK(current.m_error_visit != &s_error_visit);
6563 CHECK(current.m_allocate == original_callbacks.m_allocate);
6564 CHECK(current.m_free == original_callbacks.m_free);
6565}

Variable Documentation

◆ s_jmp_env

std::jmp_buf s_jmp_env
static

Definition at line 6389 of file quickstart.cpp.

◆ s_jmp_msg

std::string s_jmp_msg
static

Definition at line 6390 of file quickstart.cpp.