• Dana Fried's avatar
    Allow setting class properties by value for assignable types. · 490a005b
    Dana Fried authored
    Most of our Views class properties are of type ClassProperty<T*> where
    T is a value type, but we're forced to use pointers because T can be of
    any type and must therefore be allocated on the heap.
    
    This leads to a lot of code like:
    my_view->SetProperty(kMarginsKey, new Insets(kMyViewDefaultInsets));
    ...
    *my_view->GetProperty(kMarginsKey) = new_insets;
    
    (The latter pattern is to prevent a second heap allocation - but only
    works if the initial allocation happens; otherwise it crashes.)
    
    This CL shortcuts this behavior so that it behaves in the way we
    actually want to use the system 90% of the time:
    
    // Allocates a copy of kMyViewDefaultInsets for the property.
    my_view->SetProperty(kMarginsKey, kMyViewDefaultInsets);
    
    // Updates the value of the existing property.
    my_view->SetProperty(kMarginsKey, new_insets);
    
    // De-allocates the existing property value.
    my_view->ClearProperty(kMarginsKey);
    
    In order to use this new functionality:
     - The property must be an owned property of pointer type.
     - The property type behind the pointer must be copy- or
       move-assignable.
    
    Change-Id: Idec1d5b9c104814f234270214b615774fa5a7084
    Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1657288Reviewed-by: default avatarScott Violet <sky@chromium.org>
    Commit-Queue: Dana Fried <dfried@chromium.org>
    Cr-Commit-Position: refs/heads/master@{#670329}
    490a005b
class_property.h 11.3 KB