using System; using System.Runtime.CompilerServices; namespace OpenNest.Engine { /// /// A per-drawing cache entry that is current while the drawing keeps the program /// reference and source angle it was built from. /// internal abstract class DrawingCacheEntry { private readonly CNC.Program program; private readonly double sourceAngle; protected DrawingCacheEntry(Drawing source) { program = source.Program; sourceAngle = source.Source?.Angle ?? 0.0; } public bool IsCurrentFor(Drawing source) => ReferenceEquals(program, source.Program) && sourceAngle == (source.Source?.Angle ?? 0.0); /// /// Returns the current entry for , replacing a missing or /// stale one under . /// public static TEntry GetOrCreate( ConditionalWeakTable entries, object gate, Drawing source, Func create ) where TEntry : DrawingCacheEntry { if (entries.TryGetValue(source, out var entry) && entry.IsCurrentFor(source)) return entry; lock (gate) { if (entries.TryGetValue(source, out entry) && entry.IsCurrentFor(source)) return entry; entry = create(source); entries.AddOrUpdate(source, entry); return entry; } } } }