Setting since_tizen 3/4 on Tizen.NET API
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Vision / MediaVision / EngineConfiguration.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using Tizen.System;
19 using System.Runtime.InteropServices;
20 using static Interop.MediaVision;
21
22 namespace Tizen.Multimedia.Vision
23 {
24     /// <summary>
25     /// A base class for configuration classes.
26     /// </summary>
27     /// <since_tizen> 4 </since_tizen>
28     public abstract class EngineConfiguration : IDisposable
29     {
30         private IntPtr _handle = IntPtr.Zero;
31         private bool _disposed = false;
32
33         private const string _featurePath = "http://tizen.org/feature/vision.";
34
35         private bool IsSupportedEngineType(string type)
36         {
37             bool isSupported = false;
38
39             string featureType = _featurePath + type;
40
41             bool ret = Information.TryGetValue(featureType, out isSupported);
42
43             return (isSupported && ret) ? true : false;
44         }
45
46         private bool IsSupportedEngineType(string type1, string type2)
47         {
48             return (IsSupportedEngineType(type1) && IsSupportedEngineType(type2)) ? true : false;
49         }
50
51         internal EngineConfiguration(string engineType)
52         {
53             if (IsSupportedEngineType(engineType) == false)
54             {
55                 throw new NotSupportedException($"{engineType} : Not Supported");
56             }
57
58             EngineConfig.Create(out _handle).Validate("Failed to create media vision engine.");
59         }
60
61         internal EngineConfiguration(string engineType1, string engineType2)
62         {
63             if (IsSupportedEngineType(engineType1, engineType2) == false)
64             {
65                 throw new NotSupportedException($"{engineType1} or {engineType2} : Not Supported");
66             }
67
68             EngineConfig.Create(out _handle).Validate("Failed to create media vision engine.");
69         }
70
71         /// <summary>
72         /// Finalizes an instance of the EngineConfiguration class.
73         /// </summary>
74         ~EngineConfiguration()
75         {
76             Dispose(false);
77         }
78
79         internal static IntPtr GetHandle(EngineConfiguration config)
80         {
81             if (config == null)
82             {
83                 return IntPtr.Zero;
84             }
85
86             if (config._disposed)
87             {
88                 throw new ObjectDisposedException(config.GetType().Name);
89             }
90
91             return config._handle;
92         }
93
94         internal void Set(string key, double value)
95         {
96             EngineConfig.SetDouble(Handle, key, value).Validate("Failed to set attribute");
97         }
98
99         internal void Set(string key, int value)
100         {
101             EngineConfig.SetInt(Handle, key, value).Validate("Failed to set attribute");
102         }
103
104         internal void Set(string key, bool value)
105         {
106             EngineConfig.SetBool(Handle, key, value).Validate("Failed to set attribute");
107         }
108
109         internal void Set(string key, string value)
110         {
111             EngineConfig.SetString(Handle, key, value).Validate("Failed to set attribute");
112         }
113
114         internal int GetInt(string key)
115         {
116             int value = 0;
117             EngineConfig.GetInt(Handle, key, out value).Validate("Failed to get the value");
118             return value;
119         }
120
121         internal double GetDouble(string key)
122         {
123             double value = 0;
124             EngineConfig.GetDouble(Handle, key, out value).Validate("Failed to get the value");
125             return value;
126         }
127
128         internal bool GetBool(string key)
129         {
130             bool value = false;
131             EngineConfig.GetBool(Handle, key, out value).Validate("Failed to get the value");
132             return value;
133         }
134
135         internal string GetString(string key)
136         {
137             IntPtr ptr = IntPtr.Zero;
138
139             try
140             {
141                 EngineConfig.GetString(Handle, key, out ptr).Validate("Failed to get the value");
142                 return Marshal.PtrToStringAnsi(ptr);
143             }
144             finally
145             {
146                 LibcSupport.Free(ptr);
147             }
148         }
149
150         /// <summary>
151         /// Releases all resources used by the <see cref="EngineConfiguration"/> object.
152         /// </summary>
153         /// <since_tizen> 4 </since_tizen>
154         public void Dispose()
155         {
156             Dispose(true);
157             GC.SuppressFinalize(this);
158         }
159
160         /// <summary>
161         /// Releases the resources used by the <see cref="EngineConfiguration"/> object.
162         /// </summary>
163         /// <param name="disposing">
164         /// true to release both managed and unmanaged resources, otherwise false to release only unmanaged resources.
165         /// </param>
166         /// <since_tizen> 4 </since_tizen>
167         protected virtual void Dispose(bool disposing)
168         {
169             if (_disposed)
170             {
171                 return;
172             }
173
174             EngineConfig.Destroy(_handle);
175             _disposed = true;
176         }
177
178         internal IntPtr Handle
179         {
180             get
181             {
182                 if (_disposed)
183                 {
184                     throw new ObjectDisposedException(nameof(EngineConfiguration));
185                 }
186                 return _handle;
187             }
188         }
189     }
190 }