rapidyaml 0.15.2
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
Sample helpers

Helper utilities used in the quickstart. More...

Topics

 Serialize/deserialize scalar types
 Serialize/deserialize 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)
 a testing assertion, used only in this quickstart

Functions

bool report_check (int line, const char *predicate, bool result)
 used by CHECK()
void ensure_callbacks ()
 set up default callbacks when ryml does not provide them (ie when RYML_NO_DEFAULT_CALLBACKS is defined)
ryml::Callbacks default_callbacks ()
 set up a bare-bones implementation of the callbacks
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.
template<class Fn>
bool ErrorHandlerExample::check_error_occurs (Fn &&fn)
 checking that an error occurs while calling fn
void ErrorHandlerExample::on_error_basic (ryml::csubstr msg, ryml::ErrorDataBasic const &errdata)
 this is where the callback implementation goes.
void ErrorHandlerExample::on_error_parse (ryml::csubstr msg, ryml::ErrorDataParse const &errdata)
 this is where the callback implementation goes.
void ErrorHandlerExample::on_error_visit (ryml::csubstr msg, ryml::ErrorDataVisit const &errdata)
 this is where the callback implementation goes.
static void ErrorHandlerExample::s_error_basic (ryml::csubstr msg, ryml::ErrorDataBasic const &errdata, void *this_)
 trampoline function to call the object's method
static void ErrorHandlerExample::s_error_parse (ryml::csubstr msg, ryml::ErrorDataParse const &errdata, void *this_)
 trampoline function to call the object's method
static void ErrorHandlerExample::s_error_visit (ryml::csubstr msg, ryml::ErrorDataVisit const &errdata, void *this_)
 trampoline function to call the object's method
ryml::Callbacks ErrorHandlerExample::callbacks ()
 a helper to create the Callbacks object for the custom error handler
void ErrorHandlerExample::check_enabled () const
 test that this handler is currently set
void ErrorHandlerExample::check_disabled () const
 test that this handler is currently not set

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

◆ CHECK

#define CHECK ( predicate)
Value:
assert(predicate)

a testing assertion, used only in this quickstart

Definition at line 308 of file quickstart.cpp.

Referenced by GlobalAllocatorExample::check_and_reset(), PerTreeMemoryExample::check_and_reset(), ErrorHandlerExample::check_disabled(), ErrorHandlerExample::check_enabled(), my_map_type< K, V >::check_eq(), my_seq_type< T >::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(), and sample_user_scalar_types().

Function Documentation

◆ report_check()

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

used by CHECK()

Definition at line 7037 of file quickstart.cpp.

7038{
7039 ++num_checks;
7040 const char *msg = predicate ? "OK! " : "OK!";
7041 if(!result)
7042 {
7043 ++num_failed_checks; // LCOV_EXCL_LINE
7044 msg = predicate ? "FAIL: " : "FAIL"; // LCOV_EXCL_LINE
7045 }
7046 if(!result || !quiet_mode)
7047 {
7048 std::cout << __FILE__ << ':' << line << ": " << msg << (predicate ? predicate : "") << std::endl;
7049 }
7050 return result;
7051}

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

7134{
7135#ifdef RYML_NO_DEFAULT_CALLBACKS
7137#endif
7138}
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

Referenced by main().

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

7092{
7093 return ryml::Callbacks{}
7094 .set_allocate([](size_t len, void* , void *){
7095 return malloc(len); // NOLINT
7096 })
7097 .set_free([](void* mem, size_t, void *){
7098 free(mem); // NOLINT
7099 })
7100 ///
7101 /// The default error callbacks won't be called in this
7102 /// quickstart, because no errors are expected. But we
7103 /// implement them here to show how a bare-bones implementation
7104 /// looks like, and also because they are needed when
7105 /// @ref RYML_NO_DEFAULT_CALLBACKS is defined.
7106 ///
7107 /// For a different (more involved) implementation of the error
7108 /// callbacks, see the implementation of @ref ErrorHandlerExample
7109 /// below.
7110 ///
7111 // LCOV_EXCL_START
7112 .set_error_basic([](ryml::csubstr msg, ryml::ErrorDataBasic const& errdata, void *){
7113 ryml::err_basic_format(errdump, msg, errdata); // format the message, printing to stderr
7114 errend(); // print newline and flush
7115 abort(); // abort (must never return: abort, or exception or setjmp)
7116 })
7117 .set_error_parse([](ryml::csubstr msg, ryml::ErrorDataParse const& errdata, void *){
7118 ryml::err_parse_format(errdump, msg, errdata); // format the message, printing to out
7119 errend(); // print newline and flush
7120 abort(); // abort (must never return: abort, or exception or setjmp)
7121 })
7122 .set_error_visit([](ryml::csubstr msg, ryml::ErrorDataVisit const& errdata, void *){
7123 ryml::err_visit_format(errdump, msg, errdata); // format the message, printing to out
7124 errend(); // print newline and flush
7125 abort(); // abort (must never return: abort, or exception or setjmp)
7126 });
7127 // LCOV_EXCL_STOP
7128}
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.
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.
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.
basic_substring< const char > csubstr
an immutable string view
Definition substr.hpp:2356
A c-style callbacks class to customize behavior on errors or allocation.
Definition common.hpp:374
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:260
Data for a parse error.
Definition common.hpp:269
Data for a visit error.
Definition common.hpp:279

