PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/elpmis/elpmis/internalFunctions/templates.vb

http://geminibranch.codeplex.com
Visual Basic | 373 lines | 292 code | 78 blank | 3 comment | 0 complexity | da33ddc9bb7ca688eb42e5bce40d9f29 MD5 | raw file
  1. Imports std
  2. Public Module shared_parameters_mappings
  3. Public ReadOnly mi As iParametersMapping = Nothing
  4. Public ReadOnly mio As iParametersMapping = Nothing
  5. Public ReadOnly mname As iParametersMapping = Nothing
  6. Public ReadOnly mname_ps As iParametersMapping = Nothing
  7. Public ReadOnly mi_ps As iParametersMapping = Nothing
  8. Public ReadOnly mkey As iParametersMapping = Nothing
  9. Public ReadOnly m_name_value As iParametersMapping = Nothing
  10. Sub New()
  11. initialMapping(mi, "i")
  12. initialMapping(mio, "i", "o")
  13. initialMapping(mname, "name")
  14. initialSerialMapping1(mname_ps, "name", "p")
  15. initialSerialMapping1(mi_ps, "i", "p")
  16. initialMapping(mkey, "key")
  17. initialMapping(m_name_value, "name", "value")
  18. End Sub
  19. End Module
  20. Public MustInherit Class templateFunction(Of OT)
  21. Inherits internalFunction
  22. Protected Sub New(ByVal name As String, ByVal m As iParametersMapping, _
  23. ByVal ps As parameters_list, ByVal a As aliases)
  24. MyBase.New(name, m, ps, a)
  25. End Sub
  26. Protected Sub New(ByVal name As String, ByVal m As iParametersMapping, ByVal a As aliases)
  27. MyBase.New(name, m, a)
  28. End Sub
  29. Protected Sub New(ByVal name As String, ByVal aliases As aliases)
  30. MyBase.new(name, aliases)
  31. End Sub
  32. Protected Sub New(ByVal name As String, ByVal m As iParametersMapping)
  33. MyBase.New(name, m)
  34. End Sub
  35. Protected Sub New(ByVal name As String)
  36. MyBase.New(name)
  37. End Sub
  38. Protected MustOverride Function returnType() As typeDefination
  39. Protected Overridable Function elementType1() As typeDefination
  40. Return typeDefination.unknown
  41. End Function
  42. Protected Overridable Function elementType2() As typeDefination
  43. Return typeDefination.unknown
  44. End Function
  45. Protected MustOverride Overloads Function invoke(ByVal ctx As context, ByRef o As OT) As Boolean
  46. Protected NotOverridable Overrides Function invoke(ByVal ctx As context, ByRef output As bare_variable) As Boolean
  47. Dim o As OT = Nothing
  48. If Not invoke(ctx, o) Then
  49. raiseError("cannot invoke in " + Me.GetType().FullName(), errorHandle.errorType.warning)
  50. Return False
  51. End If
  52. output = New bare_variable(returnType(), elementType1(), elementType2(), o)
  53. Return True
  54. End Function
  55. End Class
  56. Public MustInherit Class template_finder_function(Of IT, OT)
  57. Inherits templateFunction(Of OT)
  58. Protected Sub New(ByVal name As String, ByVal m As iParametersMapping, _
  59. ByVal ps As parameters_list, ByVal a As aliases)
  60. MyBase.New(name, m, ps, a)
  61. End Sub
  62. Protected Sub New(ByVal name As String, ByVal m As iParametersMapping, ByVal a As aliases)
  63. MyBase.New(name, m, a)
  64. End Sub
  65. Protected Sub New(ByVal name As String, ByVal aliases As aliases)
  66. MyBase.new(name, aliases)
  67. End Sub
  68. Protected Sub New(ByVal name As String, ByVal m As iParametersMapping)
  69. MyBase.New(name, m)
  70. End Sub
  71. Protected Sub New(ByVal name As String)
  72. MyBase.New(name)
  73. End Sub
  74. Protected MustOverride Function finder() As iVariableFinder(Of IT)
  75. End Class
  76. Public MustInherit Class noInputFunction(Of OT)
  77. Inherits templateFunction(Of OT)
  78. Protected Sub New(ByVal name As String, ByVal a As aliases)
  79. MyBase.New(name, a)
  80. End Sub
  81. Protected Sub New(ByVal name As String)
  82. MyBase.New(name)
  83. End Sub
  84. Protected MustOverride Overloads Function invoke(ByRef o As OT) As Boolean
  85. Protected NotOverridable Overrides Function invoke(ByVal ctx As context, ByRef o As OT) As Boolean
  86. Return invoke(o)
  87. End Function
  88. End Class
  89. Public MustInherit Class inputIFunction(Of IT, OT)
  90. Inherits template_finder_function(Of IT, OT)
  91. Public Class parameters
  92. Public Const i As String = "i"
  93. End Class
  94. Private _noInput As Boolean = False
  95. Protected Sub New(ByVal name As String, ByVal a As aliases)
  96. MyBase.New(name, shared_parameters_mappings.mi, a)
  97. End Sub
  98. Protected Sub New(ByVal name As String)
  99. Me.New(name, Nothing)
  100. End Sub
  101. Protected MustOverride Overloads Function invoke(ByVal i As IT, ByRef o As OT) As Boolean
  102. Protected Function noInput() As Boolean
  103. Return _noInput
  104. End Function
  105. Protected Overridable Function enableNoInput() As Boolean
  106. Return False
  107. End Function
  108. Protected NotOverridable Overrides Function invoke(ByVal ctx As context, ByRef o As OT) As Boolean
  109. Dim i As IT = Nothing
  110. If Not finder().find(ctx, parameters.i, i) Then
  111. If Not enableNoInput() Then
  112. raiseError("expect parameter " + parameters.i, errorHandle.errorType.user)
  113. Return False
  114. Else
  115. _noInput = True
  116. i = Nothing
  117. End If
  118. Else
  119. _noInput = False
  120. End If
  121. Return invoke(i, o)
  122. End Function
  123. End Class
  124. Public MustInherit Class inputIOutputOFunction(Of IT, OT)
  125. Inherits template_finder_function(Of IT, Boolean)
  126. Public Class parameters
  127. Public Const i As String = "i"
  128. Public Const o As String = "o"
  129. End Class
  130. Protected Sub New(ByVal name As String, ByVal a As aliases)
  131. MyBase.New(name, shared_parameters_mappings.mio, a)
  132. End Sub
  133. Protected Sub New(ByVal name As String)
  134. Me.New(name, Nothing)
  135. End Sub
  136. Protected NotOverridable Overrides Function returnType() As typeDefination
  137. Return typeDefination.bool
  138. End Function
  139. Protected Overridable Function enableNoInput() As Boolean
  140. Return False
  141. End Function
  142. Protected Overridable Function outputType() As typeDefination
  143. Return typeDefination.var
  144. End Function
  145. Protected MustOverride Overloads Function invoke(ByVal i As IT, ByRef o As OT) As Boolean
  146. Protected NotOverridable Overrides Function invoke(ByVal ctx As context, ByRef rst As Boolean) As Boolean
  147. Dim i As IT = Nothing
  148. If Not finder().find(ctx, parameters.i, i) Then
  149. If Not enableNoInput() Then
  150. raiseError("expect parameter " + parameters.i, errorHandle.errorType.user)
  151. Return False
  152. Else
  153. i = Nothing
  154. End If
  155. End If
  156. Dim ob As bare_variable = Nothing
  157. If Not bare_variable_finder().find(ctx, parameters.o, ob) OrElse _
  158. Not ob.variable().isPointerOf(outputType()) Then
  159. raiseError("expect parameter " + parameters.o + " is pointer<" + typeNaming(outputType()) + ">.", _
  160. errorHandle.errorType.user)
  161. Return False
  162. End If
  163. Dim o As OT = Nothing
  164. rst = invoke(i, o)
  165. If rst Then
  166. If ob.variable().elementType1() = typeDefination.var Then
  167. convertor.cast(Of pointer(Of pointer(Of Object)))(ob.value()).instance().setinstance(o)
  168. Else
  169. convertor.cast(Of pointer(Of pointer(Of OT)))(ob.value()).instance().setinstance(o)
  170. End If
  171. End If
  172. Return True
  173. End Function
  174. End Class
  175. Public MustInherit Class _x_ps_function(Of xT, pT, oT)
  176. Inherits templateFunction(Of oT)
  177. Public Class parameters
  178. Public Const i As String = "i"
  179. Public Const pbase As String = "p"
  180. End Class
  181. Protected Sub New(ByVal name As String, ByVal m As iParametersMapping)
  182. MyBase.New(name, m)
  183. End Sub
  184. Protected Sub New(ByVal name As String)
  185. Me.New(name, shared_parameters_mappings.mi_ps)
  186. End Sub
  187. Protected MustOverride Function x_finder() As iVariableFinder(Of xT)
  188. Protected MustOverride Overloads Function invoke(ByVal ctx As context, ByVal x As xT, _
  189. ByVal ps As pT, ByRef o As oT) As Boolean
  190. Protected MustOverride Function findvariablesCB(ByVal name As String, ByVal var As bare_variable, _
  191. ByRef obj As Object) As Boolean
  192. Protected Overridable Function x_name() As String
  193. Return parameters.i
  194. End Function
  195. Protected NotOverridable Overrides Function invoke(ByVal ctx As context, ByRef o As oT) As Boolean
  196. Dim x As xT = Nothing
  197. If x_finder().find(ctx, x_name(), x) Then
  198. Dim ps As pT = Nothing
  199. ctx.findVariables(AddressOf findvariablesCB, bare_variable_finder(), parameters.pbase, obj:=ps)
  200. Return invoke(ctx, x, ps, o)
  201. Else
  202. raiseError("expect parameter " + x_name(), errorHandle.errorType.user)
  203. Return False
  204. End If
  205. End Function
  206. End Class
  207. Public MustInherit Class _name_ps_function(Of PT, OT)
  208. Inherits _x_ps_function(Of String, PT, OT)
  209. Public Shadows Class parameters
  210. Public Const name As String = "name"
  211. End Class
  212. Protected Sub New(ByVal name As String)
  213. MyBase.New(name, shared_parameters_mappings.mname_ps)
  214. End Sub
  215. Protected NotOverridable Overrides Function x_name() As String
  216. Return parameters.name
  217. End Function
  218. Protected NotOverridable Overrides Function x_finder() As iVariableFinder(Of String)
  219. Return string_finder()
  220. End Function
  221. End Class
  222. Public Class _object_ps
  223. Private Shared Function insertParameters(ByVal var As bare_variable, ByVal o As vector(Of Object)) As Boolean
  224. If var Is Nothing Then
  225. o.push_back(CType(Nothing, Object))
  226. Else
  227. o.push_back(var.value())
  228. End If
  229. Return True
  230. End Function
  231. Public Shared Function findvariablesCB(ByVal name As String, ByVal var As bare_variable, _
  232. ByRef obj As Object) As Boolean
  233. If obj Is Nothing Then
  234. obj = New vector(Of Object)()
  235. End If
  236. Return insertParameters(var, convertor.cast(Of vector(Of Object))(obj))
  237. End Function
  238. End Class
  239. Public Class _parameters_t_ps
  240. Private Shared Function insertParameter(ByVal name As String, ByVal var As bare_variable, _
  241. ByVal ps As parameters_t) As Boolean
  242. ps.push_back(make_pair(name, var))
  243. Return True
  244. End Function
  245. Public Shared Function findvariablesCB(ByVal name As String, ByVal var As bare_variable, _
  246. ByRef obj As Object) As Boolean
  247. If obj Is Nothing Then
  248. obj = New parameters_t()
  249. End If
  250. Return insertParameter(name, var, convertor.cast(Of parameters_t)(obj))
  251. End Function
  252. End Class
  253. 'parameters, name, the function name
  254. ' p0 ... pn, the input parameters to the function
  255. 'return, var, the return from function
  256. Public MustInherit Class name_object_ps_function(Of OT)
  257. Inherits _name_ps_function(Of vector(Of Object), OT)
  258. Protected Sub New(ByVal name As String)
  259. MyBase.New(name)
  260. End Sub
  261. Protected NotOverridable Overrides Function findvariablesCB(ByVal name As String, ByVal var As bare_variable, _
  262. ByRef obj As Object) As Boolean
  263. Return _object_ps.findvariablesCB(name, var, obj)
  264. End Function
  265. End Class
  266. Public MustInherit Class name_parameters_t_ps_function(Of OT)
  267. Inherits _name_ps_function(Of parameters_t, OT)
  268. Protected Sub New(ByVal name As String)
  269. MyBase.New(name)
  270. End Sub
  271. Protected NotOverridable Overrides Function findvariablesCB(ByVal name As String, ByVal var As bare_variable, _
  272. ByRef obj As Object) As Boolean
  273. Return _parameters_t_ps.findvariablesCB(name, var, obj)
  274. End Function
  275. End Class
  276. Public MustInherit Class x_object_ps_function(Of xT, oT)
  277. Inherits _x_ps_function(Of xT, vector(Of Object), oT)
  278. Protected Sub New(ByVal name As String)
  279. MyBase.New(name)
  280. End Sub
  281. Protected NotOverridable Overrides Function findvariablesCB(ByVal name As String, ByVal var As bare_variable, _
  282. ByRef obj As Object) As Boolean
  283. Return _object_ps.findvariablesCB(name, var, obj)
  284. End Function
  285. End Class
  286. Public MustInherit Class x_parameters_t_ps_function(Of xT, oT)
  287. Inherits _x_ps_function(Of xT, parameters_t, oT)
  288. Protected Sub New(ByVal name As String)
  289. MyBase.New(name)
  290. End Sub
  291. Protected NotOverridable Overrides Function findvariablesCB(ByVal name As String, ByVal var As bare_variable, _
  292. ByRef obj As Object) As Boolean
  293. Return _parameters_t_ps.findvariablesCB(name, var, obj)
  294. End Function
  295. End Class