1 #Region "Microsoft.VisualBasic::d84b39c59c0294a067990e3bc662355f, Microsoft.VisualBasic.Core\ComponentModel\Settings\Inf\ClassMapper.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module ClassMapper
35     
36     '         Function: (+2 OverloadsClassWriter, LoadIni, (+2 Overloads) MapParser, WriteClass
37     
38     '         Sub: (+2 OverloadsClassDumper
39     
40     
41     ' /********************************************************************************/
42
43 #End Region
44
45 Imports System.Runtime.CompilerServices
46 Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel
47 Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel.SchemaMaps
48 Imports Microsoft.VisualBasic.Linq
49
50 Namespace ComponentModel.Settings.Inf
51
52     ''' <summary>
53     ''' 使用这个属性来标记需要进行序列化的对象属性: <see cref="DataFrameColumnAttribute"/>
54     ''' </summary>
55     Public Module ClassMapper
56
57         <MethodImpl(MethodImplOptions.AggressiveInlining)>
58         Public Function MapParser(Of T As Class)() As NamedValue(Of BindProperty(Of DataFrameColumnAttribute)())
59             Return GetType(T).MapParser
60         End Function
61
62         ''' <summary>
63         ''' Get mapping data, includes section name and keys
64         ''' </summary>
65         ''' <param name="type"></param>
66         ''' <returns></returns>
67         <Extension>
68         Public Function MapParser(type As Type) As NamedValue(Of BindProperty(Of DataFrameColumnAttribute)())
69             Dim nameCLS As ClassName = type.GetAttribute(Of ClassName)
70             Dim name As String
71
72             If nameCLS Is Nothing Then
73                 name = type.Name
74             Else
75                 name = nameCLS.Name
76             End If
77
78             Dim source = DataFrameColumnAttribute.LoadMapping(type)
79             Dim binds = source.Values.ToArray
80
81             Return New NamedValue(Of BindProperty(Of DataFrameColumnAttribute)()) With {
82                 .Name = name,
83                 .Value = binds
84             }
85         End Function
86
87         ''' <summary>
88         ''' Read data from ini file.
89         ''' </summary>
90         ''' <typeparam name="T"></typeparam>
91         ''' <param name="ini"></param>
92         ''' <returns></returns>
93         ''' 
94         <Extension>
95         Public Function ClassWriter(Of T As {New, Class})(ini As IniFile) As T
96             Return DirectCast(ClassWriter(ini, GetType(T)), T)
97         End Function
98
99         Public Function ClassWriter(ini As IniFile, type As Type) As Object
100             Dim obj As Object = Activator.CreateInstance(type)
101             Dim maps = MapParser(type)
102
103             For Each map In maps.Value
104                 Dim key As String = map.field.Name
105                 Dim value As String = ini.ReadValue(maps.Name, key)
106                 Dim o As Object = Scripting.CTypeDynamic(value, map.Type)
107                 Call map.SetValue(obj, o)
108             Next
109
110             Return obj
111         End Function
112
113         <MethodImpl(MethodImplOptions.AggressiveInlining)>
114         <Extension>
115         Public Sub ClassDumper(Of T As Class)(x As T, ini As IniFile)
116             Call ClassDumper(x, GetType(T), ini)
117         End Sub
118
119         Public Sub ClassDumper(x As Object, type As Type, ini As IniFile)
120             Dim maps = MapParser(type)
121
122             For Each map In maps.Value
123                 Dim key As String = map.field.Name
124                 Dim value As String = Scripting.ToString(map.GetValue(x))
125                 Call ini.WriteValue(maps.Name, key, value)
126             Next
127         End Sub
128
129         ''' <summary>
130         ''' Load a ini section profile data from a ini file.
131         ''' </summary>
132         ''' <typeparam name="T">The section mapper</typeparam>
133         ''' <param name="path">*.ini file</param>
134         ''' <returns></returns>
135         ''' 
136         <MethodImpl(MethodImplOptions.AggressiveInlining)>
137         <Extension>
138         Public Function LoadIni(Of T As {New, Class})(path As StringAs T
139             Return New IniFile(path).ClassWriter(Of T)
140         End Function
141
142         ''' <summary>
143         ''' Write ini section into data file.
144         ''' </summary>
145         ''' <typeparam name="T"></typeparam>
146         ''' <param name="x">A section class in the ini profile file.</param>
147         ''' <param name="ini"></param>
148         ''' <returns></returns>
149         <Extension>
150         Public Function WriteClass(Of T As Class)(x As T, ini As StringAs Boolean
151             Try
152                 Call x.ClassDumper(New IniFile(ini))
153             Catch ex As Exception
154                 ex = New Exception(ini, ex)
155                 ex = New Exception(GetType(T).FullName, ex)
156                 Call ex.PrintException
157                 Call App.LogException(ex)
158                 Return False
159             End Try
160
161             Return True
162         End Function
163     End Module
164 End Namespace