VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "FunctionClass"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit


'-----------------------------------------------------
' type for parameters which belongs to a function
'-----------------------------------------------------
Const MAX_PARAMETERS_FOR_EACH_FUNCTION = 10

Private Type TypeParameters
	s(MAX_PARAMETERS_FOR_EACH_FUNCTION) As String	
End Type


'-----------------------------------------------------
' variables for functions
'-----------------------------------------------------
' number of functions
Private lCount As Long
' function name (key)
Private sNameArr() As String
' function name index
Private lNameArr() As Long

' description of the function
Private sDescriptionArr() As String
' description of the returnvalue
Private sReturnvalueArr() As String
' type of the function
Private sFunctionTypeArr() As String
' sort of the function (0 => macro, 1 => function, 2 => ISR)
Private lSortArr() As Long


' function parameters
Private sParameterName() As TypeParameters
' number of parameters for each function
Private lParameterName() As Long


'-----------------------------------------------------
' variables for parameters
'-----------------------------------------------------
' number of parameters
Private lParameterCount As Long
' parameter names (key)
Private sParameterNameArr() As String
' parameter names index
Private lParameterNameArr() As Long

' parameter name arr
Private lParameterNameFunctionNameArr() As String
' names of function for the parameter names
Private sParameterNameFunctionNameArr() As String


' parameter name
Private sParameterArr() As String
' parameter description
Private sParameterDescriptionArr() As String
' parameter type
Private sParameterTypeArr() As String

'-----------------------------------------------------
' adds a new function
'-----------------------------------------------------
Public Function AddFunction(sFuncName As String, sFuncType As String, sFuncDescription As String, sReturnvalue As String, lSort As Long) As Boolean
  On Error GoTo AddFunctionError                
  
  AddFunction = False
  
  ' increment the counter
  lCount = lCount + 1

  ' redim arrays in order to add the new entry
  ReDim Preserve sNameArr(lCount)
  ReDim Preserve lNameArr(lCount)
 	Redim Preserve sDescriptionArr(lCount)
 	Redim Preserve sReturnvalueArr(lCount)
 	Redim Preserve sFunctionTypeArr(lCount)
 	Redim Preserve lSortArr(lCount)
 	Redim Preserve sParameterName(lCount)
 	Redim Preserve lParameterName(lCount)

  ' set the values
  sNameArr(lCount) = sFuncName
  lNameArr(lCount) = lCount
    
 	sDescriptionArr(lCount) = sFuncDescription
 	sReturnvalueArr(lCount) = sReturnvalue
 	sFunctionTypeArr(lCount) = sFuncType
 	lSortArr(lCount) = lSort
 	lParameterName(lCount) = 0

  AddFunction = True
    
  Exit Function

AddFunctionError:
    Exit Function
End Function

'-----------------------------------------------------
' adds a new parameter for the last added function
'-----------------------------------------------------
Public Function AddParameter(sParName As String, sPar As String, sParType As String, sParDescription As String) As Boolean
  On Error GoTo AddParameterError

  AddParameter = False
  
  ' increment the counter
  lParameterCount = lParameterCount + 1

  ' redim arrays in order to add the new entry
  ReDim Preserve sParameterNameArr(lParameterCount)
  ReDim Preserve lParameterNameArr(lParameterCount)

  ReDim Preserve sParameterNameFunctionNameArr(lParameterCount)
  ReDim Preserve sParameterArr(lParameterCount)
 	Redim Preserve sParameterDescriptionArr(lParameterCount)
 	Redim Preserve sParameterTypeArr(lParameterCount)

  ' set the values
  sParameterNameArr(lParameterCount) = sParName
  lParameterNameArr(lParameterCount) = lParameterCount

	 sParameterArr(lParameterCount) = sPar
	 sParameterDescriptionArr(lParameterCount) = sParDescription
	 sParameterTypeArr(lParameterCount) = sParType

    
 	sParameterNameFunctionNameArr(lParameterCount) = sNameArr(lCount)

 	lParameterName(lCount) = lParameterName(lCount) + 1
	 sParameterName(lCount).s(lParameterName(lCount)) = sParName

  AddParameter = True
  Exit Function

AddParameterError:
  Exit Function
End Function


'-----------------------------------------------------
' sort both the function names and the parameter names
'-----------------------------------------------------
Public Function Sort() As Boolean
  On Error Resume Next

  ' sort the arrays
  Sort = MergeSortStart(sNameArr, lNameArr) And MergeSortStart(sParameterNameArr, lParameterNameArr)
End Function


'-----------------------------------------------------
' description of parameter
'-----------------------------------------------------
Public Property Get Parameter(sName As String) As String
	 On Error Resume Next
	 Parameter = sParameterArr(SearchParameterName(sName))
End Property


Public Property Let Parameter(sName As String, sDescription As String)
	 On Error Resume Next
 	sParameterArr(SearchParameterName(sName)) = sDescription
