rapidyaml  0.13.0
parse and emit YAML, and do it fast
Sample helpers

Helper utilities used in the sample. More...

Modules

 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)   assert(predicate)
 a quick'n'dirty assertion to verify a predicate More...
 

Functions

void ensure_callbacks ()
 set up default callbacks when ryml does not provide them (ie when RYML_NO_DEFAULT_CALLBACKS is defined) More...
 
ryml::Callbacks default_callbacks ()
 a bare-bones implementation of the callbacks More...
 
template<class CharContainer >
CharContainer file_get_contents (const char *filename)
 load a file from disk and return a newly created CharContainer More...
 
template<class CharContainer >
size_t file_get_contents (const char *filename, CharContainer *v)
 load a file from disk into an existing CharContainer More...
 
template<class CharContainer >
void file_put_contents (const char *filename, CharContainer const &v, const char *access)
 save a buffer into a file More...
 
void file_put_contents (const char *filename, const char *buf, size_t sz, const char *access)
 save a buffer into a file More...
 
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. More...
 
template<class Fn >
bool ErrorHandlerExample::check_error_occurs (Fn &&fn)
 checking that an error occurs while calling fn More...
 
void ErrorHandlerExample::on_error_basic (ryml::csubstr msg, ryml::ErrorDataBasic const &errdata)
 this is where the callback implementation goes. More...
 
void ErrorHandlerExample::on_error_parse (ryml::csubstr msg, ryml::ErrorDataParse const &errdata)
 this is where the callback implementation goes. More...
 
void ErrorHandlerExample::on_error_visit (ryml::csubstr msg, ryml::ErrorDataVisit const &errdata)
 this is where the callback implementation goes. More...
 
static void ErrorHandlerExample::s_error_basic (ryml::csubstr msg, ryml::ErrorDataBasic const &errdata, void *this_)
 trampoline function to call the object's method More...
 
static void ErrorHandlerExample::s_error_parse (ryml::csubstr msg, ryml::ErrorDataParse const &errdata, void *this_)
 trampoline function to call the object's method More...
 
static void ErrorHandlerExample::s_error_visit (ryml::csubstr msg, ryml::ErrorDataVisit const &errdata, void *this_)
 trampoline function to call the object's method More...
 
ryml::Callbacks ErrorHandlerExample::callbacks ()
 a helper to create the Callbacks object for the custom error handler More...
 
void ErrorHandlerExample::check_enabled () const
 test that this handler is currently set More...
 
void ErrorHandlerExample::check_disabled () const
 test that this handler is currently not set More...
 

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)    assert(predicate)

a quick'n'dirty assertion to verify a predicate

Definition at line 309 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 6433 of file quickstart.cpp.

6434 {
6435 #ifdef RYML_NO_DEFAULT_CALLBACKS
6437 #endif
6438 }
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 6393 of file quickstart.cpp.

6394 {
6395  return ryml::Callbacks{}
6396  .set_allocate([](size_t len, void* , void *){
6397  return malloc(len); // NOLINT
6398  })
6399  .set_free([](void* mem, size_t, void *){
6400  free(mem); // NOLINT
6401  })
6402  //
6403  // The error callbacks won't be called in this quickstart,
6404  // because no errors are expected. But we implement them here
6405  // to show how a bare-bones implementation looks like.
6406  //
6407  // For a different (more involved) implementation of the error
6408  // callbacks, see the implementation of ErrorHandlerExample
6409  // below.
6410  //
6411  // LCOV_EXCL_START
6412  .set_error_basic([](ryml::csubstr msg, ryml::ErrorDataBasic const& errdata, void *){
6413  ryml::err_basic_format(errdump, msg, errdata); // format the message, printing to stderr
6414  errend(); // print newline and flush
6415  abort(); // abort (must never return: abort, or exception or setjmp)
6416  })
6417  .set_error_parse([](ryml::csubstr msg, ryml::ErrorDataParse const& errdata, void *){
6418  ryml::err_parse_format(errdump, msg, errdata); // format the message, printing to out
6419  errend(); // print newline and flush
6420  abort(); // abort (must never return: abort, or exception or setjmp)
6421  })
6422  .set_error_visit([](ryml::csubstr msg, ryml::ErrorDataVisit const& errdata, void *){
6423  ryml::err_visit_format(errdump, msg, errdata); // format the message, printing to out
6424  errend(); // print newline and flush
6425  abort(); // abort (must never return: abort, or exception or setjmp)
6426  });
6427  // LCOV_EXCL_STOP
6428 }
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.
Definition: error.def.hpp:350
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.
Definition: error.def.hpp:318
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.
Definition: error.def.hpp:331
A c-style callbacks class to customize behavior on errors or allocation.
Definition: common.hpp:541
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:315
Data for a parse error.
Definition: common.hpp:324
Data for a visit error.
Definition: common.hpp:334

◆ file_get_contents() [1/2]

template<class CharContainer >
CharContainer file_get_contents ( const char *  filename)

load a file from disk and return a newly created CharContainer

Definition at line 6651 of file quickstart.cpp.

