Manage separate parser contexts for different application modules.
ID: 271
using uCalcSoftware;
var uc = new uCalc();
// --- Main Application Context ---
// Starting with the 'uc' object as the initial default.
uCalc.DefaultInstance.DefineConstant("PI = 3.14159");
Console.WriteLine($"Initial Count: {uCalc.DefaultCount}");
// --- A Plugin needs a temporary, isolated context ---
using (var moduleCalc = new uCalc()) {
moduleCalc.IsDefault = true; // Push the new instance onto the stack.
Console.WriteLine($"Count after module pushes new default: {uCalc.DefaultCount}");
// The module's context is now active.
// uCalc::DefaultInstance() would now return 'moduleCalc'.
}
// When 'moduleCalc' goes out of scope, it's destroyed and automatically
// removed from the stack, restoring the previous default.
Console.WriteLine($"Count after module instance is disposed: {uCalc.DefaultCount}");
// Verify the original default instance is active again.
var result = uCalc.DefaultInstance.EvalStr("PI");
Console.WriteLine($"Original context restored. PI = {result}");
Initial Count: 1
Count after module pushes new default: 2
Count after module instance is disposed: 1
Original context restored. PI = 3.14159 using uCalcSoftware; var uc = new uCalc(); // --- Main Application Context --- // Starting with the 'uc' object as the initial default. uCalc.DefaultInstance.DefineConstant("PI = 3.14159"); Console.WriteLine($"Initial Count: {uCalc.DefaultCount}"); // --- A Plugin needs a temporary, isolated context --- using (var moduleCalc = new uCalc()) { moduleCalc.IsDefault = true; // Push the new instance onto the stack. Console.WriteLine($"Count after module pushes new default: {uCalc.DefaultCount}"); // The module's context is now active. // uCalc::DefaultInstance() would now return 'moduleCalc'. } // When 'moduleCalc' goes out of scope, it's destroyed and automatically // removed from the stack, restoring the previous default. Console.WriteLine($"Count after module instance is disposed: {uCalc.DefaultCount}"); // Verify the original default instance is active again. var result = uCalc.DefaultInstance.EvalStr("PI"); Console.WriteLine($"Original context restored. PI = {result}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// --- Main Application Context ---
// Starting with the 'uc' object as the initial default.
uCalc::DefaultInstance().DefineConstant("PI = 3.14159");
cout << "Initial Count: " << uCalc::DefaultCount() << endl;
// --- A Plugin needs a temporary, isolated context ---
{
uCalc moduleCalc;
moduleCalc.Owned(); // Causes moduleCalc to be released when it goes out of scope
moduleCalc.IsDefault(true); // Push the new instance onto the stack.
cout << "Count after module pushes new default: " << uCalc::DefaultCount() << endl;
// The module's context is now active.
// uCalc::DefaultInstance() would now return 'moduleCalc'.
}
// When 'moduleCalc' goes out of scope, it's destroyed and automatically
// removed from the stack, restoring the previous default.
cout << "Count after module instance is disposed: " << uCalc::DefaultCount() << endl;
// Verify the original default instance is active again.
auto result = uCalc::DefaultInstance().EvalStr("PI");
cout << "Original context restored. PI = " << result << endl;
}
Initial Count: 1
Count after module pushes new default: 2
Count after module instance is disposed: 1
Original context restored. PI = 3.14159 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // --- Main Application Context --- // Starting with the 'uc' object as the initial default. uCalc::DefaultInstance().DefineConstant("PI = 3.14159"); cout << "Initial Count: " << uCalc::DefaultCount() << endl; // --- A Plugin needs a temporary, isolated context --- { uCalc moduleCalc; moduleCalc.Owned(); // Causes moduleCalc to be released when it goes out of scope moduleCalc.IsDefault(true); // Push the new instance onto the stack. cout << "Count after module pushes new default: " << uCalc::DefaultCount() << endl; // The module's context is now active. // uCalc::DefaultInstance() would now return 'moduleCalc'. } // When 'moduleCalc' goes out of scope, it's destroyed and automatically // removed from the stack, restoring the previous default. cout << "Count after module instance is disposed: " << uCalc::DefaultCount() << endl; // Verify the original default instance is active again. auto result = uCalc::DefaultInstance().EvalStr("PI"); cout << "Original context restored. PI = " << result << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// --- Main Application Context ---
'// Starting with the 'uc' object as the initial default.
uCalc.DefaultInstance.DefineConstant("PI = 3.14159")
Console.WriteLine($"Initial Count: {uCalc.DefaultCount}")
'// --- A Plugin needs a temporary, isolated context ---
Using moduleCalc As New uCalc()
moduleCalc.IsDefault = true '// Push the new instance onto the stack.
Console.WriteLine($"Count after module pushes new default: {uCalc.DefaultCount}")
'// The module's context is now active.
'// uCalc::DefaultInstance() would now return 'moduleCalc'.
End Using
'// When 'moduleCalc' goes out of scope, it's destroyed and automatically
'// removed from the stack, restoring the previous default.
Console.WriteLine($"Count after module instance is disposed: {uCalc.DefaultCount}")
'// Verify the original default instance is active again.
Dim result = uCalc.DefaultInstance.EvalStr("PI")
Console.WriteLine($"Original context restored. PI = {result}")
End Sub
End Module
Initial Count: 1
Count after module pushes new default: 2
Count after module instance is disposed: 1
Original context restored. PI = 3.14159 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// --- Main Application Context --- '// Starting with the 'uc' object as the initial default. uCalc.DefaultInstance.DefineConstant("PI = 3.14159") Console.WriteLine($"Initial Count: {uCalc.DefaultCount}") '// --- A Plugin needs a temporary, isolated context --- Using moduleCalc As New uCalc() moduleCalc.IsDefault = true '// Push the new instance onto the stack. Console.WriteLine($"Count after module pushes new default: {uCalc.DefaultCount}") '// The module's context is now active. '// uCalc::DefaultInstance() would now return 'moduleCalc'. End Using '// When 'moduleCalc' goes out of scope, it's destroyed and automatically '// removed from the stack, restoring the previous default. Console.WriteLine($"Count after module instance is disposed: {uCalc.DefaultCount}") '// Verify the original default instance is active again. Dim result = uCalc.DefaultInstance.EvalStr("PI") Console.WriteLine($"Original context restored. PI = {result}") End Sub End Module