1 /// Copyright 2016 by Samsung Electronics, Inc.,
3 /// This software is the confidential and proprietary information
4 /// of Samsung Electronics, Inc. ("Confidential Information"). You
5 /// shall not disclose such Confidential Information and shall use
6 /// it only in accordance with the terms of the license agreement
7 /// you entered into with Samsung.
10 using System.Collections;
11 using System.Collections.Generic;
12 using System.Text.RegularExpressions;
14 namespace Tizen.Network.IoTConnectivity
17 /// Class containing resource types
19 public class ResourceTypes : IEnumerable<string>, IDisposable
21 internal const int MaxLength = 61;
22 internal IntPtr _resourceTypeHandle = IntPtr.Zero;
23 private readonly HashSet<string> _resourceTypes = new HashSet<string>();
24 private bool _disposed = false;
29 public ResourceTypes()
31 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle);
32 if (ret != (int)IoTConnectivityError.None)
34 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
35 throw IoTConnectivityErrorFactory.GetException(ret);
42 public ResourceTypes(IEnumerable<string> types)
44 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle);
45 if (ret != (int)IoTConnectivityError.None)
47 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
48 throw IoTConnectivityErrorFactory.GetException(ret);
51 foreach (string type in types)
60 internal ResourceTypes(IntPtr typesHandleToClone)
62 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Clone(typesHandleToClone, out _resourceTypeHandle);
63 if (ret != (int)IoTConnectivityError.None)
65 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
66 throw IoTConnectivityErrorFactory.GetException(ret);
69 Interop.IoTConnectivity.Common.ResourceTypes.ForeachCallback cb = (string type, IntPtr data) =>
71 _resourceTypes.Add(type);
75 ret = Interop.IoTConnectivity.Common.ResourceTypes.Foreach(typesHandleToClone, cb, IntPtr.Zero);
76 if (ret != (int)IoTConnectivityError.None)
78 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
79 throw IoTConnectivityErrorFactory.GetException(ret);
89 /// Count of resource types in the list
95 return _resourceTypes.Count;
100 /// Inserts a resource type into the list.
102 /// <param name="item">The resource type to add</param>
103 public void Add(string item)
107 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Add(_resourceTypeHandle, item);
108 if (ret != (int)IoTConnectivityError.None)
110 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add type");
111 throw IoTConnectivityErrorFactory.GetException(ret);
113 _resourceTypes.Add(item);
117 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid type");
118 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
123 /// Removes a resource type from the list
125 /// <param name="item">The resource type to remove</param>
126 public void Remove(string item)
128 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Remove(_resourceTypeHandle, item);
129 if (ret != (int)IoTConnectivityError.None)
131 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove type");
132 throw IoTConnectivityErrorFactory.GetException(ret);
135 _resourceTypes.Remove(item);
139 /// Return enumerator for the list of types
141 /// <returns>Enumerator of the collection</returns>
142 public IEnumerator<string> GetEnumerator()
144 return _resourceTypes.GetEnumerator();
147 IEnumerator IEnumerable.GetEnumerator()
149 return _resourceTypes.GetEnumerator();
152 public void Dispose()
155 GC.SuppressFinalize(this);
158 internal static bool IsValid(string type)
160 Regex r = new Regex("^[a-zA-Z0-9.-]+$");
161 return (type.Length <= MaxLength && char.IsLower(type[0]) && r.IsMatch(type));
164 protected virtual void Dispose(bool disposing)
171 // Free managed objects
174 Interop.IoTConnectivity.Common.ResourceTypes.Destroy(_resourceTypeHandle);