Automated rollback of commit 57702831f9.

PiperOrigin-RevId: 544176749
pull/13159/head
Adam Cozzette 2 years ago committed by Copybara-Service
parent 57702831f9
commit c108278083
  1. 47
      src/google/protobuf/compiler/cpp/extension.cc
  2. 45
      src/google/protobuf/extension_set.h

@ -149,8 +149,27 @@ void ExtensionGenerator::GenerateDeclaration(io::Printer* printer) const {
}
void ExtensionGenerator::GenerateDefinition(io::Printer* printer) {
auto with_vars = printer->WithVars(&variables_);
Formatter format(printer);
Formatter format(printer, variables_);
std::string default_str;
// If this is a class member, it needs to be declared in its class scope.
if (descriptor_->cpp_type() == FieldDescriptor::CPPTYPE_STRING) {
// We need to declare a global string which will contain the default value.
// We cannot declare it at class scope because that would require exposing
// it in the header which would be annoying for other reasons. So we
// replace :: with _ in the name and declare it as a global.
default_str =
absl::StrReplaceAll(variables_["scoped_name"], {{"::", "_"}}) +
"_default";
format("const std::string $1$($2$);\n", default_str,
DefaultValue(options_, descriptor_));
} else if (descriptor_->message_type()) {
// We have to initialize the default instance for extensions at registration
// time.
default_str = absl::StrCat(FieldMessageTypeName(descriptor_, options_),
"::default_instance()");
} else {
default_str = DefaultValue(options_, descriptor_);
}
// Likewise, class members need to declare the field constant variable.
if (IsScoped()) {
@ -166,31 +185,7 @@ void ExtensionGenerator::GenerateDefinition(io::Printer* printer) {
"::$proto_ns$::internal::ExtensionIdentifier< $extendee$,\n"
" ::$proto_ns$::internal::$type_traits$, $field_type$, $packed$>\n"
" $scoped_name$($constant_name$);\n");
} else if (descriptor_->cpp_type() == descriptor_->CPPTYPE_MESSAGE) {
printer->Emit(
R"cc(
PROTOBUF_ATTRIBUTE_INIT_PRIORITY2
::$proto_ns$::internal::ExtensionIdentifier<
$extendee$, ::$proto_ns$::internal::$type_traits$, $field_type$,
$packed$>
$scoped_name$($constant_name$, $verify_fn$);
)cc");
} else {
std::string default_str;
// If this is a class member, it needs to be declared in its class scope.
if (descriptor_->cpp_type() == FieldDescriptor::CPPTYPE_STRING) {
// We need to declare a global string which will contain the default
// value. We cannot declare it at class scope because that would require
// exposing it in the header which would be annoying for other reasons. So
// we replace :: with _ in the name and declare it as a global.
default_str =
absl::StrReplaceAll(variables_["scoped_name"], {{"::", "_"}}) +
"_default";
format("const std::string $1$($2$);\n", default_str,
DefaultValue(options_, descriptor_));
} else {
default_str = DefaultValue(options_, descriptor_);
}
format(
"PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 "
"::$proto_ns$::internal::ExtensionIdentifier< $extendee$,\n"

@ -1358,7 +1358,6 @@ class RepeatedEnumTypeTraits {
template <typename Type>
class MessageTypeTraits {
public:
using T = Type;
typedef const Type& ConstType;
typedef Type* MutableType;
typedef MessageTypeTraits<Type> Singular;
@ -1417,7 +1416,6 @@ class RepeatedMessageGenericTypeTraits;
template <typename Type>
class RepeatedMessageTypeTraits {
public:
using T = Type;
typedef const Type& ConstType;
typedef Type* MutableType;
typedef RepeatedMessageTypeTraits<Type> Repeated;
@ -1531,49 +1529,6 @@ class ExtensionIdentifier {
typename TypeTraits::ConstType default_value_;
};
template <typename ExtendeeType, typename Traits, FieldType field_type>
class MessageExtensionIdentifierImpl {
public:
using Type = typename Traits::T;
using TypeTraits = Traits;
using Extendee = ExtendeeType;
MessageExtensionIdentifierImpl(int number, LazyEagerVerifyFnType verify_func)
: number_(number) {
Register(number, verify_func);
}
inline int number() const { return number_; }
static void Register(int number, LazyEagerVerifyFnType verify_func) {
TypeTraits::template Register<ExtendeeType>(number, field_type, false,
verify_func);
}
static const Type& default_value() { return Type::default_instance(); }
static const Type& default_value_ref() { return Type::default_instance(); }
private:
const int number_;
};
template <typename ExtendeeType, typename Type, FieldType field_type>
class ExtensionIdentifier<ExtendeeType, MessageTypeTraits<Type>, field_type,
false>
: public MessageExtensionIdentifierImpl<
ExtendeeType, MessageTypeTraits<Type>, field_type> {
using ExtensionIdentifier::MessageExtensionIdentifierImpl::
MessageExtensionIdentifierImpl;
};
template <typename ExtendeeType, typename Type, FieldType field_type>
class ExtensionIdentifier<ExtendeeType, RepeatedMessageTypeTraits<Type>,
field_type, false>
: public MessageExtensionIdentifierImpl<
ExtendeeType, RepeatedMessageTypeTraits<Type>, field_type> {
using ExtensionIdentifier::MessageExtensionIdentifierImpl::
MessageExtensionIdentifierImpl;
};
// -------------------------------------------------------------------
// Generated accessors

Loading…
Cancel
Save