Skip to content

Commit

Permalink
move BinaryTable class to Memory workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
bertt committed Dec 27, 2023
1 parent e75dd9d commit a9af095
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Runtime.InteropServices;
using System.Text;

namespace SharpGLTF
namespace SharpGLTF.Memory
{
/// <summary>
/// Function for converting data into binary buffers
Expand Down Expand Up @@ -54,7 +54,7 @@ public static byte[] GetBytes<T>(IReadOnlyList<T> values)
{
return Vector4ToBytes(values);
}
else if(typeof(T) == typeof(Matrix4x4))
else if (typeof(T) == typeof(Matrix4x4))
{
return Matrix4x4ToBytes(values);
}
Expand Down Expand Up @@ -166,14 +166,14 @@ public static List<int> GetStringOffsets(List<string> values)

public static List<int> GetStringOffsets(List<List<string>> values)
{
var offsets = new List<int>() {};
var offsets = new List<int>() { };
foreach (var arr in values)
{
var arrOffsets = GetStringOffsets(arr);
var last = offsets.LastOrDefault();
foreach (var offset in arrOffsets)
{
if(!offsets.Contains(last + offset))
if (!offsets.Contains(last + offset))
{
offsets.Add(last + offset);
}
Expand All @@ -197,12 +197,12 @@ public static List<int> GetArrayOffsets<T>(List<List<T>> values)

public static int GetSize<T>()
{
#if NETSTANDARD2_0
#if NETSTANDARD2_0
var isValueType = typeof(T).IsValueType;
#else
var isValueType = !System.Runtime.CompilerServices.RuntimeHelpers.IsReferenceOrContainsReferences<T>();
#endif
#else
var isValueType = !System.Runtime.CompilerServices.RuntimeHelpers.IsReferenceOrContainsReferences<T>();
#endif

Guard.IsTrue(isValueType, nameof(T), "T must be a value type");

var type = typeof(T);
Expand Down
1 change: 1 addition & 0 deletions src/SharpGLTF.Cesium/Schema2/Ext.StructuralMetadataRoot.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OneOf;
using SharpGLTF.Memory;
using SharpGLTF.Validation;
using System;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Collections.Generic;
using System.Globalization;

namespace SharpGLTF
namespace SharpGLTF.Memory
{
public class BinaryTableTests
{
Expand Down Expand Up @@ -66,9 +66,9 @@ public void ConvertMatrix4x4ToBytes()
public void TestGetArrayOffset()
{
// arrange
var list0 = new List<string>(){ "hello", "world!"};
var list1 = new List<string>(){"test", "testtest"};
var arrays = new List<List<string>>() { list0, list1 };
var list0 = new List<string>() { "hello", "world!" };
var list1 = new List<string>() { "test", "testtest" };
var arrays = new List<List<string>>() { list0, list1 };

// act
var arrayOffsets = BinaryTable.GetArrayOffsets(arrays);
Expand All @@ -81,7 +81,7 @@ public void TestGetArrayOffset()
var l1 = list1.Count;

Assert.That(arrayOffsets[1], Is.EqualTo(l0));
Assert.That(arrayOffsets[2], Is.EqualTo(l0+l1));
Assert.That(arrayOffsets[2], Is.EqualTo(l0 + l1));

Assert.That(stringOffsets.Count, Is.EqualTo(list0.Count + list1.Count + 1));
Assert.That(stringOffsets[0], Is.EqualTo(0));
Expand All @@ -99,13 +99,13 @@ public void TestBinaryConversion()

bytes = BinaryTable.GetBytes(GetTestArray<int>());
Assert.That(bytes.Length, Is.EqualTo(BinaryTable.GetSize<int>() * 2));

bytes = BinaryTable.GetBytes(new List<string>() { "a", "b" });
Assert.That(bytes.Length, Is.EqualTo(2));

bytes = BinaryTable.GetBytes(new List<bool>() { true, false });
bytes = BinaryTable.GetBytes(new List<bool>() { true, false });
Assert.That(bytes.Length, Is.EqualTo(1));

var bits = new System.Collections.BitArray(bytes);
Assert.That(bits[0] == true);
Assert.That(bits[1] == false);
Expand All @@ -119,7 +119,7 @@ public void TestBinaryConversion()
private static List<T> GetTestArray<T>()
{
var l = new List<T>();
l.Add((T)Convert.ChangeType(0, typeof(T),CultureInfo.InvariantCulture));
l.Add((T)Convert.ChangeType(0, typeof(T), CultureInfo.InvariantCulture));
l.Add((T)Convert.ChangeType(1, typeof(T), CultureInfo.InvariantCulture));
return l;
}
Expand Down

0 comments on commit a9af095

Please sign in to comment.