rapidyaml  0.9.0
parse and emit YAML, and do it fast
Sample helpers

Functions and classes used in the examples of this 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  sample::ErrorHandlerExample
 this is an example error handler, required for some of the quickstart examples. More...
 
struct  sample::ScopedErrorHandlerExample
 Shows how to easily create a scoped error handler. More...
 
struct  sample::GlobalAllocatorExample
 
struct  sample::PerTreeMemoryExample
 an example for a per-tree memory allocator More...
 

Macros

#define CHECK(predicate)   assert(predicate)
 

Functions

bool sample::report_check (int line, const char *predicate, bool result)
 
template<class CharContainer >
CharContainer sample::file_get_contents (const char *filename)
 load a file from disk and return a newly created CharContainer More...
 
template<class CharContainer >
size_t sample::file_get_contents (const char *filename, CharContainer *v)
 load a file from disk into an existing CharContainer More...
 
template<class CharContainer >
void sample::file_put_contents (const char *filename, CharContainer const &v, const char *access)
 save a buffer into a file More...
 
void sample::file_put_contents (const char *filename, const char *buf, size_t sz, const char *access)
 save a buffer into a file More...
 
int sample::report_checks ()
 
 sample::C4_IF_EXCEPTIONS_ (, static std::jmp_buf s_jmp_env;static std::string s_jmp_msg;) template< class Fn > bool ErrorHandlerExample
 this C-style callback is the one stored and used by ryml. More...
 
void sample::ErrorHandlerExample::on_error (const char *msg, size_t len, ryml::Location loc)
 this is the where the callback implementation goes. More...
 

Detailed Description

Functions and classes used in the examples of this sample.

Macro Definition Documentation

◆ CHECK

#define CHECK (   predicate)    assert(predicate)

Definition at line 229 of file quickstart.cpp.

Function Documentation

◆ report_check()

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

Definition at line 5198 of file quickstart.cpp.

5199 {
5200  ++num_checks;
5201  const char *msg = predicate ? "OK! " : "OK!";
5202  if(!result)
5203  {
5204  ++num_failed_checks;
5205  msg = predicate ? "ERROR: " : "ERROR";
5206  }
5207  std::cout << __FILE__ << ':' << line << ": " << msg << (predicate ? predicate : "") << std::endl;
5208  return result;
5209 }

◆ file_get_contents() [1/2]

template<class CharContainer >
CharContainer sample::file_get_contents ( const char *  filename)

load a file from disk and return a newly created CharContainer

Definition at line 5326 of file quickstart.cpp.

5327 {
5328  CharContainer cc;
5329  file_get_contents(filename, &cc);
5330  return cc;
5331 }
size_t file_get_contents(const char *filename, CharContainer *v)
load a file from disk into an existing CharContainer

◆ file_get_contents() [2/2]

template<class CharContainer >
size_t sample::file_get_contents ( const char *  filename,
CharContainer *  v 
)

load a file from disk into an existing CharContainer

Definition at line 5307 of file quickstart.cpp.

5308 {
5309  std::FILE *fp = std::fopen(filename, "rb");
5310  RYML_CHECK_MSG(fp != nullptr, "could not open file");
5311  std::fseek(fp, 0, SEEK_END);
5312  long sz = std::ftell(fp);
5313  v->resize(static_cast<typename CharContainer::size_type>(sz));
5314  if(sz)
5315  {
5316  std::rewind(fp);
5317  size_t ret = std::fread(&(*v)[0], 1, v->size(), fp);
5318  RYML_CHECK(ret == (size_t)sz);
5319  }
5320  std::fclose(fp);
5321  return v->size();
5322 }

◆ file_put_contents() [1/2]

template<class CharContainer >
void sample::file_put_contents ( const char *  filename,
CharContainer const &  v,
const char *  access = "wb" 
)

save a buffer into a file

Definition at line 5335 of file quickstart.cpp.

5336 {
5337  file_put_contents(filename, v.empty() ? "" : &v[0], v.size(), access);
5338 }
void file_put_contents(const char *filename, const char *buf, size_t sz, const char *access)
save a buffer into a file

