| 1 | #Region "Microsoft.VisualBasic::e13c5c435b8b1dcbc99d5e63901c9f41, Microsoft.VisualBasic.Core\ComponentModel\ValuePair\Binding.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 | ' Structure Binding |
| 35 | ' |
| 36 | ' Properties: IsEmpty |
| 37 | ' |
| 38 | ' Constructor: (+1 Overloads) Sub New |
| 39 | ' Function: ToString, Tuple, ValueTuple |
| 40 | ' |
| 41 | ' |
| 42 | ' /********************************************************************************/ |
| 43 | |
| 44 | #End Region |
| 45 | |
| 46 | Imports System.Runtime.CompilerServices |
| 47 | Imports Microsoft.VisualBasic.Language.Default |
| 48 | |
| 49 | Namespace ComponentModel |
| 50 | |
| 51 | ''' <summary> |
| 52 | ''' Functioning the same as the <see cref="KeyValuePair(Of T, K)"/>, but with more specific on the name. |
| 53 | ''' <see cref="KeyValuePair(Of T, K)"/> its name is too generic. |
| 54 | ''' (作用与<see cref="KeyValuePair(Of T, K)"/>类似,只不过类型的名称更加符合绑定的描述) |
| 55 | ''' </summary> |
| 56 | ''' <typeparam name="T"></typeparam> |
| 57 | ''' <typeparam name="K"></typeparam> |
| 58 | Public Structure Binding(Of T, K) |
| 59 | Implements IsEmpty |
| 60 | |
| 61 | Dim Bind As T |
| 62 | Dim Target As K |
| 63 | |
| 64 | ''' <summary> |
| 65 | ''' If the field <see cref="Bind"/> and <see cref="Target"/> are both nothing, then this binding is empty. |
| 66 | ''' (当<see cref="Bind"/>以及<see cref="Target"/>都同时为空值的时候这个参数才会为真) |
| 67 | ''' </summary> |
| 68 | ''' <returns></returns> |
| 69 | Public ReadOnly Property IsEmpty As Boolean Implements IsEmpty.IsEmpty |
| 70 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 71 | Get |
| 72 | Return Bind Is Nothing AndAlso Target Is Nothing |
| 73 | End Get |
| 74 | End Property |
| 75 | |
| 76 | Sub New(source As T, target As K) |
| 77 | Me.Bind = source |
| 78 | Me.Target = target |
| 79 | End Sub |
| 80 | |
| 81 | Public Overrides Function ToString() As String |
| 82 | If IsEmpty Then |
| 83 | Return "No binding" |
| 84 | Else |
| 85 | Return Bind.ToString & " --> " & Target.ToString |
| 86 | End If |
| 87 | End Function |
| 88 | |
| 89 | ''' <summary> |
| 90 | ''' Convert this binding to tuple |
| 91 | ''' </summary> |
| 92 | ''' <returns></returns> |
| 93 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 94 | Public Function Tuple() As Tuple(Of T, K) |
| 95 | Return Me |
| 96 | End Function |
| 97 | |
| 98 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 99 | Public Function ValueTuple() As (bind As T, target As K) |
| 100 | Return (Bind, Target) |
| 101 | End Function |
| 102 | |
| 103 | ''' <summary> |
| 104 | ''' Implicit convert this binding as the <see cref="System.Tuple(Of T, K)"/> |
| 105 | ''' </summary> |
| 106 | ''' <param name="b"></param> |
| 107 | ''' <returns></returns> |
| 108 | ''' |
| 109 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 110 | Public Shared Narrowing Operator CType(b As Binding(Of T, K)) As Tuple(Of T, K) |
| 111 | Return New Tuple(Of T, K)(b.Bind, b.Target) |
| 112 | End Operator |
| 113 | End Structure |
| 114 | End Namespace |