From 5676ec170aab822f01523fb65a634ca822ee0d03 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 31 Oct 2020 13:00:49 +0100 Subject: Use a custom exception type for failed construction This avoids an include of , which is slow, and is better practice anyway. --- bindings/cxx/include/pugl/pugl.hpp | 22 ++++++++++++++++++---- bindings/cxx/include/pugl/pugl.ipp | 6 ++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/bindings/cxx/include/pugl/pugl.hpp b/bindings/cxx/include/pugl/pugl.hpp index 19cf3fe..ec9ca20 100644 --- a/bindings/cxx/include/pugl/pugl.hpp +++ b/bindings/cxx/include/pugl/pugl.hpp @@ -26,7 +26,7 @@ #include #include -#include +#include #include namespace pugl { @@ -236,6 +236,20 @@ static_assert(WorldFlag(PUGL_WORLD_THREADS) == WorldFlag::threads, ""); using WorldFlags = PuglWorldFlags; ///< @copydoc PuglWorldFlags +/// An exception thrown when construction fails +class FailedConstructionError : public std::exception +{ +public: + FailedConstructionError(const char* const msg) noexcept + : _msg{msg} + {} + + virtual const char* what() const noexcept override; + +private: + const char* _msg; +}; + /// @copydoc PuglWorld class World : public detail::Wrapper { @@ -250,7 +264,7 @@ public: : Wrapper{puglNewWorld(static_cast(type), flags)} { if (!cobj()) { - throw std::runtime_error("Failed to create pugl::World"); + throw FailedConstructionError("Failed to create pugl::World"); } } @@ -258,7 +272,7 @@ public: : World{type, {}} { if (!cobj()) { - throw std::runtime_error("Failed to create pugl::World"); + throw FailedConstructionError("Failed to create pugl::World"); } } @@ -342,7 +356,7 @@ public: , _world(world) { if (!cobj()) { - throw std::runtime_error("Failed to create pugl::View"); + throw FailedConstructionError("Failed to create pugl::View"); } puglSetHandle(cobj(), this); diff --git a/bindings/cxx/include/pugl/pugl.ipp b/bindings/cxx/include/pugl/pugl.ipp index 59fb806..b90c878 100644 --- a/bindings/cxx/include/pugl/pugl.ipp +++ b/bindings/cxx/include/pugl/pugl.ipp @@ -25,6 +25,12 @@ namespace pugl { +const char* +FailedConstructionError::what() const noexcept +{ + return _msg; +} + Status View::onCreate(const CreateEvent&) { -- cgit v1.2.1