diff options
author | Kjell Ahlstedt <kjellahlstedt@gmail.com> | 2021-06-09 09:01:23 +0200 |
---|---|---|
committer | Kjell Ahlstedt <kjellahlstedt@gmail.com> | 2021-06-09 09:01:23 +0200 |
commit | b121a42a79281c165d36e56b9f2868f84ec02ac3 (patch) | |
tree | e52d1fcc40bec4afdd3309d794674e90f4da8fef | |
parent | 6d24fa4b9f37d4233754ecbc831a5240d1f5c4b3 (diff) |
Update SaveGuard
and add a test in tests/test-context.cc. Fixes #9
-rw-r--r-- | cairomm/context.cc | 11 | ||||
-rw-r--r-- | cairomm/context.h | 60 | ||||
-rw-r--r-- | tests/test-context.cc | 6 |
3 files changed, 47 insertions, 30 deletions
diff --git a/cairomm/context.cc b/cairomm/context.cc index ce1a838..aa217ba 100644 --- a/cairomm/context.cc +++ b/cairomm/context.cc @@ -886,16 +886,17 @@ RefPtr<const Surface> Context::get_group_target() const return get_surface_wrapper(surface); } -SaveGuard::SaveGuard(RefPtr<Context> context) : ctx_{context} +SaveGuard::SaveGuard(const RefPtr<Context>& context) +: ctx_{context} { - ctx_->save(); + if (ctx_) + ctx_->save(); } SaveGuard::~SaveGuard() { - ctx_->restore(); + if (ctx_) + ctx_->restore(); } } //namespace Cairo - -// vim: ts=2 sw=2 et diff --git a/cairomm/context.h b/cairomm/context.h index 1a77c78..20f1e7b 100644 --- a/cairomm/context.h +++ b/cairomm/context.h @@ -250,14 +250,14 @@ public: * It isn't necessary to clear all saved states before a cairo_t is freed. * Any saved states will be freed when the Context is destroyed. * - * @sa restore() + * @sa restore(), SaveGuard */ void save(); /** Restores cr to the state saved by a preceding call to save() and removes * that state from the stack of saved states. * - * @sa save() + * @sa save(), SaveGuard */ void restore(); @@ -1703,33 +1703,43 @@ protected: cobject* m_cobject; }; - -/** RAII-style context save/restore class. context->save() is called - * automatically when the object is created, and context->restore() is called - * when the object is destroyed. This allows you to write code such as: +/** RAII-style context save/restore class. + * Cairo::Context::save() is called automatically when the object is created, + * and Cairo::Context::restore() is called when the object is destroyed. + * This allows you to write code such as: + * @code + * // context initial state + * { + * Cairo::SaveGuard saver(context); + * ... // manipulate context + * } + * // context is restored to initial state + * @endcode * - * // context initial state - * { - * Cairo::SaveGuard saver(context); - * ... // manipulate context - * } - * // context is restored to initial state + * @newin{1,18} */ -class SaveGuard final { - public: - /// Constructor: the context is saved - explicit SaveGuard(RefPtr<Context> context); - /// Copy constructor deleted - SaveGuard(const SaveGuard &) = delete; - /// Destructor; the context is restored - ~SaveGuard(); - private: - RefPtr<Context> ctx_; -}; +class SaveGuard final +{ +public: + /// Constructor, the context is saved. + CAIROMM_API explicit SaveGuard(const RefPtr<Context>& context); + +#ifndef DOXYGEN_IGNORE_THIS + // noncopyable + SaveGuard(const SaveGuard&) = delete; + SaveGuard& operator=(const SaveGuard&) = delete; + // nonmovable + SaveGuard(SaveGuard&&) = delete; + SaveGuard& operator=(SaveGuard&&) = delete; +#endif //DOXYGEN_IGNORE_THIS + /// Destructor, the context is restored. + CAIROMM_API ~SaveGuard(); + +private: + RefPtr<Context> ctx_; +}; } // namespace Cairo #endif //__CAIROMM_CONTEXT_H - -// vim: ts=2 sw=2 et diff --git a/tests/test-context.cc b/tests/test-context.cc index 830a099..b249489 100644 --- a/tests/test-context.cc +++ b/tests/test-context.cc @@ -70,6 +70,12 @@ BOOST_AUTO_TEST_CASE(test_save_restore) BOOST_CHECK_EQUAL (4.0, cr->get_line_width ()); cr->restore (); BOOST_CHECK_EQUAL (2.3, cr->get_line_width ()); + { + Cairo::SaveGuard guard(cr); + cr->set_line_width(3.0); + BOOST_CHECK_EQUAL(3.0, cr->get_line_width()); + } + BOOST_CHECK_EQUAL(2.3, cr->get_line_width()); } BOOST_AUTO_TEST_CASE(test_operator) |