VBScript: クラスの初期化を行う可変長配列クラス
VBScriptでオブジェクトの可変長配列を実現するクラス。C++のSTL, vectorと名前を揃えている。
スカラー変数はサポート対象外。
Execute()メソッドを使って動的にコードを生成しているのが特徴。
・Class ObjectVector
1: '// A class manages mutable object array.
2: Const kErrorNotInitialized = 1 '// Class name is not defined.
3: Const kErrorAlreadyInitialized = 2 '// Class name is already defined.
4: Const kErrorInvalidInitParam = 3 '// Initialize parameter is invalid.
5: Const kErrorOutOfRange = 9 '// Specified index is out of range.
6:
7: Class ObjectVector
8: Private items_()
9: Private size_
10: Private class_name_
11:
12: '// Constructor
13: Private Sub Class_Initialize()
14: ReDim items_(7)
15: size_ = 0
16: class_name_ = ""
17: End Sub
18:
19: '// Initializer
20: Public Sub Init(ByVal class_name)
21: If "" <> class_name_ Then
22: Call Err.Raise(kErrorAlreadyInitialized, "ObjectVector.Init()")
23: End If
24: class_name_ = class_name
25: End Sub
26:
27: '// Subscript sequence with checking.
28: Public Default Property Get at(ByVal index)
29: If IsOutOfRange(index) Then
30: Call Err.Raise(kErrorOutOfRange, "ObjectVector.at()")
31: End If
32: Set at = items_(index)
33: End Property
34:
35: '// Return length of sequense.
36: Public Property Get size()
37: size = size_
38: End Property
39:
40: '// Test if sequense is empty.
41: Public Property Get is_empty()
42: is_empty = (0 = size_)
43: End Property
44:
45: '// Return first element of sequense.
46: Public Property Get front()
47: Set front = Me.at(0)
48: End Property
49:
50: '// Return last element of sequense.
51: Public Property Get back()
52: Set back = Me.at(size_ - 1)
53: End Property
54:
55: '// Insert element at end.
56: '// When the parameter "init_array" is not "Null",
57: '// execute Init() function of the created object with "init_array".
58: Public Sub push_back(ByVal init_array)
59: If "" = class_name_ Then
60: Call Err.Raise(kErrorNotInitialized, "ObjectVector.push_back()")
61: End If
62: If Not (IsNull(init_array) Or IsArray(init_array)) Then
63: Call Err.Raise(kErrorInvalidInitParam, "ObjectVector.push_back()")
64: End If
65:
66: If UBound(items_) >= size_ Then
67: ReDim Preserve items_(size_ * 2 + 1)
68: End If
69: Call Execute("Set items_(size_) = New " & class_name_)
70: If IsArray(init_array) Then
71: Dim i, arr()
72: ReDim arr(UBound(init_array))
73: For i = 0 To UBound(arr)
74: arr(i) = "init_array(" & i & ")"
75: Next
76: Call Execute("Call items_(size_).Init(" & Join(arr, ",") & ")")
77: End If
78: size_ = size_ + 1
79: End Sub
80:
81: '// Erase element at end.
82: Public Sub pop_back()
83: If IsOutOfRange(size_ - 1) Then
84: Call Err.Raise(kErrorOutOfRange, "ObjectVector.pop_back()")
85: End If
86: Set items_(size_ - 1) = Nothing
87: size_ = size_ - 1
88: End Sub
89:
90: '// Test if index is valid.
91: Private Function IsOutOfRange(ByVal index)
92: IsOutOfRange = True
93: If Not IsNumeric(index) Then Exit Function
94: If (index >= size_) Or (index < 0) Then Exit Function
95: IsOutOfRange = False
96: End Function
97: End Class
とインスタンスを生成してから、Init()関数で子クラス名を登録する。
push_back()関数はやや特殊な構文となっている。
push_back(Null) : 子クラスの生成のみ
push_back(Array(prm1, prm2, …)) : 渡されたパラメータで、子クラスのInit()関数を実行する。
※あらかじめ子クラスでのInit()関数の準備が必須
※子クラスのInit()関数が引数を取らない場合は、以下の構文を使用する
push_back(Array())
・使用例
1: '// Some class.
2: Class Nullary
3: Public x
4: Public Sub Init()
5: x = 1
6: End Sub
7: End Class
8:
9: Class Unary
10: Public x
11: Public Sub Init(ByVal value)
12: x = value
13: End Sub
14: End Class
15:
16: Set obj1 = New ObjectVector
17: Call obj1.Init("Nullary")
18: obj1.push_back(Array()) ' Execute Nullary.Init().
19: WScript.Echo obj1.back.x ' Print obj1.at(0).x (=1)
20:
21: Set obj2 = New ObjectVector
22: Call obj2.Init("Unary")
23: obj2.push_back(Null) ' Not execute Init().
24: obj2.push_back(Array(2)) ' Execute Unary.Init(2).
25: WScript.Echo obj2(0).x ' Print obj2.at(0).x (=Empty)
26: WScript.Echo obj2(1).x ' Print obj2.at(1).x (=2)
こちらへも掲載
http://www4.plala.or.jp/prj-m/files/vbscript/ObjectVector.vbs.txt
0 件のコメント:
コメントを投稿