From 01d30637b393b2f8a932b1d943076db580b1e951 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 17 Sep 2019 15:42:19 +0300 Subject: [PATCH] Use RAII to ensure consistent PartCache state end_rebuild needs to be called even if an exception is thrown during rebuilding. --- source/partcache.h | 10 ++++++++++ source/widget.cpp | 3 +-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/source/partcache.h b/source/partcache.h index 9699124..4eef116 100644 --- a/source/partcache.h +++ b/source/partcache.h @@ -22,6 +22,16 @@ struct CachedPart class PartCache { public: + class Rebuild + { + private: + PartCache &cache; + + public: + Rebuild(PartCache &c): cache(c) { cache.begin_rebuild(); } + ~Rebuild() { cache.end_rebuild(); } + }; + typedef std::list PartList; private: diff --git a/source/widget.cpp b/source/widget.cpp index b40aa75..246c8c1 100644 --- a/source/widget.cpp +++ b/source/widget.cpp @@ -207,7 +207,7 @@ void Widget::rebuild() if(!style) return; - part_cache.begin_rebuild(); + PartCache::Rebuild rebuild_cache(part_cache); const Style::PartSeq &parts = style->get_parts(); for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i) { @@ -216,7 +216,6 @@ void Widget::rebuild() else rebuild_special(*i); } - part_cache.end_rebuild(); } void Widget::rebuild_special(const Part &part) -- 2.43.0