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/bstring.ab

    r335 r478  
    11' com/bstring.ab
    2 
    3 '#require <ole2.ab>
    4 '#require <oleauto.ab>
    52
    63Namespace ActiveBasic
     
    85
    96Class BString
    10     'Inherits System.IDisposable, System.ICloneable
     7    Implements System.IDisposable ', System.ICloneable
    118Public
    129    Sub BString()
     
    1916
    2017    Sub BString(s As BString)
    21         BString.Copy(This.bs, s.bs)
    22     End Sub
    23 
    24     Sub BString(s As LPCOLESTR)
    25         bs = SysAllocString(s)
     18        If Not IsNothing(s) Then
     19            bs = copy(s.bs)
     20        End If
    2621    End Sub
    2722
    2823    Sub BString(s As LPCOLESTR, len As DWord)
    29         bs = SysAllocStringLen(s, len)
    30     End Sub
    31 
    32     Sub BString(s As PCSTR)
    33         Init(s, lstrlenA(s))
    34     End Sub
    35 
    36     Sub BString(s As PCSTR, len As DWord)
    37         Dim lenBS = MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, 0, 0)
    38         bs = SysAllocStringLen(0, lenBS)
    39         MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, bs, lenBS)
     24        If s <> 0 Then
     25            bs = SysAllocStringLen(s, len)
     26        End If
    4027    End Sub
    4128
    4229    Sub BString(s As String)
    43         Init(s.StrPtr, s.Length As DWord)
    44     End Sub
     30        If Not IsNothing(s) Then
     31            Init(s.StrPtr, s.Length As DWord)
     32        End If
     33    End Sub
     34
     35    Static Function FromBStr(bs As BSTR) As BString
     36        FromBStr = New BString(bs, SysStringLen(bs))
     37    End Function
     38
     39    Static Function FromCStr(s As PCWSTR) As BString
     40        If s <> 0 Then
     41            FromCStr = New BString(s, lstrlenW(s))
     42        Else
     43            FromCStr = New BString
     44        End If
     45    End Function
     46
     47    Static Function FromCStr(s As PCWSTR, len As DWord) As BString
     48        If s <> 0 Then
     49            FromCStr = New BString(s, len)
     50        Else
     51            FromCStr = New BString
     52        End If
     53    End Function
     54
     55    Static Function FromCStr(s As PCSTR) As BString
     56        Dim dst As PCWSTR
     57        Dim lenW = GetStr(s, dst)
     58        FromCStr = FromCStr(s, lenW)
     59    End Function
     60
     61    Static Function FromCStr(s As PCSTR, len As DWord) As BString
     62        Dim dst As PCWSTR
     63        Dim lenW = GetStr(s, len, dst)
     64        FromCStr = FromCStr(s, lenW)
     65    End Function
    4566
    4667    Sub ~BString()
     
    4869    End Sub
    4970
    50     Sub Assign(bstr As BString)
    51         Clear()
    52         BString.Copy(This.bs, bstr.bs)
    53     End Sub
    54 
    55     Sub Assign(s As LPCOLESTR)
    56         Clear()
    57         s = SysAllocString(s)
    58     End Sub
    59 
    60     Sub AssignFromBStr(bstr As BSTR)
    61         Clear()
    62         BString.Copy(bs, bstr)
    63     End Sub
    64 
    6571    Const Function Copy() As BSTR
    66         BString.Copy(Copy, bs)
     72        Copy = copy(bs)
    6773    End Function
    6874
     
    7682
    7783    Sub Clear()
    78         If bs <> 0 Then
    79             SysFreeString(bs)
    80             bs = 0
    81         End If
     84        reset(0)
    8285    End Sub
    8386
    8487    Sub Attach(ByRef bstr As BSTR)
    85         Clear()
    86         BString.Move(bs, bstr)
     88        reset(move(bstr))
    8789    End Sub
    8890
    8991    Function Detach() As BSTR
    90         BString.Move(Detach, bs)
     92        Detach = move(bs)
    9193    End Function
    9294
    9395    Function BStr() As BSTR
    9496        BStr = bs
    95     End Function
    96 /*
    97     Static Function Assgin(bs As BSTR) As BString
    98         Assgin = New BString
    99         Assgin.Assgin(bs)
    10097    End Function
    10198
     
    104101        Attach.Attach(bs)
    105102    End Function
    106 */
     103
    107104    Const Function Length() As DWord
    108         Length = SysStringLen(bs)
     105        Length = GetDWord(bs As VoidPtr - SizeOf (DWord)) 'SysStringLen(bs)
    109106    End Function
    110107
    111108    Const Function Operator [](i As SIZE_T) As OLECHAR
    112 #ifdef _DEBUG
    113109        If i > Length Then
    114             'Throw OutOfRangeException
    115         End If
    116 #endif
     110            Throw New ArgumentOutOfRangeException("i")
     111        End If
    117112        Return bs[i]
    118113    End Function
    119114
    120115    Sub Operator []=(i As SIZE_T, c As OLECHAR)
    121 #ifdef _DEBUG
    122116        If i > Length Then
    123             'Throw OutOfRangeException
    124         End If
    125 #endif
     117            Throw New ArgumentOutOfRangeException("i")
     118        End If
    126119        bs[i] = c
    127120    End Sub
     
    135128    End Function
    136129
     130    Override Function Equals(o As Object) As Boolean
     131        If Not IsNothing(o) Then
     132            If This.GetType().Equals(o.GetType()) Then
     133                Equals(o As BString)
     134            End If
     135        End If
     136    End Function
     137
     138    Const Function Equals(s As BString) As Boolean
     139        Equals = Compare(This, s) = 0
     140    End Function
     141
     142    Static Function Compare(l As BString, r As BString) As Long
     143        If IsNullOrEmpty(l) Then
     144            If IsNullOrEmpty(r) Then
     145                Compare = 0
     146            Else
     147                Compare = -1
     148            End If
     149        Else
     150            If IsNullOrEmpty(bsr) Then
     151                Compare = 1
     152            Else
     153                Compare = Strings.ChrCmp(l.bs, l.Length As SIZE_T, r.bs, r.Length As SIZE_T)
     154            End If
     155        End If
     156    End Function
     157
     158    Static Function IsNullOrEmpty(s As BString)
     159        If IsNothing(s) Then
     160            IsNullOrEmpty = True
     161        ElseIf s.bs = 0 Then
     162            IsNullOrEmpty = True
     163        ElseIf s.Length = 0 Then
     164            IsNullOrEmpty = True
     165        Else
     166            IsNullOrEmpty = False
     167        End If
     168    End Function
     169
     170    Function Operator ==(s As BString) As Boolean
     171        Return Compare(This, s) = 0
     172    End Function
     173
     174    Function Operator <>(s As BString) As Boolean
     175        Return Compare(This, s) <> 0
     176    End Function
     177
     178    Function Operator <(s As BString) As Boolean
     179        Return Compare(This, s) < 0
     180    End Function
     181
     182    Function Operator <=(s As BString) As Boolean
     183        Return Compare(This, s) <= 0
     184    End Function
     185
     186    Function Operator >(s As BString) As Boolean
     187        Return Compare(This, s) > 0
     188    End Function
     189
     190    Function Operator >=(s As BString) As Boolean
     191        Return Compare(This, s) >= 0
     192    End Function
     193
    137194Private
    138195    bs As BSTR
    139196
    140     Sub Init(s As PCSTR, len As DWord)
    141         Dim lenBS = MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, 0, 0)
    142         bs = SysAllocStringLen(0, lenBS)
    143         MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, bs, lenBS)
    144     End Sub
    145 
    146     Static Sub Copy(ByRef dst As BSTR, ByVal src As BSTR)
    147         dst = SysAllocStringLen(src, SysStringLen(src))
    148     End Sub
    149 
    150     Static Sub Move(ByRef dst As BSTR, ByRef src As BSTR)
    151         dst = src
    152         src = 0
    153     End Sub
     197    Sub init(s As PCSTR, len As DWord)
     198        If <> 0 Then
     199            Dim lenBS = MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, 0, 0)
     200            bs = SysAllocStringLen(0, lenBS)
     201            If bs <> 0 Then
     202                MultiByteToWideChar(CP_THREAD_ACP, 0, s, len As Long, bs, lenBS)
     203            End If
     204        End If
     205    End Sub
     206
     207    Sub reset(newBS As BSTR)
     208        Dim old = InterlockedExchangePointer(bs, newBS)
     209        SysFreeString(old)
     210    End Sub
     211
     212    Static Function copy(src As BSTR) As BSTR
     213        copy = SysAllocStringLen(src, SysStringLen(src))
     214    End Function
     215
     216    Static Function move(ByRef src As BSTR) As BSTR
     217        move = InterlockedExchangePointer(src, 0)
     218    End Function
    154219End Class
    155220
Note: See TracChangeset for help on using the changeset viewer.