#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

void ucalc_call SwapValues(uCalcBase::Callback cb) {
   // Get the item handles for the two variables passed by reference
   auto item1 = cb.ArgItem(1);
   auto item2 = cb.ArgItem(2);

   // Use the item's DataType object to perform a highly efficient, pointer-based swap
   item1.DataType().SwapScalarValues(item1.ValueAddr(), item2.ValueAddr());
}
int main() {
   uCalc uc;
   // Define the Swap function with ByRef parameters
   uc.DefineFunction("Swap(ByHandle a, ByHandle b)", SwapValues);

   // Define the variables to be swapped
   uc.DefineVariable("x = 100");
   uc.DefineVariable("y = 200");

   cout << "Before: x = " << uc.Eval("x") << ", y = " << uc.Eval("y") << endl;

   // Call the swap function
   uc.Eval("Swap(x, y)");

   cout << "After:  x = " << uc.Eval("x") << ", y = " << uc.Eval("y") << endl;
}