2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Collections;
20 using System.Collections.Generic;
21 using System.Text.RegularExpressions;
23 namespace Tizen.Network.IoTConnectivity
26 /// This class contains resource types and provides APIs to manage, add, remove those types.
27 /// A resource type indicates a class or category of resources.
29 public class ResourceTypes : IEnumerable<string>, IDisposable
31 internal const int MaxLength = 61;
32 internal IntPtr _resourceTypeHandle = IntPtr.Zero;
33 private readonly HashSet<string> _resourceTypes = new HashSet<string>();
34 private bool _disposed = false;
37 /// Constructor of ResourceTypes
39 /// <seealso cref="Add()"/>
40 /// <seealso cref="Remove()"/>
42 /// ResourceTypes types = new ResourceTypes();
44 public ResourceTypes()
46 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle);
47 if (ret != (int)IoTConnectivityError.None)
49 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
50 throw IoTConnectivityErrorFactory.GetException(ret);
55 /// Constructor of ResourceTypes using list of types
57 /// <param name="types">List of resource types</param>
59 /// ResourceTypes types = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
61 public ResourceTypes(IEnumerable<string> types)
63 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle);
64 if (ret != (int)IoTConnectivityError.None)
66 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
67 throw IoTConnectivityErrorFactory.GetException(ret);
70 foreach (string type in types)
76 internal ResourceTypes(IntPtr typesHandleToClone)
78 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Clone(typesHandleToClone, out _resourceTypeHandle);
79 if (ret != (int)IoTConnectivityError.None)
81 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
82 throw IoTConnectivityErrorFactory.GetException(ret);
85 Interop.IoTConnectivity.Common.ResourceTypes.ForeachCallback cb = (string type, IntPtr data) =>
87 _resourceTypes.Add(type);
91 ret = Interop.IoTConnectivity.Common.ResourceTypes.Foreach(typesHandleToClone, cb, IntPtr.Zero);
92 if (ret != (int)IoTConnectivityError.None)
94 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
95 throw IoTConnectivityErrorFactory.GetException(ret);
100 /// Destructor of the ResourceTypes class.
108 /// Indicates count of types in the list
111 /// ResourceTypes types = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
112 /// Console.WriteLine("There are {0} items", types.Count);
118 return _resourceTypes.Count;
123 /// Adds a resource type into the list.
126 /// The length of @a item should be less than or equal to 61.\n
127 /// The @a item must start with a lowercase alphabetic character, followed by a sequence
128 /// of lowercase alphabetic, numeric, ".", or "-" characters, and contains no white space.\n
129 /// Duplicate strings are not allowed.
131 /// <param name="item">The string data to insert into the resource types</param>
132 /// <seealso cref="Remove()"/>
134 /// ResourceTypes resourceTypes = new ResourceTypes();
135 /// resourceTypes.Add("org.tizen.light");
137 public void Add(string item)
141 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Add(_resourceTypeHandle, item);
142 if (ret != (int)IoTConnectivityError.None)
144 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add type");
145 throw IoTConnectivityErrorFactory.GetException(ret);
147 _resourceTypes.Add(item);
151 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid type");
152 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
157 /// Removes a resource type from the list
159 /// <param name="item">The string data to delete from the resource types</param>
160 /// <seealso cref="Add()"/>
162 /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
163 /// resourceTypes.Remove("oic.if.room");
165 public void Remove(string item)
167 bool isRemoved = _resourceTypes.Remove(item);
170 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Remove(_resourceTypeHandle, item);
171 if (ret != (int)IoTConnectivityError.None)
173 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove type");
174 throw IoTConnectivityErrorFactory.GetException(ret);
178 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
182 /// Return enumerator for the list of types
184 /// <returns>The enumerator</returns>
186 /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
187 /// foreach(string item in resourceTypes)
189 /// Console.WriteLine("Type : {0}", item);
192 public IEnumerator<string> GetEnumerator()
194 return _resourceTypes.GetEnumerator();
198 /// Return enumerator for the list of types
200 /// <returns>The enumerator</returns>
202 /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
203 /// foreach(string item in resourceTypes)
205 /// Console.WriteLine("Type : {0}", item);
208 IEnumerator IEnumerable.GetEnumerator()
210 return _resourceTypes.GetEnumerator();
214 /// Releases any unmanaged resources used by this object.
216 public void Dispose()
219 GC.SuppressFinalize(this);
222 internal static bool IsValid(string type)
224 Regex r = new Regex("^[a-zA-Z0-9.-]+$");
225 return (type.Length <= MaxLength && char.IsLower(type[0]) && r.IsMatch(type));
229 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
231 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
232 protected virtual void Dispose(bool disposing)
239 // Free managed objects
242 Interop.IoTConnectivity.Common.ResourceTypes.Destroy(_resourceTypeHandle);