Binding a uCalc variable to a host variable in your C++ code
ID: 1465
See: DefineVariable
using uCalcSoftware;
var uc = new uCalc();
// This example is mainly meant for C++ where it works with ordinary scalar variables
// See the other example where we work with a uCalc array
// 1. Use an array so the data lives on the heap as a reference type
// Boxing is used for scalar variables; a boxed copy of the scalar disconnected from the uCalc
// variable would prevent the example from working as expected.
// You technically can get around this with the C# "unsafe" directive, but it's not recommended
double[] myHostVar = new double[] { 1234.5 };
// 2. Pin the array
var myHostVarHandle = System.Runtime.InteropServices.GCHandle.Alloc(myHostVar, System.Runtime.InteropServices.GCHandleType.Pinned);
// 3. Bind the uCalc variable to the pinned memory address
uc.DefineVariable("myVar", myHostVarHandle.AddrOfPinnedObject());
Console.WriteLine(uc.Eval("myVar")); // Output: 1234.5
// Changing the uCalc variable updates the pinned array
uc.Eval("myVar = 456");
// The change is reflected in the C# array
Console.WriteLine(myHostVar[0]); // Output: 456
// Changing the C# array updates uCalc
myHostVar[0] = 9876;
Console.WriteLine(uc.Eval("myVar")); // Output: 9876
// Fast Parse & Evaluate loop
var expr = uc.Parse("myVar * 10");
for (myHostVar[0] = 1; myHostVar[0] <= 5; myHostVar[0]++) {
Console.WriteLine(expr.Evaluate());
}
// Don't forget to free the handle when you are completely done with the parser instance
myHostVarHandle.Free();
1234.5
456
9876
10
20
30
40
50 using uCalcSoftware; var uc = new uCalc(); // This example is mainly meant for C++ where it works with ordinary scalar variables // See the other example where we work with a uCalc array // 1. Use an array so the data lives on the heap as a reference type // Boxing is used for scalar variables; a boxed copy of the scalar disconnected from the uCalc // variable would prevent the example from working as expected. // You technically can get around this with the C# "unsafe" directive, but it's not recommended double[] myHostVar = new double[] { 1234.5 }; // 2. Pin the array var myHostVarHandle = System.Runtime.InteropServices.GCHandle.Alloc(myHostVar, System.Runtime.InteropServices.GCHandleType.Pinned); // 3. Bind the uCalc variable to the pinned memory address uc.DefineVariable("myVar", myHostVarHandle.AddrOfPinnedObject()); Console.WriteLine(uc.Eval("myVar")); // Output: 1234.5 // Changing the uCalc variable updates the pinned array uc.Eval("myVar = 456"); // The change is reflected in the C# array Console.WriteLine(myHostVar[0]); // Output: 456 // Changing the C# array updates uCalc myHostVar[0] = 9876; Console.WriteLine(uc.Eval("myVar")); // Output: 9876 // Fast Parse & Evaluate loop var expr = uc.Parse("myVar * 10"); for (myHostVar[0] = 1; myHostVar[0] <= 5; myHostVar[0]++) { Console.WriteLine(expr.Evaluate()); } // Don't forget to free the handle when you are completely done with the parser instance myHostVarHandle.Free();
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
double myHostVar = 1234.5;
// Bind your C++ host variable to the uCalc variable like this:
uc.DefineVariable("myVar", &myHostVar);
cout << uc.Eval("myVar") << endl; // The value of the uCalc variable reflects that of the host variable
uc.Eval("myVar = 456"); // Changing the uCalc variable updates the host variable since they're linked
cout << myHostVar << endl; // The change is reflected in the C++ host variable
myHostVar = 9876; // The uCalc variable will reflect this host variable change
cout << uc.Eval("myVar") << endl;
auto expr = uc.Parse("myVar * 10");
for (myHostVar = 1; myHostVar <= 5; myHostVar++) {
cout << expr.Evaluate() << endl;
}
}
1234.5
456
9876
10
20
30
40
50 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; double myHostVar = 1234.5; // Bind your C++ host variable to the uCalc variable like this: uc.DefineVariable("myVar", &myHostVar); cout << uc.Eval("myVar") << endl; // The value of the uCalc variable reflects that of the host variable uc.Eval("myVar = 456"); // Changing the uCalc variable updates the host variable since they're linked cout << myHostVar << endl; // The change is reflected in the C++ host variable myHostVar = 9876; // The uCalc variable will reflect this host variable change cout << uc.Eval("myVar") << endl; auto expr = uc.Parse("myVar * 10"); for (myHostVar = 1; myHostVar <= 5; myHostVar++) { cout << expr.Evaluate() << endl; } }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// This example is mainly meant for C++ where it works with ordinary scalar variables
'// See the other example where we work with a uCalc array
'// 1. Use an array so the data lives on the heap as a reference type
'// Boxing is used for scalar variables; a boxed copy of the scalar disconnected from the uCalc
'// variable would prevent the example from working as expected.
'// You technically can get around this with the C# "unsafe" directive, but it's not recommended
Dim myHostVar() As Double = { 1234.5 }
'// 2. Pin the array
Dim myHostVarHandle = System.Runtime.InteropServices.GCHandle.Alloc(myHostVar, System.Runtime.InteropServices.GCHandleType.Pinned)
'// 3. Bind the uCalc variable to the pinned memory address
uc.DefineVariable("myVar", myHostVarHandle.AddrOfPinnedObject())
Console.WriteLine(uc.Eval("myVar")) '// Output: 1234.5
'// Changing the uCalc variable updates the pinned array
uc.Eval("myVar = 456")
'// The change is reflected in the C# array
Console.WriteLine(myHostVar(0)) '// Output: 456
'// Changing the C# array updates uCalc
myHostVar(0) = 9876
Console.WriteLine(uc.Eval("myVar")) '// Output: 9876
'// Fast Parse & Evaluate loop
Dim expr = uc.Parse("myVar * 10")
For myHostVar(0) = 1 To 5
Console.WriteLine(expr.Evaluate())
Next
'// Don't forget to free the handle when you are completely done with the parser instance
myHostVarHandle.Free()
End Sub
End Module
1234.5
456
9876
10
20
30
40
50 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// This example is mainly meant for C++ where it works with ordinary scalar variables '// See the other example where we work with a uCalc array '// 1. Use an array so the data lives on the heap as a reference type '// Boxing is used for scalar variables; a boxed copy of the scalar disconnected from the uCalc '// variable would prevent the example from working as expected. '// You technically can get around this with the C# "unsafe" directive, but it's not recommended Dim myHostVar() As Double = { 1234.5 } '// 2. Pin the array Dim myHostVarHandle = System.Runtime.InteropServices.GCHandle.Alloc(myHostVar, System.Runtime.InteropServices.GCHandleType.Pinned) '// 3. Bind the uCalc variable to the pinned memory address uc.DefineVariable("myVar", myHostVarHandle.AddrOfPinnedObject()) Console.WriteLine(uc.Eval("myVar")) '// Output: 1234.5 '// Changing the uCalc variable updates the pinned array uc.Eval("myVar = 456") '// The change is reflected in the C# array Console.WriteLine(myHostVar(0)) '// Output: 456 '// Changing the C# array updates uCalc myHostVar(0) = 9876 Console.WriteLine(uc.Eval("myVar")) '// Output: 9876 '// Fast Parse & Evaluate loop Dim expr = uc.Parse("myVar * 10") For myHostVar(0) = 1 To 5 Console.WriteLine(expr.Evaluate()) Next '// Don't forget to free the handle when you are completely done with the parser instance myHostVarHandle.Free() End Sub End Module