66ecacc95b0d88a262123769391e388373154a42
[platform/core/csapi/tizenfx.git] / src / Tizen.MachineLearning.Train / Tizen.MachineLearning.Train / Model.cs
1 /*
2 * Copyright (c) 2022 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 using static Interop;
17 using System;
18 using System.IO;
19
20 namespace Tizen.MachineLearning.Train
21 {
22     /// <summary>
23     /// Constructs the neural network model
24     /// </summary>
25     /// <remarks>
26     /// Use this function to create neural network model.
27     /// The Model class provides interfaces to construct, complle, run, adding layer
28     /// and etc with neural networks.
29     /// If you want to access only internal storage by using this function,
30     /// you should add privilege %http://tizen.org/privilege/mediastorage. Or, if you
31     /// want to access only external storage by using this function, you should add
32     /// privilege %http://tizen.org/privilege/externalstorage. If you want to access
33     /// both storage, you must add all the privileges.
34     /// </remarks>
35     /// <since_tizen> 10 </since_tizen>
36     public class Model: IDisposable
37     {
38         private IntPtr _handle = IntPtr.Zero;
39         private bool _disposed = false;
40
41         /// <summary>
42         /// Create a new Model instance.
43         /// </summary>
44         /// <since_tizen> 10 </since_tizen>
45         public Model()
46         {
47             NNTrainerError ret = Interop.Model.Construct(out _handle);
48             NNTrainer.CheckException(ret, "Failed to create model instance");
49         }
50
51         /// <summary>
52         /// Destructor of Model
53         /// </summary>
54         /// <since_tizen> 10 </since_tizen>
55         ~Model()
56         {
57             Dispose(false);
58         }
59
60         /// <summary>
61         /// Releases any unmanaged resources used by this object.
62         /// </summary>
63         /// <since_tizen> 10 </since_tizen>
64         public void Dispose()
65         {
66             Dispose(true);
67             GC.SuppressFinalize(this);
68         }
69
70         /// <summary>
71         /// Releases any unmanaged resources used by this object including opened handle.
72         /// </summary>
73         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
74         /// <since_tizen> 10 </since_tizen>
75         protected virtual void Dispose(bool disposing)
76         {
77             if (_disposed)
78                 return;
79             if (disposing)
80             {
81                 // release managed object
82             }
83
84             // release unmanaged object
85             if (_handle != IntPtr.Zero)
86             {
87                 // Destroy the neural network model.
88                 NNTrainerError ret = Interop.Model.Destroy(_handle);
89                 NNTrainer.CheckException(ret, "Failed to destroy model instance");
90
91                 _handle = IntPtr.Zero;
92             }
93             _disposed = true;
94         }
95     } 
96 }