Browse Source

Added more parent-child helpers

vpenades 2 years ago
parent
commit
fa2c996d91

+ 14 - 0
src/SharpGLTF.Core/Collections/ChildSetter.cs

@@ -31,6 +31,20 @@ namespace SharpGLTF.Collections
 
         #region API        
 
+        public void SetProperty<T>(ref T target, T value)
+            where T : class, IChildOf<TParent>
+        {
+            if (value == target) return;
+
+            // orphan the current child
+            target?.SetLogicalParent(null);
+            target = null;
+
+            // adopt the new child
+            target = value;
+            target?.SetLogicalParent(_Parent);
+        }
+
         public void SetListProperty<T>(ref T target, T value)
             where T : class, IChildOfList<TParent>
         {

+ 29 - 0
src/SharpGLTF.Core/Collections/IChildOf.cs

@@ -0,0 +1,29 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace SharpGLTF.Collections
+{
+    /// <summary>
+    /// Implemented by children of <see cref="ChildSetter{TParent}"/>
+    /// </summary>
+    /// <typeparam name="TParent">The type of the parent class containing the collection.</typeparam>
+    public interface IChildOf<TParent>
+        where TParent : class
+    {
+        /// <summary>
+        /// Gets the logical parent that owns the collection containing this object.
+        /// </summary>
+        TParent LogicalParent { get; }        
+
+        /// <summary>
+        /// Assigns a parent and index to this object.
+        /// </summary>
+        /// <param name="parent">The new parent, or null</param>        
+        /// <remarks>
+        /// For internal use of the collection.<br/>
+        /// ALWAYS IMPLEMENT EXPLICITLY!
+        /// </remarks>
+        void SetLogicalParent(TParent parent);
+    }
+}