Skip to content
Draft
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
3 changes: 3 additions & 0 deletions hpb/backend/cpp/cpp_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ namespace hpb::testing {
namespace {

using ::hpb_unittest::protos::TestModel;
using ::hpb_unittest::protos::TestModel_Category_IMAGES;

TEST(CppBackend, CanCreateMessage) {
hpb::Arena arena;
hpb::Ptr<TestModel> test_model_ptr = hpb::CreateMessage<TestModel>(arena);
(void)test_model_ptr;
}

TEST(CppBackend, MessageEnums) { EXPECT_EQ(5, TestModel_Category_IMAGES); }
} // namespace
} // namespace hpb::testing
55 changes: 53 additions & 2 deletions hpb_generator/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,50 @@ void WriteForwardDecls(const google::protobuf::FileDescriptor* file, Context& ct
void WriteHeader(const google::protobuf::FileDescriptor* file, Context& ctx) {
if (ctx.options().backend == Backend::CPP) {
EmitFileWarning(file, ctx);
ctx.Emit({{"filename", ToPreproc(file->name())}},
R"cc(
#ifndef $filename$_HPB_PROTO_H_
#define $filename$_HPB_PROTO_H_
)cc");

// Import headers for proto public dependencies.
for (int i = 0; i < file->public_dependency_count(); i++) {
if (i == 0) {
ctx.Emit("// Public Imports.\n");
}
ctx.Emit({{"header", CppHeaderFilename(file->public_dependency(i))}},
"#include \"$header$\"\n");
if (i == file->public_dependency_count() - 1) {
ctx.Emit("\n");
}
}

ctx.Emit(
"#include \"hpb/internal/os_macros_undef.inc\"\n");

const std::vector<const google::protobuf::Descriptor*> this_file_messages =
SortedMessages(file);
const std::vector<const google::protobuf::FieldDescriptor*>
this_file_exts{}; // TODO: extensions

if (!this_file_messages.empty()) {
ctx.Emit("\n");
}

WriteHeaderMessageForwardDecls(file, ctx);
WriteForwardDecls(file, ctx);

std::vector<const google::protobuf::EnumDescriptor*> this_file_enums =
SortedEnums(file);

WrapNamespace(file, ctx, [&]() {
// Write Class and Enums.
WriteEnumDeclarations(this_file_enums, ctx);
ctx.Emit("\n");
// TODO: class decls
// TODO: extension identifiers
});

const auto msgs = SortedMessages(file);
for (auto message : msgs) {
ctx.Emit({{"type", QualifiedClassName(message)},
Expand Down Expand Up @@ -90,6 +134,11 @@ void WriteHeader(const google::protobuf::FileDescriptor* file, Context& ctx) {
} // namespace $namespace$
)cc");
}
ctx.Emit(
"#include "
"\"hpb/internal/os_macros_restore.inc\"\n");
ctx.Emit({{"filename", ToPreproc(file->name())}},
"#endif /* $filename$_HPB_PROTO_H_ */\n");
return;
}
EmitFileWarning(file, ctx);
Expand Down Expand Up @@ -254,8 +303,10 @@ void WriteTypedefForwardingHeader(
void WriteHeaderMessageForwardDecls(const google::protobuf::FileDescriptor* file,
Context& ctx) {
// Import forward-declaration of types defined in this file.
ctx.Emit({{"upb_filename", UpbCFilename(file)}},
"#include \"$upb_filename$\"\n");
if (ctx.options().backend == Backend::UPB) {
ctx.Emit({{"upb_filename", UpbCFilename(file)}},
"#include \"$upb_filename$\"\n");
}
WriteForwardDecls(file, ctx);
// Import forward-declaration of types in dependencies.
for (int i = 0; i < file->dependency_count(); ++i) {
Expand Down
Loading