Skip to content

Commit

Permalink
Fix NamedType generation in MATLAB codegen (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
naegelejd authored Dec 13, 2024
1 parent 073961c commit 8699221
Show file tree
Hide file tree
Showing 145 changed files with 1,419 additions and 530 deletions.
45 changes: 45 additions & 0 deletions cpp/test/generated/binary/protocols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,19 @@ struct IsTriviallySerializable<test_model::RecordContainingNestedGenericRecords>
offsetof(__T__, f1) < offsetof(__T__, f1a) && offsetof(__T__, f1a) < offsetof(__T__, f2) && offsetof(__T__, f2) < offsetof(__T__, f2a) && offsetof(__T__, f2a) < offsetof(__T__, nested);
};

template <>
struct IsTriviallySerializable<test_model::RecordContainingVectorsOfAliases> {
using __T__ = test_model::RecordContainingVectorsOfAliases;
static constexpr bool value =
std::is_standard_layout_v<__T__> &&
IsTriviallySerializable<decltype(__T__::strings)>::value &&
IsTriviallySerializable<decltype(__T__::maps)>::value &&
IsTriviallySerializable<decltype(__T__::arrays)>::value &&
IsTriviallySerializable<decltype(__T__::tuples)>::value &&
(sizeof(__T__) == (sizeof(__T__::strings) + sizeof(__T__::maps) + sizeof(__T__::arrays) + sizeof(__T__::tuples))) &&
offsetof(__T__, strings) < offsetof(__T__, maps) && offsetof(__T__, maps) < offsetof(__T__, arrays) && offsetof(__T__, arrays) < offsetof(__T__, tuples);
};

template <>
struct IsTriviallySerializable<test_model::RecordWithComputedFields> {
using __T__ = test_model::RecordWithComputedFields;
Expand Down Expand Up @@ -2566,6 +2579,30 @@ template<typename A, yardl::binary::Reader<A> ReadA, typename B, yardl::binary::
test_model::binary::ReadRecordContainingGenericRecords<std::string, yardl::binary::ReadString, int32_t, yardl::binary::ReadInteger>(stream, value.nested);
}

[[maybe_unused]] void WriteRecordContainingVectorsOfAliases(yardl::binary::CodedOutputStream& stream, test_model::RecordContainingVectorsOfAliases const& value) {
if constexpr (yardl::binary::IsTriviallySerializable<test_model::RecordContainingVectorsOfAliases>::value) {
yardl::binary::WriteTriviallySerializable(stream, value);
return;
}

yardl::binary::WriteVector<test_model::AliasedString, test_model::binary::WriteAliasedString>(stream, value.strings);
yardl::binary::WriteVector<test_model::AliasedMap<std::string, int32_t>, test_model::binary::WriteAliasedMap<std::string, yardl::binary::WriteString, int32_t, yardl::binary::WriteInteger>>(stream, value.maps);
yardl::binary::WriteVector<test_model::Image<float>, test_model::binary::WriteImage<float, yardl::binary::WriteFloatingPoint>>(stream, value.arrays);
yardl::binary::WriteVector<test_model::MyTuple<int32_t, test_model::SimpleRecord>, test_model::binary::WriteMyTuple<int32_t, yardl::binary::WriteInteger, test_model::SimpleRecord, test_model::binary::WriteSimpleRecord>>(stream, value.tuples);
}

[[maybe_unused]] void ReadRecordContainingVectorsOfAliases(yardl::binary::CodedInputStream& stream, test_model::RecordContainingVectorsOfAliases& value) {
if constexpr (yardl::binary::IsTriviallySerializable<test_model::RecordContainingVectorsOfAliases>::value) {
yardl::binary::ReadTriviallySerializable(stream, value);
return;
}

yardl::binary::ReadVector<test_model::AliasedString, test_model::binary::ReadAliasedString>(stream, value.strings);
yardl::binary::ReadVector<test_model::AliasedMap<std::string, int32_t>, test_model::binary::ReadAliasedMap<std::string, yardl::binary::ReadString, int32_t, yardl::binary::ReadInteger>>(stream, value.maps);
yardl::binary::ReadVector<test_model::Image<float>, test_model::binary::ReadImage<float, yardl::binary::ReadFloatingPoint>>(stream, value.arrays);
yardl::binary::ReadVector<test_model::MyTuple<int32_t, test_model::SimpleRecord>, test_model::binary::ReadMyTuple<int32_t, yardl::binary::ReadInteger, test_model::SimpleRecord, test_model::binary::ReadSimpleRecord>>(stream, value.tuples);
}

