rapidyaml  0.8.0
parse and emit YAML, and do it fast
common.hpp
Go to the documentation of this file.
1 #ifndef _C4_YML_COMMON_HPP_
2 #define _C4_YML_COMMON_HPP_
3 
4 /** @file common.hpp Common utilities and infrastructure used by ryml. */
5 
6 #include <cstddef>
7 #include <c4/substr.hpp>
8 #include <c4/charconv.hpp>
9 #include <c4/dump.hpp>
10 #include <c4/yml/export.hpp>
11 
12 #if defined(C4_MSVC) || defined(C4_MINGW)
13 #include <malloc.h>
14 #else
15 #include <alloca.h>
16 #endif
17 
18 
19 
20 //-----------------------------------------------------------------------------
21 
22 #ifndef RYML_ERRMSG_SIZE
23 /// size for the error message buffer
24 #define RYML_ERRMSG_SIZE (1024)
25 #endif
26 
27 #ifndef RYML_LOGBUF_SIZE
28 /// size for the buffer used to format individual values to string
29 /// while preparing an error message. This is only used for formatting
30 /// individual values in the message; final messages will be larger
31 /// than this value (see @ref RYML_ERRMSG_SIZE). This is also used for
32 /// the detailed debug log messages when RYML_DBG is defined.
33 #define RYML_LOGBUF_SIZE (256)
34 #endif
35 
36 #ifndef RYML_LOGBUF_SIZE_MAX
37 /// size for the fallback larger log buffer. When @ref
38 /// RYML_LOGBUF_SIZE is not large enough to convert a value to string,
39 /// then temporary stack memory is allocated up to
40 /// RYML_LOGBUF_SIZE_MAX. This limit is in place to prevent a stack
41 /// overflow. If the printed value requires more than
42 /// RYML_LOGBUF_SIZE_MAX, the value is silently skipped.
43 #define RYML_LOGBUF_SIZE_MAX (1024)
44 #endif
45 
46 #ifndef RYML_LOCATIONS_SMALL_THRESHOLD
47 /// threshold at which a location search will revert from linear to
48 /// binary search.
49 #define RYML_LOCATIONS_SMALL_THRESHOLD (30)
50 #endif
51 
52 
53 //-----------------------------------------------------------------------------
54 // Specify groups to have a predefined topic order in doxygen:
55 
56 /** @defgroup doc_quickstart Quickstart
57  *
58  * Example code for every feature.
59  */
60 
61 /** @defgroup doc_parse Parse utilities
62  * @see sample::sample_parse_in_place
63  * @see sample::sample_parse_in_arena
64  * @see sample::sample_parse_file
65  * @see sample::sample_parse_reuse_tree
66  * @see sample::sample_parse_reuse_parser
67  * @see sample::sample_parse_reuse_tree_and_parser
68  * @see sample::sample_location_tracking
69  */
70 
71 /** @defgroup doc_emit Emit utilities
72  *
73  * Utilities to emit YAML and JSON, either to a memory buffer or to a
74  * file or ostream-like class.
75  *
76  * @see sample::sample_emit_to_container
77  * @see sample::sample_emit_to_stream
78  * @see sample::sample_emit_to_file
79  * @see sample::sample_emit_nested_node
80  * @see sample::sample_emit_style
81  */
82 
83 /** @defgroup doc_node_type Node types
84  */
85 
86 /** @defgroup doc_tree Tree utilities
87  * @see sample::sample_quick_overview
88  * @see sample::sample_iterate_trees
89  * @see sample::sample_create_trees
90  * @see sample::sample_tree_arena
91  *
92  * @see sample::sample_static_trees
93  * @see sample::sample_location_tracking
94  *
95  * @see sample::sample_docs
96  * @see sample::sample_anchors_and_aliases
97  * @see sample::sample_tags
98  */
99 
100 /** @defgroup doc_node_classes Node classes
101  *
102  * High-level node classes.
103  *
104  * @see sample::sample_quick_overview
105  * @see sample::sample_iterate_trees
106  * @see sample::sample_create_trees
107  * @see sample::sample_tree_arena
108  */
109 
110 /** @defgroup doc_callbacks Callbacks for errors and allocation
111  *
112  * Functions called by ryml to allocate/free memory and to report
113  * errors.
114  *
115  * @see sample::sample_error_handler
116  * @see sample::sample_global_allocator
117  * @see sample::sample_per_tree_allocator
118  */
119 
120 /** @defgroup doc_serialization Serialization/deserialization
121  *
122  * Contains information on how to serialize and deserialize
123  * fundamental types, user scalar types, user container types and
124  * interop with std scalar/container types.
125  *
126  */
127 
128 /** @defgroup doc_ref_utils Anchor/Reference utilities
129  *
130  * @see sample::sample_anchors_and_aliases
131  * */
132 
133 /** @defgroup doc_tag_utils Tag utilities
134  * @see sample::sample_tags
135  */
136 
137 /** @defgroup doc_preprocessors Preprocessors
138  *
139  * Functions for preprocessing YAML prior to parsing.
140  */
141 
142 
143 //-----------------------------------------------------------------------------
144 
145 // document macros for doxygen
146 #ifdef __DOXYGEN__ // defined in Doxyfile::PREDEFINED
147 
148 /** define this macro with a boolean value to enable/disable
149  * assertions to check preconditions and assumptions throughout the
150  * codebase; this causes a slowdown of the code, and larger code
151  * size. By default, this macro is defined unless NDEBUG is defined
152  * (see C4_USE_ASSERT); as a result, by default this macro is truthy
153  * only in debug builds. */
154 # define RYML_USE_ASSERT
155 
156 /** (Undefined by default) Define this macro to disable ryml's default
157  * implementation of the callback functions; see @ref c4::yml::Callbacks */
158 # define RYML_NO_DEFAULT_CALLBACKS
159 
160 /** (Undefined by default) When this macro is defined (and
161  * @ref RYML_NO_DEFAULT_CALLBACKS is not defined), the default error
162  * handler will throw C++ exceptions of type `std::runtime_error`. */
163 # define RYML_DEFAULT_CALLBACK_USES_EXCEPTIONS
164 
165 /** Conditionally expands to `noexcept` when @ref RYML_USE_ASSERT is 0 and
166  * is empty otherwise. The user is unable to override this macro. */
167 # define RYML_NOEXCEPT
168 
169 #endif
170 
171 
172 //-----------------------------------------------------------------------------
173 
174 
175 /** @cond dev*/
176 #ifndef RYML_USE_ASSERT
177 # define RYML_USE_ASSERT C4_USE_ASSERT
178 #endif
179 
180 #if RYML_USE_ASSERT
181 # define RYML_ASSERT(cond) RYML_CHECK(cond)
182 # define RYML_ASSERT_MSG(cond, msg) RYML_CHECK_MSG(cond, msg)
183 # define _RYML_CB_ASSERT(cb, cond) _RYML_CB_CHECK((cb), (cond))
184 # define _RYML_CB_ASSERT_(cb, cond, loc) _RYML_CB_CHECK((cb), (cond), (loc))
185 # define RYML_NOEXCEPT
186 #else
187 # define RYML_ASSERT(cond)
188 # define RYML_ASSERT_MSG(cond, msg)
189 # define _RYML_CB_ASSERT(cb, cond)
190 # define _RYML_CB_ASSERT_(cb, cond, loc)
191 # define RYML_NOEXCEPT noexcept
192 #endif
193 
194 #define RYML_DEPRECATED(msg) C4_DEPRECATED(msg)
195 
196 #define RYML_CHECK(cond) \
197  do { \
198  if(C4_UNLIKELY(!(cond))) \
199  { \
200  RYML_DEBUG_BREAK(); \
201  c4::yml::error("check failed: " #cond, c4::yml::Location(__FILE__, __LINE__, 0)); \
202  C4_UNREACHABLE_AFTER_ERR(); \
203  } \
204  } while(0)
205 
206 #define RYML_CHECK_MSG(cond, msg) \
207  do \
208  { \
209  if(C4_UNLIKELY(!(cond))) \
210  { \
211  RYML_DEBUG_BREAK(); \
212  c4::yml::error(msg ": check failed: " #cond, c4::yml::Location(__FILE__, __LINE__, 0)); \
213  C4_UNREACHABLE_AFTER_ERR(); \
214  } \
215  } while(0)
216 
217 #if defined(RYML_DBG) && !defined(NDEBUG) && !defined(C4_NO_DEBUG_BREAK)
218 # define RYML_DEBUG_BREAK() \
219  do { \
220  if(c4::get_error_flags() & c4::ON_ERROR_DEBUGBREAK) \
221  { \
222  C4_DEBUG_BREAK(); \
223  } \
224  } while(0)
225 #else
226 # define RYML_DEBUG_BREAK()
227 #endif
228 
229 /** @endcond */
230 
231 
232 //-----------------------------------------------------------------------------
233 //-----------------------------------------------------------------------------
234 //-----------------------------------------------------------------------------
235 
236 namespace c4 {
237 namespace yml {
238 
239 C4_SUPPRESS_WARNING_GCC_CLANG_WITH_PUSH("-Wold-style-cast")
240 
241 
242 #ifndef RYML_ID_TYPE
243 /** The type of a node id in the YAML tree. In the future, the default
244  * will likely change to int32_t, which was observed to be faster.
245  * @see id_type */
246 #define RYML_ID_TYPE size_t
247 #endif
248 
249 
250 /** The type of a node id in the YAML tree; to override the default
251  * type, define the macro @ref RYML_ID_TYPE to a suitable integer
252  * type. */
254 static_assert(std::is_integral<id_type>::value, "id_type must be an integer type");
255 
256 
257 C4_SUPPRESS_WARNING_GCC_WITH_PUSH("-Wuseless-cast")
258 enum : id_type {
259  /** an index to none */
260  NONE = id_type(-1),
261 };
262 C4_SUPPRESS_WARNING_GCC_CLANG_POP
263 
264 
265 enum : size_t {
266  /** a null string position */
267  npos = size_t(-1)
268 };
269 
270 
271 //-----------------------------------------------------------------------------
272 //-----------------------------------------------------------------------------
273 //-----------------------------------------------------------------------------
274 
275 //! holds a position into a source buffer
277 {
278  //! number of bytes from the beginning of the source buffer
279  size_t offset;
280  //! line
281  size_t line;
282  //! column
283  size_t col;
284 
285  LineCol() = default;
286  //! construct from line and column
287  LineCol(size_t l, size_t c) : offset(0), line(l), col(c) {}
288  //! construct from offset, line and column
289  LineCol(size_t o, size_t l, size_t c) : offset(o), line(l), col(c) {}
290 };
291 static_assert(std::is_trivial<LineCol>::value, "LineCol not trivial");
292 static_assert(std::is_standard_layout<LineCol>::value, "Location not trivial");
293 
294 
295 //! a source file position
297 {
298  //! number of bytes from the beginning of the source buffer
299  size_t offset;
300  //! line
301  size_t line;
302  //! column
303  size_t col;
304  //! file name
305  csubstr name;
306 
307  operator bool () const { return !name.empty() || line != 0 || offset != 0 || col != 0; }
308  operator LineCol const& () const { return reinterpret_cast<LineCol const&>(*this); } // NOLINT
309 
310  Location() = default;
311  Location( size_t l, size_t c) : offset( ), line(l), col(c), name( ) {}
312  Location( size_t b, size_t l, size_t c) : offset(b), line(l), col(c), name( ) {}
313  Location( csubstr n, size_t l, size_t c) : offset( ), line(l), col(c), name(n) {}
314  Location( csubstr n, size_t b, size_t l, size_t c) : offset(b), line(l), col(c), name(n) {}
315  Location(const char *n, size_t l, size_t c) : offset( ), line(l), col(c), name(to_csubstr(n)) {}
316  Location(const char *n, size_t b, size_t l, size_t c) : offset(b), line(l), col(c), name(to_csubstr(n)) {}
317 };
318 static_assert(std::is_standard_layout<Location>::value, "Location not trivial");
319 
320 
321 //-----------------------------------------------------------------------------
322 
323 /** @addtogroup doc_callbacks
324  *
325  * @{ */
326 
327 struct Callbacks;
328 
329 
330 /** set the global callbacks for the library; after a call to this
331  * function, these callbacks will be used by newly created objects
332  * (unless they are copying older objects with different
333  * callbacks). If @ref RYML_NO_DEFAULT_CALLBACKS is defined, it is
334  * mandatory to call this function prior to using any other library
335  * facility.
336  *
337  * @warning This function is NOT thread-safe.
338  *
339  * @warning the error callback must never return: see @ref pfn_error
340  * for more details */
341 RYML_EXPORT void set_callbacks(Callbacks const& c);
342 
343 /** get the global callbacks
344  * @warning This function is not thread-safe. */
345 RYML_EXPORT Callbacks const& get_callbacks();
346 
347 /** set the global callbacks back to their defaults ()
348  * @warning This function is not thread-safe. */
350 
351 
352 /** the type of the function used to report errors
353  *
354  * @warning When given by the user, this function MUST interrupt
355  * execution, typically by either throwing an exception, or using
356  * `std::longjmp()` ([see
357  * documentation](https://en.cppreference.com/w/cpp/utility/program/setjmp))
358  * or by calling `std::abort()`. If the function returned, the parser
359  * would enter into an infinite loop, or the program may crash. */
360 using pfn_error = void (*) (const char* msg, size_t msg_len, Location location, void *user_data);
361 
362 
363 /** the type of the function used to allocate memory; ryml will only
364  * allocate memory through this callback. */
365 using pfn_allocate = void* (*)(size_t len, void* hint, void *user_data);
366 
367 
368 /** the type of the function used to free memory; ryml will only free
369  * memory through this callback. */
370 using pfn_free = void (*)(void* mem, size_t size, void *user_data);
371 
372 
373 /** a c-style callbacks class. Can be used globally by the library
374  * and/or locally by @ref Tree and @ref Parser objects. */
376 {
377  void * m_user_data;
381 
382  /** Construct an object with the default callbacks. If
383  * @ref RYML_NO_DEFAULT_CALLBACKS is defined, the object will have null
384  * members.*/
385  Callbacks() noexcept;
386 
387  /** Construct an object with the given callbacks.
388  *
389  * @param user_data Data to be forwarded in every call to a callback.
390  *
391  * @param alloc A pointer to an allocate function. Unless
392  * @ref RYML_NO_DEFAULT_CALLBACKS is defined, when this
393  * parameter is null, will fall back to ryml's default
394  * alloc implementation.
395  *
396  * @param free A pointer to a free function. Unless
397  * @ref RYML_NO_DEFAULT_CALLBACKS is defined, when this
398  * parameter is null, will fall back to ryml's default free
399  * implementation.
400  *
401  * @param error A pointer to an error function, which must never
402  * return (see @ref pfn_error). Unless
403  * @ref RYML_NO_DEFAULT_CALLBACKS is defined, when this
404  * parameter is null, will fall back to ryml's default
405  * error implementation.
406  */
407  Callbacks(void *user_data, pfn_allocate alloc, pfn_free free, pfn_error error);
408 
409  bool operator!= (Callbacks const& that) const { return !operator==(that); }
410  bool operator== (Callbacks const& that) const
411  {
412  return (m_user_data == that.m_user_data &&
413  m_allocate == that.m_allocate &&
414  m_free == that.m_free &&
415  m_error == that.m_error);
416  }
417 };
418 
419 
420 /** @} */
421 
422 
423 //-----------------------------------------------------------------------------
424 //-----------------------------------------------------------------------------
425 //-----------------------------------------------------------------------------
426 
427 typedef enum {
434 } Encoding_e;
435 
436 
437 //-----------------------------------------------------------------------------
438 //-----------------------------------------------------------------------------
439 //-----------------------------------------------------------------------------
440 
441 /// @cond dev
442 
443 // BEWARE! MSVC requires that [[noreturn]] appears before RYML_EXPORT
444 [[noreturn]] RYML_EXPORT void error(Callbacks const& cb, const char *msg, size_t msg_len, Location loc);
445 [[noreturn]] RYML_EXPORT void error(const char *msg, size_t msg_len, Location loc);
446 
447 [[noreturn]] inline void error(const char *msg, size_t msg_len)
448 {
449  error(msg, msg_len, Location{});
450 }
451 template<size_t N>
452 [[noreturn]] inline void error(const char (&msg)[N], Location loc)
453 {
454  error(msg, N-1, loc);
455 }
456 template<size_t N>
457 [[noreturn]] inline void error(const char (&msg)[N])
458 {
459  error(msg, N-1, Location{});
460 }
461 
462 #define _RYML_CB_ERR(cb, msg_literal) \
463  _RYML_CB_ERR_(cb, msg_literal, c4::yml::Location(__FILE__, 0, __LINE__, 0))
464 #define _RYML_CB_CHECK(cb, cond) \
465  _RYML_CB_CHECK_(cb, cond, c4::yml::Location(__FILE__, 0, __LINE__, 0))
466 #define _RYML_CB_ERR_(cb, msg_literal, loc) \
467 do \
468 { \
469  const char msg[] = msg_literal; \
470  RYML_DEBUG_BREAK(); \
471  c4::yml::error((cb), msg, sizeof(msg)-1, loc); \
472  C4_UNREACHABLE_AFTER_ERR(); \
473 } while(0)
474 #define _RYML_CB_CHECK_(cb, cond, loc) \
475  do \
476  { \
477  if(C4_UNLIKELY(!(cond))) \
478  { \
479  const char msg[] = "check failed: " #cond; \
480  RYML_DEBUG_BREAK(); \
481  c4::yml::error((cb), msg, sizeof(msg)-1, loc); \
482  C4_UNREACHABLE_AFTER_ERR(); \
483  } \
484  } while(0)
485 #define _RYML_CB_ALLOC_HINT(cb, T, num, hint) (T*) (cb).m_allocate((num) * sizeof(T), (hint), (cb).m_user_data)
486 #define _RYML_CB_ALLOC(cb, T, num) _RYML_CB_ALLOC_HINT((cb), T, (num), nullptr)
487 #define _RYML_CB_FREE(cb, buf, T, num) \
488  do { \
489  (cb).m_free((buf), (num) * sizeof(T), (cb).m_user_data); \
490  (buf) = nullptr; \
491  } while(0)
492 
493 
494 //-----------------------------------------------------------------------------
495 //-----------------------------------------------------------------------------
496 //-----------------------------------------------------------------------------
497 
498 typedef enum {
499  BLOCK_LITERAL, //!< keep newlines (|)
500  BLOCK_FOLD //!< replace newline with single space (>)
501 } BlockStyle_e;
502 
503 typedef enum {
504  CHOMP_CLIP, //!< single newline at end (default)
505  CHOMP_STRIP, //!< no newline at end (-)
506  CHOMP_KEEP //!< all newlines from end (+)
507 } BlockChomp_e;
508 
509 
510 /** Abstracts the fact that a scalar filter result may not fit in the
511  * intended memory. */
512 struct FilterResult
513 {
514  C4_ALWAYS_INLINE bool valid() const noexcept { return str.str != nullptr; }
515  C4_ALWAYS_INLINE size_t required_len() const noexcept { return str.len; }
516  C4_ALWAYS_INLINE csubstr get() const { RYML_ASSERT(valid()); return str; }
517  csubstr str;
518 };
519 /** Abstracts the fact that a scalar filter result may not fit in the
520  * intended memory. */
521 struct FilterResultExtending
522 {
523  C4_ALWAYS_INLINE bool valid() const noexcept { return str.str != nullptr; }
524  C4_ALWAYS_INLINE size_t required_len() const noexcept { return reqlen; }
525  C4_ALWAYS_INLINE csubstr get() const { RYML_ASSERT(valid()); return str; }
526  csubstr str;
527  size_t reqlen;
528 };
529 
530 
531 //-----------------------------------------------------------------------------
532 //-----------------------------------------------------------------------------
533 //-----------------------------------------------------------------------------
534 
535 
536 namespace detail {
537 // is there a better way to do this?
538 template<int8_t signedval, uint8_t unsignedval>
539 struct _charconstant_t
540  : public std::conditional<std::is_signed<char>::value,
541  std::integral_constant<int8_t, static_cast<int8_t>(unsignedval)>,
542  std::integral_constant<uint8_t, unsignedval>>::type
543 {};
544 #define _RYML_CHCONST(signedval, unsignedval) ::c4::yml::detail::_charconstant_t<INT8_C(signedval), UINT8_C(unsignedval)>::value
545 } // namespace detail
546 
547 
548 namespace detail {
549 struct _SubstrWriter
550 {
551  substr buf;
552  size_t pos;
553  _SubstrWriter(substr buf_, size_t pos_=0) : buf(buf_), pos(pos_) { C4_ASSERT(buf.str); }
554  void append(csubstr s)
555  {
556  C4_ASSERT(!s.overlaps(buf));
557  C4_ASSERT(s.str || !s.len);
558  if(s.len && pos + s.len <= buf.len)
559  {
560  C4_ASSERT(s.str);
561  memcpy(buf.str + pos, s.str, s.len);
562  }
563  pos += s.len;
564  }
565  void append(char c)
566  {
567  C4_ASSERT(buf.str);
568  if(pos < buf.len)
569  buf.str[pos] = c;
570  ++pos;
571  }
572  void append_n(char c, size_t numtimes)
573  {
574  C4_ASSERT(buf.str);
575  if(numtimes && pos + numtimes < buf.len)
576  memset(buf.str + pos, c, numtimes);
577  pos += numtimes;
578  }
579  size_t slack() const { return pos <= buf.len ? buf.len - pos : 0; }
580  size_t excess() const { return pos > buf.len ? pos - buf.len : 0; }
581  //! get the part written so far
582  csubstr curr() const { return pos <= buf.len ? buf.first(pos) : buf; }
583  //! get the part that is still free to write to (the remainder)
584  substr rem() const { return pos < buf.len ? buf.sub(pos) : buf.last(0); }
585 
586  size_t advance(size_t more) { pos += more; return pos; }
587 };
588 } // namespace detail
589 
590 
591 namespace detail {
592 // dumpfn is a function abstracting prints to terminal (or to string).
593 template<class DumpFn, class ...Args>
594 C4_NO_INLINE void _dump(DumpFn &&dumpfn, csubstr fmt, Args&& ...args)
595 {
596  DumpResults results;
597  // try writing everything:
598  {
599  // buffer for converting individual arguments. it is defined
600  // in a child scope to free it in case the buffer is too small
601  // for any of the arguments.
602  char writebuf[RYML_LOGBUF_SIZE];
603  results = format_dump_resume(std::forward<DumpFn>(dumpfn), writebuf, fmt, std::forward<Args>(args)...);
604  }
605  // if any of the arguments failed to fit the buffer, allocate a
606  // larger buffer (up to a limit) and resume writing.
607  //
608  // results.bufsize is set to the size of the largest element
609  // serialized. Eg int(1) will require 1 byte.
610  if(C4_UNLIKELY(results.bufsize > RYML_LOGBUF_SIZE))
611  {
612  const size_t bufsize = results.bufsize <= RYML_LOGBUF_SIZE_MAX ? results.bufsize : RYML_LOGBUF_SIZE_MAX;
613  #ifdef C4_MSVC
614  substr largerbuf = {static_cast<char*>(_alloca(bufsize)), bufsize};
615  #else
616  substr largerbuf = {static_cast<char*>(alloca(bufsize)), bufsize};
617  #endif
618  results = format_dump_resume(std::forward<DumpFn>(dumpfn), results, largerbuf, fmt, std::forward<Args>(args)...);
619  }
620 }
621 template<class ...Args>
622 C4_NORETURN C4_NO_INLINE void _report_err(Callbacks const& C4_RESTRICT callbacks, csubstr fmt, Args const& C4_RESTRICT ...args)
623 {
624  char errmsg[RYML_ERRMSG_SIZE] = {0};
625  detail::_SubstrWriter writer(errmsg);
626  auto dumpfn = [&writer](csubstr s){ writer.append(s); };
627  _dump(dumpfn, fmt, args...);
628  writer.append('\n');
629  const size_t len = writer.pos < RYML_ERRMSG_SIZE ? writer.pos : RYML_ERRMSG_SIZE;
630  callbacks.m_error(errmsg, len, {}, callbacks.m_user_data);
631  C4_UNREACHABLE_AFTER_ERR();
632 }
633 } // namespace detail
634 
635 
636 inline csubstr _c4prc(const char &C4_RESTRICT c) // pass by reference!
637 {
638  switch(c)
639  {
640  case '\n': return csubstr("\\n");
641  case '\t': return csubstr("\\t");
642  case '\0': return csubstr("\\0");
643  case '\r': return csubstr("\\r");
644  case '\f': return csubstr("\\f");
645  case '\b': return csubstr("\\b");
646  case '\v': return csubstr("\\v");
647  case '\a': return csubstr("\\a");
648  default: return csubstr(&c, 1);
649  }
650 }
651 
652 /// @endcond
653 
654 C4_SUPPRESS_WARNING_GCC_POP
655 
656 } // namespace yml
657 } // namespace c4
658 
659 #endif /* _C4_YML_COMMON_HPP_ */
Lightweight generic type-safe wrappers for converting individual values to/from strings.
#define RYML_ID_TYPE
The type of a node id in the YAML tree.
Definition: common.hpp:246
#define RYML_ERRMSG_SIZE
size for the error message buffer
Definition: common.hpp:24
#define RYML_LOGBUF_SIZE
size for the buffer used to format individual values to string while preparing an error message....
Definition: common.hpp:33
#define RYML_LOGBUF_SIZE_MAX
size for the fallback larger log buffer. When RYML_LOGBUF_SIZE is not large enough to convert a value...
Definition: common.hpp:43
#define RYML_EXPORT
Definition: export.hpp:15
void reset_callbacks()
set the global callbacks back to their defaults ()
Definition: common.cpp:123
void(*)(const char *msg, size_t msg_len, Location location, void *user_data) pfn_error
the type of the function used to report errors
Definition: common.hpp:360
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:113
void *(*)(size_t len, void *hint, void *user_data) pfn_allocate
the type of the function used to allocate memory; ryml will only allocate memory through this callbac...
Definition: common.hpp:365
Callbacks const & get_callbacks()
get the global callbacks
Definition: common.cpp:118
void(*)(void *mem, size_t size, void *user_data) pfn_free
the type of the function used to free memory; ryml will only free memory through this callback.
Definition: common.hpp:370
csubstr to_csubstr(substr s) noexcept
neutral version for use in generic code
Definition: substr.hpp:2186
bool operator==(const char(&s)[N], basic_substring< C > const that) noexcept
Definition: substr.hpp:2223
RYML_ID_TYPE id_type
The type of a node id in the YAML tree; to override the default type, define the macro RYML_ID_TYPE t...
Definition: common.hpp:253
@ npos
a null string position
Definition: common.hpp:267
void error(Callbacks const &cb, const char *msg, size_t msg_len, Location loc)
Definition: common.cpp:130
Encoding_e
Definition: common.hpp:427
@ UTF16BE
Definition: common.hpp:431
@ UTF8
Definition: common.hpp:429
@ UTF16LE
Definition: common.hpp:430
@ NOBOM
Definition: common.hpp:428
@ UTF32BE
Definition: common.hpp:433
@ UTF32LE
Definition: common.hpp:432
@ NONE
an index to none
Definition: common.hpp:260
Definition: common.cpp:12
a c-style callbacks class.
Definition: common.hpp:376
void * m_user_data
Definition: common.hpp:377
pfn_allocate m_allocate
Definition: common.hpp:378
pfn_free m_free
Definition: common.hpp:379
pfn_error m_error
Definition: common.hpp:380
holds a position into a source buffer
Definition: common.hpp:277
size_t line
line
Definition: common.hpp:281
LineCol(size_t o, size_t l, size_t c)
construct from offset, line and column
Definition: common.hpp:289
size_t offset
number of bytes from the beginning of the source buffer
Definition: common.hpp:279
LineCol(size_t l, size_t c)
construct from line and column
Definition: common.hpp:287
LineCol()=default
size_t col
column
Definition: common.hpp:283
a source file position
Definition: common.hpp:297
Location(csubstr n, size_t b, size_t l, size_t c)
Definition: common.hpp:314
size_t col
column
Definition: common.hpp:303
Location()=default
size_t line
line
Definition: common.hpp:301
Location(const char *n, size_t l, size_t c)
Definition: common.hpp:315
size_t offset
number of bytes from the beginning of the source buffer
Definition: common.hpp:299
Location(size_t l, size_t c)
Definition: common.hpp:311
Location(csubstr n, size_t l, size_t c)
Definition: common.hpp:313
csubstr name
file name
Definition: common.hpp:305
Location(const char *n, size_t b, size_t l, size_t c)
Definition: common.hpp:316
Location(size_t b, size_t l, size_t c)
Definition: common.hpp:312
read+write string views