Opts: Solve OptionsParser::Exception diamond inheritance problem

This commit is contained in:
2026-04-02 01:28:37 -04:00
parent 3f2d7c24ee
commit 06996d166e
2 changed files with 19 additions and 21 deletions
+7 -9
View File
@@ -29,9 +29,13 @@ public:
class Exception : public std::exception class Exception : public std::exception
{ {
public: public:
Exception() = default; explicit Exception(std::string message_);
~Exception() override = default; ~Exception() override = default;
const char* what() const noexcept override = 0;
const char* what() const noexcept override;
protected:
std::string message;
}; };
std::string argv0; std::string argv0;
@@ -45,13 +49,11 @@ public:
}; };
class OptionsParserError class OptionsParserError
: public std::invalid_argument, public OptionParser::Exception : public OptionParser::Exception
{ {
public: public:
OptionsParserError( OptionsParserError(
const std::string& errorMessage, const OptionParser& parser); const std::string& errorMessage, const OptionParser& parser);
const char* what() const noexcept override;
}; };
class JustPrintUsageNoError class JustPrintUsageNoError
@@ -59,10 +61,6 @@ class JustPrintUsageNoError
{ {
public: public:
explicit JustPrintUsageNoError(const OptionParser& parser); explicit JustPrintUsageNoError(const OptionParser& parser);
const char* what() const noexcept override;
private:
std::string message;
}; };
#endif // OPTS_H #endif // OPTS_H
+12 -12
View File
@@ -10,28 +10,28 @@
#include <spinscale/callableTracer.h> #include <spinscale/callableTracer.h>
OptionParser::Exception::Exception(std::string message_)
: message(std::move(message_))
{
}
const char* OptionParser::Exception::what() const noexcept
{
return message.c_str();
}
OptionsParserError::OptionsParserError( OptionsParserError::OptionsParserError(
const std::string& errorMessage, const OptionParser& parser const std::string& errorMessage, const OptionParser& parser
) )
: std::invalid_argument(errorMessage + "\n" + parser.getUsage()) : OptionParser::Exception(errorMessage + "\n" + parser.getUsage())
{ {
} }
const char* OptionsParserError::what() const noexcept
{
return std::invalid_argument::what();
}
JustPrintUsageNoError::JustPrintUsageNoError(const OptionParser& parser) JustPrintUsageNoError::JustPrintUsageNoError(const OptionParser& parser)
: message(parser.getUsage()) : OptionParser::Exception(parser.getUsage())
{ {
} }
const char* JustPrintUsageNoError::what() const noexcept
{
return message.c_str();
}
struct option OptionParser::longOptions[] = { struct option OptionParser::longOptions[] = {
{"dapspec", required_argument, 0, 's'}, {"dapspec", required_argument, 0, 's'},
{"spec", required_argument, 0, 's'}, {"spec", required_argument, 0, 's'},