5 #ifndef RYML_NO_DEFAULT_CALLBACKS
9 #ifdef _RYML_EXCEPTIONS
17 C4_SUPPRESS_WARNING_GCC_CLANG_WITH_PUSH(
"-Wold-style-cast")
18 C4_SUPPRESS_WARNING_MSVC_WITH_PUSH(4702)
23 #ifndef RYML_NO_DEFAULT_CALLBACKS
25 C4_NO_INLINE
void dump2stderr(csubstr s)
30 fwrite(s.str, 1, s.len, stderr);
32 C4_NO_INLINE
void endmsg()
38 [[noreturn]] C4_NO_INLINE
void error_basic_impl(csubstr msg,
ErrorDataBasic const& errdata,
void * )
42 #ifdef _RYML_WITH_EXCEPTIONS
49 [[noreturn]] C4_NO_INLINE
void error_parse_impl(csubstr msg,
ErrorDataParse const& errdata,
void * )
53 #ifdef _RYML_WITH_EXCEPTIONS
60 [[noreturn]] C4_NO_INLINE
void error_visit_impl(csubstr msg,
ErrorDataVisit const& errdata,
void * )
64 #ifdef _RYML_WITH_EXCEPTIONS
71 void* allocate_impl(
size_t length,
void * ,
void * )
73 void *mem = ::malloc(length);
75 error_basic_impl(
"could not allocate memory",
ErrorDataBasic{RYML_LOC_HERE()},
nullptr);
79 void free_impl(
void *mem,
size_t ,
void * )
91 s_default_callbacks = c;
96 return s_default_callbacks;
107 m_user_data(
nullptr),
108 #ifndef RYML_NO_DEFAULT_CALLBACKS
109 m_allocate(allocate_impl),
111 m_error_basic(error_basic_impl),
112 m_error_parse(error_parse_impl),
113 m_error_visit(error_visit_impl)
117 m_error_basic(
nullptr),
118 m_error_parse(
nullptr),
119 m_error_visit(
nullptr)
126 m_user_data(user_data),
128 m_allocate(alloc_ ? alloc_ : allocate_impl),
129 m_free(free_ ? free_ : free_impl),
130 m_error_basic(error_basic_ ? error_basic_ : error_basic_impl),
131 m_error_parse(error_parse_impl),
132 m_error_visit(error_visit_impl)
136 m_error_basic(error_),
137 m_error_parse(nullptr),
138 m_error_visit(nullptr),
153 #ifndef RYML_NO_DEFAULT_CALLBACKS
162 #ifndef RYML_NO_DEFAULT_CALLBACKS
171 #ifndef RYML_NO_DEFAULT_CALLBACKS
180 #ifndef RYML_NO_DEFAULT_CALLBACKS
189 #ifndef RYML_NO_DEFAULT_CALLBACKS
199 C4_UNREACHABLE_AFTER_ERR();
206 C4_UNREACHABLE_AFTER_ERR();
213 C4_UNREACHABLE_AFTER_ERR();
224 C4_UNREACHABLE_AFTER_ERR();
231 C4_UNREACHABLE_AFTER_ERR();
242 C4_UNREACHABLE_AFTER_ERR();
247 #ifdef _RYML_WITH_EXCEPTIONS
249 : errdata_basic(errdata_)
255 if(msg_.len >=
sizeof(msg))
257 static_assert(
sizeof(msg) > 6u,
"message buffer too small");
258 msg_.len =
sizeof(msg) - 6u;
259 msg[msg_.len ] =
'[';
260 msg[msg_.len + 1u] =
'.';
261 msg[msg_.len + 2u] =
'.';
262 msg[msg_.len + 3u] =
'.';
263 msg[msg_.len + 4u] =
']';
264 msg[msg_.len + 5u] =
'\0';
266 memcpy(msg, msg_.str, msg_.len);
270 : ExceptionBasic(msg_, {errdata_.ymlloc})
271 , errdata_parse(errdata_)
275 : ExceptionBasic(msg_, {errdata_.cpploc})
276 , errdata_visit(errdata_)
287 size_t before = text.first(pos).last_of(
'\n');
288 size_t before_count = 0;
289 while((before !=
npos) && (++before_count <= num_lines_before))
293 before = text.first(--before).last_of(
'\n');
295 if(before < text.len || before ==
npos)
297 size_t after = text.first_of(
'\n', pos);
298 size_t after_count = 0;
299 while((after !=
npos) && (++after_count <= num_lines_after))
302 if(after >= text.len)
304 after = text.first_of(
'\n', after);
306 return before <= after ? text.range(before, after) : text.first(0);
310 C4_SUPPRESS_WARNING_MSVC_POP
311 C4_SUPPRESS_WARNING_GCC_CLANG_POP
Common utilities and infrastructure used by ryml.
#define RYML_NO_DEFAULT_CALLBACKS
(Undefined by default) Define this macro to disable ryml's default implementation of the callback fun...
Definitions of error utilities used by ryml.
Error utilities used by ryml.
void reset_callbacks()
set the global callbacks back to their defaults.
void set_callbacks(Callbacks const &c)
set the global callbacks for the library; after a call to this function, these callbacks will be used...
void(*)(csubstr msg, ErrorDataVisit const &errdata, void *user_data) pfn_error_visit
the type of the function used to report visit errors.
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...
void(*)(csubstr msg, ErrorDataParse const &errdata, void *user_data) pfn_error_parse
the type of the function used to report parse errors.
Callbacks const & get_callbacks()
get the global callbacks
void(*)(csubstr msg, ErrorDataBasic const &errdata, void *user_data) pfn_error_basic
the type of the function used to report basic errors.
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.
void err_visit(ErrorDataVisit const &errdata, const char *msg)
trigger a visit error to its respective handler, with a non-formatted error message.
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(ErrorDataBasic const &errdata, const char *msg)
trigger a basic error to its respective handler, with a non-formatted 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.
void err_parse(ErrorDataParse const &errdata, const char *msg)
trigger a parse error to its respective handler, with a non-formatted error message.
csubstr to_csubstr(substr s) noexcept
neutral version for use in generic code
csubstr _get_text_region(csubstr text, size_t pos, size_t num_lines_before, size_t num_lines_after)
@ npos
a null string position
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
A c-style callbacks class to customize behavior on errors or allocation.
pfn_error_basic m_error_basic
a pointer to a basic error handler function
pfn_error_parse m_error_parse
a pointer to a parse error handler function
Callbacks & set_error_visit(pfn_error_visit error_visit=nullptr)
Set or reset the error_visit callback.
Callbacks & set_free(pfn_free free=nullptr)
Set or reset the free callback.
void * m_user_data
data to be forwarded in every call to a callback
pfn_allocate m_allocate
a pointer to an allocate handler function
Callbacks() noexcept
Construct an object with the default callbacks.
Callbacks & set_error_parse(pfn_error_parse error_parse=nullptr)
Set or reset the error_parse callback.
Callbacks & set_allocate(pfn_allocate allocate=nullptr)
Set or reset the allocate callback.
Callbacks & set_error_basic(pfn_error_basic error_basic=nullptr)
Set or reset the error_basic callback.
pfn_error_visit m_error_visit
a pointer to a visit error handler function
pfn_free m_free
a pointer to a free handler function
Callbacks & set_user_data(void *user_data)
Set the user data.
Location ymlloc
location in the YAML source buffer where the error was detected.
Location cpploc
location in the C++ source file where the error was detected.
Exception thrown by the default basic error implementation.
ExceptionBasic(csubstr msg, ErrorDataBasic const &errdata_) noexcept
Exception thrown by the default parse error implementation.
ExceptionParse(csubstr msg, ErrorDataParse const &errdata_) noexcept
Exception thrown by the default visit error implementation.
ExceptionVisit(csubstr msg, ErrorDataVisit const &errdata_) noexcept