Skip to content

Commit

Permalink
Implemented sequences of structs
Browse files Browse the repository at this point in the history
  • Loading branch information
jmmorato committed Oct 6, 2024
1 parent bdb2f1e commit 7fd9e79
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 3 deletions.
97 changes: 96 additions & 1 deletion Native/OpenDDSharp.IdlGenerator/csharp_cdr_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,24 @@ csharp_cdr_generator::implement_to_cdr(const std::vector<AST_Field *> &fields, c
ret.append(indent);
ret.append("}\n\n");

ret.append(indent);
ret.append("public ReadOnlySpan<byte> ToCDR(OpenDDSharp.Marshaller.Cdr.CdrWriter writer)\n");
ret.append(indent);
ret.append("{\n");

for (unsigned int i = 0; i < fields.size(); i++) {
AST_Field *field = fields[i];
AST_Type *field_type = field->field_type();
const char *field_name = field->local_name()->get_string();

ret.append(implement_to_cdr_field(field_type, field_name, indent));
}

ret.append(indent);
ret.append(" return writer.GetBuffer();\n");
ret.append(indent);
ret.append("}\n\n");

return ret;
}

Expand Down Expand Up @@ -1053,6 +1071,51 @@ csharp_cdr_generator::implement_to_cdr_field(AST_Type *field_type, std::string f
ret.append(".Select(e => (uint)e)).ToList());\n");
break;
}
case AST_Decl::NT_struct: {
ret.append(" if (");
ret.append(field_name);
ret.append(" != null && ");
ret.append(field_name);
ret.append(".Count > 0)\n");

ret.append(indent);
ret.append(" {\n");

ret.append(indent);
ret.append(" writer.WriteUInt32((uint)");
ret.append(field_name);
ret.append(".Count);\n");

ret.append(indent);
ret.append(" foreach (var s in ");
ret.append(field_name);
ret.append(")\n");

ret.append(indent);
ret.append(" {\n");

ret.append(indent);
ret.append(" s.ToCDR(writer);\n");

ret.append(indent);
ret.append(" }\n");

ret.append(indent);
ret.append(" }\n");

ret.append(indent);
ret.append(" else\n");

ret.append(indent);
ret.append(" {\n");

ret.append(indent);
ret.append(" writer.WriteUInt32(0);\n");

ret.append(indent);
ret.append(" }\n");
break;
}
default:
break;
}
Expand Down Expand Up @@ -1582,7 +1645,39 @@ csharp_cdr_generator::implement_from_cdr_field(AST_Type *field_type, std::string
case AST_Decl::NT_struct: {
ret.append(" ");
ret.append(field_name);
ret.append(" = reader.ReadStructSequence();\n");
ret.append(" = new List<");
ret.append(replaceString(std::string(base_type->full_name()), std::string("::"), std::string(".")));
ret.append(">();\n");

ret.append(indent);
ret.append(" var count");
ret.append(field_name);
ret.append(" = reader.ReadUInt32();\n");

ret.append(indent);
ret.append(" for (int i = 0; i < count");
ret.append(field_name);
ret.append("; i++)\n");

ret.append(indent);
ret.append(" {\n");

ret.append(indent);
ret.append(" var aux = new ");
ret.append(replaceString(std::string(base_type->full_name()), std::string("::"), std::string(".")));
ret.append("();\n");

ret.append(indent);
ret.append(" aux.FromCDR(reader);\n");

ret.append(indent);
ret.append(" ");
ret.append(field_name);
ret.append(".Add(aux);\n");

ret.append(indent);
ret.append(" }\n");

break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenDDSharp.Marshaller/Cdr/CdrReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public string ReadString()
}

/// <summary>
/// Reads an string from the stream.
/// Reads a string from the stream.
/// </summary>
/// <returns>The string value.</returns>
public string ReadWString()
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenDDSharp.Marshaller/Cdr/CdrWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public ReadOnlySpan<byte> GetBuffer()
public void WriteSByte(sbyte b) => _writer.Write(b);

/// <summary>
/// Writes a array of unsigned bytes to the stream.
/// Writes an array of unsigned bytes to the stream.
/// </summary>
/// <param name="buf">The unsigned bytes to be written.</param>
public void WriteBytes(ReadOnlySpan<byte> buf)
Expand Down
96 changes: 96 additions & 0 deletions Tests/OpenDDSharp.UnitTest/CodeGeneratorCdrWrapperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1906,6 +1906,102 @@ public void TestGeneratedStructuresTypes()
Assert.AreEqual(string.Empty, defaultStruct.NestedStructField.Message);
}

