rapidyaml  0.16.0
parseandemitYAML,anddoitfast
Sample helpers

Helper utilities used in the quickstart. More...

Modules

 Serialize/deserialize scalar types
 
 Serialize container types (brief)
 Serialization definitions used in sample_user_container_types_brief()
 
 Serialize container types
 Serialization definitions used in sample_user_container_types()
 

Classes

struct  ErrorHandlerExample
 an error handler used by 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 testing assertion, used only in this quickstart More...
 

Functions

bool report_check (int line, const char *predicate, bool result)
 used by CHECK() More...
 
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 ()
 set up a bare-bones implementation of the callbacks More...
 
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 quickstart.

Macro Definition Documentation

#define CHECK (   predicate)    assert(predicate)

a testing assertion, used only in this quickstart

Definition at line 310 of file quickstart.cpp.

Referenced by GlobalAllocatorExample::check_and_reset(), PerTreeMemoryExample::check_and_reset(), ErrorHandlerExample::check_disabled(), ErrorHandlerExample::check_enabled(), my_seq_type< int >::check_eq(), my_map_type< int, int >::check_eq(), my_type::check_eq(), GlobalAllocatorExample::free(), PerTreeMemoryExample::free(), sample_anchors_and_aliases(), sample_anchors_and_aliases_create(), sample_base64(), sample_create_tree(), sample_create_tree_style(), sample_deserialize_error(), sample_docs(), sample_emit_nested_node(), sample_emit_to_container(), sample_emit_to_stream(), sample_empty_null_values(), sample_error_basic(), sample_error_handler(), sample_error_parse(), sample_error_visit(), sample_error_visit_location(), sample_float_precision(), sample_formatting(), sample_fundamental_types(), sample_global_allocator(), sample_iterate_tree(), sample_json(), sample_lightning_overview(), sample_location_tracking(), sample_parse_file(), sample_parse_in_arena(), sample_parse_in_place(), sample_parse_reuse_parser(), sample_parse_reuse_tree(), sample_parse_reuse_tree_and_parser(), sample_parse_style(), sample_per_tree_allocator(), sample_quick_overview(), sample_serialize_basic(), sample_static_trees(), sample_std_types(), sample_style(), sample_style_flow_formatting(), sample_style_flow_ml_indent(), sample_substr(), sample_tag_directives(), sample_tags(), sample_tree_arena(), sample_user_container_types(), sample_user_container_types_brief(), and sample_user_scalar_types().

Function Documentation

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

used by CHECK()

Definition at line 7121 of file quickstart.cpp.

7122 {
7123  ++num_checks;
7124  const char *msg = predicate ? "OK! " : "OK!";
7125  if(!result)
7126  {
7127  ++num_failed_checks; // LCOV_EXCL_LINE
7128  msg = predicate ? "FAIL: " : "FAIL"; // LCOV_EXCL_LINE
7129  }
7130  if(!result || !quiet_mode)
7131  {
7132  std::cout << __FILE__ << ':' << line << ": " << msg << (predicate ? predicate : "") << std::endl;
7133  }
7134  return result;
7135 }
void ensure_callbacks ( )

set up default callbacks when ryml does not provide them (ie when RYML_NO_DEFAULT_CALLBACKS is defined)

Definition at line 7217 of file quickstart.cpp.

Referenced by main().

7218 {
7219 #ifdef RYML_NO_DEFAULT_CALLBACKS
7221 #endif
7222 }
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()
set up a bare-bones implementation of the callbacks
ryml::Callbacks default_callbacks ( )

set up a bare-bones implementation of the callbacks

The default error callbacks won't be called in this quickstart, because no errors are expected. But we implement them here to show how a bare-bones implementation looks like, and also because they are needed when RYML_NO_DEFAULT_CALLBACKS is defined.

For a different (more involved) implementation of the error callbacks, see the implementation of ErrorHandlerExample below.

Definition at line 7175 of file quickstart.cpp.

Referenced by ensure_callbacks(), and sample_static_trees().

