Line data Source code
1 0 : // Distributed under the MIT License.
2 : // See LICENSE.txt for details.
3 :
4 : #pragma once
5 :
6 : #include <exception>
7 : #include <sstream>
8 : #include <string>
9 : #include <utility>
10 :
11 : #include "Options/Context.hpp"
12 : #include "Utilities/ErrorHandling/Error.hpp"
13 :
14 : namespace Options {
15 : /// \cond
16 : namespace detail {
17 : class propagate_context : public std::exception {
18 : public:
19 : // cppcheck-suppress passedByValue
20 : explicit propagate_context(std::string message)
21 : : message_(std::move(message)) {}
22 :
23 : const char* what() const noexcept(true) override { return message_.c_str(); }
24 : const std::string& message() const { return message_; }
25 :
26 : private:
27 : std::string message_;
28 : };
29 : } // namespace detail
30 : /// \endcond
31 :
32 : /// \ingroup OptionParsingGroup
33 : /// Like ERROR("\n" << (context) << m), but instead throws an
34 : /// exception that will be caught in a higher level Options if not
35 : /// passed a top-level context. This is used to print a parsing
36 : /// "backtrace" since we can't pass any extra data through the
37 : /// yaml-cpp code.
38 : ///
39 : /// \param context Context used to print a parsing traceback
40 : /// \param m error message, as for ERROR
41 1 : #define PARSE_ERROR(context, m) \
42 : do { \
43 : if ((context).top_level) { \
44 : /* clang-tidy: macro arg in parentheses */ \
45 : ERROR_NO_TRACE("\n" << (context) << m); /* NOLINT */ \
46 : } else { \
47 : std::ostringstream avoid_name_collisions_PARSE_ERROR; \
48 : /* clang-tidy: macro arg in parentheses */ \
49 : avoid_name_collisions_PARSE_ERROR << (context) << m; /* NOLINT */ \
50 : throw ::Options::detail::propagate_context( \
51 : avoid_name_collisions_PARSE_ERROR.str()); \
52 : } \
53 : } while (false)
54 : } // namespace Options
|