rapidyaml 0.15.1
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 306 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 6677 of file quickstart.cpp.

6678{
6679#ifdef RYML_NO_DEFAULT_CALLBACKS
6681#endif
6682}
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 6637 of file quickstart.cpp.

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

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

◆ handle_args()

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

Definition at line 6573 of file quickstart.cpp.

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

◆ report_checks()

int report_checks ( )

Definition at line 6600 of file quickstart.cpp.

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

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

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

6711{
6712 saved_msg_short.clear();
6713 saved_msg_full.clear();
6715 saved_basic_loc = {};
6716 saved_parse_loc = {};
6717 saved_visit_tree = {};
6719 bool got_error = false;
6720 #ifdef C4_EXCEPTIONS
6721 try
6722 {
6723 std::forward<Fn>(fn)();
6724 }
6725 catch(std::exception const&)
6726 {
6727 got_error = true;
6728 }
6729 #else
6730 if(setjmp(s_jmp_env) == 0)
6731 {
6732 std::forward<Fn>(fn)();
6733 }
6734 else
6735 {
6736 got_error = true;
6737 }
6738 #endif
6739 return got_error;
6740}
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 6758 of file quickstart.cpp.

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

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

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

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

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

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

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

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

6838{
6839 ryml::Callbacks copy = original_callbacks;
6840 copy.set_user_data(this)
6844 return copy;
6845}
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 6848 of file quickstart.cpp.

6849{
6850 ryml::Callbacks const& current = ryml::get_callbacks();
6851 CHECK(current.m_error_basic == &s_error_basic);
6852 CHECK(current.m_error_parse == &s_error_parse);
6853 CHECK(current.m_error_visit == &s_error_visit);
6854 CHECK(current.m_allocate == original_callbacks.m_allocate);
6855 CHECK(current.m_free == original_callbacks.m_free);
6856}
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 6858 of file quickstart.cpp.

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

Variable Documentation

◆ s_jmp_env

std::jmp_buf s_jmp_env
static

Definition at line 6690 of file quickstart.cpp.

◆ s_jmp_msg

std::string s_jmp_msg
static

Definition at line 6691 of file quickstart.cpp.