rapidyaml 0.15.2
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 311 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 6680 of file quickstart.cpp.

6681{
6682#ifdef RYML_NO_DEFAULT_CALLBACKS
6684#endif
6685}
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 6640 of file quickstart.cpp.

6641{
6642 return ryml::Callbacks{}
6643 .set_allocate([](size_t len, void* , void *){
6644 return malloc(len); // NOLINT
6645 })
6646 .set_free([](void* mem, size_t, void *){
6647 free(mem); // NOLINT
6648 })
6649 //
6650 // The error callbacks won't be called in this quickstart,
6651 // because no errors are expected. But we implement them here
6652 // to show how a bare-bones implementation looks like.
6653 //
6654 // For a different (more involved) implementation of the error
6655 // callbacks, see the implementation of ErrorHandlerExample
6656 // below.
6657 //
6658 // LCOV_EXCL_START
6659 .set_error_basic([](ryml::csubstr msg, ryml::ErrorDataBasic const& errdata, void *){
6660 ryml::err_basic_format(errdump, msg, errdata); // format the message, printing to stderr
6661 errend(); // print newline and flush
6662 abort(); // abort (must never return: abort, or exception or setjmp)
6663 })
6664 .set_error_parse([](ryml::csubstr msg, ryml::ErrorDataParse const& errdata, void *){
6665 ryml::err_parse_format(errdump, msg, errdata); // format the message, printing to out
6666 errend(); // print newline and flush
6667 abort(); // abort (must never return: abort, or exception or setjmp)
6668 })
6669 .set_error_visit([](ryml::csubstr msg, ryml::ErrorDataVisit const& errdata, void *){
6670 ryml::err_visit_format(errdump, msg, errdata); // format the message, printing to out
6671 errend(); // print newline and flush
6672 abort(); // abort (must never return: abort, or exception or setjmp)
6673 });
6674 // LCOV_EXCL_STOP
6675}
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:490
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:376
Data for a parse error.
Definition common.hpp:385
Data for a visit error.
Definition common.hpp:395

◆ report_check()

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

Definition at line 6586 of file quickstart.cpp.

6587{
6588 ++num_checks;
6589 const char *msg = predicate ? "OK! " : "OK!";
6590 if(!result)
6591 {
6592 ++num_failed_checks; // LCOV_EXCL_LINE
6593 msg = predicate ? "FAIL: " : "FAIL"; // LCOV_EXCL_LINE
6594 }
6595 if(!result || !quiet_mode)
6596 {
6597 std::cout << __FILE__ << ':' << line << ": " << msg << (predicate ? predicate : "") << std::endl;
6598 }
6599 return result;
6600}

◆ handle_args()

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

Definition at line 6576 of file quickstart.cpp.

6577{
6578 auto arg_matches = [](const char *arg, const char *shortform, const char *longform) {
6579 return (0 == strcmp(arg, shortform) || 0 == strcmp(arg, longform));
6580 };
6581 for(int i = 1; i < argc; ++i)
6582 if(arg_matches(argv[i], "-q", "--quiet"))
6583 quiet_mode = true;
6584}

◆ report_checks()

int report_checks ( )

Definition at line 6603 of file quickstart.cpp.

6604{
6605 std::cout << "Completed " << num_checks << " checks." << std::endl;
6606 if(num_failed_checks)
6607 std::cout << "ERROR: " << num_failed_checks << '/' << num_checks << " checks failed." << std::endl;
6608 else
6609 std::cout << "SUCCESS!" << std::endl;
6610 return num_failed_checks;
6611}

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

6702{
6703 #if RYML_USE_ASSERT
6704 return check_error_occurs(std::forward<Fn>(fn));
6705 #else
6706 (void)fn; // do nothing otherwise, as there would be undefined behavior
6707 return true;
6708 #endif
6709}
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 6713 of file quickstart.cpp.

6714{
6715 saved_msg_short.clear();
6716 saved_msg_full.clear();
6718 saved_basic_loc = {};
6719 saved_parse_loc = {};
6720 saved_visit_tree = {};
6722 bool got_error = false;
6723 #ifdef C4_EXCEPTIONS
6724 try
6725 {
6726 std::forward<Fn>(fn)();
6727 }
6728 catch(std::exception const&)
6729 {
6730 got_error = true;
6731 }
6732 #else
6733 if(setjmp(s_jmp_env) == 0)
6734 {
6735 std::forward<Fn>(fn)();
6736 }
6737 else
6738 {
6739 got_error = true;
6740 }
6741 #endif
6742 return got_error;
6743}
static std::jmp_buf s_jmp_env
@ NONE
an index to none
Definition common.hpp:312
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 6761 of file quickstart.cpp.

