1 #Region "Microsoft.VisualBasic::1698c0af0197c40b47299d63cceef721, Microsoft.VisualBasic.Core\Extensions\Reflection\Marshal\MarshalExtensions.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 MarshalExtensions
35     
36     '     Function: (+2 Overloads) MarshalAs
37     
38     ' /********************************************************************************/
39
40 #End Region
41
42 Imports System.Runtime.CompilerServices
43 Imports Microsoft.VisualBasic.Emit.Marshal
44
45 ''' <summary>
46 ''' 
47 ''' </summary>
48 Public Module MarshalExtensions
49
50     ''' <summary>
51     ''' Read unmanaged memory using memory pointer.
52     ''' </summary>
53     ''' <typeparam name="T">
54     ''' <see cref="Integer"/>, <see cref="Char"/>, <see cref="Short"/>, <see cref="Long"/>, <see cref="Single"/>, <see cref="Byte"/>, <see cref="IntPtr"/>, <see cref="Double"/>
55     ''' </typeparam>
56     ''' <param name="p"></param>
57     ''' <returns></returns>
58     ''' <remarks>
59     ''' ```vbnet
60     ''' Public Shared Sub Copy(source As IntPtr, destination() As Integer, startIndex As Integer, length As Integer)
61     ''' Public Shared Sub Copy(source As IntPtr, destination() As Char, startIndex As Integer, length As Integer)
62     ''' Public Shared Sub Copy(source As IntPtr, destination() As Short, startIndex As Integer, length As Integer)
63     ''' Public Shared Sub Copy(source As IntPtr, destination() As Long, startIndex As Integer, length As Integer)
64     ''' Public Shared Sub Copy(source As IntPtr, destination() As Single, startIndex As Integer, length As Integer)
65     ''' Public Shared Sub Copy(source As IntPtr, destination() As Byte, startIndex As Integer, length As Integer)
66     ''' Public Shared Sub Copy(source As IntPtr, destination() As IntPtr, startIndex As Integer, length As Integer)
67     ''' Public Shared Sub Copy(source As IntPtr, destination() As Double, startIndex As Integer, length As Integer)
68     ''' ```
69     ''' </remarks>
70     <Extension> Public Function MarshalAs(Of T)(p As System.IntPtr, chunkSize As IntegerAs IntPtr(Of T)
71         Select Case GetType(T)
72             Case GetType(Integer) : Return DirectCast(CType(New [Integer](p, chunkSize), Object), IntPtr(Of T))
73             Case GetType(Char) : Return DirectCast(CType(New [Char](p, chunkSize), Object), IntPtr(Of T))
74             Case GetType(Short) : Return DirectCast(CType(New [Short](p, chunkSize), Object), IntPtr(Of T))
75             Case GetType(Long) : Return DirectCast(CType(New [Long](p, chunkSize), Object), IntPtr(Of T))
76             Case GetType(Single) : Return DirectCast(CType(New [Single](p, chunkSize), Object), IntPtr(Of T))
77             Case GetType(Byte) : Return DirectCast(CType(New [Byte](p, chunkSize), Object), IntPtr(Of T))
78             Case GetType(System.IntPtr) : Return DirectCast(CType(New IntPtr(p, chunkSize), Object), IntPtr(Of T))
79             Case GetType(Double) : Return DirectCast(CType(New [Double](p, chunkSize), Object), IntPtr(Of T))
80             Case Else
81                 Throw New MemberAccessException(GetType(T).FullName & " is not a valid memory region!")
82         End Select
83     End Function
84
85     ''' <summary>
86     ''' 方便进行数组操作的一个函数
87     ''' </summary>
88     ''' <typeparam name="T"></typeparam>
89     ''' <param name="raw"></param>
90     ''' <param name="p"></param>
91     ''' <returns></returns>
92     <Extension> Public Function MarshalAs(Of T)(ByRef raw As T(), Optional p As System.IntPtr = NothingAs IntPtr(Of T)
93         Return New IntPtr(Of T)(raw, p)
94     End Function
95 End Module