Referenced by ensure_callbacks(), and sample_static_trees().

◆ handle_args()

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

Definition at line 7027 of file quickstart.cpp.

7028{
7029 auto arg_matches = [](const char *arg, const char *shortform, const char *longform) {
7030 return (0 == strcmp(arg, shortform) || 0 == strcmp(arg, longform));
7031 };
7032 for(int i = 1; i < argc; ++i)
7033 if(arg_matches(argv[i], "-q", "--quiet"))
7034 quiet_mode = true;
7035}

Referenced by main().

◆ report_checks()

int report_checks ( )

Definition at line 7054 of file quickstart.cpp.

7055{
7056 std::cout << "Completed " << num_checks << " checks." << std::endl;
7057 if(num_failed_checks)
7058 std::cout << "ERROR: " << num_failed_checks << '/' << num_checks << " checks failed." << std::endl; // LCOV_EXCL_LINE
7059 else
7060 std::cout << "SUCCESS!" << std::endl;
7061 return num_failed_checks;
7062}

Referenced by main().

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

7155{
7156 #if RYML_USE_ASSERT
7157 return check_error_occurs(std::forward<Fn>(fn));
7158 #else
7159 (void)fn; // do nothing otherwise, as there would be undefined behavior
7160 return true;
7161 #endif
7162}
bool check_error_occurs(Fn &&fn)
checking that an error occurs while calling fn

Referenced by sample_quick_overview().

◆ check_error_occurs()

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

checking that an error occurs while calling fn

Definition at line 7166 of file quickstart.cpp.

7167{
7168 saved_msg_short.clear();
7169 saved_msg_full.clear();
7171 saved_basic_loc = {};
7172 saved_parse_loc = {};
7173 saved_visit_tree = {};
7175 bool got_error = false;
7176 #ifdef C4_EXCEPTIONS
7177 try
7178 {
7179 std::forward<Fn>(fn)();
7180 }
7181 catch(std::exception const&)
7182 {
7183 got_error = true;
7184 }
7185 #else
7186 if(setjmp(s_jmp_env) == 0)
7187 {
7188 std::forward<Fn>(fn)();
7189 }
7190 else
7191 {
7192 got_error = true;
7193 }
7194 #endif
7195 return got_error;
7196}
static std::jmp_buf s_jmp_env
@ NONE
an index to none
Definition common.hpp:131
ryml::Location saved_basic_loc
std::string saved_msg_full
ryml::id_type saved_visit_id
std::string saved_msg_short
ryml::Tree const * saved_visit_tree
ryml::Location saved_parse_loc
std::string saved_msg_full_with_context

Referenced by 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().

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

7215{
7216 saved_msg_short.assign(msg.str, msg.len);
7217 // build a full error message with location
7219 saved_msg_full.append(s.str, s.len);
7220 }, msg, errdata);
7221 // Save the error params for subsequent testing in the quickstart.
7223 saved_basic_loc = errdata.location;
7224 stopexec(saved_msg_short);
7225}
size_t len
the length of the substring
Definition substr.hpp:218
C * str
a restricted pointer to the first character of the substring
Definition substr.hpp:216

Referenced by s_error_basic().

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

