Compare commits

...

2 Commits

3 changed files with 35 additions and 4 deletions

View File

@ -9,8 +9,8 @@ target_sources(nf7_iface
common/exception.cc common/exception.cc
version.cc version.cc
PUBLIC PUBLIC
common/dealer.hh
common/container.hh common/container.hh
common/dealer.hh
common/exception.hh common/exception.hh
common/future.hh common/future.hh
common/leak_detector.hh common/leak_detector.hh
@ -32,8 +32,9 @@ target_sources(nf7_iface
add_executable(nf7_iface_test) add_executable(nf7_iface_test)
target_sources(nf7_iface_test target_sources(nf7_iface_test
PRIVATE PRIVATE
common/dealer_test.cc
common/container_test.cc common/container_test.cc
common/dealer_test.cc
common/exception_test.cc
common/future_test.cc common/future_test.cc
common/leak_detector_test.cc common/leak_detector_test.cc
common/observer_test.cc common/observer_test.cc

View File

@ -4,6 +4,8 @@
#include <exception> #include <exception>
#include <ostream> #include <ostream>
#include <source_location> #include <source_location>
#include <string>
#include <variant>
namespace nf7 { namespace nf7 {
@ -14,6 +16,10 @@ class Exception : public std::exception, std::nested_exception {
const char* what, const char* what,
std::source_location location = std::source_location::current()) : std::source_location location = std::source_location::current()) :
what_(what), location_(location) { } 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 { void RethrowNestedIf() const {
if (auto ptr = nested_ptr()) { 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_; } const std::source_location& location() const noexcept { return location_; }
private: private:
const char* what_; std::variant<const char*, std::string> what_;
std::source_location location_; std::source_location location_;
}; };

View 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);
}