Sfoglia il codice sorgente

improving single child linking

Vicente Penades Armengot 10 mesi fa
parent
commit
9f0709a803

+ 6 - 18
src/SharpGLTF.Core/Collections/ChildSetter.cs

@@ -31,32 +31,20 @@ namespace SharpGLTF.Collections
 
         #region API        
 
-        public void SetProperty<T>(ref T target, T value)
-            where T : class, IChildOf<TParent>
+        public void SetProperty<TProperty, TValue>(ref TProperty target, TValue value)
+            where TProperty : class
+            where TValue: TProperty
         {
-            if (value == target) return;
+            if (Object.ReferenceEquals(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>
-        {
-            if (value == target) return;
 
-            // orphan the current child
-            target?.SetLogicalParent(null, -1);
+            if (target is IChildOf<TParent> oldChild) oldChild.SetLogicalParent(null);
             target = null;
 
             // adopt the new child
             target = value;
-            target?.SetLogicalParent(_Parent, 0);
+            if (target is IChildOf<TParent> newChild) newChild.SetLogicalParent(null);
         }
 
         #endregion

+ 1 - 1
src/SharpGLTF.Core/Collections/IChildOf.cs

@@ -12,7 +12,7 @@ namespace SharpGLTF.Collections
         where TParent : class
     {
         /// <summary>
-        /// Gets the logical parent that owns the collection containing this object.
+        /// Gets the logical parent that owns this object.
         /// </summary>
         TParent LogicalParent { get; }        
 

+ 4 - 12
src/SharpGLTF.Core/Schema2/gltf.Asset.cs

@@ -7,7 +7,7 @@ using SharpGLTF.Collections;
 namespace SharpGLTF.Schema2
 {
     [System.Diagnostics.DebuggerDisplay("{Version} {MinVersion} {Generator} {Copyright}")]
-    public sealed partial class Asset : Collections.IChildOfList<ModelRoot>
+    public sealed partial class Asset : Collections.IChildOf<ModelRoot>
     {
         #region lifecycle
 
@@ -28,17 +28,9 @@ namespace SharpGLTF.Schema2
             };
         }
 
-        void IChildOfList<ModelRoot>.SetLogicalParent(ModelRoot parent, int index)
-        {
-            _LogicalParent = parent;
-            _LogicalIndex = index;
-        }
-
-        private ModelRoot _LogicalParent;
-        private int _LogicalIndex;
+        void IChildOf<ModelRoot>.SetLogicalParent(ModelRoot parent) { LogicalParent = parent; }        
 
-        ModelRoot IChildOfList<ModelRoot>.LogicalParent => _LogicalParent;
-        int IChildOfList<ModelRoot>.LogicalIndex => _LogicalIndex;
+        public ModelRoot LogicalParent { get; private set; }        
 
         #endregion        
 
@@ -100,7 +92,7 @@ namespace SharpGLTF.Schema2
         public Asset Asset
         {
             get => _asset;
-            set => GetChildSetter(this).SetListProperty(ref _asset, value);
+            set => SetProperty(this, ref _asset, value);
         }
     }
 }

+ 8 - 0
src/SharpGLTF.Core/Schema2/gltf.ExtraProperties.cs

@@ -69,6 +69,14 @@ namespace SharpGLTF.Schema2
 
         #region API
 
+        protected static void SetProperty<TParent, TProperty, TValue>(TParent parent, ref TProperty property, TValue value)
+            where TParent : ExtraProperties
+            where TProperty: class
+            where TValue: TProperty
+        {
+            new Collections.ChildSetter<TParent>(parent).SetProperty(ref property, value);
+        }
+
         protected static Collections.ChildSetter<T> GetChildSetter<T>(T owner) where T:ExtraProperties
         {
             return new Collections.ChildSetter<T>(owner);