rapidyaml  0.11.1
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 6094 of file quickstart.cpp.

6128  : "));

◆ default_callbacks()

ryml::Callbacks default_callbacks ( )

a bare-bones implementation of the callbacks

Definition at line 6054 of file quickstart.cpp.

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

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

6312 {
6313  #ifdef C4_EXCEPTIONS
6314  throw std::runtime_error(s);
6315  #else
6316  s_jmp_msg = s;
6317  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 6293 of file quickstart.cpp.

6297  {
6298  std::forward<Fn>(fn)();
6299  }
6300  else
6301  {
6302  got_error = true;
6303  }
6304  #endif
6305  return got_error;
6306 }
6307 
6308 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 6321 of file quickstart.cpp.

6325 {

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

6328  {
6329  saved_msg_full.append(s.str, s.len);
6330  }, msg, errdata);
6331  // Save the error params for subsequent testing in the quickstart.
6332  saved_msg_full_with_context = saved_msg_full;
6333  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 6003 of file quickstart.cpp.

6003  {"));
6004  CHECK(loc.offset == 0u);
6005  CHECK(loc.line == 0u);
6006  CHECK(loc.col == 0u);
6007  // on the next call, we only pay O(log(numlines)) because the
6008  // rebuild is already available:
6009  loc = tree["aa"].location(parser);
6010  CHECK(parser.location_contents(loc).begins_with("aa"));
6011  CHECK(loc.offset == 2u);
6012  CHECK(loc.line == 1u);
6013  CHECK(loc.col == 0u);
6014  // KEYSEQ in flow style: points at the key

◆ report_checks()

int report_checks ( )

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

6128  : "));

◆ check_error_occurs()

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

checking that an error occurs while calling fn

Definition at line 6127 of file quickstart.cpp.

6128  : "));
6129  CHECK(loc.line == 13u);
6130  CHECK(loc.col == 4u);
6131  loc = tree2["seq with key"][3][0].location(parser);
6132  CHECK(parser.location_contents(loc).begins_with("nested first: "));
6133  CHECK(loc.line == 13u);
6134  CHECK(loc.col == 4u);
6135 }
6136 
6137 
6138 //-----------------------------------------------------------------------------
6139 //-----------------------------------------------------------------------------
6140 //-----------------------------------------------------------------------------
6141 
6142 
6143 /** @addtogroup doc_sample_helpers
6144  * @{ */
6145 
6146 namespace /*anon*/ {
6147 static int num_checks = 0;
6148 static int num_failed_checks = 0;
6149 } // namespace /*anon*/
6150 
6151 
6152 bool report_check(int line, const char *predicate, bool result)
6153 {
6154  ++num_checks;
6155  const char *msg = predicate ? "OK! " : "OK!";
6156  if(!result)
6157  {

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

6181  {
6182 // LCOV_EXCL_START
6183 /** dump (part of an) error message to terminal
6184  * @ingroup doc_sample_helpers */
6185 void errdump(ryml::csubstr s)
6186 {

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

6193 {
6194  fputc('\n', stderr); // NOLINT
6195  fflush(NULL); // NOLINT
6196 }
6197 // LCOV_EXCL_STOP
6198 } // namespace
6199 
6200 
6201 /** a bare-bones implementation of the callbacks
6202  * @ingroup doc_sample_helpers */
6204 {
6205  return ryml::Callbacks{}
6206  .set_allocate([](size_t len, void* , void *){
6207  return malloc(len); // NOLINT
6208  })
6209  .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:511
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 6213 of file quickstart.cpp.

6222  {
6223  ryml::err_basic_format(errdump, msg, errdata); // format the message, printing to stderr
6224  errend(); // print newline and flush
6225  abort(); // abort (must never return: abort, or exception or setjmp)
6226  })
6227  .set_error_parse([](ryml::csubstr msg, ryml::ErrorDataParse const& errdata, void *){
6228  ryml::err_parse_format(errdump, msg, errdata); // format the message, printing to out
6229  errend(); // print newline and flush
6230  abort(); // abort (must never return: abort, or exception or setjmp)
6231  })
6232  .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:315
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:328
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 6235 of file quickstart.cpp.

6244 {

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

6244 {

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

◆ callbacks()

ryml::Callbacks ErrorHandlerExample::callbacks ( )

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

Definition at line 6254 of file quickstart.cpp.

◆ check_enabled()

void ErrorHandlerExample::check_enabled ( ) const

test that this handler is currently set

Definition at line 6265 of file quickstart.cpp.

6265 {
6266  #if RYML_USE_ASSERT
6267  return check_error_occurs(std::forward<Fn>(fn));
6268  #else
6269  (void)fn; // do nothing otherwise, as there would be undefined behavior
6270  return true;
6271  #endif
6272 }
6273 /** 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 6107 of file quickstart.cpp.

◆ s_jmp_msg

std::string s_jmp_msg
static

Definition at line 6108 of file quickstart.cpp.