Const correctness improvements for Property::Value. 74/244974/4
authorGyörgy Straub <g.straub@partner.samsung.com>
Mon, 28 Sep 2020 12:09:02 +0000 (13:09 +0100)
committerGyörgy Straub <g.straub@partner.samsung.com>
Tue, 29 Sep 2020 08:40:35 +0000 (09:40 +0100)
GetArray() and GetMap() return non-const pointers, which allow const
objects being modified. While DALi itself doesn't seem to abuse this
in client code it may still lead to hard to trace bugs or UB at worst.

It makes sense that the modification of an element of a Property::Map
or Array that a Property::Value holds, means modification of the
Property::Value itself.

- GetArray/Map() const has been changed to return pointer-to-consts;
- a non-const counterpart has been added to each;

Change-Id: I187770bbb3cf355fbf1df95c909bad3ddc8cdb23
Signed-off-by: György Straub <g.straub@partner.samsung.com>
dali/internal/event/common/property-metadata.cpp
dali/internal/event/rendering/shader-impl.cpp
dali/public-api/object/property-value.cpp
dali/public-api/object/property-value.h

index aa4d607..1df9d5e 100644 (file)
@@ -86,7 +86,7 @@ void PropertyMetadata::SetPropertyValue( const Property::Value& propertyValue )
 
     case Property::ARRAY:
     {
-      Property::Array* array = propertyValue.GetArray();
+      const Property::Array* array = propertyValue.GetArray();
       if( array )
       {
         value = *array;
@@ -96,7 +96,7 @@ void PropertyMetadata::SetPropertyValue( const Property::Value& propertyValue )
 
     case Property::MAP:
     {
-      Property::Map* map = propertyValue.GetMap();
+      const Property::Map* map = propertyValue.GetMap();
       if( map )
       {
         value = *map;
index 1a7f11f..3c3dcf9 100644 (file)
@@ -124,7 +124,7 @@ void Shader::SetDefaultProperty( Property::Index index, const Property::Value& p
     {
       if( propertyValue.GetType() == Property::MAP )
       {
-        Dali::Property::Map* map = propertyValue.GetMap();
+        const Dali::Property::Map* map = propertyValue.GetMap();
         if( map )
         {
           std::string vertex;
index 2688757..09bb5c7 100644 (file)
@@ -1001,7 +1001,7 @@ bool Property::Value::Get(Property::Map& mapValue) const
   return converted;
 }
 
-Property::Array* Property::Value::GetArray() const
+Property::Array const* Property::Value::GetArray() const
 {
   if(mImpl && (mImpl->GetType() == ARRAY))
   {
@@ -1010,7 +1010,27 @@ Property::Array* Property::Value::GetArray() const
   return nullptr;
 }
 
-Property::Map* Property::Value::GetMap() const
+Property::Array* Property::Value::GetArray()
+{
+  Property::Array* array = nullptr;
+  if(mImpl && (mImpl->GetType() == ARRAY)) // type cannot change in mImpl so array is allocated
+  {
+    array = mImpl->GetArrayPtr();
+  }
+  return array;
+}
+
+Property::Map const* Property::Value::GetMap() const
+{
+  Property::Map* map = nullptr;
+  if(mImpl && (mImpl->GetType() == MAP)) // type cannot change in mImpl so map is allocated
+  {
+    map = mImpl->GetMapPtr();
+  }
+  return map;
+}
+
+Property::Map* Property::Value::GetMap()
 {
   if(mImpl && (mImpl->GetType() == MAP))
   {
index a0e88db..d9e6f7d 100644 (file)
@@ -470,20 +470,36 @@ public:
   bool Get(Property::Map& mapValue) const;
 
   /**
-   * @brief Retrieves the Array API of the Property::Value without copying the contents of the map.
+   * @brief Retrieves the Array API of the Property::Value without copying the contents of the array.
    *
-   * @SINCE_1_0.0
+   * @SINCE_1_9.32
    * @return The Array API of the Property::Value or NULL if not a Property::Array
    */
-  Property::Array* GetArray() const;
+  const Property::Array* GetArray() const;
+
+  /**
+   * @brief Retrieves the Array API of the Property::Value without copying the contents of the array.
+   *
+   * @SINCE_1_9.32
+   * @return The Array API of the Property::Value or NULL if not a Property::Array
+   */
+  Property::Array* GetArray();
 
   /**
    * @brief Retrieves the Map API of the Property::Value without copying the contents of the map.
    *
-   * @SINCE_1_0.0
+   * @SINCE_1_9.32
+   * @return The Map API of the Property::Value or NULL if not a Property::Map
+   */
+  const Property::Map* GetMap() const;
+
+  /**
+   * @brief Retrieves the Map API of the Property::Value without copying the contents of the map.
+   *
+   * @SINCE_1_9.32
    * @return The Map API of the Property::Value or NULL if not a Property::Map
    */
-  Property::Map* GetMap() const;
+  Property::Map* GetMap();
 
   /**
    * @brief Retrieves an extents.