6652 {
6653  CharContainer cc;
6654  file_get_contents(filename, &cc);
6655  return cc;
6656 }
CharContainer file_get_contents(const char *filename)
load a file from disk and return a newly created CharContainer

◆ file_get_contents() [2/2]

template<class CharContainer >
size_t file_get_contents ( const char *  filename,
CharContainer *  v 
)

load a file from disk into an existing CharContainer

Definition at line 6632 of file quickstart.cpp.

6633 {
6634  std::FILE *fp = std::fopen(filename, "rb"); // NOLINT
6635  if(fp == nullptr) _RYML_ERR_BASIC("{}: could not open file", filename);
6636  std::fseek(fp, 0, SEEK_END); // NOLINT
6637  long sz = std::ftell(fp); // NOLINT
6638  v->resize(static_cast<typename CharContainer::size_type>(sz));
6639  if(sz)
6640  {
6641  std::rewind(fp); // NOLINT
6642  size_t ret = std::fread(&(*v)[0], 1, v->size(), fp);
6643  if(ret != (size_t)sz) _RYML_ERR_BASIC("{}: failed to read: expect {}B, got {}B", filename, sz, ret);
6644  }
6645  std::fclose(fp); // NOLINT
6646  return v->size();
6647 }

◆ file_put_contents() [1/2]

template<class CharContainer >
void file_put_contents ( const char *  filename,
CharContainer const &  v,
const char *  access = "wb" 
)

save a buffer into a file

Definition at line 6660 of file quickstart.cpp.

6661 {
6662  file_put_contents(filename, v.empty() ? "" : &v[0], v.size(), access);
6663 }
void file_put_contents(const char *filename, CharContainer const &v, const char *access="wb")
save a buffer into a file

◆ file_put_contents() [2/2]

void file_put_contents ( const char *  filename,
const char *  buf,
size_t  sz,
const char *  access 
)

save a buffer into a file

Definition at line 6666 of file quickstart.cpp.

6667 {
6668  std::FILE *fp = std::fopen(filename, access);
6669  if(fp == nullptr) _RYML_ERR_BASIC("{}: could not open file", filename);
6670  std::fwrite(buf, 1, sz, fp); // NOLINT
6671  std::fclose(fp); // NOLINT
6672 }

◆ report_check()

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

Definition at line 6339 of file quickstart.cpp.

6340 {
6341  ++num_checks;
6342  const char *msg = predicate ? "OK! " : "OK!";
6343  if(!result)
6344  {
6345  ++num_failed_checks; // LCOV_EXCL_LINE
6346  msg = predicate ? "FAIL: " : "FAIL"; // LCOV_EXCL_LINE
6347  }
6348  if(!result || !quiet_mode)
6349  {
6350  std::cout << __FILE__ << ':' << line << ": " << msg << (predicate ? predicate : "") << std::endl;
6351  }
6352  return result;
6353 }

◆ handle_args()

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

Definition at line 6329 of file quickstart.cpp.

6330 {
6331  auto arg_matches = [](const char *arg, const char *shortform, const char *longform) {
6332  return (0 == strcmp(arg, shortform) || 0 == strcmp(arg, longform));
6333  };
6334  for(int i = 1; i < argc; ++i)
6335  if(arg_matches(argv[i], "-q", "--quiet"))
6336  quiet_mode = true;
6337 }

◆ report_checks()

int report_checks ( )

Definition at line 6356 of file quickstart.cpp.

6357 {
6358  std::cout << "Completed " << num_checks << " checks." << std::endl;
6359  if(num_failed_checks)
6360  std::cout << "ERROR: " << num_failed_checks << '/' << num_checks << " checks failed." << std::endl;
6361  else
6362  std::cout << "SUCCESS!" << std::endl;
6363  return num_failed_checks;
6364 }

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

6455 {
6456  #if RYML_USE_ASSERT
6457  return check_error_occurs(std::forward<Fn>(fn));
6458  #else
6459  (void)fn; // do nothing otherwise, as there would be undefined behavior
6460  return true;
6461  #endif
6462 }
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 6466 of file quickstart.cpp.

6467 {
6468  saved_msg_short.clear();
6469  saved_msg_full.clear();
6471  saved_basic_loc = {};
6472  saved_parse_loc = {};
6473  saved_visit_tree = {};
6475  bool got_error = false;
6476  #ifdef C4_EXCEPTIONS
6477  try
6478  {
6479  std::forward<Fn>(fn)();
6480  }
6481  catch(std::exception const&)
6482  {
6483  got_error = true;
6484  }
6485  #else
6486  if(setjmp(s_jmp_env) == 0)
6487  {
6488  std::forward<Fn>(fn)();
6489  }
6490  else
6491  {
6492  got_error = true;
6493  }
6494  #endif
6495  return got_error;
6496 }
static std::jmp_buf s_jmp_env
@ NONE
an index to none
Definition: common.hpp:251
ryml::Location saved_basic_loc
Definition: quickstart.cpp:268
std::string saved_msg_full
Definition: quickstart.cpp:266
ryml::id_type saved_visit_id
Definition: quickstart.cpp:271
std::string saved_msg_short
Definition: quickstart.cpp:265
ryml::Tree const * saved_visit_tree
Definition: quickstart.cpp:270
ryml::Location saved_parse_loc
Definition: quickstart.cpp:269
std::string saved_msg_full_with_context
Definition: quickstart.cpp:267

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

