[MachineLearning.Inference] NotSupportedException support (#1122)
[platform/core/csapi/tizenfx.git] / src / Tizen.MachineLearning.Inference / Tizen.MachineLearning.Inference / TensorsData.cs
1 /*
2 * Copyright (c) 2019 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 System.IO;
19
20 namespace Tizen.MachineLearning.Inference
21 {
22     /// <summary>
23     /// The TensorsData class sets and gets the buffer data for each Tensor.
24     /// </summary>
25     /// <since_tizen> 6 </since_tizen>
26     public class TensorsData : IDisposable
27     {
28         private IntPtr _handle = IntPtr.Zero;
29         private bool _disposed = false;
30         private int _count = Tensor.InvalidCount;
31
32         /// <summary>
33         /// Creates a TensorsInfo instance with handle which is given by TensorsInfo.
34         /// </summary>
35         /// <param name="handle">The handle of tensors data.</param>
36         /// <since_tizen> 6 </since_tizen>
37         private TensorsData(IntPtr handle)
38         {
39             _handle = handle;
40         }
41
42         /// <summary>
43         /// Destructor of the TensorsData instance
44         /// </summary>
45         /// <since_tizen> 6 </since_tizen>
46         ~TensorsData()
47         {
48             Dispose(false);
49         }
50
51         internal static TensorsData CreateFromNativeHandle(IntPtr handle)
52         {
53             TensorsData retTensorsData = new TensorsData(handle);
54
55             return retTensorsData;
56         }
57
58         /// <summary>
59         /// Gets the number of Tensor in TensorsData class
60         /// </summary>
61         /// <feature>http://tizen.org/feature/machine_learning.inference</feature>
62         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
63         /// <since_tizen> 6 </since_tizen>
64         public int Count
65         {
66             get {
67                 NNStreamer.CheckNNStreamerSupport();
68
69                 if (_count != Tensor.InvalidCount)
70                     return _count;
71
72                 NNStreamerError ret = NNStreamerError.None;
73                 int count = 0;
74
75                 ret = Interop.Util.GetTensorsCount(_handle, out count);
76                 NNStreamer.CheckException(ret, "unable to get the count of TensorsData");
77
78                 _count = count;
79                 return _count;
80             }
81         }
82
83         /// <summary>
84         /// Sets a tensor data to given index.
85         /// </summary>
86         /// <param name="index">The index of the tensor.</param>
87         /// <param name="buffer">Raw tensor data to be set.</param>
88         /// <feature>http://tizen.org/feature/machine_learning.inference</feature>
89         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
90         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
91         /// <since_tizen> 6 </since_tizen>
92         public void SetTensorData(int index, byte[] buffer)
93         {
94             NNStreamerError ret = NNStreamerError.None;
95
96             NNStreamer.CheckNNStreamerSupport();
97
98             if (buffer == null)
99             {
100                 string msg = "buffer is null";
101                 throw NNStreamerExceptionFactory.CreateException(NNStreamerError.InvalidParameter, msg);
102             }
103
104             ret = Interop.Util.SetTensorData(_handle, index, buffer, buffer.Length);
105             NNStreamer.CheckException(ret, "unable to set the buffer of TensorsData: " + index.ToString());
106         }
107
108         /// <summary>
109         /// Gets a tensor data to given index.
110         /// </summary>
111         /// <param name="index">The index of the tensor.</param>
112         /// <returns>Raw tensor data</returns>
113         /// <feature>http://tizen.org/feature/machine_learning.inference</feature>
114         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
115         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
116         /// <since_tizen> 6 </since_tizen>
117         public byte[] GetTensorData(int index)
118         {
119             byte[] retBuffer = null;
120             IntPtr raw_data;
121             int size;
122             NNStreamerError ret = NNStreamerError.None;
123
124             NNStreamer.CheckNNStreamerSupport();
125
126             ret = Interop.Util.GetTensorData(_handle, index, out raw_data, out size);
127             NNStreamer.CheckException(ret, "unable to get the buffer of TensorsData: " + index.ToString());
128
129             retBuffer = Interop.Util.IntPtrToByteArray(raw_data, size);
130
131             return retBuffer;
132         }
133
134         /// <summary>
135         /// Releases any unmanaged resources used by this object.
136         /// </summary>
137         /// <since_tizen> 6 </since_tizen>
138         public void Dispose()
139         {
140             Dispose(true);
141             GC.SuppressFinalize(this);
142         }
143
144         /// <summary>
145         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
146         /// </summary>
147         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
148         protected virtual void Dispose(bool disposing)
149         {
150             if (_disposed)
151                 return;
152
153             if (disposing)
154             {
155                 // release managed object
156             }
157
158             // release unmanaged objects
159             if (_handle != IntPtr.Zero)
160             {
161                 NNStreamerError ret = Interop.Util.DestroyTensorsData(_handle);
162                 if (ret != NNStreamerError.None)
163                 {
164                     Log.Error(NNStreamer.TAG, "failed to destroy TensorsData object");
165                 }
166                 _handle = IntPtr.Zero;
167             }
168             _disposed = true;
169         }
170
171         internal IntPtr Handle
172         {
173             get { return _handle; }
174         }
175     }
176 }