Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix C++ compiler error for empty protocol definition #139

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cpp/test/generated/binary/protocols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4159,5 +4159,17 @@ void ProtocolWithKeywordStepsReader::CloseImpl() {
stream_.VerifyFinished();
}

void EmptyProtocolWriter::Flush() {
stream_.Flush();
}

void EmptyProtocolWriter::CloseImpl() {
stream_.Flush();
}

void EmptyProtocolReader::CloseImpl() {
stream_.VerifyFinished();
}

} // namespace test_model::binary

34 changes: 34 additions & 0 deletions cpp/test/generated/binary/protocols.h
Original file line number Diff line number Diff line change
Expand Up @@ -1411,4 +1411,38 @@ class ProtocolWithKeywordStepsReader : public test_model::ProtocolWithKeywordSte
size_t current_block_remaining_ = 0;
};

// Binary writer for the EmptyProtocol protocol.
class EmptyProtocolWriter : public test_model::EmptyProtocolWriterBase, yardl::binary::BinaryWriter {
public:
EmptyProtocolWriter(std::ostream& stream, Version version = Version::Current)
: yardl::binary::BinaryWriter(stream, test_model::EmptyProtocolWriterBase::SchemaFromVersion(version)), version_(version) {}

EmptyProtocolWriter(std::string file_name, Version version = Version::Current)
: yardl::binary::BinaryWriter(file_name, test_model::EmptyProtocolWriterBase::SchemaFromVersion(version)), version_(version) {}

void Flush() override;

protected:
void CloseImpl() override;

Version version_;
};

// Binary reader for the EmptyProtocol protocol.
class EmptyProtocolReader : public test_model::EmptyProtocolReaderBase, yardl::binary::BinaryReader {
public:
EmptyProtocolReader(std::istream& stream)
: yardl::binary::BinaryReader(stream), version_(test_model::EmptyProtocolReaderBase::VersionFromSchema(schema_read_)) {}

EmptyProtocolReader(std::string file_name)
: yardl::binary::BinaryReader(file_name), version_(test_model::EmptyProtocolReaderBase::VersionFromSchema(schema_read_)) {}

Version GetVersion() { return version_; }

protected:
void CloseImpl() override;

Version version_;
};

} // namespace test_model::binary
28 changes: 28 additions & 0 deletions cpp/test/generated/factories.cc
Original file line number Diff line number Diff line change
Expand Up @@ -903,4 +903,32 @@ std::unique_ptr<test_model::ProtocolWithKeywordStepsReaderBase> CreateReader<tes
}
}

template<>
std::unique_ptr<test_model::EmptyProtocolWriterBase> CreateWriter<test_model::EmptyProtocolWriterBase>(Format format, std::string const& filename) {
switch (format) {
case Format::kHdf5:
return std::make_unique<test_model::hdf5::EmptyProtocolWriter>(filename);
case Format::kBinary:
return std::make_unique<test_model::binary::EmptyProtocolWriter>(filename);
case Format::kNDJson:
return std::make_unique<test_model::ndjson::EmptyProtocolWriter>(filename);
default:
throw std::runtime_error("Unknown format");
}
}

template<>
std::unique_ptr<test_model::EmptyProtocolReaderBase> CreateReader<test_model::EmptyProtocolReaderBase>(Format format, std::string const& filename) {
switch (format) {
case Format::kHdf5:
return std::make_unique<test_model::hdf5::EmptyProtocolReader>(filename);
case Format::kBinary:
return std::make_unique<test_model::binary::EmptyProtocolReader>(filename);
case Format::kNDJson:
return std::make_unique<test_model::ndjson::EmptyProtocolReader>(filename);
default:
throw std::runtime_error("Unknown format");
}
}

}
8 changes: 8 additions & 0 deletions cpp/test/generated/hdf5/protocols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3543,5 +3543,13 @@ void ProtocolWithKeywordStepsReader::ReadFloatImpl(test_model::EnumWithKeywordSy
yardl::hdf5::ReadScalarDataset<test_model::EnumWithKeywordSymbols, test_model::EnumWithKeywordSymbols>(group_, "float", test_model::hdf5::GetEnumWithKeywordSymbolsHdf5Ddl(), value);
}

EmptyProtocolWriter::EmptyProtocolWriter(std::string path)
: yardl::hdf5::Hdf5Writer::Hdf5Writer(path, "EmptyProtocol", schema_) {
}

EmptyProtocolReader::EmptyProtocolReader(std::string path)
: yardl::hdf5::Hdf5Reader::Hdf5Reader(path, "EmptyProtocol", schema_) {
}

} // namespace test_model::hdf5

17 changes: 17 additions & 0 deletions cpp/test/generated/hdf5/protocols.h
Original file line number Diff line number Diff line change
Expand Up @@ -1119,5 +1119,22 @@ class ProtocolWithKeywordStepsReader : public test_model::ProtocolWithKeywordSte
std::unique_ptr<yardl::hdf5::DatasetReader> int_dataset_state_;
};

