Release 4.0.0-preview1-00201
[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> 3 </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
64             if (IsSupportedEngineType(engineType1, engineType2) == false)
65             {
66                 throw new NotSupportedException($"{engineType1} or {engineType2} : Not Supported");
67             }
68
69             EngineConfig.Create(out _handle).Validate("Failed to create media vision engine.");
70         }
71
72         ~EngineConfiguration()
73         {
74             Dispose(false);
75         }
76
77         internal static IntPtr GetHandle(EngineConfiguration config)
78         {
79             if (config == null)
80             {
81                 return IntPtr.Zero;
82             }
83
84             if (config._disposed)
85             {
86                 throw new ObjectDisposedException(config.GetType().Name);
87             }
88
89             return config._handle;
90         }
91
92         internal void Set(string key, double value)
93         {
94             EngineConfig.SetDouble(Handle, key, value).Validate("Failed to set attribute");
95         }
96
97         internal void Set(string key, int value)
98         {
99             EngineConfig.SetInt(Handle, key, value).Validate("Failed to set attribute");
100         }
101
102
103         internal void Set(string key, bool value)
104         {
105             EngineConfig.SetBool(Handle, key, value).Validate("Failed to set attribute");
106         }
107
108         internal void Set(string key, string value)
109         {
110             EngineConfig.SetString(Handle, key, value).Validate("Failed to set attribute");
111         }
112
113         internal int GetInt(string key)
114         {
115             int value = 0;
116             EngineConfig.GetInt(Handle, key, out value).Validate("Failed to get the value");
117             return value;
118         }
119
120         internal double GetDouble(string key)
121         {
122             double value = 0;
123             EngineConfig.GetDouble(Handle, key, out value).Validate("Failed to get the value");
124             return value;
125         }
126
127         internal bool GetBool(string key)
128         {
129             bool value = false;
130             EngineConfig.GetBool(Handle, key, out value).Validate("Failed to get the value");
131             return value;
132         }
133
134         internal string GetString(string key)
135         {
136             IntPtr ptr = IntPtr.Zero;
137
138             try
139             {
140                 EngineConfig.GetString(Handle, key, out ptr).Validate("Failed to get the value");
141                 return Marshal.PtrToStringAnsi(ptr);
142             }
143             finally
144             {
145                 LibcSupport.Free(ptr);
146             }
147         }
148
149         /// <summary>
150         /// Releases all resources used by the <see cref="EngineConfiguration"/> object.
151         /// </summary>
152         public void Dispose()
153         {
154             Dispose(true);
155             GC.SuppressFinalize(this);
156         }
157
158         /// <summary>
159         /// Releases the resources used by the <see cref="EngineConfiguration"/> object.
160         /// </summary>
161         /// <param name="disposing">
162         /// true to release both managed and unmanaged resources, otherwise false to release only unmanaged resources.
163         /// </param>
164         protected virtual void Dispose(bool disposing)
165         {
166             if (_disposed)
167             {
168                 return;
169             }
170
171             EngineConfig.Destroy(_handle);
172             _disposed = true;
173         }
174
175         internal IntPtr Handle
176         {
177             get
178             {
179                 if (_disposed)
180                 {
181                     throw new ObjectDisposedException(nameof(EngineConfiguration));
182                 }
183                 return _handle;
184             }
185         }
186     }
187 }