7231{
7232 saved_msg_short.assign(msg.str, msg.len);
7233 // build a full error message with location
7235 saved_msg_full.append(s.str, s.len);
7236 }, msg, errdata);
7237 // Save the error params for subsequent testing in the quickstart.
7238 //
7239 // To add the source context, the source buffer is required. If
7240 // the caller is interested in enriching the full message with the
7241 // source buffer context, he can ensure that the source buffer is
7242 // kept, and then arrange the handler to access it.
7243 // For now, we assign the full message without context:
7245 saved_basic_loc = errdata.cpploc;
7246 saved_parse_loc = errdata.ymlloc;
7247 stopexec(saved_msg_full);
7248}

Referenced by s_error_parse().

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

7253{
7254 saved_msg_short.assign(msg.str, msg.len);
7255 // build a full error message with location
7257 saved_msg_full.append(s.str, s.len);
7258 }, msg, errdata);
7259 // Save the error params for subsequent testing in the quickstart.
7260 //
7261 // To add the source context, the source buffer is required. If
7262 // the caller is interested in enriching the full message with the
7263 // source buffer context, he can ensure that the source buffer is
7264 // kept, and then arrange the handler to access it.
7265 // For now, we assign the full message without context:
7267 saved_basic_loc = errdata.cpploc;
7268 saved_visit_tree = errdata.tree;
7269 saved_visit_id = errdata.node;
7270 stopexec(saved_msg_full);
7271}

Referenced by s_error_visit().

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

7275{
7276 static_cast<ErrorHandlerExample*>(this_)->on_error_basic(msg, errdata);
7277}
void on_error_basic(ryml::csubstr msg, ryml::ErrorDataBasic const &errdata)
this is where the callback implementation goes.

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

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

7280{
7281 static_cast<ErrorHandlerExample*>(this_)->on_error_parse(msg, errdata);
7282}
void on_error_parse(ryml::csubstr msg, ryml::ErrorDataParse const &errdata)
this is where the callback implementation goes.

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

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

7285{
7286 static_cast<ErrorHandlerExample*>(this_)->on_error_visit(msg, errdata);
7287}
void on_error_visit(ryml::csubstr msg, ryml::ErrorDataVisit const &errdata)
this is where the callback implementation goes.

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

◆ callbacks()

ryml::Callbacks ErrorHandlerExample::callbacks ( )

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

Definition at line 7293 of file quickstart.cpp.

7294{
7295 ryml::Callbacks copy = original_callbacks;
7296 copy.set_user_data(this)
7300 return copy;
7301}
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
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

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

◆ check_enabled()

void ErrorHandlerExample::check_enabled ( ) const

test that this handler is currently set

Definition at line 7304 of file quickstart.cpp.

7305{
7306 ryml::Callbacks const& current = ryml::get_callbacks();
7307 CHECK(current.m_error_basic == &s_error_basic);
7308 CHECK(current.m_error_parse == &s_error_parse);
7309 CHECK(current.m_error_visit == &s_error_visit);
7310 CHECK(current.m_allocate == original_callbacks.m_allocate);
7311 CHECK(current.m_free == original_callbacks.m_free);
7312}
Callbacks const & get_callbacks()
get the global callbacks
Definition common.cpp:94
#define CHECK(predicate)
a testing assertion, used only in this quickstart
pfn_error_basic m_error_basic
a pointer to a basic error handler function
Definition common.hpp:378
pfn_error_parse m_error_parse
a pointer to a parse error handler function
Definition common.hpp:379
pfn_allocate m_allocate
a pointer to an allocate handler function
Definition common.hpp:376
pfn_error_visit m_error_visit
a pointer to a visit error handler function
Definition common.hpp:380
pfn_free m_free
a pointer to a free handler function
Definition common.hpp:377

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

◆ check_disabled()

void ErrorHandlerExample::check_disabled ( ) const

test that this handler is currently not set

Definition at line 7314 of file quickstart.cpp.

7315{
7316 ryml::Callbacks const& current = ryml::get_callbacks();
7317 CHECK(current.m_error_basic != &s_error_basic);
7318 CHECK(current.m_error_parse != &s_error_parse);
7319 CHECK(current.m_error_visit != &s_error_visit);
7320 CHECK(current.m_allocate == original_callbacks.m_allocate);
7321 CHECK(current.m_free == original_callbacks.m_free);
7322}

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

Variable Documentation

◆ s_jmp_env

std::jmp_buf s_jmp_env
static

Definition at line 7146 of file quickstart.cpp.

Referenced by ErrorHandlerExample::check_error_occurs().

◆ s_jmp_msg

std::string s_jmp_msg
static

Definition at line 7147 of file quickstart.cpp.