1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="_ModuleModel" script:language="StarBasic" script:moduleType="normal">REM =======================================================================================================================
REM === The ScriptForge library and its associated libraries are part of the LibreOffice project. ===
REM === Full documentation is available on https://help.libreoffice.org/ ===
REM =======================================================================================================================
Option Compatible
Option ClassModule
'Option Private Module
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' ModuleModel (aka SF_Model)
''' ===========
''' Illustration of how the ScriptForge modules are structured
''' Copy and paste this code in an empty Basic module to start a new service
''' Comment in, comment out, erase what you want, but at the end respect the overall structure
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
REM ================================================================== EXCEPTIONS
''' FAKENEWSERROR
REM ============================================================= PRIVATE MEMBERS
Private [Me] As Object ' Should be initialized immediately after the New statement
' Dim obj As Object : Set obj = New SF_Model
' Set obj.[Me] = obj
Private [_Parent] As Object ' To keep trace of the instance having created a sub-instance
' Set obj._Parent = [Me]
Private ObjectType As String ' Must be UNIQUE
REM ============================================================ MODULE CONSTANTS
Private Const SOMECONSTANT = 1
REM ====================================================== CONSTRUCTOR/DESTRUCTOR
REM -----------------------------------------------------------------------------
Private Sub Class_Initialize()
Set [Me] = Nothing
Set [_Parent] = Nothing
ObjectType = "MODEL"
End Sub ' ScriptForge.SF_Model Constructor
REM -----------------------------------------------------------------------------
Private Sub Class_Terminate()
Call Class_Initialize()
End Sub ' ScriptForge.SF_Model Destructor
REM -----------------------------------------------------------------------------
Public Function Dispose() As Variant
Call Class_Terminate()
Set Dispose = Nothing
End Function ' ScriptForge.SF_Model Explicit Destructor
REM ================================================================== PROPERTIES
REM -----------------------------------------------------------------------------
Property Get MyProperty() As Boolean
''' Returns True or False
''' Example:
''' myModel.MyProperty
MyProperty = _PropertyGet("MyProperty")
End Property ' ScriptForge.SF_Model.MyProperty
REM ===================================================================== METHODS
REM -----------------------------------------------------------------------------
Public Function GetProperty(Optional ByVal PropertyName As Variant) As Variant
''' Return the actual value of the given property
''' Args:
''' PropertyName: the name of the property as a string
''' Returns:
''' The actual value of the property
''' If the property does not exist, returns Null
''' Exceptions:
''' see the exceptions of the individual properties
''' Examples:
''' myModel.GetProperty("MyProperty")
Const cstThisSub = "Model.GetProperty"
Const cstSubArgs = ""
If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
GetProperty = Null
Check:
If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
If Not SF_Utils._Validate(PropertyName, "PropertyName", V_STRING, Properties()) Then GoTo Catch
End If
Try:
GetProperty = _PropertyGet(PropertyName)
Finally:
SF_Utils._ExitFunction(cstThisSub)
Exit Function
Catch:
GoTo Finally
End Function ' ScriptForge.SF_Model.GetProperty
REM -----------------------------------------------------------------------------
Public Function Methods() As Variant
''' Return the list of public methods of the Model service as an array
Methods = Array( _
"MyFunction" _
, "etc" _
)
End Function ' ScriptForge.SF_Model.Methods
REM -----------------------------------------------------------------------------
Public Function MyFunction(Optional ByVal Arg1 As Variant _
, Optional ByVal Arg2 As Variant _
) As Variant
''' Fictive function that concatenates Arg1 Arg2 times
''' Args:
''' Arg1 String Text
''' Arg2 Numeric Number of times (default = 2)
''' Returns:
''' The new string
''' Exceptions:
''' FAKENEWSERROR
''' Examples:
''' MyFunction("value1") returns "value1value1"
Dim sOutput As String ' Output buffer
Dim i As Integer
Const cstThisSub = "Model.myFunction"
Const cstSubArgs = "Arg1, [Arg2=2]"
' _ErrorHandling returns False when, for debugging, the standard error handling is preferred
If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
myFunction = ""
Check:
If IsMissing(Arg2) Then Arg2 = 2
' _EnterFunction returns True when current method is invoked from a user script
If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
' Check Arg1 is a string and Arg2 is a number.
' Validation rules for scalars and arrays are described in SF_Utils
If Not SF_Utils._Validate(Arg1, "Arg1", V_STRING) Then GoTo Finally
If Not SF_Utils._Validate(Arg2, "Arg2", V_NUMERIC) Then GoTo Finally
' Fatal error ?
If Arg2 < 0 Then GoTo CatchFake
End If
Try:
sOutput = ""
For i = 0 To Arg2
sOutput = sOutput & Arg1
Next i
myFunction = sOutput
Finally:
' _ExitFunction manages internal (On Local) errors
SF_Utils._ExitFunction(cstThisSub)
Exit Function
Catch:
GoTo Finally
CatchFake:
SF_Exception.RaiseFatal("FAKENEWSERROR", cstThisSub)
GoTo Finally
End Function ' ScriptForge.SF_Model.myFunction
REM -----------------------------------------------------------------------------
Public Function Properties() As Variant
''' Return the list or properties of the Model class as an array
Properties = Array( _
"MyProperty" _
, "etc" _
)
End Function ' ScriptForge.SF_Model.Properties
REM =========================================================== PRIVATE FUNCTIONS
REM -----------------------------------------------------------------------------
Private Function _PropertyGet(Optional ByVal psProperty As String) As Variant
''' Return the value of the named property
''' Args:
''' psProperty: the name of the property
Dim cstThisSub As String
Const cstSubArgs = ""
cstThisSub = "SF_Model.get" & psProperty
SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
Select Case psProperty
Case "MyProperty"
_PropertyGet = TBD
Case Else
_PropertyGet = Null
End Select
Finally:
SF_Utils._ExitFunction(cstThisSub)
Exit Function
End Function ' ScriptForge.SF_Model._PropertyGet
REM -----------------------------------------------------------------------------
Private Function _Repr() As String
''' Convert the Model instance to a readable string, typically for debugging purposes (DebugPrint ...)
''' Args:
''' Return:
''' "[MODEL]: A readable string"
_Repr = "[MODEL]: A readable string"
End Function ' ScriptForge.SF_Model._Repr
REM ============================================ END OF SCRIPTFORGE.SF_MODEL
</script:module>
|