// HDF5 writer for the EmptyProtocol protocol.
class EmptyProtocolWriter : public test_model::EmptyProtocolWriterBase, public yardl::hdf5::Hdf5Writer {
public:
EmptyProtocolWriter(std::string path);

protected:
private:
};

// HDF5 reader for the EmptyProtocol protocol.
class EmptyProtocolReader : public test_model::EmptyProtocolReaderBase, public yardl::hdf5::Hdf5Reader {
public:
EmptyProtocolReader(std::string path);

private:
};

} // namespace test_model

41 changes: 41 additions & 0 deletions cpp/test/generated/mocks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4104,6 +4104,39 @@ class TestProtocolWithKeywordStepsWriterBase : public ProtocolWithKeywordStepsWr
MockProtocolWithKeywordStepsWriter mock_writer_;
bool close_called_ = false;
};

class MockEmptyProtocolWriter : public EmptyProtocolWriterBase {
public:
void Verify() {
}
};

class TestEmptyProtocolWriterBase : public EmptyProtocolWriterBase {
public:
TestEmptyProtocolWriterBase(std::unique_ptr<test_model::EmptyProtocolWriterBase> writer, std::function<std::unique_ptr<EmptyProtocolReaderBase>()> create_reader) : writer_(std::move(writer)), create_reader_(create_reader) {
}

~TestEmptyProtocolWriterBase() {
if (!close_called_ && !std::uncaught_exceptions()) {
ADD_FAILURE() << "Close() needs to be called on 'TestEmptyProtocolWriterBase' to verify mocks";
}
}

protected:
void CloseImpl() override {
close_called_ = true;
writer_->Close();
std::unique_ptr<EmptyProtocolReaderBase> reader = create_reader_();
reader->CopyTo(mock_writer_);
mock_writer_.Verify();
}

private:
std::unique_ptr<test_model::EmptyProtocolWriterBase> writer_;
std::function<std::unique_ptr<test_model::EmptyProtocolReaderBase>()> create_reader_;
MockEmptyProtocolWriter mock_writer_;
bool close_called_ = false;
};
} // namespace
} // namespace test_model

Expand Down Expand Up @@ -4364,4 +4397,12 @@ std::unique_ptr<test_model::ProtocolWithKeywordStepsWriterBase> CreateValidating
);
}

template<>
std::unique_ptr<test_model::EmptyProtocolWriterBase> CreateValidatingWriter<test_model::EmptyProtocolWriterBase>(Format format, std::string const& filename) {
return std::make_unique<test_model::TestEmptyProtocolWriterBase>(
CreateWriter<test_model::EmptyProtocolWriterBase>(format, filename),
[format, filename](){ return CreateReader<test_model::EmptyProtocolReaderBase>(format, filename);}
);
}

}
4 changes: 4 additions & 0 deletions cpp/test/generated/model.json
Original file line number Diff line number Diff line change
Expand Up @@ -5173,6 +5173,10 @@
"type": "TestModel.EnumWithKeywordSymbols"
}
]
},
{
"name": "EmptyProtocol",
"sequence": null
}
]
}
Expand Down
12 changes: 12 additions & 0 deletions cpp/test/generated/ndjson/protocols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3777,5 +3777,17 @@ void ProtocolWithKeywordStepsReader::CloseImpl() {
VerifyFinished();
}

void EmptyProtocolWriter::Flush() {
stream_.flush();
}

void EmptyProtocolWriter::CloseImpl() {
stream_.flush();
}

void EmptyProtocolReader::CloseImpl() {
VerifyFinished();
}

} // namespace test_model::ndjson

32 changes: 32 additions & 0 deletions cpp/test/generated/ndjson/protocols.h
Original file line number Diff line number Diff line change
Expand Up @@ -1265,4 +1265,36 @@ class ProtocolWithKeywordStepsReader : public test_model::ProtocolWithKeywordSte
void CloseImpl() override;
};

// NDJSON writer for the EmptyProtocol protocol.
class EmptyProtocolWriter : public test_model::EmptyProtocolWriterBase, yardl::ndjson::NDJsonWriter {
public:
EmptyProtocolWriter(std::ostream& stream)
: yardl::ndjson::NDJsonWriter(stream, schema_) {
}

EmptyProtocolWriter(std::string file_name)
: yardl::ndjson::NDJsonWriter(file_name, schema_) {
}

void Flush() override;

protected:
void CloseImpl() override;
};

// NDJSON reader for the EmptyProtocol protocol.
class EmptyProtocolReader : public test_model::EmptyProtocolReaderBase, yardl::ndjson::NDJsonReader {
public:
EmptyProtocolReader(std::istream& stream)
: yardl::ndjson::NDJsonReader(stream, schema_) {
}

EmptyProtocolReader(std::string file_name)
: yardl::ndjson::NDJsonReader(file_name, schema_) {
}

protected:
void CloseImpl() override;
};

} // namespace test_model::ndjson
65 changes: 65 additions & 0 deletions cpp/test/generated/protocols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6404,4 +6404,69 @@ void ProtocolWithKeywordStepsReaderBase::CopyTo(ProtocolWithKeywordStepsWriterBa
writer.WriteFloat(value);
}
}

