rapidyaml  0.12.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)
 
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 294 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 6106 of file quickstart.cpp.

6140  : "));

◆ default_callbacks()

ryml::Callbacks default_callbacks ( )

a bare-bones implementation of the callbacks

Definition at line 6066 of file quickstart.cpp.

6066  : buffer
6067 to: be parsed
6068 map with key:
6069  first: value
6070  second: value
6071 seq with key:
6072  - first value
6073  - second value
6074  -
6075  - nested first value
6076  - nested second value
6077  -
6078  nested first: value
6079  nested second: value
6080 )");
6081  // (Likewise, the docval tree can no longer be used to query.)
6082  //
6083  // For key-less block-style maps, the location of the container
6084  // points at the first child's key. For example, in this case
6085  // the root does not have a key, so its location is taken
6086  // to be at the first child:
6087  loc = tree2.rootref().location(parser);
6088  CHECK(parser.location_contents(loc).begins_with("a new"));
6089  CHECK(loc.offset == 1u);
6090  CHECK(loc.line == 1u);
6091  CHECK(loc.col == 0u);
6092  // note the first child points exactly at the same place:
6093  loc = tree2["a new"].location(parser);
6094  CHECK(parser.location_contents(loc).begins_with("a new"));
6095  CHECK(loc.offset == 1u);
6096  CHECK(loc.line == 1u);
6097  CHECK(loc.col == 0u);
6098  loc = tree2["to"].location(parser);
6099  CHECK(parser.location_contents(loc).begins_with("to"));
6100  CHECK(loc.line == 2u);
6101  CHECK(loc.col == 0u);
Key< K > key(K &k)
Definition: node.hpp:45

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

6324 {
6325  #ifdef C4_EXCEPTIONS
6326  throw std::runtime_error(s);
6327  #else
6328  s_jmp_msg = s;
6329  std::longjmp(s_jmp_env, 1); // jump to the corresponding call to setjmp().
static std::string s_jmp_msg
static std::jmp_buf s_jmp_env

References s_jmp_env, and s_jmp_msg.

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

6309  {
6310  std::forward<Fn>(fn)();
6311  }
6312  else
6313  {
6314  got_error = true;
6315  }
6316  #endif
6317  return got_error;
6318 }
6319 
6320 namespace {

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

6337 {

References ErrorHandlerExample::saved_msg_short.

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

6340  {
6341  saved_msg_full.append(s.str, s.len);
6342  }, msg, errdata);
6343  // Save the error params for subsequent testing in the quickstart.
6344  saved_msg_full_with_context = saved_msg_full;
6345  saved_basic_loc = errdata.location;

References ErrorHandlerExample::saved_msg_full.

◆ report_check()

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

Definition at line 6015 of file quickstart.cpp.

6015  {"));
6016  CHECK(loc.offset == 0u);
6017  CHECK(loc.line == 0u);
6018  CHECK(loc.col == 0u);
6019  // on the next call, we only pay O(log(numlines)) because the
6020  // rebuild is already available:
6021  loc = tree["aa"].location(parser);
6022  CHECK(parser.location_contents(loc).begins_with("aa"));
6023  CHECK(loc.offset == 2u);
6024  CHECK(loc.line == 1u);
6025  CHECK(loc.col == 0u);
6026  // KEYSEQ in flow style: points at the key

◆ report_checks()

int report_checks ( )

Definition at line 6029 of file quickstart.cpp.

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

6140  : "));

◆ check_error_occurs()

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

checking that an error occurs while calling fn

Definition at line 6139 of file quickstart.cpp.

6140  : "));
6141  CHECK(loc.line == 13u);
6142  CHECK(loc.col == 4u);
6143  loc = tree2["seq with key"][3][0].location(parser);
6144  CHECK(parser.location_contents(loc).begins_with("nested first: "));
6145  CHECK(loc.line == 13u);
6146  CHECK(loc.col == 4u);
6147 }
6148 
6149 
6150 //-----------------------------------------------------------------------------
6151 //-----------------------------------------------------------------------------
6152 //-----------------------------------------------------------------------------
6153 
6154 
6155 /** @addtogroup doc_sample_helpers
6156  * @{ */
6157 
6158 namespace /*anon*/ {
6159 static int num_checks = 0;
6160 static int num_failed_checks = 0;
6161 } // namespace /*anon*/
6162 
6163 
6164 bool report_check(int line, const char *predicate, bool result)
6165 {
6166  ++num_checks;
6167  const char *msg = predicate ? "OK! " : "OK!";
6168  if(!result)
6169  {

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

6193  {
6194 // LCOV_EXCL_START
6195 /** dump (part of an) error message to terminal
6196  * @ingroup doc_sample_helpers */
6197 void errdump(ryml::csubstr s)
6198 {

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

6205 {
6206  fputc('\n', stderr); // NOLINT
6207  fflush(NULL); // NOLINT
6208 }
6209 // LCOV_EXCL_STOP
6210 } // namespace
6211 
6212 
6213 /** a bare-bones implementation of the callbacks
6214  * @ingroup doc_sample_helpers */
6216 {
6217  return ryml::Callbacks{}
6218  .set_allocate([](size_t len, void* , void *){
6219  return malloc(len); // NOLINT
6220  })
6221  .set_free([](void* mem, size_t, void *){
ryml::Callbacks default_callbacks()
a bare-bones implementation of the callbacks
A c-style callbacks class to customize behavior on errors or allocation.
Definition: common.hpp:538
Callbacks & set_allocate(pfn_allocate allocate=nullptr)
Set or reset the allocate callback.
Definition: common.cpp:150

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

6234  {
6235  ryml::err_basic_format(errdump, msg, errdata); // format the message, printing to stderr
6236  errend(); // print newline and flush
6237  abort(); // abort (must never return: abort, or exception or setjmp)
6238  })
6239  .set_error_parse([](ryml::csubstr msg, ryml::ErrorDataParse const& errdata, void *){
6240  ryml::err_parse_format(errdump, msg, errdata); // format the message, printing to out
6241  errend(); // print newline and flush
6242  abort(); // abort (must never return: abort, or exception or setjmp)
6243  })
6244  .set_error_visit([](ryml::csubstr msg, ryml::ErrorDataVisit const& errdata, void *){
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:316
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:329
Data for a parse error.
Definition: common.hpp:321
Data for a visit error.
Definition: common.hpp:331

References c4::yml::err_basic_format().

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

6256 {

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

6256 {

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

◆ callbacks()

ryml::Callbacks ErrorHandlerExample::callbacks ( )

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

Definition at line 6266 of file quickstart.cpp.

◆ check_enabled()

void ErrorHandlerExample::check_enabled ( ) const

test that this handler is currently set

Definition at line 6277 of file quickstart.cpp.

6277 {
6278  #if RYML_USE_ASSERT
6279  return check_error_occurs(std::forward<Fn>(fn));
6280  #else
6281  (void)fn; // do nothing otherwise, as there would be undefined behavior
6282  return true;
6283  #endif
6284 }
6285 /** checking that an error occurs while calling fn
bool check_error_occurs(Fn &&fn)
checking that an error occurs while calling fn

References ErrorHandlerExample::check_error_occurs().

◆ check_disabled()

void ErrorHandlerExample::check_disabled ( ) const

Variable Documentation

◆ s_jmp_env

std::jmp_buf s_jmp_env
static

Definition at line 6119 of file quickstart.cpp.

◆ s_jmp_msg

std::string s_jmp_msg
static

Definition at line 6120 of file quickstart.cpp.