Internal Test: Verifies the stack-like (LIFO) behavior of default instances using a scoped object.

ID: 269

				
					using uCalcSoftware;

var uc = new uCalc();
// Original default instance
uCalc.DefaultInstance.DefineVariable("id = 'Original'");
Console.WriteLine($"Current Default ID: {uCalc.DefaultInstance.EvalStr("id")}");

// Push a new default instance onto the stack
var ucA = new uCalc();
ucA.DefineVariable("id = 'Instance A'");
ucA.IsDefault = true;
Console.WriteLine($"Current Default ID: {uCalc.DefaultInstance.EvalStr("id")}");

// The 'NewUsing' block creates a temporary, scoped default instance

// The following instance will be auto-released at the end of the block
using (var ucB = new uCalc()) {
   ucB.DefineVariable("id = 'Instance B (temp)'");
   ucB.IsDefault = true; // Pushes 'ucB' onto the stack
   Console.WriteLine($"Current Default ID: {uCalc.DefaultInstance.EvalStr("id")}");
}

// 'ucB' has gone out of scope, so the default reverts to the previous one ('ucA')
Console.WriteLine($"Current Default ID (after scope exit): {uCalc.DefaultInstance.EvalStr("id")}");

// Manually unset 'ucA'
ucA.IsDefault = false;
Console.WriteLine($"Current Default ID (after unset): {uCalc.DefaultInstance.EvalStr("id")}");
				
			
Current Default ID: Original
Current Default ID: Instance A
Current Default ID: Instance B (temp)
Current Default ID (after scope exit): Instance A
Current Default ID (after unset): Original
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // Original default instance
   uCalc::DefaultInstance().DefineVariable("id = 'Original'");
   cout << "Current Default ID: " << uCalc::DefaultInstance().EvalStr("id") << endl;

   // Push a new default instance onto the stack
   uCalc ucA;
   ucA.DefineVariable("id = 'Instance A'");
   ucA.IsDefault(true);
   cout << "Current Default ID: " << uCalc::DefaultInstance().EvalStr("id") << endl;

   // The 'NewUsing' block creates a temporary, scoped default instance

   // The following instance will be auto-released at the end of the block
   {
      uCalc ucB;
      ucB.Owned(); // Causes ucB to be released when it goes out of scope
      ucB.DefineVariable("id = 'Instance B (temp)'");
      ucB.IsDefault(true); // Pushes 'ucB' onto the stack
      cout << "Current Default ID: " << uCalc::DefaultInstance().EvalStr("id") << endl;
   }

   // 'ucB' has gone out of scope, so the default reverts to the previous one ('ucA')
   cout << "Current Default ID (after scope exit): " << uCalc::DefaultInstance().EvalStr("id") << endl;

   // Manually unset 'ucA'
   ucA.IsDefault(false);
   cout << "Current Default ID (after unset): " << uCalc::DefaultInstance().EvalStr("id") << endl;
}
				
			
Current Default ID: Original
Current Default ID: Instance A
Current Default ID: Instance B (temp)
Current Default ID (after scope exit): Instance A
Current Default ID (after unset): Original
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// Original default instance
      uCalc.DefaultInstance.DefineVariable("id = 'Original'")
      Console.WriteLine($"Current Default ID: {uCalc.DefaultInstance.EvalStr("id")}")
      
      '// Push a new default instance onto the stack
      Dim ucA As New uCalc()
      ucA.DefineVariable("id = 'Instance A'")
      ucA.IsDefault = true
      Console.WriteLine($"Current Default ID: {uCalc.DefaultInstance.EvalStr("id")}")
      
      '// The 'NewUsing' block creates a temporary, scoped default instance
      
      '// The following instance will be auto-released at the end of the block
      Using ucB As New uCalc()
         ucB.DefineVariable("id = 'Instance B (temp)'")
         ucB.IsDefault = true '// Pushes 'ucB' onto the stack
         Console.WriteLine($"Current Default ID: {uCalc.DefaultInstance.EvalStr("id")}")
      End Using
      
      '// 'ucB' has gone out of scope, so the default reverts to the previous one ('ucA')
      Console.WriteLine($"Current Default ID (after scope exit): {uCalc.DefaultInstance.EvalStr("id")}")
      
      '// Manually unset 'ucA'
      ucA.IsDefault = false
      Console.WriteLine($"Current Default ID (after unset): {uCalc.DefaultInstance.EvalStr("id")}")
   End Sub
End Module
				
			
Current Default ID: Original
Current Default ID: Instance A
Current Default ID: Instance B (temp)
Current Default ID (after scope exit): Instance A
Current Default ID (after unset): Original