/// <summary>
/// Test the code generated for the sequence of structures.
/// </summary>
[TestMethod]
[TestCategory(TEST_CATEGORY)]
public void TestGeneratedStructureSequences()
{
using var evt = new ManualResetEventSlim(false);

var typeSupport = new TestStructSequenceTypeSupport();
var typeName = typeSupport.GetTypeName();
var ret = typeSupport.RegisterType(_participant, typeName);
Assert.AreEqual(ReturnCode.Ok, ret);

_topic = _participant.CreateTopic(TestContext.TestName, typeName);
Assert.IsNotNull(_topic);

var drQos = new DataReaderQos
{
Reliability =
{
Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos,
},
};
var dr = _subscriber.CreateDataReader(_topic, drQos);
Assert.IsNotNull(dr);
var dataReader = new TestStructSequenceDataReader(dr);

var dw = _publisher.CreateDataWriter(_topic);
Assert.IsNotNull(dw);
var dataWriter = new TestStructSequenceDataWriter(dw);

Assert.IsTrue(dataWriter.WaitForSubscriptions(1, 5000));
Assert.IsTrue(dataReader.WaitForPublications(1, 5000));

var statusCondition = dr.StatusCondition;
Assert.IsNotNull(statusCondition);
statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus;
TestHelper.CreateWaitSetThread(evt, statusCondition);

var defaultStruct = new TestStructSequence();

var data = new TestStructSequence
{
UnboundedStructSequenceField =
{
new NestedStruct { Id = 1, Message = "With your feet in the air and your head on the ground" },
new NestedStruct { Id = 2, Message = "Try this trick and spin it, yeah" },
new NestedStruct { Id = 3, Message = "Your head will collapse" },
new NestedStruct { Id = 4, Message = "But there's nothing in it" },
new NestedStruct { Id = 5, Message = "And you'll ask yourself" },
new NestedStruct { Id = 6, Message = "Where is my mind?" },
},
BoundedStructSequenceField =
{
new NestedStruct { Id = 1, Message = "With your feet in the air and your head on the ground" },
new NestedStruct { Id = 2, Message = "Try this trick and spin it, yeah" },
new NestedStruct { Id = 3, Message = "Your head will collapse" },
new NestedStruct { Id = 4, Message = "But there's nothing in it" },
new NestedStruct { Id = 5, Message = "And you'll ask yourself" },
},
};

ret = dataWriter.Write(data);
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 TestStructSequence();
var sampleInfo = new SampleInfo();
ret = dataReader.ReadNextSample(received, sampleInfo);
Assert.AreEqual(ReturnCode.Ok, ret);

for (var i = 1; i < data.BoundedStructSequenceField.Count; i++)
{
Assert.AreEqual(data.BoundedStructSequenceField[i].Id, received.BoundedStructSequenceField[i].Id);
Assert.AreEqual(data.BoundedStructSequenceField[i].Message, received.BoundedStructSequenceField[i].Message);
}
for (var i = 1; i < data.BoundedStructSequenceField.Count; i++)
{
Assert.AreEqual(data.UnboundedStructSequenceField[i].Id, received.UnboundedStructSequenceField[i].Id);
Assert.AreEqual(data.UnboundedStructSequenceField[i].Message, received.UnboundedStructSequenceField[i].Message);
}

Assert.AreEqual(data.BoundedStructSequenceField.GetType(), typeof(List<NestedStruct>));
Assert.AreEqual(data.UnboundedStructSequenceField.GetType(), typeof(List<NestedStruct>));

Assert.IsNotNull(defaultStruct.BoundedStructSequenceField);
Assert.AreEqual(0, defaultStruct.BoundedStructSequenceField.Count);
Assert.IsNotNull(defaultStruct.UnboundedStructSequenceField);
Assert.AreEqual(0, defaultStruct.UnboundedStructSequenceField.Count);
}

/// <summary>
/// Test the code generated for the constants.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions Tests/TestIdlCdr/IDL/Test.idl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ module CdrWrapper {
NestedStruct NestedStructField;
};

@topic
struct TestStructSequence {
UnboundedStructSequenceType UnboundedStructSequenceField;
BoundedStructSequenceType BoundedStructSequenceField;
};

@topic
struct TestPrimitive {
boolean BoolField;
Expand Down

0 comments on commit 7fd9e79

Please sign in to comment.