diff options
-rw-r--r-- | include/cru/platform/exception.hpp | 11 | ||||
-rw-r--r-- | include/cru/win/graph/direct/geometry.hpp | 7 | ||||
-rw-r--r-- | src/win/graph/direct/geometry.cpp | 16 |
3 files changed, 21 insertions, 13 deletions
diff --git a/include/cru/platform/exception.hpp b/include/cru/platform/exception.hpp index 6dae08c5..b1e505e0 100644 --- a/include/cru/platform/exception.hpp +++ b/include/cru/platform/exception.hpp @@ -6,6 +6,15 @@ namespace cru::platform { class PlatformException : public std::runtime_error { public: - using runtime_error::runtime_error; // inherent constructors + using runtime_error::runtime_error; // inherit constructors +}; + +// This exception is throwed when a resource has been disposed and not usable +// again. +// For example, calling Build twice on a GeometryBuild will lead to this +// exception. +class ReuseException : public std::runtime_error { + public: + using runtime_error::runtime_error; // inherit constructors }; } // namespace cru::platform diff --git a/include/cru/win/graph/direct/geometry.hpp b/include/cru/win/graph/direct/geometry.hpp index f40db7af..1ee60247 100644 --- a/include/cru/win/graph/direct/geometry.hpp +++ b/include/cru/win/graph/direct/geometry.hpp @@ -3,6 +3,7 @@ #include "direct_factory.hpp" #include "platform_id.hpp" +#include "cru/platform/exception.hpp" #include "cru/platform/graph/geometry.hpp" namespace cru::platform::graph::win::direct { @@ -16,7 +17,7 @@ class D2DGeometryBuilder : public GeometryBuilder { D2DGeometryBuilder(D2DGeometryBuilder&& other) = delete; D2DGeometryBuilder& operator=(D2DGeometryBuilder&& other) = delete; - ~D2DGeometryBuilder() override; + ~D2DGeometryBuilder() override = default; CRU_PLATFORMID_IMPLEMENT_DIRECT @@ -31,6 +32,10 @@ class D2DGeometryBuilder : public GeometryBuilder { private: bool IsValid() { return geometry_ != nullptr; } + void CheckValidation() { + if (!IsValid()) + throw ReuseException("The geometry builder is already disposed."); + } private: Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry_; diff --git a/src/win/graph/direct/geometry.cpp b/src/win/graph/direct/geometry.cpp index 6b827906..470addee 100644 --- a/src/win/graph/direct/geometry.cpp +++ b/src/win/graph/direct/geometry.cpp @@ -12,37 +12,31 @@ D2DGeometryBuilder::D2DGeometryBuilder(IDirectFactory* factory) { ThrowIfFailed(geometry_->Open(&geometry_sink_)); } -D2DGeometryBuilder::~D2DGeometryBuilder() { - if (geometry_sink_) { - ThrowIfFailed(geometry_sink_->Close()); - } -} - void D2DGeometryBuilder::BeginFigure(const Point& point) { - assert(IsValid()); + CheckValidation(); geometry_sink_->BeginFigure(Convert(point), D2D1_FIGURE_BEGIN_FILLED); } void D2DGeometryBuilder::LineTo(const Point& point) { - assert(IsValid()); + CheckValidation(); geometry_sink_->AddLine(Convert(point)); } void D2DGeometryBuilder::QuadraticBezierTo(const Point& control_point, const Point& end_point) { - assert(IsValid()); + CheckValidation(); geometry_sink_->AddQuadraticBezier( D2D1::QuadraticBezierSegment(Convert(control_point), Convert(end_point))); } void D2DGeometryBuilder::CloseFigure(bool close) { - assert(IsValid()); + CheckValidation(); geometry_sink_->EndFigure(close ? D2D1_FIGURE_END_CLOSED : D2D1_FIGURE_END_OPEN); } Geometry* D2DGeometryBuilder::Build() { - assert(IsValid()); + CheckValidation(); ThrowIfFailed(geometry_sink_->Close()); geometry_sink_ = nullptr; const auto geometry = new D2DGeometry(std::move(geometry_)); |