]> git.tdb.fi Git - libs/gltk.git/commitdiff
Use RAII to ensure consistent PartCache state
authorMikko Rasa <tdb@tdb.fi>
Tue, 17 Sep 2019 12:42:19 +0000 (15:42 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 17 Sep 2019 12:42:19 +0000 (15:42 +0300)
end_rebuild needs to be called even if an exception is thrown during
rebuilding.

source/partcache.h
source/widget.cpp

index 969912431271d501b68267f9f09bb7b05f0427b6..4eef1166d1d9e75f0bc39bd27b087350e8cbf3c1 100644 (file)
@@ -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<CachedPart> PartList;
 
 private:
index b40aa75214f2855def232ebbd48a6f85b7b3fb66..246c8c196ced2222237a9f10af93a71fcb114160 100644 (file)
@@ -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)