1 #Region "Microsoft.VisualBasic::d3327dc28b44d692c05162a07c48695c, Microsoft.VisualBasic.Core\ComponentModel\Settings\Settings.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     '     Class Settings
35     
36     '         Properties: SettingsData
37     
38     '         Constructor: (+2 OverloadsSub New
39     '         Function: __createSave, CreateEmpty, Load, LoadFile
40     
41     
42     ' /********************************************************************************/
43
44 #End Region
45
46 Imports System.Runtime.CompilerServices
47
48 #If NET_40 = 0 Then
49
50 Namespace ComponentModel.Settings
51
52     Public Class Settings(Of T As {New, IProfile}) : Inherits ConfigEngine
53         Implements IDisposable
54
55         ''' <summary>
56         ''' The target object instance that provides the data source for this config engine.
57         ''' </summary>
58         ''' <returns></returns>
59         Public ReadOnly Property SettingsData As T
60             <MethodImpl(MethodImplOptions.AggressiveInlining)>
61             Get
62                 Return DirectCast(MyBase.settingsData, T)
63             End Get
64         End Property
65
66         Sub New()
67         End Sub
68
69         ''' <summary>
70         ''' 从配置数据的实例对象创建配置映射
71         ''' </summary>
72         ''' <param name="config"></param>
73         ''' 
74         <MethodImpl(MethodImplOptions.AggressiveInlining)>
75         Sub New(config As T)
76             Call MyBase.New(config)
77         End Sub
78
79         ''' <summary>
80         '''
81         ''' </summary>
82         ''' <param name="XmlFile">目标配置文件的Xml文件的文件名</param>
83         ''' <returns>可以调用的配置项的数目,解析失败则返回0</returns>
84         ''' <remarks></remarks>
85         Public Shared Function LoadFile(XmlFile As StringOptional CreateSave As Action(Of T, String) = NothingAs Settings(Of T)
86             If Not XmlFile.FileExists Then
87                 Return __createSave(XmlFile, CreateSave)
88             Else
89                 Dim File As T = XmlFile.LoadXml(Of T)(throwEx:=False)
90
91                 If File Is Nothing Then
92                     Return __createSave(XmlFile, CreateSave)
93                 Else
94                     Return Load(Linq.SetValue(Of T).InvokeSet(File, NameOf(File.FilePath), XmlFile))
95                 End If
96             End If
97         End Function
98
99         Private Shared Function __createSave(xml As String, createSave As Action(Of T, String)) As Settings(Of T)
100             Dim FileObject As T = DirectCast(Activator.CreateInstance(Of T)(), T)
101             FileObject.FilePath = xml
102             If Not createSave Is Nothing Then
103                 Call createSave(FileObject, xml)
104             End If
105             Return Load(Data:=FileObject)
106         End Function
107
108         ''' <summary>
109         ''' 使用<see cref="ProfileItem"/>来标记想要作为变量的属性
110         ''' </summary>
111         ''' <param name="Data"></param>
112         ''' <returns></returns>
113         ''' 
114         <MethodImpl(MethodImplOptions.AggressiveInlining)>
115         Public Overloads Shared Function Load(Data As T) As Settings(Of T)
116             Return New Settings(Of T)(Data)
117         End Function
118
119         <MethodImpl(MethodImplOptions.AggressiveInlining)>
120         Public Shared Function CreateEmpty() As Settings(Of T)
121             Return New Settings(Of T)(Activator.CreateInstance(Of T))
122         End Function
123
124         <MethodImpl(MethodImplOptions.AggressiveInlining)>
125         Public Overloads Shared Narrowing Operator CType(Settings As Settings(Of T)) As T
126             Return Settings.SettingsData
127         End Operator
128     End Class
129 End Namespace
130 #End If