Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
options_error.h
1#ifndef SOURCEMETA_CORE_OPTIONS_ERROR_H_
2#define SOURCEMETA_CORE_OPTIONS_ERROR_H_
3
4#ifndef SOURCEMETA_CORE_OPTIONS_EXPORT
5#include <sourcemeta/core/options_export.h>
6#endif
7
8#include <exception> // std::exception
9#include <string> // std::string
10#include <string_view> // std::string_view
11
12namespace sourcemeta::core {
13
14// Exporting symbols that depends on the standard C++ library is considered
15// safe.
16// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN
17#if defined(_MSC_VER)
18#pragma warning(disable : 4251 4275)
19#endif
20
23class SOURCEMETA_CORE_OPTIONS_EXPORT OptionsError : public std::exception {
24public:
26 explicit OptionsError(const char *message) : message_{message} {}
27 explicit OptionsError(std::string message) = delete;
28 explicit OptionsError(std::string &&message) = delete;
29 explicit OptionsError(std::string_view message) = delete;
30
31 [[nodiscard]] auto what() const noexcept -> const char * override {
32 return this->message_;
33 }
34
35private:
36 const char *message_;
37};
38
41struct SOURCEMETA_CORE_OPTIONS_EXPORT OptionsUnknownOptionError
42 : public OptionsError {
44 explicit OptionsUnknownOptionError(const std::string_view option)
45 : OptionsError{"Unknown option"}, option_{option} {}
46
47 [[nodiscard]] auto option() const noexcept -> std::string_view {
48 return this->option_;
49 }
50
51private:
52 std::string option_;
53};
54
57struct SOURCEMETA_CORE_OPTIONS_EXPORT OptionsUnexpectedValueFlagError
58 : public OptionsError {
60 explicit OptionsUnexpectedValueFlagError(const std::string_view option)
61 : OptionsError{"This flag cannot take a value"}, option_{option} {}
62
63 [[nodiscard]] auto option() const noexcept -> std::string_view {
64 return this->option_;
65 }
66
67private:
68 std::string option_;
69};
70
73struct SOURCEMETA_CORE_OPTIONS_EXPORT OptionsMissingOptionValueError
74 : public OptionsError {
76 explicit OptionsMissingOptionValueError(const std::string_view option)
77 : OptionsError{"This option must take a value"}, option_{option} {}
78
79 [[nodiscard]] auto option() const noexcept -> std::string_view {
80 return this->option_;
81 }
82
83private:
84 std::string option_;
85};
86
87#if defined(_MSC_VER)
88#pragma warning(default : 4251 4275)
89#endif
90
91} // namespace sourcemeta::core
92
93#endif
auto option() const noexcept -> std::string_view
The offending option.
Definition options_error.h:63
OptionsUnknownOptionError(const std::string_view option)
Construct the error given the offending option.
Definition options_error.h:44
auto option() const noexcept -> std::string_view
The offending option.
Definition options_error.h:79
OptionsError(const char *message)
Construct the error given a message.
Definition options_error.h:26
OptionsMissingOptionValueError(const std::string_view option)
Construct the error given the offending option.
Definition options_error.h:76
OptionsUnexpectedValueFlagError(const std::string_view option)
Construct the error given the offending option.
Definition options_error.h:60
auto option() const noexcept -> std::string_view
The offending option.
Definition options_error.h:47
Definition options_error.h:23