rapidyaml 0.16.0
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 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)
 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 310 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(), sample_user_container_types_brief(), 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 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}

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

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

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

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

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}

Referenced by main().

◆ report_checks()

int report_checks ( )

Definition at line 7138 of file quickstart.cpp.

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}

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

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

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

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

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

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}

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

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}

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

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.

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

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.

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

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.

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

7379{
7380 ryml::Callbacks copy = original_callbacks;
7381 copy.set_user_data(this)
7385 return copy;
7386}
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 7389 of file quickstart.cpp.

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);
7395 CHECK(current.m_allocate == original_callbacks.m_allocate);
7396 CHECK(current.m_free == original_callbacks.m_free);
7397}
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 7399 of file quickstart.cpp.

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);
7405 CHECK(current.m_allocate == original_callbacks.m_allocate);
7406 CHECK(current.m_free == original_callbacks.m_free);
7407}

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

Variable Documentation

◆ s_jmp_env

std::jmp_buf s_jmp_env
static

Definition at line 7230 of file quickstart.cpp.

Referenced by ErrorHandlerExample::check_error_occurs().

◆ s_jmp_msg

std::string s_jmp_msg
static

Definition at line 7231 of file quickstart.cpp.