Compare commits
2 Commits
c2fb269d40
...
a307fe076b
Author | SHA1 | Date | |
---|---|---|---|
a307fe076b | |||
1ec5e9c03e |
@ -9,8 +9,8 @@ target_sources(nf7_iface
|
||||
common/exception.cc
|
||||
version.cc
|
||||
PUBLIC
|
||||
common/dealer.hh
|
||||
common/container.hh
|
||||
common/dealer.hh
|
||||
common/exception.hh
|
||||
common/future.hh
|
||||
common/leak_detector.hh
|
||||
@ -32,8 +32,9 @@ target_sources(nf7_iface
|
||||
add_executable(nf7_iface_test)
|
||||
target_sources(nf7_iface_test
|
||||
PRIVATE
|
||||
common/dealer_test.cc
|
||||
common/container_test.cc
|
||||
common/dealer_test.cc
|
||||
common/exception_test.cc
|
||||
common/future_test.cc
|
||||
common/leak_detector_test.cc
|
||||
common/observer_test.cc
|
||||
|
@ -4,6 +4,8 @@
|
||||
#include <exception>
|
||||
#include <ostream>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
namespace nf7 {
|
||||
|
||||
@ -14,6 +16,10 @@ class Exception : public std::exception, std::nested_exception {
|
||||
const char* what,
|
||||
std::source_location location = std::source_location::current()) :
|
||||
what_(what), location_(location) { }
|
||||
explicit Exception(
|
||||
const std::string& what,
|
||||
std::source_location location = std::source_location::current()) :
|
||||
what_(what), location_(location) { }
|
||||
|
||||
void RethrowNestedIf() const {
|
||||
if (auto ptr = nested_ptr()) {
|
||||
@ -21,11 +27,15 @@ class Exception : public std::exception, std::nested_exception {
|
||||
}
|
||||
}
|
||||
|
||||
const char* what() const noexcept override { return what_; }
|
||||
const char* what() const noexcept override {
|
||||
return std::holds_alternative<const char*>(what_)?
|
||||
std::get<const char*>(what_):
|
||||
std::get<std::string>(what_).c_str();
|
||||
}
|
||||
const std::source_location& location() const noexcept { return location_; }
|
||||
|
||||
private:
|
||||
const char* what_;
|
||||
std::variant<const char*, std::string> what_;
|
||||
std::source_location location_;
|
||||
};
|
||||
|
||||
|
20
iface/common/exception_test.cc
Normal file
20
iface/common/exception_test.cc
Normal file
@ -0,0 +1,20 @@
|
||||
// No copyright
|
||||
#include "iface/common/exception.hh"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
TEST(Exception, WithStaticMessage) {
|
||||
static const char* kMsg = "helloworld";
|
||||
nf7::Exception sut {kMsg};
|
||||
EXPECT_EQ(sut.what(), kMsg);
|
||||
}
|
||||
|
||||
TEST(Exception, WithDynamicMessage) {
|
||||
static const char* kMsg = "helloworld";
|
||||
nf7::Exception sut {std::string {kMsg}};
|
||||
EXPECT_STREQ(sut.what(), kMsg);
|
||||
EXPECT_NE(sut.what(), kMsg);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user