Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 713192418
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jan 8, 2025
1 parent 4d0ba00 commit a25e273
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/google/protobuf/compiler/cpp/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,45 @@ void MessageGenerator::GenerateFieldAccessorDefinitions(io::Printer* p) {
// Generate type-specific accessors.
field_generators_.get(field).GenerateInlineAccessorDefinitions(p);

if (!field->is_repeated()) {
switch (field->cpp_type()) {
case FieldDescriptor::CPPTYPE_INT32:
case FieldDescriptor::CPPTYPE_INT64:
case FieldDescriptor::CPPTYPE_UINT32:
case FieldDescriptor::CPPTYPE_UINT64:
case FieldDescriptor::CPPTYPE_DOUBLE:
case FieldDescriptor::CPPTYPE_FLOAT:
case FieldDescriptor::CPPTYPE_BOOL:
case FieldDescriptor::CPPTYPE_ENUM:
p->Emit( //
{{"field_type", field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM
? QualifiedClassName(field->enum_type())
: PrimitiveTypeName(field->cpp_type())},
{"has",
[&]() {
if (field->has_presence()) {
p->Emit(R"cc(&$message_type$::has_$name$)cc");
} else {
p->Emit(R"cc(nullptr)cc");
}
}}},
R"cc(
struct $message_type$_$name$_Descriptor
: ::google::protobuf::internal::NumericFieldDescriptor<
$message_type$, $field_type$, &$message_type$::$name$,
&$message_type$::set_$name$,
&$message_type$::clear_$name$, $has$> {
static constexpr absl::string_view name = "$name$";
static constexpr int number = $number$;
};
)cc");
break;
case FieldDescriptor::CPPTYPE_STRING:
case FieldDescriptor::CPPTYPE_MESSAGE:
break;
}
}

p->Emit("\n");
}

Expand Down
14 changes: 14 additions & 0 deletions src/google/protobuf/generated_message_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,20 @@ constexpr absl::optional<uintptr_t> EncodePlacementArenaOffsets(
return arena_bits;
}

template <typename message_type, typename field_type,
field_type (message_type::*get)() const,
void (message_type::*set)(field_type value),
void (message_type::*clear)(), bool (message_type::*has)() const>
struct NumericFieldDescriptor {
static field_type Get(const message_type& msg) { return (msg.*get)(); }
static void Set(message_type& msg, field_type value) { (msg.*set)(value); }
static void Clear(message_type& msg) { (msg.*clear)(); }
static constexpr bool has_presence = has != nullptr;
static std::enable_if_t<has != nullptr, bool> Has(const message_type& msg) {
return (msg.*has)();
}
};

} // namespace internal
} // namespace protobuf
} // namespace google
Expand Down

0 comments on commit a25e273

Please sign in to comment.