rapidyaml  0.7.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 5170 of file quickstart.cpp.

5171 {
5172  ++num_checks;
5173  const char *msg = predicate ? "OK! " : "OK!";
5174  if(!result)
5175  {
5176  ++num_failed_checks;
5177  msg = predicate ? "ERROR: " : "ERROR";
5178  }
5179  std::cout << __FILE__ << ':' << line << ": " << msg << (predicate ? predicate : "") << std::endl;
5180  return result;
5181 }

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

5299 {
5300  CharContainer cc;
5301  file_get_contents(filename, &cc);
5302  return cc;
5303 }
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 5279 of file quickstart.cpp.

5280 {
5281  std::FILE *fp = std::fopen(filename, "rb");
5282  RYML_CHECK_MSG(fp != nullptr, "could not open file");
5283  std::fseek(fp, 0, SEEK_END);
5284  long sz = std::ftell(fp);
5285  v->resize(static_cast<typename CharContainer::size_type>(sz));
5286  if(sz)
5287  {
5288  std::rewind(fp);
5289  size_t ret = std::fread(&(*v)[0], 1, v->size(), fp);
5290  RYML_CHECK(ret == (size_t)sz);
5291  }
5292  std::fclose(fp);
5293  return v->size();
5294 }

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

5308 {
5309  file_put_contents(filename, v.empty() ? "" : &v[0], v.size(), access);
5310 }
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 5313 of file quickstart.cpp.

5314 {
5315  std::FILE *fp = std::fopen(filename, access);
5316  RYML_CHECK_MSG(fp != nullptr, "could not open file");
5317  std::fwrite(buf, 1, sz, fp);
5318  std::fclose(fp);
5319 }

Referenced by sample::sample_parse_file().

◆ report_checks()

int sample::report_checks ( )

Definition at line 5184 of file quickstart.cpp.

5185 {
5186  std::cout << "Completed " << num_checks << " checks." << std::endl;
5187  if(num_failed_checks)
5188  std::cout << "ERROR: " << num_failed_checks << '/' << num_checks << " checks failed." << std::endl;
5189  else
5190  std::cout << "SUCCESS!" << std::endl;
5191  return num_failed_checks;
5192 }

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

5206 {
5207  bool expected_error_occurred = false;
5208  C4_IF_EXCEPTIONS_(try, if(setjmp(s_jmp_env) == 0)) // selectively picks based on availability of exceptions
5209  {
5210  fn();
5211  }
5212  C4_IF_EXCEPTIONS_(catch(...), else)
5213  {
5214  expected_error_occurred = true;
5215  }
5216  return expected_error_occurred;
5217 }
5218 template<class Fn>
5219 C4_NODISCARD bool ErrorHandlerExample::check_assertion_occurs(Fn &&fn) const
5220 {
5221 #if RYML_USE_ASSERT
5222  return check_error_occurs(fn);
5223 #else
5224  (void)fn; // do nothing otherwise, as there would be undefined behavior
5225  return true;
5226 #endif
5227 }
5228 
5229 /** this C-style callback is the one stored and used by ryml. It is a
5230  * trampoline function calling on_error() */
5231 C4_NORETURN void ErrorHandlerExample::s_error(const char* msg, size_t len, ryml::Location loc, void *this_)
5232 {
5233  ((ErrorHandlerExample*)this_)->on_error(msg, len, loc);
5234 }
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:296

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

5237 {
5238  std::string full_msg = ryml::formatrs<std::string>(
5239  "{}:{}:{} ({}B): ERROR: {}",
5240  loc.name, loc.line, loc.col, loc.offset, ryml::csubstr(msg, len));
5241  C4_IF_EXCEPTIONS(
5242  // this will execute if exceptions are enabled.
5243  throw std::runtime_error(full_msg);
5244  ,
5245  // this will execute if exceptions are disabled. It will
5246  // jump to the function calling the corresponding setjmp().
5247  s_jmp_msg = full_msg;
5248  std::longjmp(s_jmp_env, 1);
5249  );
5250 }
size_t col
column
Definition: common.hpp:302
size_t line
line
Definition: common.hpp:300
size_t offset
number of bytes from the beginning of the source buffer
Definition: common.hpp:298
csubstr name
file name
Definition: common.hpp:304

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