1 #Region "Microsoft.VisualBasic::4adfc7297c815c5a00fbe65dd22b7df5, Microsoft.VisualBasic.Core\Language\Language\Perl\die.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 ExceptionHandle
35     
36     '         Function: [Default]
37     '         Operators: (+2 OverloadsOr
38     
39     
40     ' /********************************************************************************/
41
42 #End Region
43
44 Imports System.Drawing
45 Imports Microsoft.VisualBasic.Emit.Delegates
46 Imports Microsoft.VisualBasic.Language.Default
47
48 Namespace Language.Perl
49
50     ''' <summary>
51     ''' Syntax supports for perl language like exception handler
52     ''' 
53     ''' ```vbnet
54     ''' Dim value = func() or die("message")
55     ''' ```
56     ''' </summary>
57     Public Structure ExceptionHandle
58
59         Dim message$
60         Dim failure As Assert(Of Object)
61
62         Shared ReadOnly defaultHandler As New Assert(Of Object)(AddressOf [Default])
63
64         ''' <summary>
65         ''' ReturnTrue means test failure
66         ''' </summary>
67         ''' <param name="obj"></param>
68         ''' <returns></returns>
69         Public Shared Function [Default](obj As ObjectAs Boolean
70             If obj Is Nothing Then
71                 Return True
72             End If
73
74             Select Case obj.GetType
75                 Case GetType(Boolean)
76                     Return False = DirectCast(obj, Boolean)
77
78                     ' 对于字符串而言,判断是否为空的标准则是看字符串是否为空或者空字符串
79                 Case GetType(String)
80                     Return String.IsNullOrEmpty(DirectCast(obj, String))
81
82                 Case GetType(Double)
83                     Return CDbl(obj).IsNaNImaginary
84
85                 Case GetType(TimeSpan)
86                     Return DirectCast(obj, TimeSpan) = TimeSpan.Zero
87
88                 Case GetType(Color)
89                     Return DirectCast(obj, Color).IsEmpty
90
91                 Case GetType(Rectangle)
92                     Return DirectCast(obj, Rectangle).IsEmpty
93
94                 Case GetType(RectangleF)
95                     Return DirectCast(obj, RectangleF).IsEmpty
96
97                 Case GetType(Point)
98                     Return DirectCast(obj, Point).IsEmpty
99
100                 Case GetType(PointF)
101                     Return DirectCast(obj, PointF).IsEmpty
102
103                 Case Else
104
105                     With obj.GetType
106                         If .IsInheritsFrom(GetType(Array)) Then
107                             Return DirectCast(obj, Array).Length = 0
108
109                         ElseIf .ImplementInterface(GetType(IEnumerable)) Then
110                             Return (From o In DirectCast(obj, IEnumerable).AsQueryable).Count = 0
111
112                         ElseIf .ImplementInterface(GetType(IsEmpty)) Then
113                             Return DirectCast(obj, IsEmpty).IsEmpty
114
115                         Else
116                             False表示没有错误
117                             Return False
118                         End If
119                     End With
120             End Select
121         End Function
122
123         ''' <summary>
124         ''' Perl like exception handler syntax for testing the result is failure or not?
125         ''' </summary>
126         ''' <param name="result"></param>
127         ''' <param name="h"></param>
128         ''' <returns></returns>
129         Public Shared Operator Or(result As Object, h As ExceptionHandle) As Object
130             If h.failure(result) Then
131                 Throw New Exception(h.message)
132             Else
133                 Return result
134             End If
135         End Operator
136     End Structure
137 End Namespace