6515 {
6516  saved_msg_short.assign(msg.str, msg.len);
6517  // build a full error message with location
6518  ryml::err_basic_format([this](ryml::csubstr s){
6519  saved_msg_full.append(s.str, s.len);
6520  }, msg, errdata);
6521  // Save the error params for subsequent testing in the quickstart.
6523  saved_basic_loc = errdata.location;
6524  stopexec(saved_msg_short);
6525 }

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

6531 {
6532  saved_msg_short.assign(msg.str, msg.len);
6533  // build a full error message with location
6534  ryml::err_parse_format([this](ryml::csubstr s){
6535  saved_msg_full.append(s.str, s.len);
6536  }, msg, errdata);
6537  // Save the error params for subsequent testing in the quickstart.
6538  //
6539  // To add the source context, the source buffer is required. If
6540  // the caller is interested in enriching the full message with the
6541  // source buffer context, he can ensure that the source buffer is
6542  // kept, and then arrange the handler to access it.
6543  // For now, we assign the full message without context:
6545  saved_basic_loc = errdata.cpploc;
6546  saved_parse_loc = errdata.ymlloc;
6547  stopexec(saved_msg_full);
6548 }

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

6553 {
6554  saved_msg_short.assign(msg.str, msg.len);
6555  // build a full error message with location
6556  ryml::err_visit_format([this](ryml::csubstr s){
6557  saved_msg_full.append(s.str, s.len);
6558  }, msg, errdata);
6559  // Save the error params for subsequent testing in the quickstart.
6560  //
6561  // To add the source context, the source buffer is required. If
6562  // the caller is interested in enriching the full message with the
6563  // source buffer context, he can ensure that the source buffer is
6564  // kept, and then arrange the handler to access it.
6565  // For now, we assign the full message without context:
6567  saved_basic_loc = errdata.cpploc;
6568  saved_visit_tree = errdata.tree;
6569  saved_visit_id = errdata.node;
6570  stopexec(saved_msg_full);
6571 }

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

6575 {
6576  static_cast<ErrorHandlerExample*>(this_)->on_error_basic(msg, errdata);
6577 }
void on_error_basic(ryml::csubstr msg, ryml::ErrorDataBasic const &errdata)
this is where the callback implementation goes.
an example error handler, required for some of the quickstart examples.
Definition: quickstart.cpp:241

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

6580 {
6581  static_cast<ErrorHandlerExample*>(this_)->on_error_parse(msg, errdata);
6582 }
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 6584 of file quickstart.cpp.

6585 {
6586  static_cast<ErrorHandlerExample*>(this_)->on_error_visit(msg, errdata);
6587 }
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 6593 of file quickstart.cpp.

6594 {
6596  copy.set_user_data(this)
6600  return copy;
6601 }
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
Definition: quickstart.cpp:243
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 6604 of file quickstart.cpp.

6605 {
6606  ryml::Callbacks const& current = ryml::get_callbacks();
6607  CHECK(current.m_error_basic == &s_error_basic);
6608  CHECK(current.m_error_parse == &s_error_parse);
6609  CHECK(current.m_error_visit == &s_error_visit);
6611  CHECK(current.m_free == original_callbacks.m_free);
6612 }
Callbacks const & get_callbacks()
get the global callbacks
Definition: common.cpp:94
#define CHECK(predicate)
a quick'n'dirty assertion to verify a predicate
Definition: quickstart.cpp:309
pfn_error_basic m_error_basic
a pointer to a basic error handler function
Definition: common.hpp:545
pfn_error_parse m_error_parse
a pointer to a parse error handler function
Definition: common.hpp:546
pfn_allocate m_allocate
a pointer to an allocate handler function
Definition: common.hpp:543
pfn_error_visit m_error_visit
a pointer to a visit error handler function
Definition: common.hpp:547
pfn_free m_free
a pointer to a free handler function
Definition: common.hpp:544

◆ check_disabled()

void ErrorHandlerExample::check_disabled ( ) const

test that this handler is currently not set

Definition at line 6614 of file quickstart.cpp.

6615 {
6616  ryml::Callbacks const& current = ryml::get_callbacks();
6617  CHECK(current.m_error_basic != &s_error_basic);
6618  CHECK(current.m_error_parse != &s_error_parse);
6619  CHECK(current.m_error_visit != &s_error_visit);
6621  CHECK(current.m_free == original_callbacks.m_free);
6622 }

Variable Documentation

◆ s_jmp_env

std::jmp_buf s_jmp_env
static

Definition at line 6446 of file quickstart.cpp.

◆ s_jmp_msg

std::string s_jmp_msg
static

Definition at line 6447 of file quickstart.cpp.