7176 {
7177  return ryml::Callbacks{}
7178  .set_allocate([](size_t len, void* , void *){
7179  return malloc(len); // NOLINT
7180  })
7181  .set_free([](void* mem, size_t, void *){
7182  free(mem); // NOLINT
7183  })
7184  ///
7185  /// The default error callbacks won't be called in this
7186  /// quickstart, because no errors are expected. But we
7187  /// implement them here to show how a bare-bones implementation
7188  /// looks like, and also because they are needed when
7189  /// @ref RYML_NO_DEFAULT_CALLBACKS is defined.
7190  ///
7191  /// For a different (more involved) implementation of the error
7192  /// callbacks, see the implementation of @ref ErrorHandlerExample
7193  /// below.
7194  ///
7195  // LCOV_EXCL_START
7196  .set_error_basic([](ryml::csubstr msg, ryml::ErrorDataBasic const& errdata, void *){
7197  ryml::err_basic_format(errdump, msg, errdata); // format the message, printing to stderr
7198  errend(); // print newline and flush
7199  abort(); // abort (must never return: abort, or exception or setjmp)
7200  })
7201  .set_error_parse([](ryml::csubstr msg, ryml::ErrorDataParse const& errdata, void *){
7202  ryml::err_parse_format(errdump, msg, errdata); // format the message, printing to out
7203  errend(); // print newline and flush
7204  abort(); // abort (must never return: abort, or exception or setjmp)
7205  })
7206  .set_error_visit([](ryml::csubstr msg, ryml::ErrorDataVisit const& errdata, void *){
7207  ryml::err_visit_format(errdump, msg, errdata); // format the message, printing to out
7208  errend(); // print newline and flush
7209  abort(); // abort (must never return: abort, or exception or setjmp)
7210  });
7211  // LCOV_EXCL_STOP
7212 }
Callbacks & set_allocate(pfn_allocate allocate=nullptr)
Set or reset the allocate callback.
Definition: common.cpp:150
A c-style callbacks class to customize behavior on errors or allocation.
Definition: common.hpp:373
Data for a parse error.
Definition: common.hpp:268
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
Data for a visit error.
Definition: common.hpp:278
Data for a basic error.
Definition: common.hpp:259
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
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 handle_args ( int  argc,
const char *  argv[] 
)

Definition at line 7111 of file quickstart.cpp.

Referenced by main().

7112 {
7113  auto arg_matches = [](const char *arg, const char *shortform, const char *longform) {
7114  return (0 == strcmp(arg, shortform) || 0 == strcmp(arg, longform));
7115  };
7116  for(int i = 1; i < argc; ++i)
7117  if(arg_matches(argv[i], "-q", "--quiet"))
7118  quiet_mode = true;
7119 }
int report_checks ( )

Definition at line 7138 of file quickstart.cpp.

Referenced by main().

7139 {
7140  std::cout << "Completed " << num_checks << " checks." << std::endl;
7141  if(num_failed_checks)
7142  std::cout << "ERROR: " << num_failed_checks << '/' << num_checks << " checks failed." << std::endl; // LCOV_EXCL_LINE
7143  else
7144  std::cout << "SUCCESS!" << std::endl;
7145  return num_failed_checks;
7146 }
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 7238 of file quickstart.cpp.

Referenced by sample_quick_overview().

7239 {
7240  #if RYML_USE_ASSERT
7241  return check_error_occurs(std::forward<Fn>(fn));
7242  #else
7243  (void)fn; // do nothing otherwise, as there would be undefined behavior
7244  return true;
7245  #endif
7246 }
bool check_error_occurs(Fn &&fn)
checking that an error occurs while calling fn
template<class Fn >
bool ErrorHandlerExample::check_error_occurs ( Fn &&  fn)

checking that an error occurs while calling fn

Definition at line 7250 of file quickstart.cpp.

Referenced by ErrorHandlerExample::check_assertion_occurs(), sample_deserialize_error(), sample_docs(), sample_error_basic(), sample_error_handler(), sample_error_parse(), sample_error_visit(), sample_error_visit_location(), sample_fundamental_types(), and sample_quick_overview().

