From 44d76e853e3000ae8233515f316fdf35791cc5d1 Mon Sep 17 00:00:00 2001 From: jose_morato Date: Wed, 9 Oct 2024 15:47:53 +0200 Subject: [PATCH] [feature] Implemented CDR include IDL files --- .../csharp_cdr_generator.cpp | 25 +++++- .../CodeGeneratorCdrWrapperTest.cs | 79 +++++++++++++++++++ .../OpenDDSharp.UnitTest.csproj | 4 +- Tests/TestIdlCdr/IDL/Test.idl | 8 ++ Tests/TestIdlCdr/IDL/TestInclude.idl | 8 ++ 5 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 Tests/TestIdlCdr/IDL/TestInclude.idl diff --git a/Native/OpenDDSharp.IdlGenerator/csharp_cdr_generator.cpp b/Native/OpenDDSharp.IdlGenerator/csharp_cdr_generator.cpp index 8a9bed74..db0e7181 100644 --- a/Native/OpenDDSharp.IdlGenerator/csharp_cdr_generator.cpp +++ b/Native/OpenDDSharp.IdlGenerator/csharp_cdr_generator.cpp @@ -929,10 +929,27 @@ csharp_cdr_generator::implement_to_cdr_field(AST_Type *field_type, std::string f break; } case AST_Decl::NT_struct: { - ret.append(indent); - ret.append(" writer.WriteBytes("); - ret.append(field_name); - ret.append(".ToCDR());\n"); + ret.append(" if ("); + ret.append(field_name); + ret.append(" == null)\n"); + + ret.append(indent); + ret.append(" {\n"); + + ret.append(indent); + ret.append(" "); + ret.append(field_name); + ret.append(" = new "); + ret.append(replaceString(std::string(field_type->full_name()), std::string("::"), std::string("."))); + ret.append("();\n"); + + ret.append(indent); + ret.append(" }\n"); + + ret.append(indent); + ret.append(" "); + ret.append(field_name); + ret.append(".ToCDR(writer);\n"); break; } case AST_Decl::NT_string: { diff --git a/Tests/OpenDDSharp.UnitTest/CodeGeneratorCdrWrapperTest.cs b/Tests/OpenDDSharp.UnitTest/CodeGeneratorCdrWrapperTest.cs index 55477cda..883c5d66 100644 --- a/Tests/OpenDDSharp.UnitTest/CodeGeneratorCdrWrapperTest.cs +++ b/Tests/OpenDDSharp.UnitTest/CodeGeneratorCdrWrapperTest.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Threading; using CdrWrapper; +using CdrWrapperInclude; using Microsoft.VisualStudio.TestTools.UnitTesting; using OpenDDSharp.DDS; using OpenDDSharp.UnitTest.Helpers; @@ -74,6 +75,84 @@ public void TestCleanup() #endregion #region Test Methods + /// + /// Test include idl files. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestInclude() + { + using var evt = new ManualResetEventSlim(false); + + var test = new TestInclude(); + Assert.AreEqual(typeof(IncludeStruct), test.IncludeField.GetType()); + Assert.IsNotNull(test.IncludeField); + Assert.AreEqual(test.IncludeField.Message.GetType(), typeof(string)); + + var typeSupport = new TestIncludeTypeSupport(); + var typeName = typeSupport.GetTypeName(); + var ret = typeSupport.RegisterType(_participant, typeName); + Assert.AreEqual(ReturnCode.Ok, ret); + + var topic = _participant.CreateTopic("TestTopic", typeName); + Assert.IsNotNull(topic); + + var drQos = new DataReaderQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + }; + var dr = _subscriber.CreateDataReader(topic, drQos); + Assert.IsNotNull(dr); + var dataReader = new TestIncludeDataReader(dr); + + var statusCondition = dr.StatusCondition; + Assert.IsNotNull(statusCondition); + statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; + TestHelper.CreateWaitSetThread(evt, statusCondition); + + var dw = _publisher.CreateDataWriter(topic); + Assert.IsNotNull(dw); + var dataWriter = new TestIncludeDataWriter(dw); + + Assert.IsTrue(dataWriter.WaitForSubscriptions(1, 5000)); + Assert.IsTrue(dataReader.WaitForPublications(1, 5000)); + + test = new TestInclude + { + Id = "1", + IncludeField = new IncludeStruct + { + Message = "Test", + }, + }; + ret = dataWriter.Write(test); + Assert.AreEqual(ReturnCode.Ok, ret); + + ret = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); + Assert.AreEqual(ReturnCode.Ok, ret); + + Assert.IsTrue(evt.Wait(1_500)); + + var received = new TestInclude(); + var sampleInfo = new SampleInfo(); + ret = dataReader.ReadNextSample(received, sampleInfo); + Assert.AreEqual(ReturnCode.Ok, ret); + + Assert.AreEqual(test.Id, received.Id); + Assert.AreEqual(test.IncludeField.Message, received.IncludeField.Message); + + dr.DeleteContainedEntities(); + _subscriber.DeleteDataReader(dr); + _subscriber.DeleteContainedEntities(); + _publisher.DeleteDataWriter(dw); + _publisher.DeleteContainedEntities(); + _participant.DeleteTopic(topic); + _participant.DeleteContainedEntities(); + } + /// /// Test the code generated for the primitives types. /// diff --git a/Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj b/Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj index d1ac2ee2..ac15fbc7 100644 --- a/Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj +++ b/Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj @@ -62,8 +62,8 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - - + + diff --git a/Tests/TestIdlCdr/IDL/Test.idl b/Tests/TestIdlCdr/IDL/Test.idl index e591f60b..017fcbea 100644 --- a/Tests/TestIdlCdr/IDL/Test.idl +++ b/Tests/TestIdlCdr/IDL/Test.idl @@ -1,3 +1,5 @@ +#include "TestInclude.idl" + module CdrWrapper { const short TEST_SHORT_CONST = -1; @@ -149,6 +151,12 @@ module CdrWrapper { StructMultiArrayType StructMultiArrayField; }; + @topic + struct TestInclude { + @key string Id; + CdrWrapperInclude::IncludeStruct IncludeField; + }; + @topic struct TestPrimitive { boolean BoolField; diff --git a/Tests/TestIdlCdr/IDL/TestInclude.idl b/Tests/TestIdlCdr/IDL/TestInclude.idl new file mode 100644 index 00000000..e276c221 --- /dev/null +++ b/Tests/TestIdlCdr/IDL/TestInclude.idl @@ -0,0 +1,8 @@ +module CdrWrapperInclude { + + @topic + struct IncludeStruct { + @key string Message; + }; + +}; \ No newline at end of file