6762{
6763 saved_msg_short.assign(msg.str, msg.len);
6764 // build a full error message with location
6766 saved_msg_full.append(s.str, s.len);
6767 }, msg, errdata);
6768 // Save the error params for subsequent testing in the quickstart.
6770 saved_basic_loc = errdata.location;
6771 stopexec(saved_msg_short);
6772}
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 6777 of file quickstart.cpp.

6778{
6779 saved_msg_short.assign(msg.str, msg.len);
6780 // build a full error message with location
6782 saved_msg_full.append(s.str, s.len);
6783 }, msg, errdata);
6784 // Save the error params for subsequent testing in the quickstart.
6785 //
6786 // To add the source context, the source buffer is required. If
6787 // the caller is interested in enriching the full message with the
6788 // source buffer context, he can ensure that the source buffer is
6789 // kept, and then arrange the handler to access it.
6790 // For now, we assign the full message without context:
6792 saved_basic_loc = errdata.cpploc;
6793 saved_parse_loc = errdata.ymlloc;
6794 stopexec(saved_msg_full);
6795}

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

6800{
6801 saved_msg_short.assign(msg.str, msg.len);
6802 // build a full error message with location
6804 saved_msg_full.append(s.str, s.len);
6805 }, msg, errdata);
6806 // Save the error params for subsequent testing in the quickstart.
6807 //
6808 // To add the source context, the source buffer is required. If
6809 // the caller is interested in enriching the full message with the
6810 // source buffer context, he can ensure that the source buffer is
6811 // kept, and then arrange the handler to access it.
6812 // For now, we assign the full message without context:
6814 saved_basic_loc = errdata.cpploc;
6815 saved_visit_tree = errdata.tree;
6816 saved_visit_id = errdata.node;
6817 stopexec(saved_msg_full);
6818}

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

6822{
6823 static_cast<ErrorHandlerExample*>(this_)->on_error_basic(msg, errdata);
6824}
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 6826 of file quickstart.cpp.

6827{
6828 static_cast<ErrorHandlerExample*>(this_)->on_error_parse(msg, errdata);
6829}
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 6831 of file quickstart.cpp.

6832{
6833 static_cast<ErrorHandlerExample*>(this_)->on_error_visit(msg, errdata);
6834}
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 6840 of file quickstart.cpp.

6841{
6842 ryml::Callbacks copy = original_callbacks;
6843 copy.set_user_data(this)
6847 return copy;
6848}
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 6851 of file quickstart.cpp.

6852{
6853 ryml::Callbacks const& current = ryml::get_callbacks();
6854 CHECK(current.m_error_basic == &s_error_basic);
6855 CHECK(current.m_error_parse == &s_error_parse);
6856 CHECK(current.m_error_visit == &s_error_visit);
6857 CHECK(current.m_allocate == original_callbacks.m_allocate);
6858 CHECK(current.m_free == original_callbacks.m_free);
6859}
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:494
pfn_error_parse m_error_parse
a pointer to a parse error handler function
Definition common.hpp:495
pfn_allocate m_allocate
a pointer to an allocate handler function
Definition common.hpp:492
pfn_error_visit m_error_visit
a pointer to a visit error handler function
Definition common.hpp:496
pfn_free m_free
a pointer to a free handler function
Definition common.hpp:493

◆ check_disabled()

void ErrorHandlerExample::check_disabled ( ) const

test that this handler is currently not set

Definition at line 6861 of file quickstart.cpp.

6862{
6863 ryml::Callbacks const& current = ryml::get_callbacks();
6864 CHECK(current.m_error_basic != &s_error_basic);
6865 CHECK(current.m_error_parse != &s_error_parse);
6866 CHECK(current.m_error_visit != &s_error_visit);
6867 CHECK(current.m_allocate == original_callbacks.m_allocate);
6868 CHECK(current.m_free == original_callbacks.m_free);
6869}

Variable Documentation

◆ s_jmp_env

std::jmp_buf s_jmp_env
static

Definition at line 6693 of file quickstart.cpp.

◆ s_jmp_msg

std::string s_jmp_msg
static

Definition at line 6694 of file quickstart.cpp.