7251 {
7252  RYML_SAVE_TEST_EXPFAIL_(); // a dev helper to dump all tests
7253  saved_msg_short.clear();
7254  saved_msg_full.clear();
7256  saved_basic_loc = {};
7257  saved_parse_loc = {};
7258  saved_visit_tree = {};
7260  bool got_error = false;
7261  #ifdef C4_EXCEPTIONS
7262  try
7263  {
7264  std::forward<Fn>(fn)();
7265  }
7266  catch(std::exception const&)
7267  {
7268  got_error = true;
7269  }
7270  #else
7271  if(setjmp(s_jmp_env) == 0)
7272  {
7273  std::forward<Fn>(fn)();
7274  }
7275  else
7276  {
7277  got_error = true;
7278  }
7279  #endif
7280  return got_error;
7281 }
ryml::Location saved_basic_loc
Definition: quickstart.cpp:359
an index to none
Definition: common.hpp:131
ryml::Location saved_parse_loc
Definition: quickstart.cpp:360
std::string saved_msg_full
Definition: quickstart.cpp:357
ryml::id_type saved_visit_id
Definition: quickstart.cpp:362
std::string saved_msg_full_with_context
Definition: quickstart.cpp:358
static std::jmp_buf s_jmp_env
std::string saved_msg_short
Definition: quickstart.cpp:356
ryml::Tree const * saved_visit_tree
Definition: quickstart.cpp:361
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 7299 of file quickstart.cpp.

Referenced by ErrorHandlerExample::s_error_basic().

7300 {
7301  saved_msg_short.assign(msg.str, msg.len);
7302  // build a full error message with location
7304  saved_msg_full.append(s.str, s.len);
7305  }, msg, errdata);
7306  // Save the error params for subsequent testing in the quickstart.
7308  saved_basic_loc = errdata.location;
7309  stopexec(saved_msg_short);
7310 }
ryml::Location saved_basic_loc
Definition: quickstart.cpp:359
C * str
a restricted pointer to the first character of the substring
Definition: substr.hpp:216
std::string saved_msg_full
Definition: quickstart.cpp:357
std::string saved_msg_full_with_context
Definition: quickstart.cpp:358
size_t len
the length of the substring
Definition: substr.hpp:218
std::string saved_msg_short
Definition: quickstart.cpp:356
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 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 7315 of file quickstart.cpp.

Referenced by ErrorHandlerExample::s_error_parse().

7316 {
7317  saved_msg_short.assign(msg.str, msg.len);
7318  // build a full error message with location
7320  saved_msg_full.append(s.str, s.len);
7321  }, msg, errdata);
7322  // Save the error params for subsequent testing in the quickstart.
7323  //
7324  // To add the source context, the source buffer is required. If
7325  // the caller is interested in enriching the full message with the
7326  // source buffer context, he can ensure that the source buffer is
7327  // kept, and then arrange the handler to access it.
7328  // For now, we assign the full message without context:
7330  saved_basic_loc = errdata.cpploc;
7331  saved_parse_loc = errdata.ymlloc;
7332  stopexec(saved_msg_full);
7333 }
ryml::Location saved_basic_loc
Definition: quickstart.cpp:359
C * str
a restricted pointer to the first character of the substring
Definition: substr.hpp:216
ryml::Location saved_parse_loc
Definition: quickstart.cpp:360
std::string saved_msg_full
Definition: quickstart.cpp:357
std::string saved_msg_full_with_context
Definition: quickstart.cpp:358
size_t len
the length of the substring
Definition: substr.hpp:218
std::string saved_msg_short
Definition: quickstart.cpp:356
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
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 7337 of file quickstart.cpp.

Referenced by ErrorHandlerExample::s_error_visit().

