Ignore:
Timestamp:
Mar 13, 2008, 9:06:43 PM (17 years ago)
Author:
イグトランス (egtra)
Message:

現在向けに修正(参照型のポインタの排除など)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Include/com/variant.ab

    r355 r478  
    11' com/variant.ab
    22
    3 #ifndef _COM_VARIANT_AB
    4 #define _COM_VARIANT_AB
    5 
    6 '#require <oaidl.ab>
    7 '#require <oleauto.ab>
    83'#require <com/index.ab>
    94
     
    7974    Sub Variant(bs As BString)
    8075        v.vt = VT_BSTR
    81         SetPointer(VarPtr(v.val), SysAllocStringLen(bs.BStr, bs.Length))
    82     End Sub
    83 
    84     Sub Variant(p As *IUnknown)
    85         p->AddRef()
     76        SetPointer(VarPtr(v.val), bs.Copy))
     77    End Sub
     78
     79    Sub Variant(unk As IUnknown)
     80        If Not IsNothing(unk) Then unk.AddRef()
    8681        v.vt = VT_UNKNOWN
    87         SetPointer(VarPtr(v.val), p)
    88     End Sub
    89 
    90     Sub Variant(p As *IDispatch)
    91         p->AddRef()
     82        SetPointer(VarPtr(v.val), ObjPtr(unk))
     83    End Sub
     84
     85    Sub Variant(disp As IDispatch)
     86        If Not IsNothing(disp) Then disp.AddRef()
    9287        v.vt = VT_DISPATCH
    93         SetPointer(VarPtr(v.val), p)
     88        SetPointer(VarPtr(v.val), ObjPtr(disp))
    9489    End Sub
    9590/*
     
    284279
    285280    Static Function Compare(x As Variant, y As Variant, lcid As LCID, flags As DWord) As HRESULT
    286         Return VarCmp(x.v, y.v, lcid, flags)
     281        Return VarCmp(removeNull(x).v, removeNull(y).v, lcid, flags)
    287282    End Function
    288283
     
    290285        Return VarCmp(x.v, y.v, LOCALE_USER_DEFAULT, 0) 'VARCMP_NULL = 3を返す場合があるので注意
    291286    End Function
    292 /*
     287
    293288    Const Function Operator ==(y As Variant) As Boolean
    294289        Dim c = Compare(This, y)
    295         If c = VARCMP_EQ Then
    296             Return True
    297         Else
    298             Return False
    299         End If
     290        Return c = VARCMP_EQ
    300291    End Function
    301292
    302293    Const Function Operator <>(y As Variant) As Boolean
    303294        Dim c = Compare(This, y)
    304         If c <> VARCMP_EQ Then
    305             Return True
    306         Else
    307             Return False
    308         End If
     295        Return c <> VARCMP_EQ
    309296    End Function
    310297
    311298    Const Function Operator <(y As Variant) As Boolean
    312299        Dim c = Compare(This, y)
    313         If c = VARCMP_LT Then
    314             Return True
    315         Else
    316             Return False
    317         End If
    318     End Function
    319 /*
     300        Return c = VARCMP_LT
     301    End Function
     302
    320303    Const Function Operator >(y As Variant) As Boolean
    321304        Dim c = Compare(This, y)
    322         If c = VARCMP_GT Then
    323             Return True
    324         Else
    325             Return False
    326         End If
    327     End Function
    328 */
    329 /*
     305        Return c = VARCMP_GT
     306    End Function
     307
    330308    Const Function Operator <=(y As Variant) As Boolean
    331309        Dim c = Compare(This, y)
    332         If c = VARCMP_LT Or c = VARCMP_EQ Then
    333             Return True
    334         Else
    335             Return False
    336         End If
     310        Return c = VARCMP_LT Or c = VARCMP_EQ
    337311    End Function
    338312
    339313    Const Function Operator >=(y As Variant) As Boolean
    340314        Dim c = Compare(This, y)
    341         If c = VARCMP_GT Or c = VARCMP_EQ Then
    342             Return True
    343         Else
    344             Return False
    345         End If
    346     End Function
    347 */
     315        Return c = VARCMP_GT Or c = VARCMP_EQ
     316    End Function
     317
    348318    Const Function ChangeType(vt As VARTYPE, flags As Word) As Variant
    349         Dim ret = New Variant
    350         ChangeType(ret, flags, vt)
    351         Return ret
     319        ChangeType = New Variant
     320        ChangeType(ChangeType, flags, vt)
    352321    End Function
    353322
     
    369338
    370339    Override Function ToString() As String
    371         Dim tmp = ChangeType(VT_BSTR, VARIANT_ALPHABOOL)
    372         Dim bs = ((tmp.v.val As ULONG_PTR) As BSTR)
    373         Return New String(bs As PCWSTR, SysStringLen(bs) As Long)
     340        /*Using*/ Dim bs = ValStr
     341        ToString = bs.ToString
     342        bstr.Dispose() 'End Using
    374343    End Function
    375344
     
    526495
    527496    Const Function ValStr() As BString
     497        ValStr = New BString
    528498        Dim r As VARIANT
    529499        ChangeType(r, VARIANT_ALPHABOOL, VT_BSTR)
    530         Dim bs = New BString
    531         bs.Attach(GetPointer(VarPtr(r.val)) As BSTR)
    532         Return bs
     500        ValStr.Attach(GetPointer(VarPtr(r.val)) As BSTR)
    533501    End Function
    534502
     
    536504        Clear()
    537505        v.vt = VT_BSTR
    538         SetPointer(VarPtr(v.val), x.Copy())
    539     End Sub
    540 
    541     Const Function ValUnknown() As *IUnknown
     506        If IsNothing(x) Then
     507            SetPointer(VarPtr(v.val), SysAllocStringLen(0))
     508        Else
     509            SetPointer(VarPtr(v.val), x.Copy())
     510        End If
     511    End Sub
     512
     513    Const Function ValUnknown() As IUnknown
    542514        Dim r As VARIANT
    543515        ChangeType(r, 0, VT_UNKNOWN)
    544         Return GetPointer(VarPtr(r.val)) As *IUnknown
    545     End Function
    546 
    547     Sub ValUnknown(x As *IUnknown)
    548         Clear()
    549         SetPointer(VarPtr(v.val), x)
    550         x->AddRef()
     516        Return _System_PtrUnknown(r.val As ULONG_PTR As VoidPtr)
     517    End Function
     518
     519    Sub ValUnknown(x As IUnknown)
     520        Clear()
     521        SetPointer(VarPtr(v.val), ObjPtr(x))
     522        If Not IsNothing(x) Then
     523            x.AddRef()
     524        End If
    551525        v.vt = VT_UNKNOWN
    552526    End Sub
     
    587561
    588562    Static Function OptionalParam() As Variant
    589 '       If _System_VariantOptionalParam = Nothing Then
    590 '           'ToDo マルチスレッド対応
    591             VariantOptionalParam = New Variant
    592             VariantOptionalParam.ValError = DISP_E_PARAMNOTFOUND
    593 '       End If
    594         Return VariantOptionalParam
     563        If IsNothing(optionalParam) Then
     564            Dim t = New Variant
     565            t.ValError = DISP_E_PARAMNOTFOUND
     566            InterlockedCompareExchangePointer(ByVal VarPtr(optionalParam), ObjPtr(t), 0)
     567        End If
     568        Return optionalParam
     569    End Function
     570
     571    Static Function Null() As Variant
     572        If IsNothing(optionalParam) Then
     573            Dim t = New Variant
     574            Dim p = t.PtrToVariant
     575            p->vt = VT_NULL
     576            InterlockedCompareExchangePointer(ByVal VarPtr(optionalParam), ObjPtr(t), 0)
     577        End If
     578        Return optionalParam
    595579    End Function
    596580Private
     
    605589'       src.vt = VT_EMPTY
    606590    End Sub
     591
     592    Static Function removeNull(v As Variant) As Varinat
     593        If IsNothing(v) Then
     594            removeNull = Null
     595        Else
     596            removeNull = v
     597        End If
     598    End Function
     599
     600    Static optionalParam = Nothing As Variant
     601    Static null = Nothing As Variant
    607602End Class
    608 
    609 Dim VariantOptionalParam = Nothing As Variant
    610603
    611604/*
     
    629622End Namespace 'COM
    630623End Namespace 'ActiveBasic
    631 
    632 #endif '_COM_VARIANT_AB
Note: See TracChangeset for help on using the changeset viewer.