forked from Pramod-Devireddy/go-exprtk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexprtk.go
More file actions
100 lines (86 loc) · 2.6 KB
/
exprtk.go
File metadata and controls
100 lines (86 loc) · 2.6 KB
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
package exprtk
// #cgo CXXFLAGS: -std=c++11
// #cgo LDFLAGS: -L.
// #include <stdlib.h>
// #include "exprtkwrapper.h"
import "C"
import (
"errors"
"unsafe"
)
// GoExprtk ...Exprtk Structure
type GoExprtk struct {
exprtk C.exprtkWrapper
}
// NewExprtk ... Creates a new object
func NewExprtk() GoExprtk {
var obj GoExprtk
obj.exprtk = C.exprtkWrapperInit()
return obj
}
// SetExpression ... Sets an Expression
func (obj GoExprtk) SetExpression(expr string) {
cExpr := C.CString(expr)
C.setExpressionString(obj.exprtk, cExpr)
C.free(unsafe.Pointer(cExpr))
}
// AddDoubleVariable ... Adds variable to the expression
func (obj GoExprtk) AddDoubleVariable(x string) {
cX := C.CString(x)
C.addDoubleVariable(obj.exprtk, cX)
C.free(unsafe.Pointer(cX))
}
// AddStringVariable ... Adds variable to the expression
func (obj GoExprtk) AddStringVariable(x string) {
cX := C.CString(x)
C.addStringVariable(obj.exprtk, cX)
C.free(unsafe.Pointer(cX))
}
// AddVectorVariable ... Adds variable to the expression
func (obj GoExprtk) AddVectorVariable(x string) {
cX := C.CString(x)
C.addVectorVariable(obj.exprtk, cX)
C.free(unsafe.Pointer(cX))
}
// SetDoubleVariableValue ... Sets value to the variable
func (obj GoExprtk) SetDoubleVariableValue(varName string, val float64) {
cName := C.CString(varName)
C.setDoubleVariableValue(obj.exprtk, cName, C.double(val))
C.free(unsafe.Pointer(cName))
}
// SetStringVariableValue ... Sets value to the variable
func (obj GoExprtk) SetStringVariableValue(varName string, val string) {
cName := C.CString(varName)
cValue := C.CString(val)
C.setStringVariableValue(obj.exprtk, cName, cValue)
C.free(unsafe.Pointer(cName))
C.free(unsafe.Pointer(cValue))
}
// SetVectorVariableValue ... Sets value to the variable
func (obj GoExprtk) SetVectorVariableValue(varName string, val []float64) {
arr := make([]C.double, 0)
for i := 0; i < len(val); i++ {
arr = append(arr, C.double(val[i]))
}
firstValue := &(arr[0])
var arrayLength C.int = C.int(len(arr))
cName := C.CString(varName)
C.setVectorVariableValue(obj.exprtk, cName, firstValue, arrayLength)
C.free(unsafe.Pointer(cName))
}
// CompileExpression ... Compiles the Expression
func (obj GoExprtk) CompileExpression() error {
value := C.compileExpression(obj.exprtk)
if value == 0 {
return errors.New("failed to compile the expression")
}
return nil
}
// GetEvaluatedValue ... Returns the evaluated value
func (obj GoExprtk) GetEvaluatedValue() float64 {
return float64(C.getEvaluatedValue(obj.exprtk))
}
// Delete ... Destroys the created object and releases the memory
func (obj GoExprtk) Delete() {
C.deleteExprtk(obj.exprtk)
}