Fix Property::Value memory leak
[platform/core/uifw/dali-core.git] / dali / public-api / object / property-value.h
index 5a4abed..f9d6269 100644 (file)
@@ -1,8 +1,8 @@
-#ifndef __DALI_PROPERTY_VALUE_H__
-#define __DALI_PROPERTY_VALUE_H__
+#ifndef DALI_PROPERTY_VALUE_H
+#define DALI_PROPERTY_VALUE_H
 
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  */
 
 // EXTERNAL INCLUDES
+#include <initializer_list>
 #include <iosfwd>
+#include <type_traits>
+#include <utility>
 
 // INTERNAL INCLUDES
-#include <dali/public-api/object/property.h>
 #include <dali/public-api/math/rect.h>
+#include <dali/public-api/object/property.h>
 
 namespace Dali
 {
@@ -39,15 +42,17 @@ struct Vector3;
 struct Vector4;
 class Matrix3;
 class Matrix;
+struct Extents;
+
+using KeyValuePair = std::pair<Property::Key, Property::Value>;
 
 /**
  * @brief A value-type representing a property value.
  * @SINCE_1_0.0
  */
-class DALI_IMPORT_API Property::Value
+class DALI_CORE_API Property::Value
 {
 public:
-
   /**
    * @brief Default constructor.
    *
@@ -62,7 +67,7 @@ public:
    * @SINCE_1_0.0
    * @param[in] boolValue A boolean value
    */
-  Value( bool boolValue );
+  Value(bool boolValue);
 
   /**
    * @brief Creates an integer property value.
@@ -70,7 +75,7 @@ public:
    * @SINCE_1_0.0
    * @param[in] integerValue An integer value
    */
-  Value( int integerValue );
+  Value(int32_t integerValue);
 
   /**
    * @brief Creates a float property value.
@@ -78,7 +83,7 @@ public:
    * @SINCE_1_0.0
    * @param[in] floatValue A floating-point value
    */
-  Value( float floatValue );
+  Value(float floatValue);
 
   /**
    * @brief Creates a Vector2 property value.
@@ -86,7 +91,7 @@ public:
    * @SINCE_1_0.0
    * @param[in] vectorValue A vector of 2 floating-point values
    */
-  Value( const Vector2& vectorValue );
+  Value(const Vector2& vectorValue);
 
   /**
    * @brief Creates a Vector3 property value.
@@ -94,7 +99,7 @@ public:
    * @SINCE_1_0.0
    * @param[in] vectorValue A vector of 3 floating-point values
    */
-  Value( const Vector3& vectorValue );
+  Value(const Vector3& vectorValue);
 
   /**
    * @brief Creates a Vector4 property value.
@@ -102,7 +107,7 @@ public:
    * @SINCE_1_0.0
    * @param[in] vectorValue A vector of 4 floating-point values
    */
-  Value( const Vector4& vectorValue );
+  Value(const Vector4& vectorValue);
 
   /**
    * @brief Creates a Matrix3 property value.
@@ -110,7 +115,7 @@ public:
    * @SINCE_1_0.0
    * @param[in] matrixValue A matrix of 3x3 floating-point values
    */
-  Value( const Matrix3& matrixValue );
+  Value(const Matrix3& matrixValue);
 
   /**
    * @brief Creates a Matrix property value.
@@ -118,7 +123,7 @@ public:
    * @SINCE_1_0.0
    * @param[in] matrixValue A matrix of 4x4 floating-point values
    */
-  Value( const Matrix& matrixValue );
+  Value(const Matrix& matrixValue);
 
   /**
    * @brief Creates a Vector4 property value.
@@ -126,7 +131,15 @@ public:
    * @SINCE_1_0.0
    * @param[in] vectorValue A vector of 4 integer values
    */
-  Value( const Rect<int>& vectorValue );
+  Value(const Rect<int32_t>& vectorValue);
+
+  /**
+   * @brief Creates a Vector4 property value.
+   *
+   * @SINCE_1_9.14
+   * @param[in] vectorValue A vector of 4 float values
+   */
+  Value(const Rect<float>& vectorValue);
 
   /**
    * @brief Creates an orientation property value.
@@ -134,7 +147,7 @@ public:
    * @SINCE_1_0.0
    * @param[in] angleAxis An angle-axis representing the rotation
    */
-  Value( const AngleAxis& angleAxis );
+  Value(const AngleAxis& angleAxis);
 
   /**
    * @brief Creates an orientation property value.
@@ -142,7 +155,7 @@ public:
    * @SINCE_1_0.0
    * @param[in] quaternion A quaternion representing the rotation
    */
-  Value( const Quaternion& quaternion );
+  Value(const Quaternion& quaternion);
 
   /**
    * @brief Creates an string property value.
@@ -150,7 +163,7 @@ public:
    * @SINCE_1_0.0
    * @param[in] stringValue A string
    */
-  Value( const std::string& stringValue );
+  Value(std::string stringValue);
 
   /**
    * @brief Creates a string property value.
@@ -158,23 +171,51 @@ public:
    * @SINCE_1_0.0
    * @param[in] stringValue A string
    */
-  Value( const char* stringValue );
+  Value(const char* stringValue);
 
   /**
    * @brief Creates an array property value.
    *
-   * @SINCE_1_0.0
-   * @param[in] arrayValue An array
+   * @SINCE_1_9.30
+   * @param[in] arrayValue A property array
    */
-  Value( Property::Array& arrayValue );
+  Value(Property::Array arrayValue);
 
   /**
    * @brief Creates a map property value.
    *
-   * @SINCE_1_0.0
-   * @param[in] mapValue An array
+   * @SINCE_1_9.30
+   * @param[in] mapValue A property map
+   */
+  Value(Property::Map mapValue);
+
+  /**
+   * @brief Create a map property value from an initializer_list.
+   *
+   * @SINCE_1_4.16
+   * @param [in] values An initializer_list of pairs of index and value.
+   */
+  Value(const std::initializer_list<KeyValuePair>& values);
+
+  /**
+   * @brief Creates an extents property value.
+   *
+   * @SINCE_1_2.62
+   * @param[in] extentsValue A collection of 4 uint16_t values
+   */
+  Value(const Extents& extentsValue);
+
+  /**
+   * @brief Creates an enumeration property value.
+   *
+   * @SINCE_1_4.36
+   * @param[in] enumValue An enumeration value
    */
-  Value( Property::Map& mapValue );
+  template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
+  Value(T enumValue)
+  : Value(static_cast<int32_t>(enumValue))
+  {
+  }
 
   /**
    * @brief Explicitly sets a type and initialize it.
@@ -182,7 +223,7 @@ public:
    * @SINCE_1_0.0
    * @param[in] type The property value type
    */
-  explicit Value( Type type );
+  explicit Value(Type type);
 
   /**
    * @brief Copy constructor.
@@ -190,7 +231,16 @@ public:
    * @SINCE_1_0.0
    * @param[in] value The property value to copy
    */
-  Value( const Value& value );
+  Value(const Value& value);
+
+  /**
+   * @brief Move constructor.
+   *
+   * A move constructor enables the resources owned by an rvalue object to be moved into an lvalue without copying.
+   * @SINCE_1_4.16
+   * @param[in] value The property value to move from
+   */
+  Value(Value&& value) noexcept;
 
   /**
    * @brief Assigns a property value.
@@ -199,7 +249,16 @@ public:
    * @param[in] value The property value to assign from
    * @return a reference to this
    */
-  Value& operator=( const Value& value );
+  Value& operator=(const Value& value);
+
+  /**
+   * @brief Move assignment operator.
+   *
+   * @SINCE_1_4.16
+   * @param[in] value The property value to move from
+   * @return a reference to this
+   */
+  Value& operator=(Value&& value) noexcept;
 
   /**
    * @brief Non-virtual destructor.
@@ -220,20 +279,57 @@ public:
   /**
    * @brief Retrieves a specific value.
    *
-   * Works on a best-effort approach; if value type is not convertible returns a default value of the type.
+   * Works on a best-effort approach; if value type is different returns a default value of the type.
    *
-   * @SINCE_1_0.0
+   * @SINCE_1_4.36
    * @return A value of type T
    */
-  template <typename T>
+  template<typename T, typename std::enable_if<!std::is_enum<T>::value>::type* = nullptr>
   T DALI_INTERNAL Get() const
   {
     T temp = T(); // value (zero) initialize
-    Get( temp );
+    Get(temp);
     return temp;
   }
 
   /**
+   * @brief Retrieves a specific value.
+   *
+   * Works on a best-effort approach; if value type is different returns a default value of the type.
+   * Specialization for enumeration values
+   *
+   * @SINCE_1_4.36
+   * @return A value of type T
+   */
+  template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
+  T DALI_INTERNAL Get() const
+  {
+    int32_t temp = 0; // value (zero) initialize
+    Get(temp);
+    return static_cast<T>(temp);
+  }
+
+  /**
+   * @brief Retrieves an enumeration value.
+   *
+   * @SINCE_1_4.36
+   * @param[out] enumValue On return, an enumeration value
+   * @return @c true if the value is successfully retrieved, @c false if the type is different
+   * @pre GetType() is any enumeration
+   */
+  template<typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
+  bool DALI_INTERNAL Get(T& enumValue) const
+  {
+    int32_t temp = 0;
+    if(!Get(temp))
+    {
+      return false;
+    }
+    enumValue = static_cast<T>(temp);
+    return true;
+  }
+
+  /**
    * @brief Retrieves a boolean value.
    *
    * @SINCE_1_0.0
@@ -241,7 +337,7 @@ public:
    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
    * @pre GetType() is a type convertible to bool.
    */
-  bool Get( bool& boolValue ) const;
+  bool Get(bool& boolValue) const;
 
   /**
    * @brief Retrieves a floating-point value.
@@ -251,7 +347,7 @@ public:
    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
    * @pre GetType() is a type convertible to float.
    */
-  bool Get( float& floatValue ) const;
+  bool Get(float& floatValue) const;
 
   /**
    * @brief Retrieves an integer value.
@@ -261,7 +357,7 @@ public:
    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
    * @pre GetType() is a type convertible to int.
    */
-  bool Get( int& integerValue ) const;
+  bool Get(int32_t& integerValue) const;
 
   /**
    * @brief Retrieves an integer rectangle.
@@ -271,7 +367,7 @@ public:
    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
    * @pre GetType() is a type convertible to Rect<int>.
    */
-  bool Get( Rect<int>& rect ) const;
+  bool Get(Rect<int32_t>& rect) const;
 
   /**
    * @brief Retrieves a vector value.
@@ -281,7 +377,7 @@ public:
    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
    * @pre GetType() is a type convertible to Vector2.
    */
-  bool Get( Vector2& vectorValue ) const;
+  bool Get(Vector2& vectorValue) const;
 
   /**
    * @brief Retrieves a vector value.
@@ -291,7 +387,7 @@ public:
    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
    * @pre GetType() is a type convertible to Vector3.
    */
-  bool Get( Vector3& vectorValue ) const;
+  bool Get(Vector3& vectorValue) const;
 
   /**
    * @brief Retrieves a vector value.
@@ -301,7 +397,7 @@ public:
    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
    * @pre GetType() is a type convertible to Vector4.
    */
-  bool Get( Vector4& vectorValue ) const;
+  bool Get(Vector4& vectorValue) const;
 
   /**
    * @brief Retrieves a matrix3 value.
@@ -311,7 +407,7 @@ public:
    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
    * @pre GetType() is a type convertible to Matrix3.
    */
-  bool Get( Matrix3& matrixValue ) const;
+  bool Get(Matrix3& matrixValue) const;
 
   /**
    * @brief Retrieves a matrix value.
@@ -321,7 +417,7 @@ public:
    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
    * @pre GetType() is a type convertible to Matrix.
    */
-  bool Get( Matrix& matrixValue ) const;
+  bool Get(Matrix& matrixValue) const;
 
   /**
    * @brief Retrieves an angle-axis value.
@@ -331,7 +427,7 @@ public:
    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
    * @pre GetType() is a type convertible to AngleAxis.
    */
-  bool Get( AngleAxis& angleAxisValue ) const;
+  bool Get(AngleAxis& angleAxisValue) const;
 
   /**
    * @brief Retrieves a quaternion value.
@@ -341,7 +437,7 @@ public:
    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
    * @pre GetType() is a type convertible to Quaternion.
    */
-  bool Get( Quaternion& quaternionValue ) const;
+  bool Get(Quaternion& quaternionValue) const;
 
   /**
    * @brief Retrieves an string property value.
@@ -351,7 +447,7 @@ public:
    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
    * @pre GetType() is a type convertible to string.
    */
-  bool Get( std::string& stringValue ) const;
+  bool Get(std::string& stringValue) const;
 
   /**
    * @brief Retrieves an array property value.
@@ -361,7 +457,7 @@ public:
    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
    * @pre GetType() returns Property::ARRAY.
    */
-  bool Get( Property::Array& arrayValue ) const;
+  bool Get(Property::Array& arrayValue) const;
 
   /**
    * @brief Retrieves an map property value.
@@ -371,35 +467,82 @@ public:
    * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
    * @pre GetType() returns Property::MAP.
    */
-  bool Get( Property::Map& mapValue ) const;
+  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
+   */
+  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() const;
+  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.
+   *
+   * @SINCE_1_2.62
+   * @param[out] extentsValue Extents, a collection of 4 uint16_t
+   * @return @c true if the value is successfully retrieved, @c false if the type is not convertible
+   * @pre GetType() is a type convertible to Extents.
+   */
+  bool Get(Extents& extentsValue) const;
 
   /**
    * @brief Output to stream.
    * @SINCE_1_0.0
    */
-  friend std::ostream& operator<<( std::ostream& ouputStream, const Property::Value& value );
+  friend DALI_CORE_API std::ostream& operator<<(std::ostream& ouputStream, const Property::Value& value);
 
 private:
-
+  /// @cond internal
   struct DALI_INTERNAL Impl;
-  Impl* mImpl; ///< Pointer to the implementation
 
+  /**
+   * @brief Retrieves an already constructed Impl object from the storage buffer.
+   * @return A const reference to the Impl object
+   */
+  DALI_INTERNAL const Impl& Read() const;
+
+  /**
+   * @brief Retrieves an already constructed Impl object from the storage buffer.
+   * @return A non const reference to the Impl object
+   */
+  DALI_INTERNAL Impl& Write();
+
+  /**
+   * @brief An aligned storage buffer to create Impl object inplace.
+   */
+  struct DALI_INTERNAL Storage
+  {
+    alignas(sizeof(Impl*)) unsigned char buffer[16];
+  };
+
+  Storage mStorage;
+  /// @endcond
 };
 
 /**
@@ -410,11 +553,11 @@ private:
  * @param[in] value The value to insert
  * @return The output stream operator
  */
-DALI_IMPORT_API std::ostream& operator<<( std::ostream& ouputStream, const Property::Value& value );
+DALI_CORE_API std::ostream& operator<<(std::ostream& ouputStream, const Property::Value& value);
 
 /**
  * @}
  */
 } // namespace Dali
 
-#endif // __DALI_PROPERTY_VALUE_H__
+#endif // DALI_PROPERTY_VALUE_H