[[maybe_unused]] void WriteAliasedIntOrSimpleRecord(yardl::binary::CodedOutputStream& stream, test_model::AliasedIntOrSimpleRecord const& value) {
if constexpr (yardl::binary::IsTriviallySerializable<test_model::AliasedIntOrSimpleRecord>::value) {
yardl::binary::WriteTriviallySerializable(stream, value);
Expand Down Expand Up @@ -4387,6 +4424,10 @@ void AliasesWriter::EndStreamOfAliasedGenericUnion2Impl() {
yardl::binary::WriteInteger(stream_, 0U);
}

void AliasesWriter::WriteVectorsImpl(std::vector<test_model::RecordContainingVectorsOfAliases> const& value) {
yardl::binary::WriteVector<test_model::RecordContainingVectorsOfAliases, test_model::binary::WriteRecordContainingVectorsOfAliases>(stream_, value);
}

void AliasesWriter::Flush() {
stream_.Flush();
}
Expand Down Expand Up @@ -4442,6 +4483,10 @@ bool AliasesReader::ReadStreamOfAliasedGenericUnion2Impl(std::vector<test_model:
return current_block_remaining_ != 0;
}

void AliasesReader::ReadVectorsImpl(std::vector<test_model::RecordContainingVectorsOfAliases>& value) {
yardl::binary::ReadVector<test_model::RecordContainingVectorsOfAliases, test_model::binary::ReadRecordContainingVectorsOfAliases>(stream_, value);
}

void AliasesReader::CloseImpl() {
stream_.VerifyFinished();
}
Expand Down
2 changes: 2 additions & 0 deletions cpp/test/generated/binary/protocols.h
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,7 @@ class AliasesWriter : public test_model::AliasesWriterBase, yardl::binary::Binar
void WriteStreamOfAliasedGenericUnion2Impl(test_model::AliasedGenericUnion2<test_model::AliasedString, test_model::AliasedEnum> const& value) override;
void WriteStreamOfAliasedGenericUnion2Impl(std::vector<test_model::AliasedGenericUnion2<test_model::AliasedString, test_model::AliasedEnum>> const& values) override;
void EndStreamOfAliasedGenericUnion2Impl() override;
void WriteVectorsImpl(std::vector<test_model::RecordContainingVectorsOfAliases> const& value) override;
void CloseImpl() override;

Version version_;
Expand Down Expand Up @@ -1369,6 +1370,7 @@ class AliasesReader : public test_model::AliasesReaderBase, yardl::binary::Binar
void ReadAliasedGenericFixedVectorImpl(test_model::AliasedGenericFixedVector<float>& value) override;
bool ReadStreamOfAliasedGenericUnion2Impl(test_model::AliasedGenericUnion2<test_model::AliasedString, test_model::AliasedEnum>& value) override;
bool ReadStreamOfAliasedGenericUnion2Impl(std::vector<test_model::AliasedGenericUnion2<test_model::AliasedString, test_model::AliasedEnum>>& values) override;
void ReadVectorsImpl(std::vector<test_model::RecordContainingVectorsOfAliases>& value) override;
void CloseImpl() override;

Version version_;
Expand Down
40 changes: 40 additions & 0 deletions cpp/test/generated/hdf5/protocols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,28 @@ struct _Inner_RecordContainingNestedGenericRecords {
test_model::hdf5::_Inner_RecordContainingGenericRecords<yardl::hdf5::InnerVlenString, std::string, int32_t, int32_t> nested;
};

struct _Inner_RecordContainingVectorsOfAliases {
_Inner_RecordContainingVectorsOfAliases() {}
_Inner_RecordContainingVectorsOfAliases(test_model::RecordContainingVectorsOfAliases const& o)
: strings(o.strings),
maps(o.maps),
arrays(o.arrays),
tuples(o.tuples) {
}

void ToOuter (test_model::RecordContainingVectorsOfAliases& o) const {
yardl::hdf5::ToOuter(strings, o.strings);
yardl::hdf5::ToOuter(maps, o.maps);
yardl::hdf5::ToOuter(arrays, o.arrays);
yardl::hdf5::ToOuter(tuples, o.tuples);
}

yardl::hdf5::InnerVlen<yardl::hdf5::InnerVlenString, test_model::AliasedString> strings;
yardl::hdf5::InnerVlen<yardl::hdf5::InnerMap<yardl::hdf5::InnerVlenString, std::string, int32_t, int32_t>, test_model::AliasedMap<std::string, int32_t>> maps;
yardl::hdf5::InnerVlen<yardl::hdf5::InnerNdArray<float, float, 2>, test_model::Image<float>> arrays;
yardl::hdf5::InnerVlen<tuples::Tuple<int32_t, test_model::SimpleRecord>, test_model::MyTuple<int32_t, test_model::SimpleRecord>> tuples;
};

struct _Inner_RecordWithComputedFields {
_Inner_RecordWithComputedFields() {}
_Inner_RecordWithComputedFields(test_model::RecordWithComputedFields const& o)
Expand Down Expand Up @@ -1706,6 +1728,16 @@ template <typename _A_Inner, typename A, typename _B_Inner, typename B>
return t;
}

[[maybe_unused]] H5::CompType GetRecordContainingVectorsOfAliasesHdf5Ddl() {
using RecordType = test_model::hdf5::_Inner_RecordContainingVectorsOfAliases;
H5::CompType t(sizeof(RecordType));
t.insertMember("strings", HOFFSET(RecordType, strings), yardl::hdf5::InnerVlenDdl(yardl::hdf5::InnerVlenStringDdl()));
t.insertMember("maps", HOFFSET(RecordType, maps), yardl::hdf5::InnerVlenDdl(yardl::hdf5::InnerMapDdl<yardl::hdf5::InnerVlenString, int32_t>(yardl::hdf5::InnerVlenStringDdl(), H5::PredType::NATIVE_INT32)));
t.insertMember("arrays", HOFFSET(RecordType, arrays), yardl::hdf5::InnerVlenDdl(yardl::hdf5::NDArrayDdl<float, float, 2>(H5::PredType::NATIVE_FLOAT)));
t.insertMember("tuples", HOFFSET(RecordType, tuples), yardl::hdf5::InnerVlenDdl(tuples::hdf5::GetTupleHdf5Ddl<int32_t, int32_t, test_model::SimpleRecord, test_model::SimpleRecord>(H5::PredType::NATIVE_INT32, test_model::hdf5::GetSimpleRecordHdf5Ddl())));
return t;
}

[[maybe_unused]] H5::CompType GetRecordWithComputedFieldsHdf5Ddl() {
using RecordType = test_model::hdf5::_Inner_RecordWithComputedFields;
H5::CompType t(sizeof(RecordType));
Expand Down Expand Up @@ -3709,6 +3741,10 @@ void AliasesWriter::EndStreamOfAliasedGenericUnion2Impl() {
streamOfAliasedGenericUnion2_dataset_state_.reset();
}

void AliasesWriter::WriteVectorsImpl(std::vector<test_model::RecordContainingVectorsOfAliases> const& value) {
yardl::hdf5::WriteScalarDataset<yardl::hdf5::InnerVlen<test_model::hdf5::_Inner_RecordContainingVectorsOfAliases, test_model::RecordContainingVectorsOfAliases>, std::vector<test_model::RecordContainingVectorsOfAliases>>(group_, "vectors", yardl::hdf5::InnerVlenDdl(test_model::hdf5::GetRecordContainingVectorsOfAliasesHdf5Ddl()), value);
}

void AliasesWriter::Flush() {
if (streamOfAliasedGenericUnion2_dataset_state_) {
streamOfAliasedGenericUnion2_dataset_state_->Flush();
Expand Down Expand Up @@ -3782,6 +3818,10 @@ bool AliasesReader::ReadStreamOfAliasedGenericUnion2Impl(test_model::AliasedGene
return true;
}

void AliasesReader::ReadVectorsImpl(std::vector<test_model::RecordContainingVectorsOfAliases>& value) {
yardl::hdf5::ReadScalarDataset<yardl::hdf5::InnerVlen<test_model::hdf5::_Inner_RecordContainingVectorsOfAliases, test_model::RecordContainingVectorsOfAliases>, std::vector<test_model::RecordContainingVectorsOfAliases>>(group_, "vectors", yardl::hdf5::InnerVlenDdl(test_model::hdf5::GetRecordContainingVectorsOfAliasesHdf5Ddl()), value);
}

StreamsOfAliasedUnionsWriter::StreamsOfAliasedUnionsWriter(std::string path)
: yardl::hdf5::Hdf5Writer::Hdf5Writer(path, "StreamsOfAliasedUnions", schema_) {
}
Expand Down
4 changes: 4 additions & 0 deletions cpp/test/generated/hdf5/protocols.h
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,8 @@ class AliasesWriter : public test_model::AliasesWriterBase, public yardl::hdf5::

void EndStreamOfAliasedGenericUnion2Impl() override;

void WriteVectorsImpl(std::vector<test_model::RecordContainingVectorsOfAliases> const& value) override;

public:
void Flush() override;

Expand Down Expand Up @@ -1106,6 +1108,8 @@ class AliasesReader : public test_model::AliasesReaderBase, public yardl::hdf5::

bool ReadStreamOfAliasedGenericUnion2Impl(test_model::AliasedGenericUnion2<test_model::AliasedString, test_model::AliasedEnum>& value) override;

void ReadVectorsImpl(std::vector<test_model::RecordContainingVectorsOfAliases>& value) override;

private:
std::unique_ptr<yardl::hdf5::UnionDatasetReader<2>> streamOfAliasedGenericUnion2_dataset_state_;
};
Expand Down
24 changes: 24 additions & 0 deletions cpp/test/generated/mocks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3994,6 +3994,22 @@ class MockAliasesWriter : public AliasesWriterBase {
EndStreamOfAliasedGenericUnion2Impl_expected_call_count_++;
}

void WriteVectorsImpl (std::vector<test_model::RecordContainingVectorsOfAliases> const& value) override {
if (WriteVectorsImpl_expected_values_.empty()) {
throw std::runtime_error("Unexpected call to WriteVectorsImpl");
}
if (WriteVectorsImpl_expected_values_.front() != value) {
throw std::runtime_error("Unexpected argument value for call to WriteVectorsImpl");
}
WriteVectorsImpl_expected_values_.pop();
}

std::queue<std::vector<test_model::RecordContainingVectorsOfAliases>> WriteVectorsImpl_expected_values_;

void ExpectWriteVectorsImpl (std::vector<test_model::RecordContainingVectorsOfAliases> const& value) {
WriteVectorsImpl_expected_values_.push(value);
}

void Verify() {
if (!WriteAliasedStringImpl_expected_values_.empty()) {
throw std::runtime_error("Expected call to WriteAliasedStringImpl was not received");
Expand Down Expand Up @@ -4028,6 +4044,9 @@ class MockAliasesWriter : public AliasesWriterBase {
if (EndStreamOfAliasedGenericUnion2Impl_expected_call_count_ > 0) {
throw std::runtime_error("Expected call to EndStreamOfAliasedGenericUnion2Impl was not received");
}
if (!WriteVectorsImpl_expected_values_.empty()) {
throw std::runtime_error("Expected call to WriteVectorsImpl was not received");
}
}
};

Expand Down Expand Up @@ -4105,6 +4124,11 @@ class TestAliasesWriterBase : public AliasesWriterBase {
mock_writer_.ExpectEndStreamOfAliasedGenericUnion2Impl();
}

void WriteVectorsImpl(std::vector<test_model::RecordContainingVectorsOfAliases> const& value) override {
writer_->WriteVectors(value);
mock_writer_.ExpectWriteVectorsImpl(value);
}

void CloseImpl() override {
close_called_ = true;
writer_->Close();
Expand Down
64 changes: 64 additions & 0 deletions cpp/test/generated/model.json
Original file line number Diff line number Diff line change
Expand Up @@ -2326,6 +2326,62 @@
]
}
},
{
"record": {
"name": "RecordContainingVectorsOfAliases",
"fields": [
{
"name": "strings",
"type": {
"vector": {
"items": "TestModel.AliasedString"
}
}
},
{
"name": "maps",
"type": {
"vector": {
"items": {
"name": "TestModel.AliasedMap",
"typeArguments": [
"string",
"int32"
]
}
}
}
},
{
"name": "arrays",
"type": {
"vector": {
"items": {
"name": "TestModel.Image",
"typeArguments": [
"float32"
]
}
}
}
},
{
"name": "tuples",
"type": {
"vector": {
"items": {
"name": "TestModel.MyTuple",
"typeArguments": [
"int32",
"TestModel.SimpleRecord"
]
}
}
}
}
]
}
},
{
"alias": {
"name": "AliasedIntOrSimpleRecord",
Expand Down Expand Up @@ -5435,6 +5491,14 @@
}
}
}
},
{
"name": "vectors",
"type": {
"vector": {
"items": "TestModel.RecordContainingVectorsOfAliases"
}
}
}
]
},
Expand Down
42 changes: 42 additions & 0 deletions cpp/test/generated/ndjson/protocols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ void from_json(ordered_json const& j, test_model::RecordContainingGenericRecords
void to_json(ordered_json& j, test_model::RecordContainingNestedGenericRecords const& value);
void from_json(ordered_json const& j, test_model::RecordContainingNestedGenericRecords& value);

void to_json(ordered_json& j, test_model::RecordContainingVectorsOfAliases const& value);
void from_json(ordered_json const& j, test_model::RecordContainingVectorsOfAliases& value);

void to_json(ordered_json& j, test_model::RecordWithComputedFields const& value);
void from_json(ordered_json const& j, test_model::RecordWithComputedFields& value);

Expand Down Expand Up @@ -2477,6 +2480,37 @@ void from_json(ordered_json const& j, test_model::RecordContainingNestedGenericR
}
}

