Make PropertyMap look like Dictionary.
authorhuayong.xu <huayong.xu@samsung.com>
Wed, 23 Mar 2022 10:57:37 +0000 (18:57 +0800)
committerdongsug-song <35130733+dongsug-song@users.noreply.github.com>
Tue, 29 Mar 2022 03:10:12 +0000 (12:10 +0900)
src/Tizen.NUI/src/internal/Interop/Interop.PropertyMap.cs
src/Tizen.NUI/src/public/Common/PropertyMap.cs [changed mode: 0644->0755]
test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/PropertyMapSample.cs [new file with mode: 0755]

index 099c3dfc179f69bbfcc5222cb9d9ce0c5a18a83a..3a44d7a7bd15fe1fab95180947c53d5aa826a3e5 100755 (executable)
@@ -49,6 +49,12 @@ namespace Tizen.NUI
             [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_Property_Map_Add__SWIG_2")]
             public static extern global::System.IntPtr Add(global::System.Runtime.InteropServices.HandleRef jarg1, int jarg2, global::System.Runtime.InteropServices.HandleRef jarg3);
 
+            [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_Property_Map_Remove__SWIG_0")]
+            public static extern bool Remove(global::System.Runtime.InteropServices.HandleRef jarg1, int jarg2);
+
+            [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_Property_Map_Remove__SWIG_1")]
+            public static extern bool Remove(global::System.Runtime.InteropServices.HandleRef jarg1, string jarg2);
+
             [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_Property_Map_GetValue")]
             public static extern global::System.IntPtr GetValue(global::System.Runtime.InteropServices.HandleRef jarg1, uint jarg2);
 
old mode 100644 (file)
new mode 100755 (executable)
index 004c326..c03c8ce
@@ -14,6 +14,7 @@
  * limitations under the License.
  *
  */
+using System.Collections.Generic;
 using System.ComponentModel;
 
 namespace Tizen.NUI
@@ -24,7 +25,6 @@ namespace Tizen.NUI
     /// <since_tizen> 3 </since_tizen>
     public class PropertyMap : Disposable
     {
-
         /// <summary>
         /// The constructor.
         /// </summary>
@@ -48,6 +48,42 @@ namespace Tizen.NUI
         {
         }
 
+        /// <summary>
+        /// Retrieves all keys.
+        /// </summary>
+        /// <returns>A list of keys.</returns>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public IList<PropertyKey> Keys
+        {
+            get
+            {
+                List<PropertyKey> keys = new List<PropertyKey>();
+                for (uint i = 0; i < Count(); i++)
+                {
+                    keys.Add(GetKeyAt(i));
+                }
+                return keys;
+            }
+        }
+
+        /// <summary>
+        /// Retrieves all values.
+        /// </summary>
+        /// <returns>A list of values.</returns>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public IList<PropertyValue> Values
+        {
+            get
+            {
+                List<PropertyValue> values = new List<PropertyValue>();
+                for (uint i = 0; i < Count(); i++)
+                {
+                    values.Add(GetValue(i));
+                }
+                return values;
+            }
+        }
+
         /// <summary>
         /// The operator to access the element with the specified string key.<br />
         /// If an element with the key does not exist, then it is created.<br />
@@ -61,9 +97,12 @@ namespace Tizen.NUI
             {
                 return ValueOfIndex(key);
             }
-            internal set
+            set
             {
-                SetValue(key, value);
+                using (PropertyKey pKey = new PropertyKey(key))
+                {
+                    SetValue(pKey, value);
+                }
             }
         }
 
@@ -80,9 +119,12 @@ namespace Tizen.NUI
             {
                 return ValueOfIndex(key);
             }
-            internal set
+            set
             {
-                SetValue(key, value);
+                using (PropertyKey pKey = new PropertyKey(key))
+                {
+                    SetValue(pKey, value);
+                }
             }
         }
 
@@ -112,33 +154,43 @@ namespace Tizen.NUI
 
         /// <summary>
         /// Inserts the key-value pair in the map, with the key type as string.<br />
-        /// Does not check for duplicates.<br />
+        /// The exception would be thrown if the pair with the same key already exists.
         /// </summary>
         /// <param name="key">The key to insert.</param>
         /// <param name="value">The value to insert.</param>
         /// <since_tizen> 3 </since_tizen>
         public void Insert(string key, PropertyValue value)
         {
+            global::System.IntPtr cPtr = Interop.PropertyMap.Find(SwigCPtr, key);
+            if (cPtr != global::System.IntPtr.Zero)
+            {
+                throw new global::System.ArgumentException($"The key {key} already exists.", nameof(key));
+            }
             Interop.PropertyMap.Insert(SwigCPtr, key, PropertyValue.getCPtr(value));
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
         }
 
         /// <summary>
         /// Inserts the key-value pair in the map, with the key type as index.<br />
-        /// Does not check for duplicates.<br />
+        /// The exception would be thrown if the pair with the same key already exists.
         /// </summary>
         /// <param name="key">The key to insert.</param>
         /// <param name="value">The value to insert.</param>
         /// <since_tizen> 3 </since_tizen>
         public void Insert(int key, PropertyValue value)
         {
+            global::System.IntPtr cPtr = Interop.PropertyMap.Find(SwigCPtr, key);
+            if (cPtr != global::System.IntPtr.Zero)
+            {
+                throw new global::System.ArgumentException($"The key {key} already exists.", nameof(key));
+            }
             Interop.PropertyMap.Insert(SwigCPtr, key, PropertyValue.getCPtr(value));
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
         }
 
         /// <summary>
         /// Inserts the key-value pair in the map, with the key type as string.<br />
-        /// Does not check for duplicates.<br />
+        /// The exception would be thrown if the pair with the same key already exists.
         /// </summary>
         /// <param name="key">The key to insert.</param>
         /// <param name="value">The value to insert.</param>
@@ -146,6 +198,11 @@ namespace Tizen.NUI
         /// <since_tizen> 3 </since_tizen>
         public PropertyMap Add(string key, PropertyValue value)
         {
+            global::System.IntPtr cPtr = Interop.PropertyMap.Find(SwigCPtr, key);
+            if (cPtr != global::System.IntPtr.Zero)
+            {
+                throw new global::System.ArgumentException($"The key {key} already exists.", nameof(key));
+            }
             Interop.PropertyMap.Add(SwigCPtr, key, PropertyValue.getCPtr(value));
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
             return this;
@@ -153,7 +210,7 @@ namespace Tizen.NUI
 
         /// <summary>
         /// Inserts the key-value pair in the map, with the key type as string.<br />
-        /// Does not check for duplicates.<br />
+        /// The exception would be thrown if the pair with the same key already exists.
         /// </summary>
         /// <param name="key">The key to insert.</param>
         /// <param name="value">The value to insert.</param>
@@ -161,6 +218,11 @@ namespace Tizen.NUI
         /// <since_tizen> 3 </since_tizen>
         public PropertyMap Add(int key, PropertyValue value)
         {
+            global::System.IntPtr cPtr = Interop.PropertyMap.Find(SwigCPtr, key);
+            if (cPtr != global::System.IntPtr.Zero)
+            {
+                throw new global::System.ArgumentException($"The key {key} already exists.", nameof(key));
+            }
             Interop.PropertyMap.Add(SwigCPtr, key, PropertyValue.getCPtr(value));
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
             return this;
@@ -168,7 +230,7 @@ namespace Tizen.NUI
 
         /// <summary>
         /// Inserts the keyvalue to the map.<br />
-        /// Does not check for duplicates.<br />
+        /// The exception would be thrown if the pair with the same key already exists.
         /// </summary>
         /// <param name="keyValue">The keyvalue to insert.</param>
         /// <returns>Returns a reference to this object.</returns>
@@ -179,19 +241,66 @@ namespace Tizen.NUI
             {
                 throw new global::System.ArgumentNullException(nameof(keyValue));
             }
-            if (keyValue.KeyInt != null)
+            if (keyValue.IntegerKey != null)
             {
-                Interop.PropertyMap.Add(SwigCPtr, (int)keyValue.KeyInt, PropertyValue.getCPtr(keyValue.TrueValue));
+                Add((int)keyValue.IntegerKey, keyValue.PropertyValue);
             }
-            else if (keyValue.KeyString != null)
+            else if (keyValue.StringKey != null)
             {
-                Interop.PropertyMap.Add(SwigCPtr, keyValue.KeyString, PropertyValue.getCPtr(keyValue.TrueValue));
+                Add(keyValue.StringKey, keyValue.PropertyValue);
             }
-            if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
 
             return this;
         }
 
+        /// <summary>
+        /// Removes the element by the specified key.
+        /// </summary>
+        /// <param name="key">The index key to find.</param>
+        /// <returns>True if the element is removed, false otherwise.</returns>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public bool Remove(PropertyKey key)
+        {
+            if (null == key)
+            {
+                throw new global::System.ArgumentNullException(nameof(key));
+            }
+            bool isRemoved = false;
+            if (key.Type == PropertyKey.KeyType.Index)
+            {
+                isRemoved = Interop.PropertyMap.Remove(SwigCPtr, key.IndexKey);
+            }
+            else if(key.Type == PropertyKey.KeyType.String)
+            {
+                isRemoved = Interop.PropertyMap.Remove(SwigCPtr, key.StringKey);
+            }
+            return isRemoved;
+        }
+
+        /// <summary>
+        /// Determines whether the PropertyMap contains the specified key.
+        /// </summary>
+        /// <param name="key">The index key to find.</param>
+        /// <returns>True if it exists, false otherwise.</returns>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public bool Contains(PropertyKey key)
+        {
+            if (null == key)
+            {
+                throw new global::System.ArgumentNullException(nameof(key));
+            }
+            global::System.IntPtr cPtr = global::System.IntPtr.Zero;
+            if (key.Type == PropertyKey.KeyType.Index)
+            {
+                cPtr = Interop.PropertyMap.Find(SwigCPtr, key.IndexKey);
+            }
+            else if (key.Type == PropertyKey.KeyType.String)
+            {
+                cPtr = Interop.PropertyMap.Find(SwigCPtr, key.StringKey);
+            }
+            return cPtr != global::System.IntPtr.Zero;
+        }
+
         /// <summary>
         /// Retrieves the value at the specified position.
         /// </summary>
@@ -265,6 +374,10 @@ namespace Tizen.NUI
         /// <since_tizen> 3 </since_tizen>
         public void Merge(PropertyMap from)
         {
+            if (from == null)
+            {
+                throw new global::System.ArgumentNullException(nameof(from));
+            }
             Interop.PropertyMap.Merge(SwigCPtr, PropertyMap.getCPtr(from));
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
         }
@@ -293,15 +406,16 @@ namespace Tizen.NUI
             return ret;
         }
 
-        internal void SetValue(int key, PropertyValue value)
+        internal void SetValue(PropertyKey key, PropertyValue value)
         {
-            Interop.PropertyMap.SetValueIntKey(SwigCPtr, key, PropertyValue.getCPtr(value));
-            if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
-        }
-
-        internal void SetValue(string key, PropertyValue value)
-        {
-            Interop.PropertyMap.SetValueStringKey(SwigCPtr, key, PropertyValue.getCPtr(value));
+            if (key.Type == PropertyKey.KeyType.Index)
+            {
+                Interop.PropertyMap.SetValueIntKey(SwigCPtr, key.IndexKey, PropertyValue.getCPtr(value));
+            }
+            else if (key.Type == PropertyKey.KeyType.String)
+            {
+                Interop.PropertyMap.SetValueStringKey(SwigCPtr, key.StringKey, PropertyValue.getCPtr(value));
+            }
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
         }
 
diff --git a/test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/PropertyMapSample.cs b/test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/PropertyMapSample.cs
new file mode 100755 (executable)
index 0000000..ccc874d
--- /dev/null
@@ -0,0 +1,64 @@
+using System.Collections.Generic;
+using Tizen.NUI.BaseComponents;
+using Tizen.NUI.Components;
+
+namespace Tizen.NUI.Samples
+{
+    public class PropertyMapSample : IExample
+    {
+        private TextLabel view1;
+
+        public void Activate()
+        {
+            Window window = NUIApplication.GetDefaultWindow();
+
+            view1 = new TextLabel
+            {
+                Position = new Position(400, 400),
+                Size = new Size(600, 100),
+                Text = "Shadow on Label"
+            };
+
+            PropertyMap shadow = new PropertyMap();
+
+            // insert
+            shadow.Insert("offset", new PropertyValue(new Vector2(10, 10)));
+            shadow.Insert("color", new PropertyValue(Color.Red));
+            shadow.Insert("blurRadius", new PropertyValue(10f));
+
+            // update
+            shadow["color"] = new PropertyValue(Color.Blue);
+
+            // remove
+            shadow.Remove(new PropertyKey("color"));
+            Log.Info("test", $"removed color: {shadow.Contains(new PropertyKey("color"))}");
+
+            // add
+            shadow.Add("color", new PropertyValue(Color.Blue));
+
+            view1.Shadow = shadow;
+            var map = view1.Shadow;
+
+            // query
+            Log.Info("test", $"offset : {map.Contains(new PropertyKey("offset"))}");
+            Log.Info("test", $"color: {map.Contains(new PropertyKey("color"))}");
+            Log.Info("test", $"color: {map["color"]}");
+
+            Color vectorValue = new Color();
+            map["color"].Get(vectorValue);
+            bool isBlue = vectorValue.EqualTo(Color.Blue);
+            Log.Info("test", $"color: {isBlue}");
+
+            window.Add(view1);
+        }
+
+        public void Deactivate()
+        {
+            if (view1 != null)
+            {
+                NUIApplication.GetDefaultWindow().Remove(view1);
+                view1.Dispose();
+            }
+        }
+    }
+}