End Property


'-----------------------------------------------------
' description of parameter
'-----------------------------------------------------
Public Property Get ParameterDescription(sName As String) As String
	 On Error Resume Next
	 ParameterDescription = sParameterDescriptionArr(SearchParameterName(sName))
End Property


Public Property Let ParameterDescription(sName As String, sDescription As String)
	 On Error Resume Next
 	sParameterDescriptionArr(SearchParameterName(sName)) = sDescription
End Property


'-----------------------------------------------------
' type of parameter
'-----------------------------------------------------
Public Property Get ParameterType(sName As String) As String
	 On Error Resume Next
	 ParameterType = sParameterTypeArr(SearchParameterName(sName))
End Property


Public Property Let ParameterType(sName As String, sType As String)
	 On Error Resume Next
 	sParameterTypeArr(SearchParameterName(sName)) = sType
End Property


'-----------------------------------------------------
' parameter name of a function by index
'-----------------------------------------------------
Public Property Get ParameterName(sName As String, lIndex As Long) As String
	 On Error Resume Next
	 ParameterName = sParameterName(SearchFunctionName(sName)).s(lIndex)
End Property


'-----------------------------------------------------
' count of parameter names of a function
'-----------------------------------------------------
Public Property Get ParameterCount(sName As String) As Long
	 On Error Resume Next
	 ParameterCount = lParameterName(SearchFunctionName(sName))
End Property

'-----------------------------------------------------
' description of a function 
'-----------------------------------------------------
Public Property Get FunctionDescription(sName As String) As String
   On Error Resume Next    
	  FunctionDescription = sDescriptionArr(SearchFunctionName(sName))
End Property

Public Property Let FunctionDescription(sName As String, sNew As String)
  On Error Resume Next
 	sDescriptionArr(SearchFunctionName(sName)) = sNew
End Property

'-----------------------------------------------------
' sort of a function (macro, function, ISR)
'-----------------------------------------------------
Public Property Get FunctionSort(sName As String) As Long
   On Error Resume Next    
	  FunctionSort = lSortArr(SearchFunctionName(sName))
End Property

Public Property Let FunctionSort(sName As String, lSort As Long)
  On Error Resume Next
 	lSortArr(SearchFunctionName(sName)) = lSort
End Property


'-----------------------------------------------------
' description of the return value from a function
'-----------------------------------------------------
Public Property Get FunctionReturnvalue(sName As String) As String
   On Error Resume Next    
	  FunctionReturnvalue = sReturnvalueArr(SearchFunctionName(sName))
End Property

Public Property Let FunctionReturnvalue(sName As String, sNew As String)
  On Error Resume Next
 	sReturnvalueArr(SearchFunctionName(sName)) = sNew
End Property


'-----------------------------------------------------
' function type
'-----------------------------------------------------
Public Property Get FunctionType(sName As String) As String
   On Error Resume Next    
	  FunctionType = sFunctionTypeArr(SearchFunctionName(sName))
End Property

Public Property Let FunctionType(sName As String, sNew As String)
  On Error Resume Next
 	sFunctionTypeArr(SearchFunctionName(sName)) = sNew
End Property


'*****************************************************************************
'Description:
'        This property returns the number of alternate pin names of this pin.
'-----------------------------------------------------------------------------
'Parameters:
'        sName           the name of the pin (specified
'                        with function 'AddPin' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        The number of alternate pin names.
'*****************************************************************************
Public Property Get FunctionCount As Long
	 On Error Resume Next
	 FunctionCount = UBound(sNameArr)
End Property


'-----------------------------------------------------
' function name by index
'-----------------------------------------------------
Public Property Get FunctionName(lIndex As Long) As String
	 On Error Resume Next
	 FunctionName = sNameArr(lIndex)
End Property



Private Function SearchFunctionName(sName As String) As Long
	Static lLastIndex As Long
	Static sLastName As String

	If (sLastName = sName) Then
		SearchFunctionName = lLastIndex
	Else
		sLastName = sName
		lLastIndex = BinarySearch(sName, sNameArr, lNameArr)
		SearchFunctionName = lLastIndex
	End if
End Function


Private Function SearchParameterName(sName As String) As Long
	Static lLastIndex As Long
	Static sLastName As String

	If (sLastName = sName) Then
		SearchParameterName = lLastIndex	
	Else
		sLastName = sName
		lLastIndex = BinarySearch(sName, sParameterNameArr, lParameterNameArr)
		SearchParameterName = lLastIndex
	End if
End Function


'-----------------------------------------------------
' *** 
'-----------------------------------------------------
Private Sub Class_Initialize()
  On Error Resume Next
  lCount = 0
	 lParameterCount = 0
End Sub

'-----------------------------------------------------
' *** 
'-----------------------------------------------------
Private Sub Class_Terminate()
End Sub
