Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
imihito committed Aug 20, 2017
1 parent c025e9a commit 01c0551
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 58 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@

bin/~$LinqVBA.xlsm
77 changes: 35 additions & 42 deletions src/CallByFunc.cls
Expand Up @@ -13,73 +13,66 @@ Implements IFunc
Option Explicit

'スコープ適当。Argumentsをどんな形で見せるか…
Private clsMemberName As String
Private clsCallType As VBA.VbCallType
Private clsArguments() As Variant
Private clsChildCallBack As IFunc
Private memberName_ As String
Private callType_ As VBA.VbCallType
Private arguments_() As Variant
Private childFunc_ As IFunc

Property Get MemberName() As String: Let MemberName = clsMemberName: End Property
Property Get CallType() As VBA.VbCallType: Let CallType = clsCallType: End Property
Function Arguments() As Variant: Let Arguments = clsArguments: End Function
Property Get ChildCallBack() As IFunc: Set ChildCallBack = clsChildCallBack: End Property
Property Set ChildCallBack(ByVal iCB As IFunc): Set clsChildCallBack = iCB: End Property
Property Get MemberName() As String: Let MemberName = memberName_: End Property
Property Get CallType() As VBA.VbCallType: Let CallType = callType_: End Property
Property Get Arguments() As Variant: Let Arguments = arguments_: End Property
Property Get ChildFunc() As IFunc: Set ChildFunc = childFunc_: End Property
Property Set ChildFunc(ByVal func As IFunc): Set childFunc_ = func: End Property

'コンストラクタ・既定のプロシージャ
'既定のインスタンスを使って
'CallByFunc( ~
'と使用することを想定


