Remove sealed modifier and implement standard dispose pattern for CpSolver

The sealed modifier was unnecessarily breaking inheritance for users who need
to extend CpSolver. The standard IDisposable pattern with protected virtual
Dispose(bool) allows safe inheritance while maintaining proper resource cleanup.

This change:
- Removes the sealed modifier from CpSolver class
- Implements the standard dispose pattern with protected virtual Dispose(bool)
- Allows derived classes to override disposal behavior safely
- Maintains backward compatibility for existing non-inheriting code
This commit is contained in:
AliReZa Sabouri
2025-12-17 00:32:15 +01:00
committed by Corentin Le Molgat
parent 447ab9bfbb
commit ad91b2ddd3

View File

@@ -26,7 +26,7 @@ namespace Google.OrTools.Sat
* variables in the best solution, as well as general statistics of the search.
* </remarks>
*/
public sealed class CpSolver : IDisposable
public class CpSolver : IDisposable
{
private LogCallback? _log_callback;
private BestBoundCallback? _best_bound_callback;
@@ -207,19 +207,36 @@ public sealed class CpSolver : IDisposable
public string SolutionInfo() => Response!.SolutionInfo;
public void Dispose()
/// <summary>
/// Releases unmanaged resources and optionally releases managed resources.
/// </summary>
/// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
_best_bound_callback?.Dispose();
_log_callback?.Dispose();
ReleaseSolveWrapper();
if (disposing)
{
_best_bound_callback?.Dispose();
_log_callback?.Dispose();
ReleaseSolveWrapper();
}
_disposed = true;
}
/// <summary>
/// Releases all resources used by the CpSolver.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
[MethodImpl(MethodImplOptions.Synchronized)]
private void CreateSolveWrapper()
{
@@ -253,4 +270,4 @@ class BestBoundCallbackDelegate : BestBoundCallback
public override void NewBestBound(double bound) => _delegate(bound);
}
} // namespace Google.OrTools.Sat
} // namespace Google.OrTools.Sat