rapidyaml 0.15.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 6570 of file quickstart.cpp.

6571{
6572#ifdef RYML_NO_DEFAULT_CALLBACKS
6574#endif
6575}
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 6530 of file quickstart.cpp.

6531{
6532 return ryml::Callbacks{}
6533 .set_allocate([](size_t len, void* , void *){
6534 return malloc(len); // NOLINT
6535 })
6536 .set_free([](void* mem, size_t, void *){
6537 free(mem); // NOLINT
6538 })
6539 //
6540 // The error callbacks won't be called in this quickstart,
6541 // because no errors are expected. But we implement them here
6542 // to show how a bare-bones implementation looks like.
6543 //
6544 // For a different (more involved) implementation of the error
6545 // callbacks, see the implementation of ErrorHandlerExample
6546 // below.
6547 //
6548 // LCOV_EXCL_START
6549 .set_error_basic([](ryml::csubstr msg, ryml::ErrorDataBasic const& errdata, void *){
6550 ryml::err_basic_format(errdump, msg, errdata); // format the message, printing to stderr
6551 errend(); // print newline and flush
6552 abort(); // abort (must never return: abort, or exception or setjmp)
6553 })
6554 .set_error_parse([](ryml::csubstr msg, ryml::ErrorDataParse const& errdata, void *){
6555 ryml::err_parse_format(errdump, msg, errdata); // format the message, printing to out
6556 errend(); // print newline and flush
6557 abort(); // abort (must never return: abort, or exception or setjmp)
6558 })
6559 .set_error_visit([](ryml::csubstr msg, ryml::ErrorDataVisit const& errdata, void *){
6560 ryml::err_visit_format(errdump, msg, errdata); // format the message, printing to out
6561 errend(); // print newline and flush
6562 abort(); // abort (must never return: abort, or exception or setjmp)
6563 });
6564 // LCOV_EXCL_STOP
6565}
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:441
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:327
Data for a parse error.
Definition common.hpp:336
Data for a visit error.
Definition common.hpp:346

◆ report_check()

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

Definition at line 6476 of file quickstart.cpp.

6477{
6478 ++num_checks;
6479 const char *msg = predicate ? "OK! " : "OK!";
6480 if(!result)
6481 {
6482 ++num_failed_checks; // LCOV_EXCL_LINE
6483 msg = predicate ? "FAIL: " : "FAIL"; // LCOV_EXCL_LINE
6484 }
6485 if(!result || !quiet_mode)
6486 {
6487 std::cout << __FILE__ << ':' << line << ": " << msg << (predicate ? predicate : "") << std::endl;
6488 }
6489 return result;
6490}

◆ handle_args()

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

Definition at line 6466 of file quickstart.cpp.

6467{
6468 auto arg_matches = [](const char *arg, const char *shortform, const char *longform) {
6469 return (0 == strcmp(arg, shortform) || 0 == strcmp(arg, longform));
6470 };
6471 for(int i = 1; i < argc; ++i)
6472 if(arg_matches(argv[i], "-q", "--quiet"))
6473 quiet_mode = true;
6474}

◆ report_checks()

int report_checks ( )

Definition at line 6493 of file quickstart.cpp.

6494{
6495 std::cout << "Completed " << num_checks << " checks." << std::endl;
6496 if(num_failed_checks)
6497 std::cout << "ERROR: " << num_failed_checks << '/' << num_checks << " checks failed." << std::endl;
6498 else
6499 std::cout << "SUCCESS!" << std::endl;
6500 return num_failed_checks;
6501}

◆ 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 6591 of file quickstart.cpp.

6592{
6593 #if RYML_USE_ASSERT
6594 return check_error_occurs(std::forward<Fn>(fn));
6595 #else
6596 (void)fn; // do nothing otherwise, as there would be undefined behavior
6597 return true;
6598 #endif
6599}
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 6603 of file quickstart.cpp.

6604{
6605 saved_msg_short.clear();
6606 saved_msg_full.clear();
6608 saved_basic_loc = {};
6609 saved_parse_loc = {};
6610 saved_visit_tree = {};
6612 bool got_error = false;
6613 #ifdef C4_EXCEPTIONS
6614 try
6615 {
6616 std::forward<Fn>(fn)();
6617 }
6618 catch(std::exception const&)
6619 {
6620 got_error = true;
6621 }
6622 #else
6623 if(setjmp(s_jmp_env) == 0)
6624 {
6625 std::forward<Fn>(fn)();
6626 }
6627 else
6628 {
6629 got_error = true;
6630 }
6631 #endif
6632 return got_error;
6633}
static std::jmp_buf s_jmp_env
@ NONE
an index to none
Definition common.hpp:263
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 6651 of file quickstart.cpp.

