lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master d39ff1d 07/46: Use wxGridCellAttrPtr instead


From: Greg Chicares
Subject: [lmi-commits] [lmi] master d39ff1d 07/46: Use wxGridCellAttrPtr instead of manual reference counting
Date: Wed, 22 Jul 2020 11:05:09 -0400 (EDT)

branch: master
commit d39ff1d5491c8f31b59df825c80dec5e2ca9069b
Author: Vadim Zeitlin <vadim@tt-solutions.com>
Commit: Vadim Zeitlin <vadim@tt-solutions.com>

    Use wxGridCellAttrPtr instead of manual reference counting
    
    Update CensusViewGridCellAttrProvider::GetAttr() to use smart pointer
    class instead of manual IncRef() and DecRef() calls, this is shorter and
    less error-prone.
    
    Also add a comment explaining why do we need to clone the attribute
    returned by the base class in the first place.
---
 census_view.cpp | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/census_view.cpp b/census_view.cpp
index a6cdf04..fbbd2ed 100644
--- a/census_view.cpp
+++ b/census_view.cpp
@@ -1566,22 +1566,23 @@ class CensusViewGridCellAttrProvider
         ,wxGridCellAttr::wxAttrKind kind
         ) const override
     {
-        wxGridCellAttr* attr = wxGridCellAttrProvider::GetAttr(row, col, kind);
+        wxGridCellAttrPtr attr{wxGridCellAttrProvider::GetAttr(row, col, 
kind)};
 
         if(row % 2)
             {
-            if(attr == nullptr)
+            if(!attr)
                 {
-                attr = attrForOddRows_.get();
-                attr->IncRef();
+                attr = attrForOddRows_;
                 }
             else
                 {
                 if(!attr->HasBackgroundColour())
                     {
-                    wxGridCellAttr* attrNew = attr->Clone();
-                    attr->DecRef();
-                    attr = attrNew;
+                    // Note that we can't modify attr itself, as it can be used
+                    // for other cells and changing its background would change
+                    // their appearance, so allocate a new attribute for this
+                    // cell only.
+                    attr = attr->Clone();
                     attr->SetBackgroundColour
                         (attrForOddRows_->GetBackgroundColour()
                         );
@@ -1589,11 +1590,11 @@ class CensusViewGridCellAttrProvider
                 }
             }
 
-        return attr;
+        return attr.release();
     }
 
   private:
-    wxObjectDataPtr<wxGridCellAttr> attrForOddRows_;
+    wxGridCellAttrPtr attrForOddRows_;
 };
 
 /// Interface to the data for wxGrid.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]