rapidyaml  0.11.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

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 ()
 
void stopexec (std::string const &s)
 interrupt execution More...
 
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 278 of file quickstart.cpp.

Function Documentation

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

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

6191  {};
6192  saved_visit_id = ryml::NONE;
6193  bool got_error = false;
6194  #ifdef C4_EXCEPTIONS
6195  try
6196  {
6197  std::forward<Fn>(fn)();
6198  }
6199  catch(std::exception const&)
6200  {
6201  got_error = true;
6202  }
6203  #else
6204  if(setjmp(s_jmp_env) == 0)
6205  {
6206  std::forward<Fn>(fn)();
static std::jmp_buf s_jmp_env
@ NONE
an index to none
Definition: common.hpp:251

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

6219 {
6220  #ifdef C4_EXCEPTIONS
6221  throw std::runtime_error(s);
6222  #else

References s_jmp_env, and s_jmp_msg.

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

6231 {

◆ report_check()

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

Definition at line 5986 of file quickstart.cpp.

5986  {"));
5987  CHECK(loc.offset == 0u);
5988  CHECK(loc.line == 0u);
5989  CHECK(loc.col == 0u);
5990  // on the next call, we only pay O(log(numlines)) because the
5991  // rebuild is already available:
5992  loc = tree["aa"].location(parser);
5993  CHECK(parser.location_contents(loc).begins_with("aa"));
5994  CHECK(loc.offset == 2u);
5995  CHECK(loc.line == 1u);
5996  CHECK(loc.col == 0u);
5997  // KEYSEQ in flow style: points at the key

◆ report_checks()

int report_checks ( )

Definition at line 6000 of file quickstart.cpp.

◆ stopexec()

void stopexec ( std::string const &  s)

interrupt execution

Definition at line 6069 of file quickstart.cpp.

6111  : "));

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

◆ check_error_occurs()

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

checking that an error occurs while calling fn

Definition at line 6035 of file quickstart.cpp.

6037  : buffer
6038 to: be parsed
6039 map with key:
6040  first: value
6041  second: value
6042 seq with key:
6043  - first value
6044  - second value
6045  -
6046  - nested first value
6047  - nested second value
6048  -
6049  nested first: value
6050  nested second: value
6051 )");
6052  // (Likewise, the docval tree can no longer be used to query.)
6053  //
6054  // For key-less block-style maps, the location of the container
6055  // points at the first child's key. For example, in this case
6056  // the root does not have a key, so its location is taken
6057  // to be at the first child:
6058  loc = tree2.rootref().location(parser);
6059  CHECK(parser.location_contents(loc).begins_with("a new"));
6060  CHECK(loc.offset == 1u);
6061  CHECK(loc.line == 1u);
6062  CHECK(loc.col == 0u);
6063  // note the first child points exactly at the same place:
6064  loc = tree2["a new"].location(parser);
6065  CHECK(parser.location_contents(loc).begins_with("a new"));
Key< K > key(K &k)
Definition: node.hpp:43

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

6111  : "));

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

6111  : "));
6112  CHECK(loc.line == 13u);

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

6129  {
6130 static int num_checks = 0;
6131 static int num_failed_checks = 0;
6132 } // namespace /*anon*/
6133 

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

6136 {
6137  ++num_checks;
6138  const char *msg = predicate ? "OK! " : "OK!";
6139  if(!result)

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

6142  : " : "FAIL";
6143  }
6144  std::cout << __FILE__ << ':' << line << ": " << msg << (predicate ? predicate : "") << std::endl;

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

6150 {

◆ callbacks()

ryml::Callbacks ErrorHandlerExample::callbacks ( )

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

Definition at line 6155 of file quickstart.cpp.

◆ check_enabled()

void ErrorHandlerExample::check_enabled ( ) const

test that this handler is currently set

Definition at line 6165 of file quickstart.cpp.

6173 {

◆ check_disabled()

void ErrorHandlerExample::check_disabled ( ) const

test that this handler is currently not set

Definition at line 6175 of file quickstart.cpp.

Variable Documentation

◆ s_jmp_env

std::jmp_buf s_jmp_env
static

Definition at line 6015 of file quickstart.cpp.

◆ s_jmp_msg

std::string s_jmp_msg
static

Definition at line 6016 of file quickstart.cpp.