7338 {
7339  saved_msg_short.assign(msg.str, msg.len);
7340  // build a full error message with location
7342  saved_msg_full.append(s.str, s.len);
7343  }, msg, errdata);
7344  // Save the error params for subsequent testing in the quickstart.
7345  //
7346  // To add the source context, the source buffer is required. If
7347  // the caller is interested in enriching the full message with the
7348  // source buffer context, he can ensure that the source buffer is
7349  // kept, and then arrange the handler to access it.
7350  // For now, we assign the full message without context:
7352  saved_basic_loc = errdata.cpploc;
7353  saved_visit_tree = errdata.tree;
7354  saved_visit_id = errdata.node;
7355  stopexec(saved_msg_full);
7356 }
ryml::Location saved_basic_loc
Definition: quickstart.cpp:359
C * str
a restricted pointer to the first character of the substring
Definition: substr.hpp:216
std::string saved_msg_full
Definition: quickstart.cpp:357
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
ryml::id_type saved_visit_id
Definition: quickstart.cpp:362
std::string saved_msg_full_with_context
Definition: quickstart.cpp:358
size_t len
the length of the substring
Definition: substr.hpp:218
std::string saved_msg_short
Definition: quickstart.cpp:356
ryml::Tree const * saved_visit_tree
Definition: quickstart.cpp:361
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 7359 of file quickstart.cpp.

Referenced by ErrorHandlerExample::callbacks(), ErrorHandlerExample::check_disabled(), and ErrorHandlerExample::check_enabled().

7360 {
7361  static_cast<ErrorHandlerExample*>(this_)->on_error_basic(msg, errdata);
7362 }
void on_error_basic(ryml::csubstr msg, ryml::ErrorDataBasic const &errdata)
this is where the callback implementation goes.
an error handler used by some of the quickstart examples.
Definition: quickstart.cpp:331
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 7364 of file quickstart.cpp.

Referenced by ErrorHandlerExample::callbacks(), ErrorHandlerExample::check_disabled(), and ErrorHandlerExample::check_enabled().

7365 {
7366  static_cast<ErrorHandlerExample*>(this_)->on_error_parse(msg, errdata);
7367 }
void on_error_parse(ryml::csubstr msg, ryml::ErrorDataParse const &errdata)
this is where the callback implementation goes.
an error handler used by some of the quickstart examples.
Definition: quickstart.cpp:331
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 7369 of file quickstart.cpp.

Referenced by ErrorHandlerExample::callbacks(), ErrorHandlerExample::check_disabled(), and ErrorHandlerExample::check_enabled().

7370 {
7371  static_cast<ErrorHandlerExample*>(this_)->on_error_visit(msg, errdata);
7372 }
void on_error_visit(ryml::csubstr msg, ryml::ErrorDataVisit const &errdata)
this is where the callback implementation goes.
an error handler used by some of the quickstart examples.
Definition: quickstart.cpp:331
ryml::Callbacks ErrorHandlerExample::callbacks ( )

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

Definition at line 7378 of file quickstart.cpp.

Referenced by sample_docs(), sample_error_handler(), sample_error_parse(), sample_error_visit(), sample_error_visit_location(), sample_fundamental_types(), sample_quick_overview(), and ScopedErrorHandlerExample::ScopedErrorHandlerExample().

7379 {
7381  copy.set_user_data(this)
7385  return copy;
7386 }
Callbacks & set_error_parse(pfn_error_parse error_parse=nullptr)
Set or reset the error_parse callback.
Definition: common.cpp:177
static void s_error_basic(ryml::csubstr msg, ryml::ErrorDataBasic const &errdata, void *this_)
trampoline function to call the object's method
A c-style callbacks class to customize behavior on errors or allocation.
Definition: common.hpp:373
Callbacks & set_user_data(void *user_data)
Set the user data.
Definition: common.cpp:144
static void s_error_parse(ryml::csubstr msg, ryml::ErrorDataParse const &errdata, void *this_)
trampoline function to call the object's method
Callbacks & set_error_basic(pfn_error_basic error_basic=nullptr)
Set or reset the error_basic callback.
Definition: common.cpp:168
ryml::Callbacks original_callbacks
Definition: quickstart.cpp:334
static void s_error_visit(ryml::csubstr msg, ryml::ErrorDataVisit const &errdata, void *this_)
trampoline function to call the object's method
Callbacks & set_error_visit(pfn_error_visit error_visit=nullptr)
Set or reset the error_visit callback.
Definition: common.cpp:186
void ErrorHandlerExample::check_enabled ( ) const