void to_json(ordered_json& j, test_model::RecordContainingVectorsOfAliases const& value) {
j = ordered_json::object();
if (yardl::ndjson::ShouldSerializeFieldValue(value.strings)) {
j.push_back({"strings", value.strings});
}
if (yardl::ndjson::ShouldSerializeFieldValue(value.maps)) {
j.push_back({"maps", value.maps});
}
if (yardl::ndjson::ShouldSerializeFieldValue(value.arrays)) {
j.push_back({"arrays", value.arrays});
}
if (yardl::ndjson::ShouldSerializeFieldValue(value.tuples)) {
j.push_back({"tuples", value.tuples});
}
}

void from_json(ordered_json const& j, test_model::RecordContainingVectorsOfAliases& value) {
if (auto it = j.find("strings"); it != j.end()) {
it->get_to(value.strings);
}
if (auto it = j.find("maps"); it != j.end()) {
it->get_to(value.maps);
}
if (auto it = j.find("arrays"); it != j.end()) {
it->get_to(value.arrays);
}
if (auto it = j.find("tuples"); it != j.end()) {
it->get_to(value.tuples);
}
}

void to_json(ordered_json& j, test_model::RecordWithComputedFields const& value) {
j = ordered_json::object();
if (yardl::ndjson::ShouldSerializeFieldValue(value.array_field)) {
Expand Down Expand Up @@ -3915,6 +3949,10 @@ void AliasesWriter::WriteStreamOfAliasedGenericUnion2Impl(test_model::AliasedGen
ordered_json json_value = value;
yardl::ndjson::WriteProtocolValue(stream_, "streamOfAliasedGenericUnion2", json_value);}

void AliasesWriter::WriteVectorsImpl(std::vector<test_model::RecordContainingVectorsOfAliases> const& value) {
ordered_json json_value = value;
yardl::ndjson::WriteProtocolValue(stream_, "vectors", json_value);}

void AliasesWriter::Flush() {
stream_.flush();
}
Expand Down Expand Up @@ -3963,6 +4001,10 @@ bool AliasesReader::ReadStreamOfAliasedGenericUnion2Impl(test_model::AliasedGene
return yardl::ndjson::ReadProtocolValue(stream_, line_, "streamOfAliasedGenericUnion2", false, unused_step_, value);
}

void AliasesReader::ReadVectorsImpl(std::vector<test_model::RecordContainingVectorsOfAliases>& value) {
yardl::ndjson::ReadProtocolValue(stream_, line_, "vectors", true, unused_step_, value);
}

void AliasesReader::CloseImpl() {
VerifyFinished();
}
Expand Down
2 changes: 2 additions & 0 deletions cpp/test/generated/ndjson/protocols.h
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,7 @@ class AliasesWriter : public test_model::AliasesWriterBase, yardl::ndjson::NDJso
void WriteAliasedGenericFixedVectorImpl(test_model::AliasedGenericFixedVector<float> const& value) override;
void WriteStreamOfAliasedGenericUnion2Impl(test_model::AliasedGenericUnion2<test_model::AliasedString, test_model::AliasedEnum> const& value) override;
void EndStreamOfAliasedGenericUnion2Impl() override {}
void WriteVectorsImpl(std::vector<test_model::RecordContainingVectorsOfAliases> const& value) override;
void CloseImpl() override;
};

Expand All @@ -1233,6 +1234,7 @@ class AliasesReader : public test_model::AliasesReaderBase, yardl::ndjson::NDJso
void ReadAliasedGenericVectorImpl(test_model::AliasedGenericVector<float>& value) override;
void ReadAliasedGenericFixedVectorImpl(test_model::AliasedGenericFixedVector<float>& value) override;
bool ReadStreamOfAliasedGenericUnion2Impl(test_model::AliasedGenericUnion2<test_model::AliasedString, test_model::AliasedEnum>& value) override;
void ReadVectorsImpl(std::vector<test_model::RecordContainingVectorsOfAliases>& value) override;
void CloseImpl() override;
};

Expand Down
Loading

0 comments on commit 8699221

Please sign in to comment.