Public Function Init( _
ByVal iMemeberName As String, _
ByVal iCallType As VBA.VbCallType, _
ParamArray iArgs() As Variant _
Friend Function Init( _
ByVal iMemeberName As String, _
ByVal iCallType As VBA.VbCallType, _
ByRef iArgs() As Variant _
) As CallByFunc
Attribute Init.VB_UserMemId = 0
'

'Attribute Init.VB_UserMemId = 0
If Me Is CallByFunc Then
Dim callArgs() As Variant
ReDim callArgs(LBound(iArgs) To UBound(iArgs) + 2)
callArgs(0) = iMemeberName: callArgs(1) = iCallType
Dim i As Long
For i = 2 To UBound(callArgs)
callArgs(i) = iArgs(i - 2)
Next i

Dim tCallByFunc As CallByFunc
Set tCallByFunc = New CallByFunc
Set Init = CallByNameEx(tCallByFunc, "Init", VbMethod, callArgs)
Exit Function
With New CallByFunc
Set Init = .Init(iMemeberName, iCallType, iArgs)
End With 'New CallByFunc
Else

Static alreadyInit As Boolean
If alreadyInit Then Err.Raise 17
If alreadyInit Then _
ThrowLINQ InvalidOperationException

Let clsMemberName = iMemeberName
Let clsCallType = iCallType
Let clsArguments = iArgs
Let memberName_ = iMemeberName
Let callType_ = iCallType
Let arguments_ = iArgs

Let alreadyInit = True

Set Init = Me
End If
End Function

Function SetChild(ByVal callBack As IFunc) As CallByFunc
Set clsChildCallBack = callBack
Function SetChild(ByVal func As IFunc) As CallByFunc
Set childFunc_ = func
Set SetChild = Me
End Function

Function RemoveChild() As CallByFunc
Set childFunc_ = Nothing
Set RemoveChild = Me
End Function

Function AsIFunc() As IFunc
Set AsIFunc = Me
End Function

Private Function IFunc_Exec(iElement As Variant) As Variant
Dim tmp As Variant
AssignVal tmp, CallByNameEx(iElement, clsMemberName, clsCallType, clsArguments)
AssignVal tmp, CallByNameEx(iElement, memberName_, callType_, arguments_)

If clsChildCallBack Is Nothing Then
If childFunc_ Is Nothing Then
AssignVal IFunc_Exec, tmp
Else
AssignVal IFunc_Exec, clsChildCallBack.Exec(tmp)
AssignVal IFunc_Exec, childFunc_.Exec(tmp)
End If
End Function

Expand Down
9 changes: 3 additions & 6 deletions src/CompareOperator.cls
Expand Up @@ -39,19 +39,16 @@ Public Expression As Variant

Private clsRegExp As VBScript_RegExp_55.RegExp


Public Function Init( _
Friend Function Init( _
ByVal iOperator As CompareOperators, _
ByVal iExpresion As Variant _
) As CompareOperator
Attribute Init.VB_UserMemId = 0
'Attribute Init.VB_UserMemId = 0



If Me Is CompareOperator Then
With New CompareOperator
Set Init = .Init(iOperator, iExpresion)
End With
End With 'New CompareOperator
Exit Function
End If

Expand Down
109 changes: 99 additions & 10 deletions src/Enumerable.cls
Expand Up @@ -9,40 +9,129 @@ Attribute VB_PredeclaredId = True
Attribute VB_Exposed = True
Option Explicit

Private clsCollection As VBA.Collection
Private sequence_ As VBA.Collection

Public Function From(ByVal iEnumerable As Variant) As Enumerable
Attribute From.VB_UserMemId = 0
'Attribute From.VB_UserMemId = 0
If Me Is Enumerable Then
With New Enumerable
Set From = .From(iEnumerable)
End With 'New Enumerable
Exit Function
End If

If TypeOf iEnumerable Is VBA.Collection Then
Set clsCollection = iEnumerable
Set sequence_ = iEnumerable
Else
Set clsCollection = New VBA.Collection
Set sequence_ = New VBA.Collection
Dim iter As Variant
For Each iter In iEnumerable
clsCollection.Add iter
sequence_.Add iter
Next iter
End If

Set From = Me
End Function

Public Function Where(ByVal iPredicate As IFunc) As Enumerable
'Select‚ª—\–ñŒê‚È‚Ì‚ÅSelect1
'Select is reserved as VBA keyword.
Public Function Select1(ByVal func As IFunc) As Enumerable
Dim newCol As VBA.Collection: Set newCol = New VBA.Collection

Dim iter As Variant
For Each iter In sequence_
newCol.Add func.Exec(iter)
Next iter

Set Select1 = Enumerable.From(newCol)
End Function

Public Function SelectMany(ByVal func As IFunc) As Enumerable
Dim newCol As VBA.Collection: Set newCol = New VBA.Collection

Dim iter As Variant, childIter As Variant
For Each iter In sequence_
For Each childIter In func.Exec(iter)
newCol.Add childIter
Next childIter
Next iter

Set SelectMany = Enumerable.From(newCol)
End Function

Public Sub ForEach(ByVal func As IFunc)
Dim iter As Variant
For Each iter In sequence_
Call func.Exec(iter)
Next iter
End Sub

Public Function Where(ByVal predicate As IFunc) As Enumerable
Dim newCol As VBA.Collection: Set newCol = New VBA.Collection

Dim iter As Variant
For Each iter In sequence_
If predicate.Exec(iter) Then _
newCol.Add iter

Next iter

Set Where = Enumerable.From(newCol)
End Function

Public Function OfType(ByVal iTypeName As String) As Enumerable
Dim newCol As VBA.Collection: Set newCol = New VBA.Collection

Dim iter As Variant
For Each iter In clsCollection
If iPredicate.Exec(iter) Then
For Each iter In sequence_
If VBA.TypeName(iter) Like iTypeName Then _
newCol.Add iter
End If

Next iter

Set OfType = Enumerable.From(newCol)
End Function

Public Function OrderBy(ByVal func As IFunc) As Enumerable
Set OrderBy = Enumerable.From(MergeSort(sequence_, func, True))
End Function

Public Function OrderByDescending(ByVal func As IFunc) As Enumerable
Set OrderByDescending = Enumerable.From(MergeSort(sequence_, func, False))
End Function

Public Function ToCollection() As VBA.Collection
Dim newCol As VBA.Collection: Set newCol = New VBA.Collection

Dim iter As Variant
For Each iter In sequence_
newCol.Add iter
Next iter

Set Where = LinqFrom(newCol)
Set ToCollection = newCol

End Function

Public Function Count(Optional ByVal predicate As IFunc) As Long
If predicate Is Nothing Then
Let Count = sequence_.Count
Exit Function
End If

Dim iter As Variant, cnt As Long
For Each iter In sequence_
If predicate.Exec(iter) Then _
cnt = cnt + 1

Next iter

Let Count = cnt

End Function

Public Function GetEnumerator() As stdole.IEnumVARIANT
Attribute GetEnumerator.VB_UserMemId = -4
'Attribute GetEnumerator.VB_UserMemId = -4
Set GetEnumerator = clsCollection.[_NewEnum]
Set GetEnumerator = sequence_.[_NewEnum]
End Function
16 changes: 16 additions & 0 deletions src/IFunc.cls
@@ -0,0 +1,16 @@
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "IFunc"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
'JPN:汎用コールバック用インターフェイス
Option Explicit

'JPN:何かしらの処理を実行する
Public Function Exec(iElement As Variant) As Variant
Attribute Exec.VB_UserMemId = 0
End Function

0 comments on commit 01c0551

Please sign in to comment.