test that this handler is currently set

Definition at line 7389 of file quickstart.cpp.

Referenced by sample_error_handler(), and ScopedErrorHandlerExample::ScopedErrorHandlerExample().

7390 {
7391  ryml::Callbacks const& current = ryml::get_callbacks();
7392  CHECK(current.m_error_basic == &s_error_basic);
7393  CHECK(current.m_error_parse == &s_error_parse);
7394  CHECK(current.m_error_visit == &s_error_visit);
7396  CHECK(current.m_free == original_callbacks.m_free);
7397 }
static void s_error_basic(ryml::csubstr msg, ryml::ErrorDataBasic const &errdata, void *this_)
trampoline function to call the object's method
A c-style callbacks class to customize behavior on errors or allocation.
Definition: common.hpp:373
static void s_error_parse(ryml::csubstr msg, ryml::ErrorDataParse const &errdata, void *this_)
trampoline function to call the object's method
ryml::Callbacks original_callbacks
Definition: quickstart.cpp:334
pfn_error_parse m_error_parse
a pointer to a parse error handler function
Definition: common.hpp:379
static void s_error_visit(ryml::csubstr msg, ryml::ErrorDataVisit const &errdata, void *this_)
trampoline function to call the object's method
pfn_free m_free
a pointer to a free handler function
Definition: common.hpp:377
#define CHECK(predicate)
a testing assertion, used only in this quickstart
Definition: quickstart.cpp:310
pfn_error_basic m_error_basic
a pointer to a basic error handler function
Definition: common.hpp:378
pfn_error_visit m_error_visit
a pointer to a visit error handler function
Definition: common.hpp:380
pfn_allocate m_allocate
a pointer to an allocate handler function
Definition: common.hpp:376
Callbacks const & get_callbacks()
get the global callbacks
Definition: common.cpp:94
void ErrorHandlerExample::check_disabled ( ) const

test that this handler is currently not set

Definition at line 7399 of file quickstart.cpp.

Referenced by sample_error_handler(), and ScopedErrorHandlerExample::~ScopedErrorHandlerExample().

7400 {
7401  ryml::Callbacks const& current = ryml::get_callbacks();
7402  CHECK(current.m_error_basic != &s_error_basic);
7403  CHECK(current.m_error_parse != &s_error_parse);
7404  CHECK(current.m_error_visit != &s_error_visit);
7406  CHECK(current.m_free == original_callbacks.m_free);
7407 }
static void s_error_basic(ryml::csubstr msg, ryml::ErrorDataBasic const &errdata, void *this_)
trampoline function to call the object's method
A c-style callbacks class to customize behavior on errors or allocation.
Definition: common.hpp:373
static void s_error_parse(ryml::csubstr msg, ryml::ErrorDataParse const &errdata, void *this_)
trampoline function to call the object's method
ryml::Callbacks original_callbacks
Definition: quickstart.cpp:334
pfn_error_parse m_error_parse
a pointer to a parse error handler function
Definition: common.hpp:379
static void s_error_visit(ryml::csubstr msg, ryml::ErrorDataVisit const &errdata, void *this_)
trampoline function to call the object's method
pfn_free m_free
a pointer to a free handler function
Definition: common.hpp:377
#define CHECK(predicate)
a testing assertion, used only in this quickstart
Definition: quickstart.cpp:310
pfn_error_basic m_error_basic
a pointer to a basic error handler function
Definition: common.hpp:378
pfn_error_visit m_error_visit
a pointer to a visit error handler function
Definition: common.hpp:380
pfn_allocate m_allocate
a pointer to an allocate handler function
Definition: common.hpp:376
Callbacks const & get_callbacks()
get the global callbacks
Definition: common.cpp:94

Variable Documentation

std::jmp_buf s_jmp_env
static

Definition at line 7230 of file quickstart.cpp.

std::string s_jmp_msg
static

Definition at line 7231 of file quickstart.cpp.