6652{
6653 saved_msg_short.assign(msg.str, msg.len);
6654 // build a full error message with location
6656 saved_msg_full.append(s.str, s.len);
6657 }, msg, errdata);
6658 // Save the error params for subsequent testing in the quickstart.
6660 saved_basic_loc = errdata.location;
6661 stopexec(saved_msg_short);
6662}
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 6667 of file quickstart.cpp.

6668{
6669 saved_msg_short.assign(msg.str, msg.len);
6670 // build a full error message with location
6672 saved_msg_full.append(s.str, s.len);
6673 }, msg, errdata);
6674 // Save the error params for subsequent testing in the quickstart.
6675 //
6676 // To add the source context, the source buffer is required. If
6677 // the caller is interested in enriching the full message with the
6678 // source buffer context, he can ensure that the source buffer is
6679 // kept, and then arrange the handler to access it.
6680 // For now, we assign the full message without context:
6682 saved_basic_loc = errdata.cpploc;
6683 saved_parse_loc = errdata.ymlloc;
6684 stopexec(saved_msg_full);
6685}

◆ 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 6689 of file quickstart.cpp.

6690{
6691 saved_msg_short.assign(msg.str, msg.len);
6692 // build a full error message with location
6694 saved_msg_full.append(s.str, s.len);
6695 }, msg, errdata);
6696 // Save the error params for subsequent testing in the quickstart.
6697 //
6698 // To add the source context, the source buffer is required. If
6699 // the caller is interested in enriching the full message with the
6700 // source buffer context, he can ensure that the source buffer is
6701 // kept, and then arrange the handler to access it.
6702 // For now, we assign the full message without context:
6704 saved_basic_loc = errdata.cpploc;
6705 saved_visit_tree = errdata.tree;
6706 saved_visit_id = errdata.node;
6707 stopexec(saved_msg_full);
6708}

◆ 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 6711 of file quickstart.cpp.

6712{
6713 static_cast<ErrorHandlerExample*>(this_)->on_error_basic(msg, errdata);
6714}
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 6716 of file quickstart.cpp.

6717{
6718 static_cast<ErrorHandlerExample*>(this_)->on_error_parse(msg, errdata);
6719}
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 6721 of file quickstart.cpp.

6722{
6723 static_cast<ErrorHandlerExample*>(this_)->on_error_visit(msg, errdata);
6724}
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 6730 of file quickstart.cpp.

6731{
6732 ryml::Callbacks copy = original_callbacks;
6733 copy.set_user_data(this)
6737 return copy;
6738}
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 6741 of file quickstart.cpp.

6742{
6743 ryml::Callbacks const& current = ryml::get_callbacks();
6744 CHECK(current.m_error_basic == &s_error_basic);
6745 CHECK(current.m_error_parse == &s_error_parse);
6746 CHECK(current.m_error_visit == &s_error_visit);
6747 CHECK(current.m_allocate == original_callbacks.m_allocate);
6748 CHECK(current.m_free == original_callbacks.m_free);
6749}
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:445
pfn_error_parse m_error_parse
a pointer to a parse error handler function
Definition common.hpp:446
pfn_allocate m_allocate
a pointer to an allocate handler function
Definition common.hpp:443
pfn_error_visit m_error_visit
a pointer to a visit error handler function
Definition common.hpp:447
pfn_free m_free
a pointer to a free handler function
Definition common.hpp:444

◆ check_disabled()

void ErrorHandlerExample::check_disabled ( ) const

test that this handler is currently not set

Definition at line 6751 of file quickstart.cpp.

6752{
6753 ryml::Callbacks const& current = ryml::get_callbacks();
6754 CHECK(current.m_error_basic != &s_error_basic);
6755 CHECK(current.m_error_parse != &s_error_parse);
6756 CHECK(current.m_error_visit != &s_error_visit);
6757 CHECK(current.m_allocate == original_callbacks.m_allocate);
6758 CHECK(current.m_free == original_callbacks.m_free);
6759}

Variable Documentation

◆ s_jmp_env

std::jmp_buf s_jmp_env
static

Definition at line 6583 of file quickstart.cpp.

◆ s_jmp_msg

std::string s_jmp_msg
static

Definition at line 6584 of file quickstart.cpp.