Changeset 195 for Include/Classes


Ignore:
Timestamp:
Mar 30, 2007, 4:22:30 AM (18 years ago)
Author:
dai
Message:

オブジェクトの循環参照を許容した(構造体はダメ)。
抽象クラスをメンバの型に指定できるようにした。
メンバがオブジェクトだったとき、自動的にNewするのをやめ、初期値としてNothingを指定するようにした。

【ArrayListの改良】
・ArrayList_Elementを廃止し、実装をArrayListのprivateに置いた。
・一通りのパラメータを*ObjectからObjectへ変更した。

【その他】
・TypeInfo改良中...
・Objectクラスに実行時型情報用のtypeInfoメンバを追加した。

Location:
Include/Classes/System
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • Include/Classes/System/Collections/ArrayList.ab

    r188 r195  
    55Class ArrayList
    66    Inherits IList /*, ICollection, IEnumerable, ICloneable*/
     7
     8    pObject As *Object
     9    size As Long
     10    capacity As Long
     11
     12    Sub Init( size As Long )
     13        If size < 0 Then
     14            ' Error
     15            debug
     16        End If
     17
     18        This.size = size
     19        This.capacity = size
     20
     21        pObject = GC_malloc( SizeOf(*Object) * size + 1 )
     22        If pObject = 0 Then
     23            ' OutOfMemoryException
     24            Debug
     25        End If
     26    End Sub
     27
     28    Sub Realloc( size As Long )
     29        If size > capacity Then
     30            ' miss!
     31            Debug
     32        End If
     33
     34        pObject = realloc( pObject, SizeOf(*Object) * size + 1  )
     35        If pObject = 0 Then
     36            ' OutOfMemoryException
     37            Debug
     38        End If
     39    End Sub
     40
     41    Sub SetLeastCapacity( capacity As Long )
     42        If This.capacity < capacity Then
     43            This.capacity = capacity
     44            Realloc( capacity )
     45        End If
     46    End Sub
     47
    748Public
    8     ' --------------------------------
     49
     50    '----------------------------------------------------------------
    951    ' Properties
     52    '----------------------------------------------------------------
     53
    1054    /*Const*/ Virtual Function Capacity() As Long
    11         Return data.Capacity
     55        Return capacity
    1256    End Function
    1357
    1458    Virtual Sub Capacity(c As Long)
    15         If c <> data.Capacity Then
    16             If c < data.Size Then
     59        If c <> capacity Then
     60            If c < size Then
    1761                'Throw ArgumentOutOfRangeException
    1862                Debug
    1963            Else
    20                 reallocBuffer(c)
     64                Realloc(c)
    2165            End If
    2266        End If
     
    2468
    2569    /*Const*/ /*Override*/ Virtual Function Count() As Long
    26         Return data.Size
     70        Return size
    2771    End Function
    2872
     
    4185
    4286    ' Operators
    43     /*Const*/ /*Override*/ Virtual Function Operator [](i As Long) As *Object
    44         If 0 <= i And i < data.Size Then
    45             Return data.Elm[i]
     87    /*Const*/ /*Override*/ Virtual Function Operator [](index As Long) As Object
     88        If 0 <= index And index < size Then
     89            Return pObject[index]
    4690        Else
    4791            'Throw OutOfRangeException?
     
    5094    End Function
    5195   
    52     /*Override*/ Virtual Sub Operator []=(i As Long, x As *Object)
    53         If 0 <= i And i < data.Size Then
    54             data.Elm[i] = 0
     96    /*Override*/ Virtual Sub Operator []=(index As Long, object As Object)
     97        If 0 <= index And index < size Then
     98            pObject[i] = object
    5599        Else
    56100            'Throw OutOfRangeException?
     
    59103    End Sub
    60104
    61     ' --------------------------------
     105
     106
     107    '----------------------------------------------------------------
    62108    ' Methods
     109    '----------------------------------------------------------------
     110
    63111    '   Constractors
    64112    Sub ArrayList()
    65         data.Init(16)
     113        Init( 0 )
    66114    End Sub
    67115/*
     
    72120*/
    73121    Sub ArrayList(c As Long)
    74         data.Init(c)
     122        Init( c )
    75123    End Sub
    76124
     
    81129'   Sub Operator =(ByRef a As ArrayList)
    82130
    83     /*Override*/ Virtual Function Add(x As *Object) As Long
    84         setLeastCapacity(data.Size + 1)
    85         data.Elm[data.Size] = x
    86         Add = data.Size
    87         data.Size++
    88     End Function
    89 
    90     /*Override*/ Virtual Function Add(x As Object) As Long
    91         setLeastCapacity(data.Size + 1)
    92         data.Elm[data.Size] = VarPtr(x)
    93         Add = data.Size
    94         data.Size++
     131    /*Override*/ Virtual Function Add( object As Object ) As Long
     132        SetLeastCapacity( size + 1 )
     133        pObject[size] = object
     134        size++
     135        Return size - 1
    95136    End Function
    96137
     
    112153
    113154    /*Override*/ Virtual Sub Clear()
    114         data.Size = 0
    115     End Sub
    116 
    117     Virtual Function Clone() As *ArrayList
    118         Dim p As *ArrayList
    119         p = New ArrayList(data.Size)
    120         p->data.Size = data.Size
    121         memcpy(p->data.Elm, This.data.Elm, SizeOf (*Object) * data.Size)
    122         Return p
     155        size = 0
     156    End Sub
     157
     158    Virtual Function Clone() As ArrayList
     159        Dim arrayList = New ArrayList( size )
     160        memcpy( arrayList.pObject, This.pObject, SizeOf(*Object) * size )
     161        Return arrayList
    123162    End Function
    124163
    125164    /*Const*/ /*Override*/ /*Virtual Function Contains(x As *Object) As Boolean
    126165        Dim i As Long
    127         For i = 0 To ELM(data.Size)
     166        For i = 0 To ELM(size)
    128167            If x->Equals(data.Elm[i]) Then
    129168                Return True
     
    145184    End Function
    146185
    147     /*Const*/ Override Function IndexOf(x As *Object) As Long
    148         Return IndexOf(x, 0, data.Size)
    149     End Function
    150 
    151     /*Const*/ Virtual Function IndexOf(x As *Object, startIndex As Long) As Long
    152         Return IndexOf(x, startIndex, data.Size - startIndex)
    153     End Function
    154 
    155     /*Const*/ Virtual Function IndexOf(x As *Object, startIndex As Long, count As Long) As Long
     186    /*Const*/ Override Function IndexOf(object As Object) As Long
     187        Return IndexOf(object, 0, size)
     188    End Function
     189
     190    /*Const*/ Virtual Function IndexOf(object As Object, startIndex As Long) As Long
     191        Return IndexOf(object, startIndex, size - startIndex)
     192    End Function
     193
     194    /*Const*/ Virtual Function IndexOf(object As Object, startIndex As Long, count As Long) As Long
    156195        Dim i As Long
    157         Dim last = Math.Min(startIndex + count - 1, data.Size)
     196        Dim last = Math.Min(startIndex + count - 1, size)
    158197        For i = startIndex To last
    159             If x->Equals(ByVal data.Elm[i]) Then
     198            If object.Equals( pObject[i] ) Then
    160199                Return i
    161200            End If
    162201        Next
    163     End Function
    164 
    165     Override Sub Insert(i As Long, x As *Object)
    166         setLeastCapacity(data.Size + 1)
    167         memmove(VarPtr(data.Elm[i + 1]), VarPtr(data.Elm[i]), SizeOf (*Object) * (data.Size - i))
    168         data.Elm[i] = x
    169         data.Size++
     202        Return -1
     203    End Function
     204
     205    Override Sub Insert(i As Long, object As Object)
     206        SetLeastCapacity(size + 1)
     207        memmove(VarPtr(pObject[i + 1]), VarPtr(pObject[i]), SizeOf (*Object) * (size - i))
     208        pObject[i] = object
     209        size++
    170210    End Sub
    171211
     
    174214    End Sub
    175215
    176     /*Const*/ Virtual Function LastIndexOf(x As *Object) As Long
    177         LastIndexOf(x, 0, data.Size)
    178     End Function
    179 
    180     /*Const*/ Virtual Function LastIndexOf(x As *Object, startIndex As Long) As Long
    181         Return LastIndexOf(x, startIndex, data.Size - startIndex)
    182     End Function
    183 
    184     /*Const*/ Virtual Function LastIndexOf(x As *Object, startIndex As Long, count As Long) As Long
    185         ' TODO: 実装
    186     End Function
    187 
    188     Override Sub Remove(x As *Object)
    189         Dim i = IndexOf(x)
     216    /*Const*/ Virtual Function LastIndexOf(object As Object) As Long
     217        LastIndexOf(object, 0, size)
     218    End Function
     219
     220    /*Const*/ Virtual Function LastIndexOf(object As Object, startIndex As Long) As Long
     221        Return LastIndexOf(object, startIndex, size - startIndex)
     222    End Function
     223
     224    /*Const*/ Virtual Function LastIndexOf(object As Object, startIndex As Long, count As Long) As Long
     225        ' TODO: 実装
     226    End Function
     227
     228    Override Sub Remove(object As Object)
     229        Dim i = IndexOf(object)
    190230        If i > 0 Then
    191231            RemoveAt(i)
     232            size--
    192233        End If
    193234    End Sub
     
    195236    Override Sub RemoveAt(i As Long)
    196237        RemoveRange(i, 1)
     238        size--
    197239    End Sub
    198240   
    199241    Virtual Sub RemoveRange(i As Long, count As Long)
    200242        Dim removeLastPos = i + count
    201         memmove(VarPtr(data.Elm[i]), VarPtr(data.Elm[removeLastPos]), SizeOf (*Object) * (data.Size - removeLastPos))
     243        memmove(VarPtr(pObject[i]), VarPtr(pObject[removeLastPos]), SizeOf(*Object) * (size - removeLastPos))
     244        size -= count
    202245    End Sub
    203246
    204247    Virtual Sub Reverse()
    205         Reverse(0, data.Size)
     248        Reverse(0, size)
    206249    End Sub
    207250
     
    219262
    220263    Virtual Sub Sort(ByRef c As IComparer)
    221         Sort(0, data.Size, c)
     264        Sort(0, size, c)
    222265    End Sub
    223266
     
    228271    ' ToArray
    229272    Virtual Sub TrimToSize()
    230         reallocBuffer(data.Size)
     273        Realloc(size)
    231274    End Sub
    232275
     
    273316    End Function
    274317
    275 Private
    276     Sub setLeastCapacity(c As Long)
    277         If data.Capacity < c Then
    278             reallocBuffer(c)
    279         End If
    280     End Sub
    281 
    282     Sub reallocBuffer(c As Long)
    283         Dim newBuf As ArrayList_Element(c)
    284         newBuf.Size = data.Size
    285         memcpy(newBuf.Elm, data.Elm, SizeOf (*Object) * newBuf.Size)
    286         newBuf.Swap(data)
    287     End Sub
    288 
    289     data As ArrayList_Element
    290318End Class
    291319
     320/*
    292321Class ArrayList_Element
    293322Public
     
    335364    Capacity As Long
    336365End Class
     366*/
  • Include/Classes/System/Collections/misc.ab

    r118 r195  
    6969    'Sub Clear()
    7070    '/*Const*/ Function Contains(value As *Object) As Boolean
    71     /*Const*/ Function IndexOf(value As *Object) As Long
    72     Sub Insert(index As Long, value As *Object)
    73     Sub Remove(value As *Object)
     71    /*Const*/ Function IndexOf(value As Object) As Long
     72    Sub Insert(index As Long, value As Object)
     73    Sub Remove(value As Object)
    7474    Sub RemoveAt(index As Long)
    7575    ' Properties
  • Include/Classes/System/Object.ab

    r70 r195  
    11Class Object
     2
     3    ' 実行時型情報
     4    typeInfo As TypeInfo
     5
    26Public
    37
     
    812
    913    ' 2つのオブジェクトが等しいかどうかを判断する
    10     Virtual Function Equals( ByRef obj As Object ) As Boolean
    11         If VarPtr( This ) = VarPtr( obj ) Then
     14    Virtual Function Equals( object As Object ) As Boolean
     15        If This.GetHashCode() = object.GetHashCode() Then
    1216            Return True
    1317        Else
     
    1519        End If
    1620    End Function
    17     Static Function Equals( ByRef objA As Object, ByRef objB As Object ) As Boolean
    18         Return objA.Equals( objB )
     21    Static Function Equals( objectA As Object, objectB As Object ) As Boolean
     22        Return objectA.Equals( objectB )
    1923    End Function
    2024
     
    2832        Return "Object"
    2933    End Function
     34
     35/*
     36    Function Operator Downcast() As VoidPtr
     37    End Function
     38*/
    3039End Class
  • Include/Classes/System/TypeInfo.ab

    r186 r195  
    11' 実装中...
    22'(※ まだ組み込んでいません)
     3
     4
     5'Namespace System
     6
    37
    48Class TypeInfo
     
    3539
    3640' 中間的な実装(継承専用)
    37 Class TypeImpl
     41Class TypeBaseImpl
    3842    Inherits TypeInfo
    3943
     
    4650Protected
    4751
    48     Sub TypeImpl()
     52    Sub TypeBaseImpl()
    4953        name = ""
    5054        strNamespace = ""
     
    5256    End Sub
    5357
    54     Sub TypeImpl( name As String, strNamespace As String )
     58    Sub TypeBaseImpl( name As String, strNamespace As String )
    5559        This.name = name
    5660        This.strNamespace = strNamespace
    57         baseType = Nothing
    58     End Sub
    59 
    60     Sub TypeImpl( name As String, strNamespace As String, baseType As Type )
    61         TypeImpl( name, strNamespace )
     61        This.baseType = Nothing
     62    End Sub
     63
     64    Sub TypeBaseImpl( name As String, strNamespace As String, baseType As TypeInfo )
     65        This.name = name
     66        This.strNamespace = strNamespace
    6267        This.baseType = baseType
    6368    End Sub
    6469
    6570    /*
    66     Sub TypeImpl( name As String, strNamespace As String, baseType As Type, interfaces As ... )
    67         TypeImpl( name, strNamespace, baseType )
     71    Sub TypeBaseImpl( name As String, strNamespace As String, baseType As Type, interfaces As ... )
     72        TypeBaseImpl( name, strNamespace, baseType )
    6873    End Sub
    6974    */
     
    131136' 値型を管理するためのクラス
    132137Class _System_TypeForValueType
    133     Inherits TypeImpl
    134 Public
    135     Sub _System_TypeForValueType( name )
     138    Inherits TypeBaseImpl
     139Public
     140    Sub _System_TypeForValueType( name As String )
    136141        TypeInfo( name, "" )
    137142    End Sub
     
    144149' クラスを管理するためのクラス
    145150Class _System_TypeForClass
    146     Inherits TypeImpl
    147 Public
     151    Inherits TypeBaseImpl
     152Public
     153    Sub _System_TypeForClass( name As String, strNamespace As String, baseType As TypeInfo )
     154        TypeBaseImpl( name, strNamespace, baseType )
     155    End Sub
     156    Sub ~_System_TypeForClass()
     157    End Sub
     158    Override Function IsClass() As Boolean
     159        Return True
     160    End Function
    148161End Class
    149162
    150163' インターフェイスを管理するためのクラス
    151164Class _System_TypeForInterface
    152     Inherits TypeImpl
     165    Inherits TypeBaseImpl
    153166Public
    154167End Class
     
    156169' 列挙体を管理するためのクラス
    157170Class _System_TypeForEnum
    158     Inherits TypeImpl
     171    Inherits TypeBaseImpl
    159172Public
    160173End Class
     
    162175' デリゲートを管理するためのクラス
    163176Class _System_TypeForDelegate
    164     Inherits TypeImpl
     177    Inherits TypeBaseImpl
    165178Public
    166179End Class
     
    171184'--------------------------------------------------------------------
    172185Class _System_TypeBase
    173     ppTypes As *Type
     186    pTypes As *TypeInfo
    174187    count As Long
    175188
    176189    Sub _System_TypeBase()
    177         ppTypes = GC_malloc( 1 )
     190        pTypes = GC_malloc( 1 )
    178191        count = 0
    179192    End Sub
    180193    Sub ~_System_TypeBase()
    181         Dim i As Long
    182         For i=0 To ELM( count )
    183             Delete ppTypes[i]
    184         Next
     194    End Sub
     195
     196    Sub _add( typeInfo As TypeInfo )
     197        pTypes = realloc( pTypes, ( count + 1 ) * SizeOf(*TypeInfo) )
     198        pTypes[count] = typeInfo
     199        count++
    185200    End Sub
    186201
     
    190205Public
    191206
    192     Sub Add( pType As *Type )
    193         ppTypes = realloc( ppTypes, ( count + 1 ) * SizeOf(*Type) )
    194         ppTypes[count] = pType
    195         count++
     207    Static Sub Add( typeInfo As TypeInfo )
     208        obj._add( typeInfo )
    196209    End Sub
    197210
     
    214227        obj.Add( New _System_TypeForValueType( "Double" ) )
    215228    End Sub
    216 End Class
     229
     230    Static Sub InitializeUserTypes()
     231        ' このメソッドの実装はコンパイラが自動生成する
     232    End Sub
     233
     234    Static Sub Initialize()
     235        ' 値型を初期化
     236        InitializeValueType()
     237
     238        ' Class / Interface / Enum / Delegate を初期化
     239        InitializeUserTypes()
     240    End Sub
     241End Class
     242
     243' End Namespace ' System
  • Include/Classes/System/index.ab

    r175 r195  
    44#require <Classes/System/String.ab.
    55#require <Classes/System/TimeSpan.ab>
     6#require <Classes/System/TypeInfo.ab>
    67#require <Classes/System/OperatingSystem.ab>
    78#require <Classes/System/Version.ab>
Note: See TracChangeset for help on using the changeset viewer.