diff options
author | David Robillard <d@drobilla.net> | 2020-10-31 13:00:52 +0100 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-10-31 13:00:52 +0100 |
commit | 71312135e580b247d500070ea699462a6f3c26eb (patch) | |
tree | 6908e225d732e80b9bf2ceca4e2f6bb080b3fc31 /bindings/cxx/include | |
parent | 745bc88be59f1cd88cd1b3353f8dda92766f06a7 (diff) | |
download | pugl-71312135e580b247d500070ea699462a6f3c26eb.tar.gz pugl-71312135e580b247d500070ea699462a6f3c26eb.tar.bz2 pugl-71312135e580b247d500070ea699462a6f3c26eb.zip |
Make use of exceptions optional
Diffstat (limited to 'bindings/cxx/include')
-rw-r--r-- | bindings/cxx/include/pugl/pugl.hpp | 52 | ||||
-rw-r--r-- | bindings/cxx/include/pugl/pugl.ipp | 4 |
2 files changed, 39 insertions, 17 deletions
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 <cstdint> -#include <exception> + +#if defined(PUGL_HPP_THROW_FAILED_CONSTRUCTION) +# include <exception> +#elif defined(PUGL_HPP_ASSERT_CONSTRUCTION) +# include <cassert> +#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<PuglWorld, puglFreeWorld> { @@ -261,18 +281,12 @@ public: explicit World(WorldType type, WorldFlags flags) : Wrapper{puglNewWorld(static_cast<PuglWorldType>(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<View*>(puglGetHandle(view)); + View* self = static_cast<View*>(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&) { |