From 20ba35f4917fc1886e4862b94c4eba47cd6571e4 Mon Sep 17 00:00:00 2001 From: Thad House Date: Fri, 16 Feb 2024 14:15:00 -0800 Subject: [PATCH] Ungenerate string, finish the rest of the log types --- .../LogGenerator.cs | 2 +- src/ntcore/Generated/BooleanArrayEntryImpl.cs | 1 - src/ntcore/Generated/BooleanArrayPublisher.cs | 1 - src/ntcore/Generated/BooleanArrayTopic.cs | 12 +- src/ntcore/Generated/BooleanEntryImpl.cs | 1 - src/ntcore/Generated/BooleanPublisher.cs | 1 - src/ntcore/Generated/DoubleArrayEntryImpl.cs | 1 - src/ntcore/Generated/DoubleArrayPublisher.cs | 1 - src/ntcore/Generated/DoubleArrayTopic.cs | 12 +- src/ntcore/Generated/DoubleEntryImpl.cs | 1 - src/ntcore/Generated/DoublePublisher.cs | 1 - src/ntcore/Generated/FloatArrayEntryImpl.cs | 1 - src/ntcore/Generated/FloatArrayPublisher.cs | 1 - src/ntcore/Generated/FloatArrayTopic.cs | 12 +- src/ntcore/Generated/FloatEntryImpl.cs | 1 - src/ntcore/Generated/FloatPublisher.cs | 1 - src/ntcore/Generated/GenericEntryImpl.cs | 50 --- src/ntcore/Generated/GenericPublisher.cs | 44 --- src/ntcore/Generated/GenericSubscriber.cs | 16 - src/ntcore/Generated/IntegerArrayEntryImpl.cs | 1 - src/ntcore/Generated/IntegerArrayPublisher.cs | 1 - src/ntcore/Generated/IntegerArrayTopic.cs | 12 +- src/ntcore/Generated/IntegerEntryImpl.cs | 1 - src/ntcore/Generated/IntegerPublisher.cs | 1 - src/ntcore/Generated/NetworkTable.cs | 20 -- src/ntcore/Generated/NetworkTableInstance.cs | 54 --- src/ntcore/Generated/NetworkTableValue.cs | 92 +---- src/ntcore/Generated/RawEntryImpl.cs | 1 - src/ntcore/Generated/RawPublisher.cs | 1 - src/ntcore/Generated/RawTopic.cs | 8 +- src/ntcore/Generated/RefNetworkTableValue.cs | 141 +------- src/ntcore/Generated/StringPublisher.cs | 41 --- .../Natives/RefNetworkTableValueMarshaller.cs | 31 +- src/ntcore/NetworkTable.cs | 20 ++ src/ntcore/NetworkTableInstance.cs | 54 +++ src/ntcore/NetworkTableValue.cs | 80 +++++ src/ntcore/RefNetworkTableValue.cs | 130 +++++++ .../StringArrayEntry.cs | 0 .../StringArrayEntryImpl.cs | 4 +- .../StringArrayPublisher.cs | 0 .../StringArraySubscriber.cs | 0 .../StringArrayTopic.cs | 12 +- .../{Generated => Strings}/StringEntry.cs | 0 .../{Generated => Strings}/StringEntryImpl.cs | 40 ++- src/ntcore/Strings/StringPublisher.cs | 80 +++++ .../StringSubscriber.cs | 0 .../{Generated => Strings}/StringTopic.cs | 12 +- .../Stereologue/DataLogSendableBulder.cs | 316 ------------------ src/thirdparty/Stereologue/DataLogger.cs | 6 - .../Stereologue/EvaluateAttributes.cs | 12 - src/thirdparty/Stereologue/EvaluateField.cs | 24 -- src/thirdparty/Stereologue/LogType.cs | 1 + src/thirdparty/Stereologue/NTLogger.cs | 10 - src/thirdparty/Stereologue/Stereologuer.cs | 161 ++++++++- src/wpiutil/Logging/StringLogEntry.cs | 10 + test/stereologue.test/TestTree.cs | 8 +- 56 files changed, 637 insertions(+), 908 deletions(-) delete mode 100644 src/ntcore/Generated/StringPublisher.cs rename src/ntcore/{Generated => Strings}/StringArrayEntry.cs (100%) rename src/ntcore/{Generated => Strings}/StringArrayEntryImpl.cs (98%) rename src/ntcore/{Generated => Strings}/StringArrayPublisher.cs (100%) rename src/ntcore/{Generated => Strings}/StringArraySubscriber.cs (100%) rename src/ntcore/{Generated => Strings}/StringArrayTopic.cs (98%) rename src/ntcore/{Generated => Strings}/StringEntry.cs (100%) rename src/ntcore/{Generated => Strings}/StringEntryImpl.cs (71%) create mode 100644 src/ntcore/Strings/StringPublisher.cs rename src/ntcore/{Generated => Strings}/StringSubscriber.cs (100%) rename src/ntcore/{Generated => Strings}/StringTopic.cs (98%) delete mode 100644 src/thirdparty/Stereologue/DataLogSendableBulder.cs delete mode 100644 src/thirdparty/Stereologue/DataLogger.cs delete mode 100644 src/thirdparty/Stereologue/EvaluateAttributes.cs delete mode 100644 src/thirdparty/Stereologue/EvaluateField.cs delete mode 100644 src/thirdparty/Stereologue/NTLogger.cs diff --git a/sourcegeneration/StereologueSourceGenerator/LogGenerator.cs b/sourcegeneration/StereologueSourceGenerator/LogGenerator.cs index 1030a36f..1b129a34 100644 --- a/sourcegeneration/StereologueSourceGenerator/LogGenerator.cs +++ b/sourcegeneration/StereologueSourceGenerator/LogGenerator.cs @@ -219,7 +219,7 @@ static void ConstructCall(LogData data, StringBuilder builder) "System.UInt64" => ("LogInteger", "(long)", ""), "System.Boolean" => ("LogBoolean", "", ""), "System.Char" => ("LogChar", "", ""), - "System.String" => ("LogString", "", ""), + "System.String" => ("LogString", "", ".AsSpan()"), "System.Single[]" => ("LogFloatArray", "", ".AsSpan()"), "System.Double[]" => ("LogDoubleArray", "", ".AsSpan()"), "System.Int64[]" => ("LogIntegerArray", "", ".AsSpan()"), diff --git a/src/ntcore/Generated/BooleanArrayEntryImpl.cs b/src/ntcore/Generated/BooleanArrayEntryImpl.cs index fc714bf4..8b377caa 100644 --- a/src/ntcore/Generated/BooleanArrayEntryImpl.cs +++ b/src/ntcore/Generated/BooleanArrayEntryImpl.cs @@ -76,7 +76,6 @@ public bool[][] ReadQueueValues() return timestamped; } - public void Set(ReadOnlySpan value) { RefNetworkTableValue ntValue = RefNetworkTableValue.MakeBooleanArray(value, 0); diff --git a/src/ntcore/Generated/BooleanArrayPublisher.cs b/src/ntcore/Generated/BooleanArrayPublisher.cs index 749804d9..b7672845 100644 --- a/src/ntcore/Generated/BooleanArrayPublisher.cs +++ b/src/ntcore/Generated/BooleanArrayPublisher.cs @@ -18,7 +18,6 @@ public interface IBooleanArrayPublisher : IPublisher /// new BooleanArrayTopic Topic { get; } - /// /// Publish a new value using the current NT time. /// diff --git a/src/ntcore/Generated/BooleanArrayTopic.cs b/src/ntcore/Generated/BooleanArrayTopic.cs index 75991405..dc3f086e 100644 --- a/src/ntcore/Generated/BooleanArrayTopic.cs +++ b/src/ntcore/Generated/BooleanArrayTopic.cs @@ -62,7 +62,7 @@ public IBooleanArraySubscriber Subscribe( NtCore.Subscribe( Handle, NetworkTableType.BooleanArray, "boolean[]"u8, options), - defaultValue); + defaultValue ?? []); } /// @@ -91,7 +91,7 @@ public IBooleanArraySubscriber SubscribeEx( NtCore.Subscribe( Handle, NetworkTableType.BooleanArray, typeString, options), - defaultValue); + defaultValue ?? []); } /// @@ -120,7 +120,7 @@ public IBooleanArraySubscriber SubscribeEx( NtCore.Subscribe( Handle, NetworkTableType.BooleanArray, typeString, options), - defaultValue); + defaultValue ?? []); } /// @@ -292,7 +292,7 @@ public IBooleanArrayEntry GetEntry( NtCore.GetEntry( Handle, NetworkTableType.BooleanArray, "boolean[]"u8, options), - defaultValue); + defaultValue ?? []); } /// @@ -326,7 +326,7 @@ public IBooleanArrayEntry GetEntryEx( NtCore.GetEntry( Handle, NetworkTableType.BooleanArray, typeString, options), - defaultValue); + defaultValue ?? []); } /// @@ -360,7 +360,7 @@ public IBooleanArrayEntry GetEntryEx( NtCore.GetEntry( Handle, NetworkTableType.BooleanArray, typeString, options), - defaultValue); + defaultValue ?? []); } } diff --git a/src/ntcore/Generated/BooleanEntryImpl.cs b/src/ntcore/Generated/BooleanEntryImpl.cs index 681dce87..c21db2eb 100644 --- a/src/ntcore/Generated/BooleanEntryImpl.cs +++ b/src/ntcore/Generated/BooleanEntryImpl.cs @@ -76,7 +76,6 @@ public bool[] ReadQueueValues() return timestamped; } - public void Set(bool value) { RefNetworkTableValue ntValue = RefNetworkTableValue.MakeBoolean(value, 0); diff --git a/src/ntcore/Generated/BooleanPublisher.cs b/src/ntcore/Generated/BooleanPublisher.cs index 2584dedf..a2c5e99a 100644 --- a/src/ntcore/Generated/BooleanPublisher.cs +++ b/src/ntcore/Generated/BooleanPublisher.cs @@ -18,7 +18,6 @@ public interface IBooleanPublisher : IPublisher /// new BooleanTopic Topic { get; } - /// /// Publish a new value using the current NT time. /// diff --git a/src/ntcore/Generated/DoubleArrayEntryImpl.cs b/src/ntcore/Generated/DoubleArrayEntryImpl.cs index 77f67cd9..63907cda 100644 --- a/src/ntcore/Generated/DoubleArrayEntryImpl.cs +++ b/src/ntcore/Generated/DoubleArrayEntryImpl.cs @@ -76,7 +76,6 @@ public double[][] ReadQueueValues() return timestamped; } - public void Set(ReadOnlySpan value) { RefNetworkTableValue ntValue = RefNetworkTableValue.MakeDoubleArray(value, 0); diff --git a/src/ntcore/Generated/DoubleArrayPublisher.cs b/src/ntcore/Generated/DoubleArrayPublisher.cs index 6297f33a..5018588a 100644 --- a/src/ntcore/Generated/DoubleArrayPublisher.cs +++ b/src/ntcore/Generated/DoubleArrayPublisher.cs @@ -18,7 +18,6 @@ public interface IDoubleArrayPublisher : IPublisher /// new DoubleArrayTopic Topic { get; } - /// /// Publish a new value using the current NT time. /// diff --git a/src/ntcore/Generated/DoubleArrayTopic.cs b/src/ntcore/Generated/DoubleArrayTopic.cs index c8c722ad..c86dcc29 100644 --- a/src/ntcore/Generated/DoubleArrayTopic.cs +++ b/src/ntcore/Generated/DoubleArrayTopic.cs @@ -62,7 +62,7 @@ public IDoubleArraySubscriber Subscribe( NtCore.Subscribe( Handle, NetworkTableType.DoubleArray, "double[]"u8, options), - defaultValue); + defaultValue ?? []); } /// @@ -91,7 +91,7 @@ public IDoubleArraySubscriber SubscribeEx( NtCore.Subscribe( Handle, NetworkTableType.DoubleArray, typeString, options), - defaultValue); + defaultValue ?? []); } /// @@ -120,7 +120,7 @@ public IDoubleArraySubscriber SubscribeEx( NtCore.Subscribe( Handle, NetworkTableType.DoubleArray, typeString, options), - defaultValue); + defaultValue ?? []); } /// @@ -292,7 +292,7 @@ public IDoubleArrayEntry GetEntry( NtCore.GetEntry( Handle, NetworkTableType.DoubleArray, "double[]"u8, options), - defaultValue); + defaultValue ?? []); } /// @@ -326,7 +326,7 @@ public IDoubleArrayEntry GetEntryEx( NtCore.GetEntry( Handle, NetworkTableType.DoubleArray, typeString, options), - defaultValue); + defaultValue ?? []); } /// @@ -360,7 +360,7 @@ public IDoubleArrayEntry GetEntryEx( NtCore.GetEntry( Handle, NetworkTableType.DoubleArray, typeString, options), - defaultValue); + defaultValue ?? []); } } diff --git a/src/ntcore/Generated/DoubleEntryImpl.cs b/src/ntcore/Generated/DoubleEntryImpl.cs index 9a6f8e55..33a0abf0 100644 --- a/src/ntcore/Generated/DoubleEntryImpl.cs +++ b/src/ntcore/Generated/DoubleEntryImpl.cs @@ -76,7 +76,6 @@ public double[] ReadQueueValues() return timestamped; } - public void Set(double value) { RefNetworkTableValue ntValue = RefNetworkTableValue.MakeDouble(value, 0); diff --git a/src/ntcore/Generated/DoublePublisher.cs b/src/ntcore/Generated/DoublePublisher.cs index c2882142..2be17170 100644 --- a/src/ntcore/Generated/DoublePublisher.cs +++ b/src/ntcore/Generated/DoublePublisher.cs @@ -18,7 +18,6 @@ public interface IDoublePublisher : IPublisher /// new DoubleTopic Topic { get; } - /// /// Publish a new value using the current NT time. /// diff --git a/src/ntcore/Generated/FloatArrayEntryImpl.cs b/src/ntcore/Generated/FloatArrayEntryImpl.cs index ff53af7e..6cd35865 100644 --- a/src/ntcore/Generated/FloatArrayEntryImpl.cs +++ b/src/ntcore/Generated/FloatArrayEntryImpl.cs @@ -76,7 +76,6 @@ public float[][] ReadQueueValues() return timestamped; } - public void Set(ReadOnlySpan value) { RefNetworkTableValue ntValue = RefNetworkTableValue.MakeFloatArray(value, 0); diff --git a/src/ntcore/Generated/FloatArrayPublisher.cs b/src/ntcore/Generated/FloatArrayPublisher.cs index d952c32b..c4116a9d 100644 --- a/src/ntcore/Generated/FloatArrayPublisher.cs +++ b/src/ntcore/Generated/FloatArrayPublisher.cs @@ -18,7 +18,6 @@ public interface IFloatArrayPublisher : IPublisher /// new FloatArrayTopic Topic { get; } - /// /// Publish a new value using the current NT time. /// diff --git a/src/ntcore/Generated/FloatArrayTopic.cs b/src/ntcore/Generated/FloatArrayTopic.cs index 96ca6b42..9b707178 100644 --- a/src/ntcore/Generated/FloatArrayTopic.cs +++ b/src/ntcore/Generated/FloatArrayTopic.cs @@ -62,7 +62,7 @@ public IFloatArraySubscriber Subscribe( NtCore.Subscribe( Handle, NetworkTableType.FloatArray, "float[]"u8, options), - defaultValue); + defaultValue ?? []); } /// @@ -91,7 +91,7 @@ public IFloatArraySubscriber SubscribeEx( NtCore.Subscribe( Handle, NetworkTableType.FloatArray, typeString, options), - defaultValue); + defaultValue ?? []); } /// @@ -120,7 +120,7 @@ public IFloatArraySubscriber SubscribeEx( NtCore.Subscribe( Handle, NetworkTableType.FloatArray, typeString, options), - defaultValue); + defaultValue ?? []); } /// @@ -292,7 +292,7 @@ public IFloatArrayEntry GetEntry( NtCore.GetEntry( Handle, NetworkTableType.FloatArray, "float[]"u8, options), - defaultValue); + defaultValue ?? []); } /// @@ -326,7 +326,7 @@ public IFloatArrayEntry GetEntryEx( NtCore.GetEntry( Handle, NetworkTableType.FloatArray, typeString, options), - defaultValue); + defaultValue ?? []); } /// @@ -360,7 +360,7 @@ public IFloatArrayEntry GetEntryEx( NtCore.GetEntry( Handle, NetworkTableType.FloatArray, typeString, options), - defaultValue); + defaultValue ?? []); } } diff --git a/src/ntcore/Generated/FloatEntryImpl.cs b/src/ntcore/Generated/FloatEntryImpl.cs index 13f1771b..de3116f9 100644 --- a/src/ntcore/Generated/FloatEntryImpl.cs +++ b/src/ntcore/Generated/FloatEntryImpl.cs @@ -76,7 +76,6 @@ public float[] ReadQueueValues() return timestamped; } - public void Set(float value) { RefNetworkTableValue ntValue = RefNetworkTableValue.MakeFloat(value, 0); diff --git a/src/ntcore/Generated/FloatPublisher.cs b/src/ntcore/Generated/FloatPublisher.cs index 4009b78f..6d99ae25 100644 --- a/src/ntcore/Generated/FloatPublisher.cs +++ b/src/ntcore/Generated/FloatPublisher.cs @@ -18,7 +18,6 @@ public interface IFloatPublisher : IPublisher /// new FloatTopic Topic { get; } - /// /// Publish a new value using the current NT time. /// diff --git a/src/ntcore/Generated/GenericEntryImpl.cs b/src/ntcore/Generated/GenericEntryImpl.cs index 7be8087b..00dbf325 100644 --- a/src/ntcore/Generated/GenericEntryImpl.cs +++ b/src/ntcore/Generated/GenericEntryImpl.cs @@ -112,31 +112,6 @@ public bool SetDefaultDouble(double value) return NtCore.SetDefaultEntryValue(Handle, RefNetworkTableValue.MakeDouble(value)); } - public string GetString(string defaultValue) - { - NetworkTableValue value = NtCore.GetEntryValue(Handle); - if (value.IsString) - { - return value.GetString(); - } - return defaultValue; - } - - public bool SetString(string value) - { - return SetString(value, 0); - } - - public bool SetString(string value, long time) - { - return NtCore.SetEntryValue(Handle, RefNetworkTableValue.MakeString(value, time)); - } - - public bool SetDefaultString(string value) - { - return NtCore.SetDefaultEntryValue(Handle, RefNetworkTableValue.MakeString(value)); - } - public byte[] GetRaw(byte[] defaultValue) { NetworkTableValue value = NtCore.GetEntryValue(Handle); @@ -262,31 +237,6 @@ public bool SetDefaultDoubleArray(double[] value) return NtCore.SetDefaultEntryValue(Handle, RefNetworkTableValue.MakeDoubleArray(value)); } - public string[] GetStringArray(string[] defaultValue) - { - NetworkTableValue value = NtCore.GetEntryValue(Handle); - if (value.IsStringArray) - { - return value.GetStringArray(); - } - return defaultValue; - } - - public bool SetStringArray(string[] value) - { - return SetStringArray(value, 0); - } - - public bool SetStringArray(string[] value, long time) - { - return NtCore.SetEntryValue(Handle, RefNetworkTableValue.MakeStringArray(value, time)); - } - - public bool SetDefaultStringArray(string[] value) - { - return NtCore.SetDefaultEntryValue(Handle, RefNetworkTableValue.MakeStringArray(value)); - } - public void Unpublish() { diff --git a/src/ntcore/Generated/GenericPublisher.cs b/src/ntcore/Generated/GenericPublisher.cs index e118c760..a22e19f2 100644 --- a/src/ntcore/Generated/GenericPublisher.cs +++ b/src/ntcore/Generated/GenericPublisher.cs @@ -97,28 +97,6 @@ public partial interface IGenericPublisher /// False if the entry already exists with a different type bool SetDefaultDouble(double defaultValue); - /// - /// Publish a new value. - /// - /// value to publish - /// timestamp; 0 indicates current NT time should be used - /// False if the topic already exists with a different type - bool SetString(string value, long time); - - /// - /// Publish a new value. - /// - /// value to publish - /// False if the topic already exists with a different type - bool SetString(string value); - - /// - /// Sets the entry's value if it does not exist. - /// - /// the default value to set - /// False if the entry already exists with a different type - bool SetDefaultString(string defaultValue); - /// /// Publish a new value. /// @@ -229,26 +207,4 @@ public partial interface IGenericPublisher /// False if the entry already exists with a different type bool SetDefaultDoubleArray(double[] defaultValue); - /// - /// Publish a new value. - /// - /// value to publish - /// timestamp; 0 indicates current NT time should be used - /// False if the topic already exists with a different type - bool SetStringArray(string[] value, long time); - - /// - /// Publish a new value. - /// - /// value to publish - /// False if the topic already exists with a different type - bool SetStringArray(string[] value); - - /// - /// Sets the entry's value if it does not exist. - /// - /// the default value to set - /// False if the entry already exists with a different type - bool SetDefaultStringArray(string[] defaultValue); - } diff --git a/src/ntcore/Generated/GenericSubscriber.cs b/src/ntcore/Generated/GenericSubscriber.cs index 722647eb..aab51a3c 100644 --- a/src/ntcore/Generated/GenericSubscriber.cs +++ b/src/ntcore/Generated/GenericSubscriber.cs @@ -41,14 +41,6 @@ public partial interface IGenericSubscriber /// the entry's value or the given default value double GetDouble(double defaultValue); - /// - /// Gets the entry's value as a string. If the entry does not exist - /// or is of a different type, it will return the default value. - /// - /// the value to be returned if no value is found - /// the entry's value or the given default value - string GetString(string defaultValue); - /// /// Gets the entry's value as a byte[]. If the entry does not exist /// or is of a different type, it will return the default value. @@ -89,12 +81,4 @@ public partial interface IGenericSubscriber /// the entry's value or the given default value double[] GetDoubleArray(double[] defaultValue); - /// - /// Gets the entry's value as a string[]. If the entry does not exist - /// or is of a different type, it will return the default value. - /// - /// the value to be returned if no value is found - /// the entry's value or the given default value - string[] GetStringArray(string[] defaultValue); - } diff --git a/src/ntcore/Generated/IntegerArrayEntryImpl.cs b/src/ntcore/Generated/IntegerArrayEntryImpl.cs index a91f0020..3e0818b6 100644 --- a/src/ntcore/Generated/IntegerArrayEntryImpl.cs +++ b/src/ntcore/Generated/IntegerArrayEntryImpl.cs @@ -76,7 +76,6 @@ public long[][] ReadQueueValues() return timestamped; } - public void Set(ReadOnlySpan value) { RefNetworkTableValue ntValue = RefNetworkTableValue.MakeIntegerArray(value, 0); diff --git a/src/ntcore/Generated/IntegerArrayPublisher.cs b/src/ntcore/Generated/IntegerArrayPublisher.cs index 3b8eadf6..735cf733 100644 --- a/src/ntcore/Generated/IntegerArrayPublisher.cs +++ b/src/ntcore/Generated/IntegerArrayPublisher.cs @@ -18,7 +18,6 @@ public interface IIntegerArrayPublisher : IPublisher /// new IntegerArrayTopic Topic { get; } - /// /// Publish a new value using the current NT time. /// diff --git a/src/ntcore/Generated/IntegerArrayTopic.cs b/src/ntcore/Generated/IntegerArrayTopic.cs index c183e4dc..e3efe7ab 100644 --- a/src/ntcore/Generated/IntegerArrayTopic.cs +++ b/src/ntcore/Generated/IntegerArrayTopic.cs @@ -62,7 +62,7 @@ public IIntegerArraySubscriber Subscribe( NtCore.Subscribe( Handle, NetworkTableType.IntegerArray, "int[]"u8, options), - defaultValue); + defaultValue ?? []); } /// @@ -91,7 +91,7 @@ public IIntegerArraySubscriber SubscribeEx( NtCore.Subscribe( Handle, NetworkTableType.IntegerArray, typeString, options), - defaultValue); + defaultValue ?? []); } /// @@ -120,7 +120,7 @@ public IIntegerArraySubscriber SubscribeEx( NtCore.Subscribe( Handle, NetworkTableType.IntegerArray, typeString, options), - defaultValue); + defaultValue ?? []); } /// @@ -292,7 +292,7 @@ public IIntegerArrayEntry GetEntry( NtCore.GetEntry( Handle, NetworkTableType.IntegerArray, "int[]"u8, options), - defaultValue); + defaultValue ?? []); } /// @@ -326,7 +326,7 @@ public IIntegerArrayEntry GetEntryEx( NtCore.GetEntry( Handle, NetworkTableType.IntegerArray, typeString, options), - defaultValue); + defaultValue ?? []); } /// @@ -360,7 +360,7 @@ public IIntegerArrayEntry GetEntryEx( NtCore.GetEntry( Handle, NetworkTableType.IntegerArray, typeString, options), - defaultValue); + defaultValue ?? []); } } diff --git a/src/ntcore/Generated/IntegerEntryImpl.cs b/src/ntcore/Generated/IntegerEntryImpl.cs index f885ee63..6b820beb 100644 --- a/src/ntcore/Generated/IntegerEntryImpl.cs +++ b/src/ntcore/Generated/IntegerEntryImpl.cs @@ -76,7 +76,6 @@ public long[] ReadQueueValues() return timestamped; } - public void Set(long value) { RefNetworkTableValue ntValue = RefNetworkTableValue.MakeInteger(value, 0); diff --git a/src/ntcore/Generated/IntegerPublisher.cs b/src/ntcore/Generated/IntegerPublisher.cs index d4ff6c00..cf07b2f4 100644 --- a/src/ntcore/Generated/IntegerPublisher.cs +++ b/src/ntcore/Generated/IntegerPublisher.cs @@ -18,7 +18,6 @@ public interface IIntegerPublisher : IPublisher /// new IntegerTopic Topic { get; } - /// /// Publish a new value using the current NT time. /// diff --git a/src/ntcore/Generated/NetworkTable.cs b/src/ntcore/Generated/NetworkTable.cs index d2028224..18612eb2 100644 --- a/src/ntcore/Generated/NetworkTable.cs +++ b/src/ntcore/Generated/NetworkTable.cs @@ -48,16 +48,6 @@ public DoubleTopic GetDoubleTopic(string name) return Instance.GetDoubleTopic($"{m_pathWithSep}{name}"); } - /// - /// Gets a string topic. - /// - /// topic name - /// StringTopic - public StringTopic GetStringTopic(string name) - { - return Instance.GetStringTopic($"{m_pathWithSep}{name}"); - } - /// /// Gets a byte[] topic. /// @@ -107,14 +97,4 @@ public DoubleArrayTopic GetDoubleArrayTopic(string name) { return Instance.GetDoubleArrayTopic($"{m_pathWithSep}{name}"); } - - /// - /// Gets a string[] topic. - /// - /// topic name - /// StringArrayTopic - public StringArrayTopic GetStringArrayTopic(string name) - { - return Instance.GetStringArrayTopic($"{m_pathWithSep}{name}"); - } } diff --git a/src/ntcore/Generated/NetworkTableInstance.cs b/src/ntcore/Generated/NetworkTableInstance.cs index 7c2f1de8..d5c24670 100644 --- a/src/ntcore/Generated/NetworkTableInstance.cs +++ b/src/ntcore/Generated/NetworkTableInstance.cs @@ -118,33 +118,6 @@ public DoubleTopic GetDoubleTopic(string name) return (DoubleTopic)m_topics.AddOrUpdate(name, DoubleTopicCreator, DoubleTopicUpdator, this); } - private static StringTopic StringTopicCreator(string name, NetworkTableInstance instance) - { - StringTopic topic = new StringTopic(instance, NtCore.GetTopic(instance.Handle, name)); - instance.m_topicsByHandle.AddOrUpdate(topic.Handle, TopicByHandleAdder, TopicByHandleUpdater, topic); - return topic; - } - private static StringTopic StringTopicUpdator(string name, Topic existingTopic, NetworkTableInstance instance) - { - // Exists, but might be wrong type - if (existingTopic is StringTopic StringTopic) - { - return StringTopic; - } - StringTopic topic = new StringTopic(instance, existingTopic.Handle); - instance.m_topicsByHandle.AddOrUpdate(topic.Handle, TopicByHandleAdder, TopicByHandleUpdater, topic); - return topic; - } - /// - /// Gets a string topic. - /// - /// topic name - /// StringTopic - public StringTopic GetStringTopic(string name) - { - return (StringTopic)m_topics.AddOrUpdate(name, StringTopicCreator, StringTopicUpdator, this); - } - private static RawTopic RawTopicCreator(string name, NetworkTableInstance instance) { RawTopic topic = new RawTopic(instance, NtCore.GetTopic(instance.Handle, name)); @@ -279,31 +252,4 @@ public DoubleArrayTopic GetDoubleArrayTopic(string name) { return (DoubleArrayTopic)m_topics.AddOrUpdate(name, DoubleArrayTopicCreator, DoubleArrayTopicUpdator, this); } - - private static StringArrayTopic StringArrayTopicCreator(string name, NetworkTableInstance instance) - { - StringArrayTopic topic = new StringArrayTopic(instance, NtCore.GetTopic(instance.Handle, name)); - instance.m_topicsByHandle.AddOrUpdate(topic.Handle, TopicByHandleAdder, TopicByHandleUpdater, topic); - return topic; - } - private static StringArrayTopic StringArrayTopicUpdator(string name, Topic existingTopic, NetworkTableInstance instance) - { - // Exists, but might be wrong type - if (existingTopic is StringArrayTopic StringArrayTopic) - { - return StringArrayTopic; - } - StringArrayTopic topic = new StringArrayTopic(instance, existingTopic.Handle); - instance.m_topicsByHandle.AddOrUpdate(topic.Handle, TopicByHandleAdder, TopicByHandleUpdater, topic); - return topic; - } - /// - /// Gets a string[] topic. - /// - /// topic name - /// StringArrayTopic - public StringArrayTopic GetStringArrayTopic(string name) - { - return (StringArrayTopic)m_topics.AddOrUpdate(name, StringArrayTopicCreator, StringArrayTopicUpdator, this); - } } diff --git a/src/ntcore/Generated/NetworkTableValue.cs b/src/ntcore/Generated/NetworkTableValue.cs index fbbf90b3..2f6e8e81 100644 --- a/src/ntcore/Generated/NetworkTableValue.cs +++ b/src/ntcore/Generated/NetworkTableValue.cs @@ -31,11 +31,6 @@ public readonly partial struct NetworkTableValue /// public bool IsDouble => Type == NetworkTableType.Double; - /// - /// Gets if entry value contains a string. - /// - public bool IsString => Type == NetworkTableType.String; - /// /// Gets if entry value contains a byte[]. /// @@ -61,11 +56,6 @@ public readonly partial struct NetworkTableValue /// public bool IsDoubleArray => Type == NetworkTableType.DoubleArray; - /// - /// Gets if entry value contains a string[]. - /// - public bool IsStringArray => Type == NetworkTableType.StringArray; - /// /// Get the bool value. @@ -123,20 +113,6 @@ public double GetDouble() return m_structValue.doubleValue; } - /// - /// Get the string value. - /// - /// The string value. - /// Thrown if the entry value is not of string type. - public string GetString() - { - if (Type != NetworkTableType.String) - { - throw new InvalidCastException($"cannot convert {Type} to string"); - } - return (string)m_objectValue!; - } - /// /// Get the byte[] value. /// @@ -148,7 +124,7 @@ public byte[] GetRaw() { throw new InvalidCastException($"cannot convert {Type} to byte[]"); } - return (byte[])m_objectValue!; + return m_objectValue as byte[] ?? []; } /// @@ -162,7 +138,7 @@ public bool[] GetBooleanArray() { throw new InvalidCastException($"cannot convert {Type} to bool[]"); } - return (bool[])m_objectValue!; + return m_objectValue as bool[] ?? []; } /// @@ -176,7 +152,7 @@ public long[] GetIntegerArray() { throw new InvalidCastException($"cannot convert {Type} to long[]"); } - return (long[])m_objectValue!; + return m_objectValue as long[] ?? []; } /// @@ -190,7 +166,7 @@ public float[] GetFloatArray() { throw new InvalidCastException($"cannot convert {Type} to float[]"); } - return (float[])m_objectValue!; + return m_objectValue as float[] ?? []; } /// @@ -204,21 +180,7 @@ public double[] GetDoubleArray() { throw new InvalidCastException($"cannot convert {Type} to double[]"); } - return (double[])m_objectValue!; - } - - /// - /// Get the string[] value. - /// - /// The string[] value. - /// Thrown if the entry value is not of string[] type. - public string[] GetStringArray() - { - if (Type != NetworkTableType.StringArray) - { - throw new InvalidCastException($"cannot convert {Type} to string[]"); - } - return (string[])m_objectValue!; + return m_objectValue as double[] ?? []; } @@ -311,28 +273,6 @@ public static NetworkTableValue MakeDouble(double value, long time) } - /// - /// Creates a string value. - /// - /// the value - /// The entry value - public static NetworkTableValue MakeString(string value) - { - return new NetworkTableValue(NetworkTableType.String, value); - } - - /// - /// Creates a string value. - /// - /// the value - /// the creation time to use (instead of the current time) - /// The entry value - public static NetworkTableValue MakeString(string value, long time) - { - return new NetworkTableValue(NetworkTableType.String, value, time); - } - - /// /// Creates a byte[] value. /// @@ -442,26 +382,4 @@ public static NetworkTableValue MakeDoubleArray(double[] value, long time) return new NetworkTableValue(NetworkTableType.DoubleArray, value, time); } - - /// - /// Creates a string[] value. - /// - /// the value - /// The entry value - public static NetworkTableValue MakeStringArray(string[] value) - { - return new NetworkTableValue(NetworkTableType.StringArray, value); - } - - /// - /// Creates a string[] value. - /// - /// the value - /// the creation time to use (instead of the current time) - /// The entry value - public static NetworkTableValue MakeStringArray(string[] value, long time) - { - return new NetworkTableValue(NetworkTableType.StringArray, value, time); - } - } diff --git a/src/ntcore/Generated/RawEntryImpl.cs b/src/ntcore/Generated/RawEntryImpl.cs index 4f8a0c04..7bf9904a 100644 --- a/src/ntcore/Generated/RawEntryImpl.cs +++ b/src/ntcore/Generated/RawEntryImpl.cs @@ -76,7 +76,6 @@ public byte[][] ReadQueueValues() return timestamped; } - public void Set(ReadOnlySpan value) { RefNetworkTableValue ntValue = RefNetworkTableValue.MakeRaw(value, 0); diff --git a/src/ntcore/Generated/RawPublisher.cs b/src/ntcore/Generated/RawPublisher.cs index 7ad2585f..82f5b922 100644 --- a/src/ntcore/Generated/RawPublisher.cs +++ b/src/ntcore/Generated/RawPublisher.cs @@ -18,7 +18,6 @@ public interface IRawPublisher : IPublisher /// new RawTopic Topic { get; } - /// /// Publish a new value using the current NT time. /// diff --git a/src/ntcore/Generated/RawTopic.cs b/src/ntcore/Generated/RawTopic.cs index 129b166c..ee4227a7 100644 --- a/src/ntcore/Generated/RawTopic.cs +++ b/src/ntcore/Generated/RawTopic.cs @@ -55,7 +55,7 @@ public IRawSubscriber Subscribe( NtCore.Subscribe( Handle, NetworkTableType.Raw, typeString, options), - defaultValue); + defaultValue ?? []); } /// @@ -84,7 +84,7 @@ public IRawSubscriber Subscribe( NtCore.Subscribe( Handle, NetworkTableType.Raw, typeString, options), - defaultValue); + defaultValue ?? []); } /// @@ -287,7 +287,7 @@ public IRawEntry GetEntry( NtCore.GetEntry( Handle, NetworkTableType.Raw, typeString, options), - defaultValue); + defaultValue ?? []); } /// @@ -321,7 +321,7 @@ public IRawEntry GetEntry( NtCore.GetEntry( Handle, NetworkTableType.Raw, typeString, options), - defaultValue); + defaultValue ?? []); } } diff --git a/src/ntcore/Generated/RefNetworkTableValue.cs b/src/ntcore/Generated/RefNetworkTableValue.cs index 7cd35b18..7b8b160b 100644 --- a/src/ntcore/Generated/RefNetworkTableValue.cs +++ b/src/ntcore/Generated/RefNetworkTableValue.cs @@ -5,6 +5,7 @@ // THIS FILE WAS AUTO-GENERATED BY ./ntcore/generate_topics.py. DO NOT MODIFY using System; +using System.Runtime.InteropServices; using NetworkTables.Natives; namespace NetworkTables; @@ -156,92 +157,18 @@ public static RefNetworkTableValue MakeDouble(double value, long time) } - internal readonly string? m_stringValue; - - internal RefNetworkTableValue(string value) - { - Type = NetworkTableType.String; - Time = NtCore.Now(); - m_stringValue = value; - } - internal RefNetworkTableValue(string value, long time) - { - Type = NetworkTableType.String; - Time = time; - m_stringValue = value; - } - - internal RefNetworkTableValue(ReadOnlySpan value, bool isString) - { - Type = NetworkTableType.String; - Time = NtCore.Now(); - m_byteSpan = value; - } - - internal RefNetworkTableValue(ReadOnlySpan value, long time, bool isString) - { - Type = NetworkTableType.String; - Time = time; - m_byteSpan = value; - } - - /// - /// Creates a string value. - /// - /// the value - /// The entry value - public static RefNetworkTableValue MakeString(string value) - { - return new RefNetworkTableValue(value); - } - - /// - /// Creates a string value. - /// - /// the value - /// the creation time to use (instead of the current time) - /// The entry value - public static RefNetworkTableValue MakeString(string value, long time) - { - return new RefNetworkTableValue(value, time); - } - - /// - /// Creates a string value from a UFT8 string. - /// - /// the value - /// The entry value - public static RefNetworkTableValue MakeString(ReadOnlySpan value) - { - return new RefNetworkTableValue(value); - } - - /// - /// Creates a string value from a UFT8 string. - /// - /// the value - /// the creation time to use (instead of the current time) - /// The entry value - public static RefNetworkTableValue MakeString(ReadOnlySpan value, long time) - { - return new RefNetworkTableValue(value, time); - } - - - internal readonly ReadOnlySpan m_byteSpan; - internal RefNetworkTableValue(ReadOnlySpan value) { Type = NetworkTableType.Raw; Time = NtCore.Now(); - m_byteSpan = value; + m_byteSpan = MemoryMarshal.AsBytes(value); } internal RefNetworkTableValue(ReadOnlySpan value, long time) { Type = NetworkTableType.Raw; Time = time; - m_byteSpan = value; + m_byteSpan = MemoryMarshal.AsBytes(value); } /// @@ -266,20 +193,18 @@ public static RefNetworkTableValue MakeRaw(ReadOnlySpan value, long time) } - internal readonly ReadOnlySpan m_boolSpan; - internal RefNetworkTableValue(ReadOnlySpan value) { Type = NetworkTableType.BooleanArray; Time = NtCore.Now(); - m_boolSpan = value; + m_byteSpan = MemoryMarshal.AsBytes(value); } internal RefNetworkTableValue(ReadOnlySpan value, long time) { Type = NetworkTableType.BooleanArray; Time = time; - m_boolSpan = value; + m_byteSpan = MemoryMarshal.AsBytes(value); } /// @@ -304,20 +229,18 @@ public static RefNetworkTableValue MakeBooleanArray(ReadOnlySpan value, lo } - internal readonly ReadOnlySpan m_longSpan; - internal RefNetworkTableValue(ReadOnlySpan value) { Type = NetworkTableType.IntegerArray; Time = NtCore.Now(); - m_longSpan = value; + m_byteSpan = MemoryMarshal.AsBytes(value); } internal RefNetworkTableValue(ReadOnlySpan value, long time) { Type = NetworkTableType.IntegerArray; Time = time; - m_longSpan = value; + m_byteSpan = MemoryMarshal.AsBytes(value); } /// @@ -342,20 +265,18 @@ public static RefNetworkTableValue MakeIntegerArray(ReadOnlySpan value, lo } - internal readonly ReadOnlySpan m_floatSpan; - internal RefNetworkTableValue(ReadOnlySpan value) { Type = NetworkTableType.FloatArray; Time = NtCore.Now(); - m_floatSpan = value; + m_byteSpan = MemoryMarshal.AsBytes(value); } internal RefNetworkTableValue(ReadOnlySpan value, long time) { Type = NetworkTableType.FloatArray; Time = time; - m_floatSpan = value; + m_byteSpan = MemoryMarshal.AsBytes(value); } /// @@ -380,20 +301,18 @@ public static RefNetworkTableValue MakeFloatArray(ReadOnlySpan value, lon } - internal readonly ReadOnlySpan m_doubleSpan; - internal RefNetworkTableValue(ReadOnlySpan value) { Type = NetworkTableType.DoubleArray; Time = NtCore.Now(); - m_doubleSpan = value; + m_byteSpan = MemoryMarshal.AsBytes(value); } internal RefNetworkTableValue(ReadOnlySpan value, long time) { Type = NetworkTableType.DoubleArray; Time = time; - m_doubleSpan = value; + m_byteSpan = MemoryMarshal.AsBytes(value); } /// @@ -417,42 +336,4 @@ public static RefNetworkTableValue MakeDoubleArray(ReadOnlySpan value, l return new RefNetworkTableValue(value, time); } - - internal readonly ReadOnlySpan m_stringSpan; - - internal RefNetworkTableValue(ReadOnlySpan value) - { - Type = NetworkTableType.StringArray; - Time = NtCore.Now(); - m_stringSpan = value; - } - - internal RefNetworkTableValue(ReadOnlySpan value, long time) - { - Type = NetworkTableType.StringArray; - Time = time; - m_stringSpan = value; - } - - /// - /// Creates a string[] value. - /// - /// the value - /// The entry value - public static RefNetworkTableValue MakeStringArray(ReadOnlySpan value) - { - return new RefNetworkTableValue(value); - } - - /// - /// Creates a string[] value. - /// - /// the value - /// the creation time to use (instead of the current time) - /// The entry value - public static RefNetworkTableValue MakeStringArray(ReadOnlySpan value, long time) - { - return new RefNetworkTableValue(value, time); - } - } diff --git a/src/ntcore/Generated/StringPublisher.cs b/src/ntcore/Generated/StringPublisher.cs deleted file mode 100644 index 7b44157b..00000000 --- a/src/ntcore/Generated/StringPublisher.cs +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright (c) FIRST and other WPILib contributors. -// Open Source Software; you can modify and/or share it under the terms of -// the WPILib BSD license file in the root directory of this project. - -// THIS FILE WAS AUTO-GENERATED BY ./ntcore/generate_topics.py. DO NOT MODIFY - -using System; - -namespace NetworkTables; - -/// -/// NetworkTables String publisher. -/// -public interface IStringPublisher : IPublisher -{ - /// - /// Gets the corresponding topic. - /// - new StringTopic Topic { get; } - - - /// - /// Publish a new value using the current NT time. - /// - /// value to publish - void Set(string value); - - /// - /// Publish a new value. - /// - /// value to publish - /// timestamp; 0 indicates current NT time should be used - void Set(string value, long time); - - /// - /// Publish a default value. On reconnect, a default value will never be used - /// in prference to a published value - /// - /// value - void SetDefault(string value); -} diff --git a/src/ntcore/Natives/RefNetworkTableValueMarshaller.cs b/src/ntcore/Natives/RefNetworkTableValueMarshaller.cs index 2c7bfa4d..2accb042 100644 --- a/src/ntcore/Natives/RefNetworkTableValueMarshaller.cs +++ b/src/ntcore/Natives/RefNetworkTableValueMarshaller.cs @@ -44,36 +44,36 @@ public void FromManaged(in RefNetworkTableValue managed, Span callerAlloca m_nativeValue.data.valueRaw.size = (nuint)managed.m_byteSpan.Length; break; case NetworkTableType.DoubleArray: - m_toPin = ref MemoryMarshal.AsBytes(managed.m_doubleSpan).GetPinnableReference(); + m_toPin = ref managed.m_byteSpan.GetPinnableReference(); m_toAssignPin = ref m_nativeValue.data.arrDouble.arr.GetPinnableByteReference(); - m_nativeValue.data.arrDouble.size = (nuint)managed.m_doubleSpan.Length; + m_nativeValue.data.arrDouble.size = (nuint)managed.m_byteSpan.Length / 8; break; case NetworkTableType.IntegerArray: - m_toPin = ref MemoryMarshal.AsBytes(managed.m_longSpan).GetPinnableReference(); + m_toPin = ref managed.m_byteSpan.GetPinnableReference(); m_toAssignPin = ref m_nativeValue.data.arrInt.arr.GetPinnableByteReference(); - m_nativeValue.data.arrInt.size = (nuint)managed.m_longSpan.Length; + m_nativeValue.data.arrInt.size = (nuint)managed.m_byteSpan.Length / 8; break; case NetworkTableType.FloatArray: - m_toPin = ref MemoryMarshal.AsBytes(managed.m_floatSpan).GetPinnableReference(); + m_toPin = ref managed.m_byteSpan.GetPinnableReference(); m_toAssignPin = ref m_nativeValue.data.arrFloat.arr.GetPinnableByteReference(); - m_nativeValue.data.arrFloat.size = (nuint)managed.m_floatSpan.Length; + m_nativeValue.data.arrFloat.size = (nuint)managed.m_byteSpan.Length / 4; break; case NetworkTableType.BooleanArray: Span boolArraySpan = MemoryMarshal.Cast(callerAllocatedBuffer); - if (boolArraySpan.Length < managed.m_boolSpan.Length) + if (boolArraySpan.Length < managed.m_byteSpan.Length) { - boolArraySpan = new int[managed.m_boolSpan.Length]; + boolArraySpan = new int[managed.m_byteSpan.Length]; } for (int i = 0; i < boolArraySpan.Length; i++) { - boolArraySpan[i] = managed.m_boolSpan[i] ? 1 : 0; + boolArraySpan[i] = managed.m_byteSpan[i]; } m_toPin = ref MemoryMarshal.AsBytes(boolArraySpan).GetPinnableReference(); m_toAssignPin = ref m_nativeValue.data.arrBoolean.arr.GetPinnableByteReference(); - m_nativeValue.data.arrBoolean.size = (nuint)managed.m_boolSpan.Length; + m_nativeValue.data.arrBoolean.size = (nuint)managed.m_byteSpan.Length; break; case NetworkTableType.String: - if (managed.m_stringValue == null) + if (managed.m_structValue.stringStorageType == RefNetworkTableValue.StringStorageType.Utf8) { // String is stored as utf-8 in raw span m_toPin = ref managed.m_byteSpan.GetPinnableReference(); @@ -83,7 +83,8 @@ public void FromManaged(in RefNetworkTableValue managed, Span callerAlloca else { // Is string, convert to UTF-8 - int byteCount = Encoding.UTF8.GetByteCount(managed.m_stringValue!); + ReadOnlySpan stringValue = MemoryMarshal.Cast(managed.m_byteSpan); + int byteCount = Encoding.UTF8.GetByteCount(stringValue); Span stringSpan = callerAllocatedBuffer; if (byteCount > stringSpan.Length) { @@ -93,7 +94,7 @@ public void FromManaged(in RefNetworkTableValue managed, Span callerAlloca { stringSpan = stringSpan[..byteCount]; } - int exactBytes = Encoding.UTF8.GetBytes(managed.m_stringValue!, stringSpan); + int exactBytes = Encoding.UTF8.GetBytes(stringValue, stringSpan); Debug.Assert(exactBytes == byteCount); m_toPin = ref stringSpan.GetPinnableReference(); m_nativeValue.data.valueString = new(null, (nuint)stringSpan.Length); @@ -105,9 +106,9 @@ public void FromManaged(in RefNetworkTableValue managed, Span callerAlloca for (int i = 0; i < strings.Length; i++) { - int len = Encoding.UTF8.GetByteCount(managed.m_stringSpan[i]); + int len = Encoding.UTF8.GetByteCount(managed.m_stringSpan[i].AsSpan()); byte* mem = (byte*)NativeMemory.Alloc((nuint)len); - int exactLen = Encoding.UTF8.GetBytes(managed.m_stringSpan[i], new Span(mem, len)); + int exactLen = Encoding.UTF8.GetBytes(managed.m_stringSpan[i].AsSpan(), new Span(mem, len)); Debug.Assert(exactLen == len); strings[i] = new WpiStringMarshaller.WpiStringNative(mem, (nuint)len); } diff --git a/src/ntcore/NetworkTable.cs b/src/ntcore/NetworkTable.cs index 1b5717ee..f893ba08 100644 --- a/src/ntcore/NetworkTable.cs +++ b/src/ntcore/NetworkTable.cs @@ -297,4 +297,24 @@ public override int GetHashCode() { return !(left == right); } + + /// + /// Gets a string topic. + /// + /// topic name + /// StringTopic + public StringTopic GetStringTopic(string name) + { + return Instance.GetStringTopic($"{m_pathWithSep}{name}"); + } + + /// + /// Gets a string[] topic. + /// + /// topic name + /// StringArrayTopic + public StringArrayTopic GetStringArrayTopic(string name) + { + return Instance.GetStringArrayTopic($"{m_pathWithSep}{name}"); + } } diff --git a/src/ntcore/NetworkTableInstance.cs b/src/ntcore/NetworkTableInstance.cs index bbd7ea8e..0ddb9466 100644 --- a/src/ntcore/NetworkTableInstance.cs +++ b/src/ntcore/NetworkTableInstance.cs @@ -738,4 +738,58 @@ private void AddSchemaImpl(IStructBase strct, HashSet seen) } seen.Remove(typeString); } + + private static StringTopic StringTopicCreator(string name, NetworkTableInstance instance) + { + StringTopic topic = new StringTopic(instance, NtCore.GetTopic(instance.Handle, name)); + instance.m_topicsByHandle.AddOrUpdate(topic.Handle, TopicByHandleAdder, TopicByHandleUpdater, topic); + return topic; + } + private static StringTopic StringTopicUpdator(string name, Topic existingTopic, NetworkTableInstance instance) + { + // Exists, but might be wrong type + if (existingTopic is StringTopic StringTopic) + { + return StringTopic; + } + StringTopic topic = new StringTopic(instance, existingTopic.Handle); + instance.m_topicsByHandle.AddOrUpdate(topic.Handle, TopicByHandleAdder, TopicByHandleUpdater, topic); + return topic; + } + /// + /// Gets a string topic. + /// + /// topic name + /// StringTopic + public StringTopic GetStringTopic(string name) + { + return (StringTopic)m_topics.AddOrUpdate(name, StringTopicCreator, StringTopicUpdator, this); + } + + private static StringArrayTopic StringArrayTopicCreator(string name, NetworkTableInstance instance) + { + StringArrayTopic topic = new StringArrayTopic(instance, NtCore.GetTopic(instance.Handle, name)); + instance.m_topicsByHandle.AddOrUpdate(topic.Handle, TopicByHandleAdder, TopicByHandleUpdater, topic); + return topic; + } + private static StringArrayTopic StringArrayTopicUpdator(string name, Topic existingTopic, NetworkTableInstance instance) + { + // Exists, but might be wrong type + if (existingTopic is StringArrayTopic StringArrayTopic) + { + return StringArrayTopic; + } + StringArrayTopic topic = new StringArrayTopic(instance, existingTopic.Handle); + instance.m_topicsByHandle.AddOrUpdate(topic.Handle, TopicByHandleAdder, TopicByHandleUpdater, topic); + return topic; + } + /// + /// Gets a string[] topic. + /// + /// topic name + /// StringArrayTopic + public StringArrayTopic GetStringArrayTopic(string name) + { + return (StringArrayTopic)m_topics.AddOrUpdate(name, StringArrayTopicCreator, StringArrayTopicUpdator, this); + } } diff --git a/src/ntcore/NetworkTableValue.cs b/src/ntcore/NetworkTableValue.cs index 2a6daee1..2fd32c80 100644 --- a/src/ntcore/NetworkTableValue.cs +++ b/src/ntcore/NetworkTableValue.cs @@ -16,6 +16,16 @@ public static unsafe void FreeArray(NetworkTableValueMarshaller.NativeNetworkTab NtCore.DisposeValueArray(ptr, (nuint)len); } + /// + /// Gets if entry value contains a string. + /// + public bool IsString => Type == NetworkTableType.String; + + /// + /// Gets if entry value contains a string. + /// + public bool IsStringArray => Type == NetworkTableType.StringArray; + internal NetworkTableValue(NetworkTableType type, object? obj, long time, long serverTime) { Type = type; @@ -95,6 +105,34 @@ internal NetworkTableValue(NetworkTableType type, object value, long time) : thi */ public bool IsValid => Type != NetworkTableType.Unassigned; + /// + /// Get the string value. + /// + /// The string value. + /// Thrown if the entry value is not of string type. + public string GetString() + { + if (Type != NetworkTableType.String) + { + throw new InvalidCastException($"cannot convert {Type} to string"); + } + return m_objectValue as string ?? ""; + } + + /// + /// Get the string[] value. + /// + /// The string[] value. + /// Thrown if the entry value is not of string[] type. + public string[] GetStringArray() + { + if (Type != NetworkTableType.StringArray) + { + throw new InvalidCastException($"cannot convert {Type} to string[]"); + } + return m_objectValue as string[] ?? []; + } + public object? Value { get @@ -139,6 +177,48 @@ public static NetworkTableValue MakeUnassigned(long time) return new NetworkTableValue(NetworkTableType.Unassigned, null, time, 1); } + /// + /// Creates a string value. + /// + /// the value + /// The entry value + public static NetworkTableValue MakeString(string value) + { + return new NetworkTableValue(NetworkTableType.String, value); + } + + /// + /// Creates a string value. + /// + /// the value + /// the creation time to use (instead of the current time) + /// The entry value + public static NetworkTableValue MakeString(string value, long time) + { + return new NetworkTableValue(NetworkTableType.String, value, time); + } + + /// + /// Creates a string[] value. + /// + /// the value + /// The entry value + public static NetworkTableValue MakeStringArray(string[] value) + { + return new NetworkTableValue(NetworkTableType.StringArray, value); + } + + /// + /// Creates a string[] value. + /// + /// the value + /// the creation time to use (instead of the current time) + /// The entry value + public static NetworkTableValue MakeStringArray(string[] value, long time) + { + return new NetworkTableValue(NetworkTableType.StringArray, value, time); + } + // TODO Equals and HashCode /** diff --git a/src/ntcore/RefNetworkTableValue.cs b/src/ntcore/RefNetworkTableValue.cs index 13803cc6..3e435795 100644 --- a/src/ntcore/RefNetworkTableValue.cs +++ b/src/ntcore/RefNetworkTableValue.cs @@ -1,3 +1,4 @@ +using System; using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; using NetworkTables.Natives; @@ -47,6 +48,120 @@ public static RefNetworkTableValue MakeUnassigned(long time) return new RefNetworkTableValue(NetworkTableType.Unassigned, time); } + internal RefNetworkTableValue(ReadOnlySpan value, StringStorageType storageType) + { + Type = NetworkTableType.String; + Time = NtCore.Now(); + m_structValue = new ValueStorage(storageType); + m_byteSpan = value; + } + + internal RefNetworkTableValue(ReadOnlySpan value, long time, StringStorageType storageType) + { + Type = NetworkTableType.String; + Time = time; + m_structValue = new ValueStorage(storageType); + m_byteSpan = value; + } + + /// + /// Creates a string value. + /// + /// the value + /// The entry value + public static RefNetworkTableValue MakeString(string value) + { + return new RefNetworkTableValue(MemoryMarshal.AsBytes(value.AsSpan()), StringStorageType.Char); + } + + /// + /// Creates a string value. + /// + /// the value + /// the creation time to use (instead of the current time) + /// The entry value + public static RefNetworkTableValue MakeString(ReadOnlySpan value, long time) + { + return new RefNetworkTableValue(MemoryMarshal.AsBytes(value), time, StringStorageType.Char); + } + + /// + /// Creates a string value. + /// + /// the value + /// The entry value + public static RefNetworkTableValue MakeString(ReadOnlySpan value) + { + return new RefNetworkTableValue(MemoryMarshal.AsBytes(value), StringStorageType.Char); + } + + /// + /// Creates a string value. + /// + /// the value + /// the creation time to use (instead of the current time) + /// The entry value + public static RefNetworkTableValue MakeString(string value, long time) + { + return new RefNetworkTableValue(MemoryMarshal.AsBytes(value.AsSpan()), time, StringStorageType.Char); + } + + /// + /// Creates a string value from a UFT8 string. + /// + /// the value + /// The entry value + public static RefNetworkTableValue MakeString(ReadOnlySpan value) + { + return new RefNetworkTableValue(value, StringStorageType.Utf8); + } + + /// + /// Creates a string value from a UFT8 string. + /// + /// the value + /// the creation time to use (instead of the current time) + /// The entry value + public static RefNetworkTableValue MakeString(ReadOnlySpan value, long time) + { + return new RefNetworkTableValue(value, time, StringStorageType.Utf8); + } + + internal RefNetworkTableValue(ReadOnlySpan value) + { + Type = NetworkTableType.String; + Time = NtCore.Now(); + m_stringSpan = value; + } + + internal RefNetworkTableValue(ReadOnlySpan value, long time) + { + Type = NetworkTableType.String; + Time = time; + m_stringSpan = value; + } + + /// + /// Creates a string[] value. + /// + /// the value + /// The entry value + public static RefNetworkTableValue MakeStringArray(ReadOnlySpan value) + { + return new RefNetworkTableValue(value); + } + + /// + /// Creates a string[] value. + /// + /// the value + /// the creation time to use (instead of the current time) + /// The entry value + public static RefNetworkTableValue MakeStringArray(ReadOnlySpan value, long time) + { + return new RefNetworkTableValue(value, time); + } + // TODO Equals and HashCode /** @@ -57,6 +172,14 @@ public static RefNetworkTableValue MakeUnassigned(long time) public NetworkTableType Type { get; } internal readonly ValueStorage m_structValue; + internal readonly ReadOnlySpan m_stringSpan; + internal readonly ReadOnlySpan m_byteSpan; + + internal enum StringStorageType + { + Char, + Utf8 + } [StructLayout(LayoutKind.Explicit)] internal readonly struct ValueStorage @@ -81,6 +204,11 @@ public ValueStorage(double value) doubleValue = value; } + public ValueStorage(StringStorageType value) + { + stringStorageType = value; + } + [FieldOffset(0)] public readonly bool boolValue; [FieldOffset(0)] @@ -89,5 +217,7 @@ public ValueStorage(double value) public readonly float floatValue; [FieldOffset(0)] public readonly double doubleValue; + [FieldOffset(0)] + public readonly StringStorageType stringStorageType; } } diff --git a/src/ntcore/Generated/StringArrayEntry.cs b/src/ntcore/Strings/StringArrayEntry.cs similarity index 100% rename from src/ntcore/Generated/StringArrayEntry.cs rename to src/ntcore/Strings/StringArrayEntry.cs diff --git a/src/ntcore/Generated/StringArrayEntryImpl.cs b/src/ntcore/Strings/StringArrayEntryImpl.cs similarity index 98% rename from src/ntcore/Generated/StringArrayEntryImpl.cs rename to src/ntcore/Strings/StringArrayEntryImpl.cs index 85ac1959..696b722b 100644 --- a/src/ntcore/Generated/StringArrayEntryImpl.cs +++ b/src/ntcore/Strings/StringArrayEntryImpl.cs @@ -37,7 +37,7 @@ public string[] Get(string[] defaultValue) { return value.GetStringArray(); } - return defaultValue; + return defaultValue ?? []; } public TimestampedObject GetAtomic() @@ -50,7 +50,7 @@ public TimestampedObject GetAtomic() public TimestampedObject GetAtomic(string[] defaultValue) { NetworkTableValue value = NtCore.GetEntryValue(Handle); - string[] baseValue = value.IsStringArray ? value.GetStringArray() : defaultValue; + string[] baseValue = value.IsStringArray ? value.GetStringArray() : defaultValue ?? []; return new TimestampedObject(value.Time, value.ServerTime, baseValue); } diff --git a/src/ntcore/Generated/StringArrayPublisher.cs b/src/ntcore/Strings/StringArrayPublisher.cs similarity index 100% rename from src/ntcore/Generated/StringArrayPublisher.cs rename to src/ntcore/Strings/StringArrayPublisher.cs diff --git a/src/ntcore/Generated/StringArraySubscriber.cs b/src/ntcore/Strings/StringArraySubscriber.cs similarity index 100% rename from src/ntcore/Generated/StringArraySubscriber.cs rename to src/ntcore/Strings/StringArraySubscriber.cs diff --git a/src/ntcore/Generated/StringArrayTopic.cs b/src/ntcore/Strings/StringArrayTopic.cs similarity index 98% rename from src/ntcore/Generated/StringArrayTopic.cs rename to src/ntcore/Strings/StringArrayTopic.cs index eb6f9d24..cbd5284e 100644 --- a/src/ntcore/Generated/StringArrayTopic.cs +++ b/src/ntcore/Strings/StringArrayTopic.cs @@ -62,7 +62,7 @@ public IStringArraySubscriber Subscribe( NtCore.Subscribe( Handle, NetworkTableType.StringArray, "string[]"u8, options), - defaultValue); + defaultValue ?? []); } /// @@ -91,7 +91,7 @@ public IStringArraySubscriber SubscribeEx( NtCore.Subscribe( Handle, NetworkTableType.StringArray, typeString, options), - defaultValue); + defaultValue ?? []); } /// @@ -120,7 +120,7 @@ public IStringArraySubscriber SubscribeEx( NtCore.Subscribe( Handle, NetworkTableType.StringArray, typeString, options), - defaultValue); + defaultValue ?? []); } /// @@ -292,7 +292,7 @@ public IStringArrayEntry GetEntry( NtCore.GetEntry( Handle, NetworkTableType.StringArray, "string[]"u8, options), - defaultValue); + defaultValue ?? []); } /// @@ -326,7 +326,7 @@ public IStringArrayEntry GetEntryEx( NtCore.GetEntry( Handle, NetworkTableType.StringArray, typeString, options), - defaultValue); + defaultValue ?? []); } /// @@ -360,7 +360,7 @@ public IStringArrayEntry GetEntryEx( NtCore.GetEntry( Handle, NetworkTableType.StringArray, typeString, options), - defaultValue); + defaultValue ?? []); } } diff --git a/src/ntcore/Generated/StringEntry.cs b/src/ntcore/Strings/StringEntry.cs similarity index 100% rename from src/ntcore/Generated/StringEntry.cs rename to src/ntcore/Strings/StringEntry.cs diff --git a/src/ntcore/Generated/StringEntryImpl.cs b/src/ntcore/Strings/StringEntryImpl.cs similarity index 71% rename from src/ntcore/Generated/StringEntryImpl.cs rename to src/ntcore/Strings/StringEntryImpl.cs index 4ed7483d..53964080 100644 --- a/src/ntcore/Generated/StringEntryImpl.cs +++ b/src/ntcore/Strings/StringEntryImpl.cs @@ -37,7 +37,7 @@ public string Get(string defaultValue) { return value.GetString(); } - return defaultValue; + return defaultValue ?? ""; } public TimestampedObject GetAtomic() @@ -50,7 +50,7 @@ public TimestampedObject GetAtomic() public TimestampedObject GetAtomic(string defaultValue) { NetworkTableValue value = NtCore.GetEntryValue(Handle); - string baseValue = value.IsString ? value.GetString() : defaultValue; + string baseValue = value.IsString ? value.GetString() : defaultValue ?? ""; return new TimestampedObject(value.Time, value.ServerTime, baseValue); } @@ -95,6 +95,42 @@ public void SetDefault(string value) NtCore.SetDefaultEntryValue(Handle, ntValue); } + public void Set(ReadOnlySpan value) + { + RefNetworkTableValue ntValue = RefNetworkTableValue.MakeString(value, 0); + NtCore.SetEntryValue(Handle, ntValue); + } + + public void Set(ReadOnlySpan value, long time) + { + RefNetworkTableValue ntValue = RefNetworkTableValue.MakeString(value, time); + NtCore.SetEntryValue(Handle, ntValue); + } + + public void SetDefault(ReadOnlySpan value) + { + RefNetworkTableValue ntValue = RefNetworkTableValue.MakeString(value); + NtCore.SetDefaultEntryValue(Handle, ntValue); + } + + public void Set(ReadOnlySpan value) + { + RefNetworkTableValue ntValue = RefNetworkTableValue.MakeString(value, 0); + NtCore.SetEntryValue(Handle, ntValue); + } + + public void Set(ReadOnlySpan value, long time) + { + RefNetworkTableValue ntValue = RefNetworkTableValue.MakeString(value, time); + NtCore.SetEntryValue(Handle, ntValue); + } + + public void SetDefault(ReadOnlySpan value) + { + RefNetworkTableValue ntValue = RefNetworkTableValue.MakeString(value); + NtCore.SetDefaultEntryValue(Handle, ntValue); + } + public void Unpublish() { NtCore.Unpublish(Handle); diff --git a/src/ntcore/Strings/StringPublisher.cs b/src/ntcore/Strings/StringPublisher.cs new file mode 100644 index 00000000..805ad665 --- /dev/null +++ b/src/ntcore/Strings/StringPublisher.cs @@ -0,0 +1,80 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +// THIS FILE WAS AUTO-GENERATED BY ./ntcore/generate_topics.py. DO NOT MODIFY + +using System; + +namespace NetworkTables; + +/// +/// NetworkTables String publisher. +/// +public interface IStringPublisher : IPublisher +{ + /// + /// Gets the corresponding topic. + /// + new StringTopic Topic { get; } + + /// + /// Publish a new value using the current NT time. + /// + /// value to publish + void Set(string value); + + /// + /// Publish a new value. + /// + /// value to publish + /// timestamp; 0 indicates current NT time should be used + void Set(string value, long time); + + /// + /// Publish a default value. On reconnect, a default value will never be used + /// in prference to a published value + /// + /// value + void SetDefault(string value); + + /// + /// Publish a new value using the current NT time. + /// + /// value to publish + void Set(ReadOnlySpan value); + + /// + /// Publish a new value. + /// + /// value to publish + /// timestamp; 0 indicates current NT time should be used + void Set(ReadOnlySpan value, long time); + + /// + /// Publish a default value. On reconnect, a default value will never be used + /// in prference to a published value + /// + /// value + void SetDefault(ReadOnlySpan value); + + /// + /// Publish a new UTF8 value using the current NT time. + /// + /// value to publish + void Set(ReadOnlySpan value); + + /// + /// Publish a new value. + /// + /// value to publish + /// timestamp; 0 indicates current NT time should be used + void Set(ReadOnlySpan value, long time); + + /// + /// Publish a default value. On reconnect, a default value will never be used + /// in prference to a published value + /// + /// value + void SetDefault(ReadOnlySpan value); +} diff --git a/src/ntcore/Generated/StringSubscriber.cs b/src/ntcore/Strings/StringSubscriber.cs similarity index 100% rename from src/ntcore/Generated/StringSubscriber.cs rename to src/ntcore/Strings/StringSubscriber.cs diff --git a/src/ntcore/Generated/StringTopic.cs b/src/ntcore/Strings/StringTopic.cs similarity index 98% rename from src/ntcore/Generated/StringTopic.cs rename to src/ntcore/Strings/StringTopic.cs index e50f4c6f..0413efb5 100644 --- a/src/ntcore/Generated/StringTopic.cs +++ b/src/ntcore/Strings/StringTopic.cs @@ -62,7 +62,7 @@ public IStringSubscriber Subscribe( NtCore.Subscribe( Handle, NetworkTableType.String, "string"u8, options), - defaultValue); + defaultValue ?? ""); } /// @@ -91,7 +91,7 @@ public IStringSubscriber SubscribeEx( NtCore.Subscribe( Handle, NetworkTableType.String, typeString, options), - defaultValue); + defaultValue ?? ""); } /// @@ -120,7 +120,7 @@ public IStringSubscriber SubscribeEx( NtCore.Subscribe( Handle, NetworkTableType.String, typeString, options), - defaultValue); + defaultValue ?? ""); } /// @@ -292,7 +292,7 @@ public IStringEntry GetEntry( NtCore.GetEntry( Handle, NetworkTableType.String, "string"u8, options), - defaultValue); + defaultValue ?? ""); } /// @@ -326,7 +326,7 @@ public IStringEntry GetEntryEx( NtCore.GetEntry( Handle, NetworkTableType.String, typeString, options), - defaultValue); + defaultValue ?? ""); } /// @@ -360,7 +360,7 @@ public IStringEntry GetEntryEx( NtCore.GetEntry( Handle, NetworkTableType.String, typeString, options), - defaultValue); + defaultValue ?? ""); } } diff --git a/src/thirdparty/Stereologue/DataLogSendableBulder.cs b/src/thirdparty/Stereologue/DataLogSendableBulder.cs deleted file mode 100644 index 23746eb9..00000000 --- a/src/thirdparty/Stereologue/DataLogSendableBulder.cs +++ /dev/null @@ -1,316 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Runtime.InteropServices; -using NetworkTables; -using WPIUtil.Logging; -using static WPIUtil.Sendable.ISendableBuilder; - -namespace Stereologue; - -internal class DataLogSendableBuilder : INtSendableBuilder -{ - private static readonly DataLog log = null!; - private static NetworkTable rootTable = NetworkTableInstance.Default.GetTable("DataLogSendable"); - - private static NetworkTable? networkTable = null; - - private readonly Dictionary> dataLogMap = []; - private readonly List updateTables = []; - private readonly List disposables = []; - private string prefix; - - /// - /// - /// - /// - public DataLogSendableBuilder(string prefix) - { - if (!prefix.EndsWith('/')) - { - this.prefix = $"{prefix}/"; - } - else - { - this.prefix = prefix; - } - } - - public BackingKind BackendKind => BackingKind.Unknown; - - public Action UpdateTable - { - set - { - updateTables.Add(value); - } - } - - public NetworkTable Table - { - get - { - if (networkTable == null) - { - networkTable = rootTable.GetSubTable(prefix); - } - return networkTable; - } - } - - public bool IsPublished => true; - - public void AddBooleanArrayProperty(string key, Func getter, Action setter) - { - if (getter != null) - { - CollectionsMarshal.GetValueRefOrAddDefault(dataLogMap, new BooleanArrayLogEntry(log, $"{prefix}{key}"), out _) = () => getter(); - } - } - - public void AddBooleanProperty(string key, Func getter, Action setter) - { - if (getter != null) - { - CollectionsMarshal.GetValueRefOrAddDefault(dataLogMap, new BooleanLogEntry(log, $"{prefix}{key}"), out _) = () => getter(); - } - } - - public void AddDisposable(IDisposable disposable) - { - disposables.Add(disposable); - } - - public void AddDoubleArrayProperty(string key, Func getter, Action setter) - { - if (getter != null) - { - CollectionsMarshal.GetValueRefOrAddDefault(dataLogMap, new DoubleArrayLogEntry(log, $"{prefix}{key}"), out _) = () => getter(); - } - } - - public void AddDoubleProperty(string key, Func getter, Action setter) - { - if (getter != null) - { - CollectionsMarshal.GetValueRefOrAddDefault(dataLogMap, new DoubleLogEntry(log, $"{prefix}{key}"), out _) = () => getter(); - } - } - - public void AddFloatArrayProperty(string key, Func getter, Action setter) - { - if (getter != null) - { - CollectionsMarshal.GetValueRefOrAddDefault(dataLogMap, new FloatArrayLogEntry(log, $"{prefix}{key}"), out _) = () => getter(); - } - } - - public void AddFloatProperty(string key, Func getter, Action setter) - { - if (getter != null) - { - CollectionsMarshal.GetValueRefOrAddDefault(dataLogMap, new FloatLogEntry(log, $"{prefix}{key}"), out _) = () => getter(); - } - } - - public void AddIntegerArrayProperty(string key, Func getter, Action setter) - { - if (getter != null) - { - CollectionsMarshal.GetValueRefOrAddDefault(dataLogMap, new IntegerArrayLogEntry(log, $"{prefix}{key}"), out _) = () => getter(); - } - } - - public void AddIntegerProperty(string key, Func getter, Action setter) - { - if (getter != null) - { - CollectionsMarshal.GetValueRefOrAddDefault(dataLogMap, new IntegerLogEntry(log, $"{prefix}{key}"), out _) = () => getter(); - } - } - - public void AddRawProperty(string key, string typeString, Func getter, Action setter) - { - if (getter != null) - { - CollectionsMarshal.GetValueRefOrAddDefault(dataLogMap, new RawLogEntry(log, $"{prefix}{key}({typeString})"), out _) = () => getter(); - } - } - - public void AddStringArrayProperty(string key, Func getter, Action setter) - { - if (getter != null) - { - CollectionsMarshal.GetValueRefOrAddDefault(dataLogMap, new StringArrayLogEntry(log, $"{prefix}{key}"), out _) = () => getter(); - } - } - - public void AddStringProperty(string key, Func getter, Action setter) - { - if (getter != null) - { - CollectionsMarshal.GetValueRefOrAddDefault(dataLogMap, new StringLogEntry(log, $"{prefix}{key}"), out _) = () => getter(); - } - } - - public void ClearProperties() - { - dataLogMap.Clear(); - } - - public void Dispose() - { - ClearProperties(); - - foreach (var d in disposables) - { - try - { - d?.Dispose(); - } - catch (Exception ex) - { - Console.WriteLine($"Ignoring Dispose Exception {ex}"); - // Ignore - } - } - } - - public Topic GetTopic(string key) - { - return Table.GetTopic(key); - } - - public void PublishConstBoolean(string key, bool value) - { - var handle = log.Start($"{prefix}{key}", BooleanLogEntry.DataTypeUft8); - log.AppendBoolean(handle, value); - } - - public void PublishConstBooleanArray(string key, ReadOnlySpan value) - { - var handle = log.Start($"{prefix}{key}", BooleanArrayLogEntry.DataTypeUft8); - log.AppendBooleanArray(handle, value); - } - - public void PublishConstDouble(string key, double value) - { - var handle = log.Start($"{prefix}{key}", DoubleLogEntry.DataTypeUft8); - log.AppendDouble(handle, value); - } - - public void PublishConstDoubleArray(string key, ReadOnlySpan value) - { - var handle = log.Start($"{prefix}{key}", DoubleArrayLogEntry.DataTypeUft8); - log.AppendDoubleArray(handle, value); - } - - public void PublishConstFloat(string key, float value) - { - var handle = log.Start($"{prefix}{key}", FloatLogEntry.DataTypeUft8); - log.AppendFloat(handle, value); - } - - public void PublishConstFloatArray(string key, ReadOnlySpan value) - { - var handle = log.Start($"{prefix}{key}", FloatArrayLogEntry.DataTypeUft8); - log.AppendFloatArray(handle, value); - } - - public void PublishConstInteger(string key, long value) - { - var handle = log.Start($"{prefix}{key}", IntegerLogEntry.DataTypeUft8); - log.AppendInteger(handle, value); - } - - public void PublishConstIntegerArray(string key, ReadOnlySpan value) - { - var handle = log.Start($"{prefix}{key}", IntegerArrayLogEntry.DataTypeUft8); - log.AppendIntegerArray(handle, value); - } - - public void PublishConstRaw(string key, string typeString, ReadOnlySpan value) - { - var handle = log.Start($"{prefix}{key}({typeString})", RawLogEntry.DataTypeUft8); - log.AppendRaw(handle, value); - } - - public void PublishConstString(string key, string value) - { - var handle = log.Start($"{prefix}{key}", StringLogEntry.DataTypeUft8); - log.AppendString(handle, value); - } - - public void PublishConstString(string key, ReadOnlySpan value) - { - var handle = log.Start($"{prefix}{key}", StringLogEntry.DataTypeUft8); - log.AppendString(handle, value); - } - - public void PublishConstStringArray(string key, ReadOnlySpan value) - { - var handle = log.Start($"{prefix}{key}", StringArrayLogEntry.DataTypeUft8); - log.AppendStringArray(handle, value); - } - - public void SetActuator(bool value) - { - } - - public void SetSafeState(Action func) - { - } - - public void SetSmartDashboardType(string type) - { - } - - public void Update() - { - long timestamp = 0; // Get FPGA timestamp - foreach (var entry in dataLogMap) - { - var key = entry.Key; - var val = entry.Value(); - switch (key) - { - case BooleanArrayLogEntry logEntry: - logEntry.Append((bool[])val, timestamp); - break; - case BooleanLogEntry logEntry: - logEntry.Append((bool)val, timestamp); - break; - case DoubleArrayLogEntry logEntry: - logEntry.Append((double[])val, timestamp); - break; - case DoubleLogEntry logEntry: - logEntry.Append((double)val, timestamp); - break; - case FloatArrayLogEntry logEntry: - logEntry.Append((float[])val, timestamp); - break; - case FloatLogEntry logEntry: - logEntry.Append((float)val, timestamp); - break; - case IntegerArrayLogEntry logEntry: - logEntry.Append((long[])val, timestamp); - break; - case IntegerLogEntry logEntry: - logEntry.Append((long)val, timestamp); - break; - case RawLogEntry logEntry: - logEntry.Append((byte[])val, timestamp); - break; - case StringArrayLogEntry logEntry: - logEntry.Append((string[])val, timestamp); - break; - case StringLogEntry logEntry: - logEntry.Append((string)val, timestamp); - break; - } - } - foreach (var updateTable in updateTables) - { - updateTable(); - } - } -} diff --git a/src/thirdparty/Stereologue/DataLogger.cs b/src/thirdparty/Stereologue/DataLogger.cs deleted file mode 100644 index 18d1c0f2..00000000 --- a/src/thirdparty/Stereologue/DataLogger.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Stereologue; - -public sealed class DataLogger -{ - -} diff --git a/src/thirdparty/Stereologue/EvaluateAttributes.cs b/src/thirdparty/Stereologue/EvaluateAttributes.cs deleted file mode 100644 index eebcdd93..00000000 --- a/src/thirdparty/Stereologue/EvaluateAttributes.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Collections.Generic; -using System.Reflection; - -namespace Stereologue; - -internal static class EvaluateAttributes -{ - public static IEnumerable From(MemberInfo member) - { - return member.GetCustomAttributes(); - } -} diff --git a/src/thirdparty/Stereologue/EvaluateField.cs b/src/thirdparty/Stereologue/EvaluateField.cs deleted file mode 100644 index 9cf33761..00000000 --- a/src/thirdparty/Stereologue/EvaluateField.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Reflection; - -namespace Stereologue; - -internal class EvaluateField -{ - private static Func GetSupplier(FieldInfo field, ILogged logged) - { - return () => - { - return field.GetValue(logged)!; - }; - } - - private static object? GetField(FieldInfo field, ILogged logged) - { - return field.GetValue(logged); - } - - public static void EvalField(FieldInfo field, ILogged loggable, string rootPath) - { - } -} diff --git a/src/thirdparty/Stereologue/LogType.cs b/src/thirdparty/Stereologue/LogType.cs index 184fcbb3..a8bde70e 100644 --- a/src/thirdparty/Stereologue/LogType.cs +++ b/src/thirdparty/Stereologue/LogType.cs @@ -5,6 +5,7 @@ namespace Stereologue; [Flags] public enum LogType { + None = 0, File = 1, Nt = 2, Once = 4, diff --git a/src/thirdparty/Stereologue/NTLogger.cs b/src/thirdparty/Stereologue/NTLogger.cs deleted file mode 100644 index aa2d3dc7..00000000 --- a/src/thirdparty/Stereologue/NTLogger.cs +++ /dev/null @@ -1,10 +0,0 @@ -using NetworkTables; - -namespace Stereologue; - -public sealed class NTLogger -{ - private readonly NetworkTableInstance table = NetworkTableInstance.Default; - - -} diff --git a/src/thirdparty/Stereologue/Stereologuer.cs b/src/thirdparty/Stereologue/Stereologuer.cs index 5e49edba..82ff91c8 100644 --- a/src/thirdparty/Stereologue/Stereologuer.cs +++ b/src/thirdparty/Stereologue/Stereologuer.cs @@ -13,6 +13,10 @@ public sealed class Stereologuer { private string rootPath = "NOTSET"; + private readonly NetworkTableInstance ntInstance = NetworkTableInstance.Default; + + private Stereologuer() { } + public static Stereologuer Logger { get; } = new(); public void Setup(string name, bool fileOnly, bool lazyLogging) @@ -33,12 +37,23 @@ public static void UpdateAll(ILogged logged) private record struct TypedLogs(TNT? topic, TDataLog? logEntry) where TNT : class, IPublisher where TDataLog : DataLogEntry; - private readonly Dictionary> integerLogs = []; private readonly Dictionary> booleanLogs = []; + private readonly Dictionary> integerLogs = []; + private readonly Dictionary> floatLogs = []; private readonly Dictionary> doubleLogs = []; + private readonly Dictionary> stringLogs = []; private readonly Dictionary> structLogs = []; private readonly Dictionary> protobufLogs = []; + private readonly Dictionary> booleanArrayLogs = []; + private readonly Dictionary> integerArrayLogs = []; + private readonly Dictionary> floatArrayLogs = []; + private readonly Dictionary> doubleArrayLogs = []; + private readonly Dictionary> stringArrayLogs = []; + private readonly Dictionary> structArrayLogs = []; + private readonly Dictionary> protobufArrayLogs = []; + private readonly Dictionary> rawLogs = []; + public void LogBoolean(string path, LogType logType, bool value, LogLevel logLevel = LogLevel.Default) { ref var logs = ref CheckDoLog(path, ref logType, logLevel, booleanLogs); @@ -48,7 +63,7 @@ public void LogBoolean(string path, LogType logType, bool value, LogLevel logLev } if (logType.HasFlag(LogType.Nt)) { - logs.topic ??= NetworkTableInstance.Default.GetBooleanTopic(path).Publish(PubSubOptions.None); + logs.topic ??= ntInstance.GetBooleanTopic(path).Publish(PubSubOptions.None); logs.topic.Set(value); } if (logType.HasFlag(LogType.File)) @@ -67,7 +82,7 @@ public void LogInteger(string path, LogType logType, long value, LogLevel logLev } if (logType.HasFlag(LogType.Nt)) { - logs.topic ??= NetworkTableInstance.Default.GetIntegerTopic(path).Publish(PubSubOptions.None); + logs.topic ??= ntInstance.GetIntegerTopic(path).Publish(PubSubOptions.None); logs.topic.Set(value); } if (logType.HasFlag(LogType.File)) @@ -86,7 +101,7 @@ public void LogStruct(string path, LogType logType, in T value, LogLevel logL } if (logType.HasFlag(LogType.Nt)) { - logs.topic ??= NetworkTableInstance.Default.GetStructTopic(path).Publish(PubSubOptions.None); + logs.topic ??= ntInstance.GetStructTopic(path).Publish(PubSubOptions.None); IStructPublisher tl = (IStructPublisher)logs.topic; tl.Set(value); } @@ -107,7 +122,7 @@ public void LogProto(string path, LogType logType, in T value, LogLevel logLe } if (logType.HasFlag(LogType.Nt)) { - logs.topic ??= NetworkTableInstance.Default.GetProtobufTopic(path).Publish(PubSubOptions.None); + logs.topic ??= ntInstance.GetProtobufTopic(path).Publish(PubSubOptions.None); IProtobufPublisher tl = (IProtobufPublisher)logs.topic; tl.Set(value); } @@ -121,11 +136,22 @@ public void LogProto(string path, LogType logType, in T value, LogLevel logLe public void LogChar(string path, LogType logType, char value, LogLevel logLevel = LogLevel.Default) { - ref var logs = ref CheckDoLog(path, ref logType, logLevel, doubleLogs); + ref var logs = ref CheckDoLog(path, ref logType, logLevel, stringLogs); if (Unsafe.IsNullRef(ref logs)) { return; } + var stringValue = $"{value}"; + if (logType.HasFlag(LogType.Nt)) + { + logs.topic ??= ntInstance.GetStringTopic(path).Publish(PubSubOptions.None); + logs.topic.Set(stringValue); + } + if (logType.HasFlag(LogType.File)) + { + logs.logEntry ??= new StringLogEntry(log, path); + logs.logEntry.Append(stringValue); + } } public void LogFloat(string path, LogType logType, float value, LogLevel logLevel = LogLevel.Default) @@ -146,72 +172,175 @@ public void LogDouble(string path, LogType logType, double value, LogLevel logLe } } - public void LogString(string path, LogType logType, string? value, LogLevel logLevel = LogLevel.Default) + public void LogString(string path, LogType logType, string value, LogLevel logLevel = LogLevel.Default) { - LogString(path, logType, value.AsSpan()); + ref var logs = ref CheckDoLog(path, ref logType, logLevel, stringLogs); + if (Unsafe.IsNullRef(ref logs)) + { + return; + } + if (logType.HasFlag(LogType.Nt)) + { + logs.topic ??= ntInstance.GetStringTopic(path).Publish(PubSubOptions.None); + logs.topic.Set(value); + } + if (logType.HasFlag(LogType.File)) + { + logs.logEntry ??= new StringLogEntry(log, path); + logs.logEntry.Append(value); + } } public void LogString(string path, LogType logType, ReadOnlySpan value, LogLevel logLevel = LogLevel.Default) { - ref var logs = ref CheckDoLog(path, ref logType, logLevel, doubleLogs); + ref var logs = ref CheckDoLog(path, ref logType, logLevel, stringLogs); if (Unsafe.IsNullRef(ref logs)) { return; } + if (logType.HasFlag(LogType.Nt)) + { + logs.topic ??= ntInstance.GetStringTopic(path).Publish(PubSubOptions.None); + logs.topic.Set(value); + } + if (logType.HasFlag(LogType.File)) + { + logs.logEntry ??= new StringLogEntry(log, path); + logs.logEntry.Append(value); + } + } + + public void LogString(string path, LogType logType, ReadOnlySpan value, LogLevel logLevel = LogLevel.Default) + { + ref var logs = ref CheckDoLog(path, ref logType, logLevel, stringLogs); + if (Unsafe.IsNullRef(ref logs)) + { + return; + } + if (logType.HasFlag(LogType.Nt)) + { + logs.topic ??= ntInstance.GetStringTopic(path).Publish(PubSubOptions.None); + logs.topic.Set(value); + } + if (logType.HasFlag(LogType.File)) + { + logs.logEntry ??= new StringLogEntry(log, path); + logs.logEntry.Append(value); + } } public void LogBooleanArray(string path, LogType logType, ReadOnlySpan value, LogLevel logLevel = LogLevel.Default) { - ref var logs = ref CheckDoLog(path, ref logType, logLevel, doubleLogs); + ref var logs = ref CheckDoLog(path, ref logType, logLevel, booleanArrayLogs); if (Unsafe.IsNullRef(ref logs)) { return; } + if (logType.HasFlag(LogType.Nt)) + { + logs.topic ??= ntInstance.GetBooleanArrayTopic(path).Publish(PubSubOptions.None); + logs.topic.Set(value); + } + if (logType.HasFlag(LogType.File)) + { + logs.logEntry ??= new BooleanArrayLogEntry(log, path); + logs.logEntry.Append(value); + } } - public void LogStringArray(string path, LogType logType, ReadOnlySpan value, LogLevel logLevel = LogLevel.Default) + public void LogStringArray(string path, LogType logType, ReadOnlySpan value, LogLevel logLevel = LogLevel.Default) { - ref var logs = ref CheckDoLog(path, ref logType, logLevel, doubleLogs); + ref var logs = ref CheckDoLog(path, ref logType, logLevel, stringArrayLogs); if (Unsafe.IsNullRef(ref logs)) { return; } + if (logType.HasFlag(LogType.Nt)) + { + logs.topic ??= ntInstance.GetStringArrayTopic(path).Publish(PubSubOptions.None); + logs.topic.Set(value); + } + if (logType.HasFlag(LogType.File)) + { + logs.logEntry ??= new StringArrayLogEntry(log, path); + logs.logEntry.Append(value); + } } public void LogRaw(string path, LogType logType, ReadOnlySpan value, LogLevel logLevel = LogLevel.Default) { - ref var logs = ref CheckDoLog(path, ref logType, logLevel, doubleLogs); + ref var logs = ref CheckDoLog(path, ref logType, logLevel, rawLogs); if (Unsafe.IsNullRef(ref logs)) { return; } + if (logType.HasFlag(LogType.Nt)) + { + logs.topic ??= ntInstance.GetRawTopic(path).Publish("raw", PubSubOptions.None); + logs.topic.Set(value); + } + if (logType.HasFlag(LogType.File)) + { + logs.logEntry ??= new RawLogEntry(log, path); + logs.logEntry.Append(value); + } } public void LogIntegerArray(string path, LogType logType, ReadOnlySpan value, LogLevel logLevel = LogLevel.Default) { - ref var logs = ref CheckDoLog(path, ref logType, logLevel, doubleLogs); + ref var logs = ref CheckDoLog(path, ref logType, logLevel, integerArrayLogs); if (Unsafe.IsNullRef(ref logs)) { return; } + if (logType.HasFlag(LogType.Nt)) + { + logs.topic ??= ntInstance.GetIntegerArrayTopic(path).Publish(PubSubOptions.None); + logs.topic.Set(value); + } + if (logType.HasFlag(LogType.File)) + { + logs.logEntry ??= new IntegerArrayLogEntry(log, path); + logs.logEntry.Append(value); + } } public void LogFloatArray(string path, LogType logType, ReadOnlySpan value, LogLevel logLevel = LogLevel.Default) { - ref var logs = ref CheckDoLog(path, ref logType, logLevel, doubleLogs); + ref var logs = ref CheckDoLog(path, ref logType, logLevel, floatArrayLogs); if (Unsafe.IsNullRef(ref logs)) { return; } + if (logType.HasFlag(LogType.Nt)) + { + logs.topic ??= ntInstance.GetFloatArrayTopic(path).Publish(PubSubOptions.None); + logs.topic.Set(value); + } + if (logType.HasFlag(LogType.File)) + { + logs.logEntry ??= new FloatArrayLogEntry(log, path); + logs.logEntry.Append(value); + } } public void LogDoubleArray(string path, LogType logType, ReadOnlySpan value, LogLevel logLevel = LogLevel.Default) { - ref var logs = ref CheckDoLog(path, ref logType, logLevel, doubleLogs); + ref var logs = ref CheckDoLog(path, ref logType, logLevel, doubleArrayLogs); if (Unsafe.IsNullRef(ref logs)) { return; } + if (logType.HasFlag(LogType.Nt)) + { + logs.topic ??= ntInstance.GetDoubleArrayTopic(path).Publish(PubSubOptions.None); + logs.topic.Set(value); + } + if (logType.HasFlag(LogType.File)) + { + logs.logEntry ??= new DoubleArrayLogEntry(log, path); + logs.logEntry.Append(value); + } } private ref TypedLogs CheckDoLog(string path, diff --git a/src/wpiutil/Logging/StringLogEntry.cs b/src/wpiutil/Logging/StringLogEntry.cs index 934841ec..2657b875 100644 --- a/src/wpiutil/Logging/StringLogEntry.cs +++ b/src/wpiutil/Logging/StringLogEntry.cs @@ -37,6 +37,16 @@ public void Append(string value, long timestamp = 0) m_log.AppendString(m_entry, value, timestamp); } + /// + /// Appends a record to the log. + /// + /// Value to record + /// Time stamp (0 to indicate now) + public void Append(ReadOnlySpan value, long timestamp = 0) + { + m_log.AppendString(m_entry, value, timestamp); + } + /// /// Appends a record to the log. /// diff --git a/test/stereologue.test/TestTree.cs b/test/stereologue.test/TestTree.cs index ff410f24..449e3637 100644 --- a/test/stereologue.test/TestTree.cs +++ b/test/stereologue.test/TestTree.cs @@ -92,7 +92,7 @@ public partial record class GenerateRecordClass [GenerateLog] public partial class GenerateAllKnownTypes { - [Log(LogType = 0)] + [Log(LogType = LogType.None)] public bool boolVar; [Log(Key = "CustomKey")] public char charVar; @@ -101,11 +101,11 @@ public partial class GenerateAllKnownTypes [Log(LogType = LogType.File | LogType.Nt | LogType.Once)] public sbyte sbyteVar; public short shortVar; - [Log] + [Log(Key = null!)] public ushort ushortVar; - [Log] + [Log(LogType = LogType.File)] public int intVar; - [Log] + [Log(LogType = LogType.File)] public uint uintVar; [Log] public long longVar;