From: Mikko Rasa Date: Tue, 17 Sep 2019 12:42:19 +0000 (+0300) Subject: Use RAII to ensure consistent PartCache state X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=01d30637b393b2f8a932b1d943076db580b1e951;p=libs%2Fgltk.git Use RAII to ensure consistent PartCache state end_rebuild needs to be called even if an exception is thrown during rebuilding. --- 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)