From 71312135e580b247d500070ea699462a6f3c26eb Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 31 Oct 2020 13:00:52 +0100 Subject: Make use of exceptions optional --- bindings/cxx/include/pugl/pugl.hpp | 52 +++++++++++++++++++++++++------------- bindings/cxx/include/pugl/pugl.ipp | 4 +++ 2 files changed, 39 insertions(+), 17 deletions(-) (limited to 'bindings/cxx') diff --git a/bindings/cxx/include/pugl/pugl.hpp b/bindings/cxx/include/pugl/pugl.hpp index 29b2d11..54c0648 100644 --- a/bindings/cxx/include/pugl/pugl.hpp +++ b/bindings/cxx/include/pugl/pugl.hpp @@ -25,7 +25,12 @@ #include "pugl/pugl.h" #include -#include + +#if defined(PUGL_HPP_THROW_FAILED_CONSTRUCTION) +# include +#elif defined(PUGL_HPP_ASSERT_CONSTRUCTION) +# include +#endif namespace pugl { @@ -234,6 +239,8 @@ static_assert(WorldFlag(PUGL_WORLD_THREADS) == WorldFlag::threads, ""); using WorldFlags = PuglWorldFlags; ///< @copydoc PuglWorldFlags +#if defined(PUGL_HPP_THROW_FAILED_CONSTRUCTION) + /// An exception thrown when construction fails class FailedConstructionError : public std::exception { @@ -248,6 +255,19 @@ private: const char* _msg; }; +# define PUGL_CHECK_CONSTRUCTION(cond, msg) \ + do { \ + if (!(cond)) { \ + throw FailedConstructionError(msg); \ + } \ + } while (0) + +#elif defined(PUGL_HPP_ASSERT_CONSTRUCTION) +# define PUGL_CHECK_CONSTRUCTION(cond, msg) assert(cond); +#else +# define PUGL_CHECK_CONSTRUCTION(cond, msg) +#endif + /// @copydoc PuglWorld class World : public detail::Wrapper { @@ -261,18 +281,12 @@ public: explicit World(WorldType type, WorldFlags flags) : Wrapper{puglNewWorld(static_cast(type), flags)} { - if (!cobj()) { - throw FailedConstructionError("Failed to create pugl::World"); - } + PUGL_CHECK_CONSTRUCTION(cobj(), "Failed to create pugl::World"); } explicit World(WorldType type) : World{type, {}} - { - if (!cobj()) { - throw FailedConstructionError("Failed to create pugl::World"); - } - } + {} /// @copydoc puglGetNativeWorld void* nativeWorld() noexcept { return puglGetNativeWorld(cobj()); } @@ -353,15 +367,15 @@ public: : Wrapper{puglNewView(world.cobj())} , _world(world) { - if (!cobj()) { - throw FailedConstructionError("Failed to create pugl::View"); + if (cobj()) { + puglSetHandle(cobj(), this); + puglSetEventFunc(cobj(), dispatchEvent); } - puglSetHandle(cobj(), this); - puglSetEventFunc(cobj(), dispatchEvent); + PUGL_CHECK_CONSTRUCTION(cobj(), "Failed to create pugl::View"); } - virtual ~View() = default; + virtual ~View() noexcept = default; View(const View&) = delete; View& operator=(const View&) = delete; @@ -587,16 +601,20 @@ private: static PuglStatus dispatchEvent(PuglView* view, const PuglEvent* event) noexcept { - try { - View* self = static_cast(puglGetHandle(view)); + View* self = static_cast(puglGetHandle(view)); +#ifdef __cpp_exceptions + try { return self->dispatch(event); } catch (...) { return PUGL_UNKNOWN_ERROR; } +#else + return self->dispatch(event); +#endif } - PuglStatus dispatch(const PuglEvent* event) + PuglStatus dispatch(const PuglEvent* event) noexcept { switch (event->type) { case PUGL_NOTHING: diff --git a/bindings/cxx/include/pugl/pugl.ipp b/bindings/cxx/include/pugl/pugl.ipp index b90c878..c11f7d0 100644 --- a/bindings/cxx/include/pugl/pugl.ipp +++ b/bindings/cxx/include/pugl/pugl.ipp @@ -25,12 +25,16 @@ namespace pugl { +#ifdef PUGL_HPP_THROW_FAILED_CONSTRUCTION + const char* FailedConstructionError::what() const noexcept { return _msg; } +#endif + Status View::onCreate(const CreateEvent&) { -- cgit v1.2.1