namespace {
void EmptyProtocolWriterBaseInvalidState(uint8_t attempted, [[maybe_unused]] bool end, uint8_t current) {
std::string expected_method;
switch (current) {
}
std::string attempted_method;
switch (attempted) {
case 0: attempted_method = "Close()"; break;
}
throw std::runtime_error("Expected call to " + expected_method + " but received call to " + attempted_method + " instead.");
}

void EmptyProtocolReaderBaseInvalidState(uint8_t attempted, uint8_t current) {
auto f = [](uint8_t i) -> std::string {
switch (i/2) {
case 0: return "Close()";
default: return "<unknown>";
}
};
throw std::runtime_error("Expected call to " + f(current) + " but received call to " + f(attempted) + " instead.");
}

} // namespace

std::string EmptyProtocolWriterBase::schema_ = R"({"protocol":{"name":"EmptyProtocol","sequence":null},"types":null})";

std::vector<std::string> EmptyProtocolWriterBase::previous_schemas_ = {
};

std::string EmptyProtocolWriterBase::SchemaFromVersion(Version version) {
switch (version) {
case Version::Current: return EmptyProtocolWriterBase::schema_; break;
default: throw std::runtime_error("The version does not correspond to any schema supported by protocol EmptyProtocol.");
}

}
void EmptyProtocolWriterBase::Close() {
if (unlikely(state_ != 0)) {
EmptyProtocolWriterBaseInvalidState(0, false, state_);
}

CloseImpl();
}

std::string EmptyProtocolReaderBase::schema_ = EmptyProtocolWriterBase::schema_;

std::vector<std::string> EmptyProtocolReaderBase::previous_schemas_ = EmptyProtocolWriterBase::previous_schemas_;

Version EmptyProtocolReaderBase::VersionFromSchema(std::string const& schema) {
if (schema == EmptyProtocolWriterBase::schema_) {
return Version::Current;
}
throw std::runtime_error("The schema does not match any version supported by protocol EmptyProtocol.");
}
void EmptyProtocolReaderBase::Close() {
if (unlikely(state_ != 0)) {
EmptyProtocolReaderBaseInvalidState(0, state_);
}

CloseImpl();
}
void EmptyProtocolReaderBase::CopyTo(EmptyProtocolWriterBase& writer) {
(void)(writer);
}
} // namespace test_model
48 changes: 48 additions & 0 deletions cpp/test/generated/protocols.h
Original file line number Diff line number Diff line change
Expand Up @@ -2654,4 +2654,52 @@ class ProtocolWithKeywordStepsReaderBase {
private:
uint8_t state_ = 0;
};

// Abstract writer for the EmptyProtocol protocol.
class EmptyProtocolWriterBase {
public:
// Optionaly close this writer before destructing. Validates that all steps were completed.
void Close();

virtual ~EmptyProtocolWriterBase() = default;

// Flushes all buffered data.
virtual void Flush() {}

protected:
virtual void CloseImpl() {}

static std::string schema_;

static std::vector<std::string> previous_schemas_;

static std::string SchemaFromVersion(Version version);

private:
uint8_t state_ = 0;

friend class EmptyProtocolReaderBase;
};

// Abstract reader for the EmptyProtocol protocol.
class EmptyProtocolReaderBase {
public:
// Optionaly close this writer before destructing. Validates that all steps were completely read.
void Close();

void CopyTo(EmptyProtocolWriterBase& writer);

virtual ~EmptyProtocolReaderBase() = default;

protected:
virtual void CloseImpl() {}
static std::string schema_;

static std::vector<std::string> previous_schemas_;

static Version VersionFromSchema(const std::string& schema);

private:
uint8_t state_ = 0;
};
} // namespace test_model
11 changes: 11 additions & 0 deletions cpp/test/generated/translator_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,17 @@ void TranslateStream(std::string const& protocol_name, yardl::testing::Format in
reader->CopyTo(*writer);
return;
}
if (protocol_name == "EmptyProtocol") {
auto reader = input_format == yardl::testing::Format::kBinary
? std::unique_ptr<test_model::EmptyProtocolReaderBase>(new test_model::binary::EmptyProtocolReader(input))
: std::unique_ptr<test_model::EmptyProtocolReaderBase>(new test_model::ndjson::EmptyProtocolReader(input));

auto writer = output_format == yardl::testing::Format::kBinary
? std::unique_ptr<test_model::EmptyProtocolWriterBase>(new test_model::binary::EmptyProtocolWriter(output))
: std::unique_ptr<test_model::EmptyProtocolWriterBase>(new test_model::ndjson::EmptyProtocolWriter(output));
reader->CopyTo(*writer);
return;
}
throw std::runtime_error("Unsupported protocol " + protocol_name);
}
} // namespace yardl::testing
3 changes: 3 additions & 0 deletions models/test/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -708,3 +708,6 @@ ProtocolWithKeywordSteps: !protocol
items: RecordWithKeywordFields
float: EnumWithKeywordSymbols
# END delibrately using C++ keywords and macros as identitiers

EmptyProtocol: !protocol
sequence:
Loading
Loading