◆ file_put_contents() [2/2]

void sample::file_put_contents ( const char *  filename,
const char *  buf,
size_t  sz,
const char *  access 
)

save a buffer into a file

Definition at line 5341 of file quickstart.cpp.

5342 {
5343  std::FILE *fp = std::fopen(filename, access);
5344  RYML_CHECK_MSG(fp != nullptr, "could not open file");
5345  std::fwrite(buf, 1, sz, fp);
5346  std::fclose(fp);
5347 }

Referenced by sample::sample_parse_file().

◆ report_checks()

int sample::report_checks ( )

Definition at line 5212 of file quickstart.cpp.

5213 {
5214  std::cout << "Completed " << num_checks << " checks." << std::endl;
5215  if(num_failed_checks)
5216  std::cout << "ERROR: " << num_failed_checks << '/' << num_checks << " checks failed." << std::endl;
5217  else
5218  std::cout << "SUCCESS!" << std::endl;
5219  return num_failed_checks;
5220 }

◆ C4_IF_EXCEPTIONS_()

sample::C4_IF_EXCEPTIONS_ ( static std::jmp_buf s_jmp_env;static std::string s_jmp_msg;  )

this C-style callback is the one stored and used by ryml.

It is a trampoline function calling on_error()

Definition at line 5225 of file quickstart.cpp.

5234 {
5235  bool expected_error_occurred = false;
5236  C4_IF_EXCEPTIONS_(try, if(setjmp(s_jmp_env) == 0)) // selectively picks based on availability of exceptions
5237  {
5238  fn();
5239  }
5240  C4_IF_EXCEPTIONS_(catch(...), else)
5241  {
5242  expected_error_occurred = true;
5243  }
5244  return expected_error_occurred;
5245 }
5246 template<class Fn>
5247 C4_NODISCARD bool ErrorHandlerExample::check_assertion_occurs(Fn &&fn) const
5248 {
5249 #if RYML_USE_ASSERT
5250  return check_error_occurs(fn);
5251 #else
5252  (void)fn; // do nothing otherwise, as there would be undefined behavior
5253  return true;
5254 #endif
5255 }
5256 
5257 /** this C-style callback is the one stored and used by ryml. It is a
5258  * trampoline function calling on_error() */
5259 C4_NORETURN void ErrorHandlerExample::s_error(const char* msg, size_t len, ryml::Location loc, void *this_)
5260 {
5261  ((ErrorHandlerExample*)this_)->on_error(msg, len, loc);
5262 }
C4_IF_EXCEPTIONS_(, static std::jmp_buf s_jmp_env;static std::string s_jmp_msg;) template< class Fn > bool ErrorHandlerExample
this C-style callback is the one stored and used by ryml.
a source file position
Definition: common.hpp:297

◆ on_error()

void sample::ErrorHandlerExample::on_error ( const char *  msg,
size_t  len,
ryml::Location  loc 
)

this is the where the callback implementation goes.

Remember that it must not return.

Definition at line 5264 of file quickstart.cpp.

5265 {
5266  std::string full_msg = ryml::formatrs<std::string>(
5267  "{}:{}:{} ({}B): ERROR: {}",
5268  loc.name, loc.line, loc.col, loc.offset, ryml::csubstr(msg, len));
5269  C4_IF_EXCEPTIONS(
5270  // this will execute if exceptions are enabled.
5271  throw std::runtime_error(full_msg);
5272  ,
5273  // this will execute if exceptions are disabled. It will
5274  // jump to the function calling the corresponding setjmp().
5275  s_jmp_msg = full_msg;
5276  std::longjmp(s_jmp_env, 1);
5277  );
5278 }
size_t col
column
Definition: common.hpp:303
size_t line
line
Definition: common.hpp:301
size_t offset
number of bytes from the beginning of the source buffer
Definition: common.hpp:299
csubstr name
file name
Definition: common.hpp:305

References c4::yml::Location::col, c4::yml::Location::line, c4::yml::Location::name, and c4::yml::Location::offset.