Owned

Method

Marks a Rule object for automatic memory release when it goes out of scope, enabling RAII-style lifetime management in C++.

Product: 

Class: 

Warning

uCalc API Preview Release Notice:The uCalc engine has successfully transitioned to modern cross-platform environments.The next phase envolves some structural changes, performance optimizations, and API refinements.The API is subject to breaking changes prior to the stable release. Please evaluate the preview version thoroughly before production use.

Syntax

Owned()

Parameters

[None]

Return

Rule

Returns the current Rule instance to allow for method chaining.

Remarks

🛡️ Object Ownership and Automatic Release

The Owned() method is a crucial memory management tool, primarily for C++, that flags a uCalc object for automatic release when its corresponding variable goes out of scope. This aligns with the C++ RAII (Resource Acquisition Is Initialization) paradigm, preventing memory leaks by ensuring resources are cleaned up deterministically.

This method returns the current Rule instance, enabling a fluent, chainable syntax.

⚙️ How It Works

By default, creating a uCalc object (like an Item, Expression, or Rule) allocates resources that persist until you explicitly call Release(). This is true even if the variable holding the object goes out of scope.

Calling Owned() on an object changes this behavior. The uCalc engine marks the object as "owned" by its current scope. When the scope is exited, the object's destructor is called, which in turn automatically calls Release().

Platform-Specific Usage: Owned() vs. using

The need for Owned() is specific to C++'s memory model. Other languages achieve the same result with different syntax:

  • C++: Use Owned() on a stack-allocated object or in conjunction with smart pointers.

using uCalcSoftware; var uc = new uCalc();

*   **C# / VB.NET**: Use the `using` statement (which relies on the `IDisposable` interface).    ```csharpusing uCalcSoftware;

var uc = new uCalc();


// The 'using' statement ensures myRule.Release() is called automatically.
using (var myRule = t.FromTo("a", "b"))
{
   // ... use myRule here ...
} // myRule is released here.

⚖️ Comparative Analysis

Owned() vs. Standard C++ RAII

In native C++, you would typically wrap a raw pointer in a smart pointer like std::unique_ptr or std::shared_ptr to manage its lifetime. uCalc's Owned() method provides a similar, built-in mechanism that is simpler and more direct for uCalc objects. It avoids the need for boilerplate smart pointer declarations.

Owned() vs. C# IDisposable

The concept is identical. Owned() is uCalc's C++-idiomatic way of achieving what IDisposable and using do in .NET. It provides developers with a consistent cross-platform mental model for resource management, even if the syntax differs.

Best Practice: In C++, always call Owned() on stack-allocated uCalc objects or objects whose lifetime is tied to a specific scope to prevent resource leaks. For heap-allocated objects that need to persist beyond their initial scope, manual Release() is still required.

Examples