summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Rhinelander <jason@imaginary.ca>2021-06-08 21:27:54 +0200
committerKjell Ahlstedt <kjellahlstedt@gmail.com>2021-06-08 21:27:54 +0200
commit29b9e2f36f17e08e402770bf4eca1d63a8afd0e0 (patch)
tree56c972481a851a296c3311b7b3a126f9d69ec13e
parent61286896d11ed961add217ff4a209d10d9efb700 (diff)
Add Cairo::SaveGuard save/restore object
See #9
-rw-r--r--cairomm/context.cc10
-rw-r--r--cairomm/context.h25
2 files changed, 35 insertions, 0 deletions
diff --git a/cairomm/context.cc b/cairomm/context.cc
index 25673e8..ce1a838 100644
--- a/cairomm/context.cc
+++ b/cairomm/context.cc
@@ -886,6 +886,16 @@ RefPtr<const Surface> Context::get_group_target() const
return get_surface_wrapper(surface);
}
+SaveGuard::SaveGuard(RefPtr<Context> context) : ctx_{context}
+{
+ ctx_->save();
+}
+
+SaveGuard::~SaveGuard()
+{
+ ctx_->restore();
+}
+
} //namespace Cairo
// vim: ts=2 sw=2 et
diff --git a/cairomm/context.h b/cairomm/context.h
index 6bf6bde..1a77c78 100644
--- a/cairomm/context.h
+++ b/cairomm/context.h
@@ -1703,6 +1703,31 @@ 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:
+ *
+ * // context initial state
+ * {
+ * Cairo::SaveGuard saver(context);
+ * ... // manipulate context
+ * }
+ * // context is restored to initial state
+ */
+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_;
+};
+
+
} // namespace Cairo
#endif //__CAIROMM_CONTEXT_H