using System; using System.Collections.Concurrent; namespace Rive { /// /// Base class representing any value that can be a property of a ViewModelInstance. /// This includes both primitive property values and nested view model instances. /// public abstract class ViewModelInstanceProperty { /// /// Cache of property instances to avoid creating multiple instances for the same native property /// private static readonly ConcurrentDictionary> s_propertiesCache = new ConcurrentDictionary>(); /// /// Adds a globally cached view model property for a given pointer. /// /// The pointer to the property. /// The C# property to cache. internal static void AddGloballyCachedVMPropertyForPointer(IntPtr ptr, ViewModelInstanceProperty property) { s_propertiesCache[ptr] = new WeakReference(property); } /// /// Tries to get a globally cached view model property for a given pointer. /// /// The pointer to the property. /// The C# property to cache. /// internal static bool TryGetGloballyCachedVMPropertyForPointer(IntPtr ptr, out ViewModelInstanceProperty property) { if (s_propertiesCache.TryGetValue(ptr, out var weakReference) && weakReference.TryGetTarget(out property)) { return true; } property = null; return false; } /// /// Removes a cached view model property for a given pointer. /// /// /// The pointer to the property. internal static void RemoveCachedPropertyForPointer(IntPtr ptr) { s_propertiesCache.TryRemove(ptr, out _); } } }