Add internally visible Arena enabled copy constructor

PiperOrigin-RevId: 547364398
pull/13282/head
Martijn Vels 1 year ago committed by Copybara-Service
parent 9772a5af19
commit befdd1672d
  1. 20
      src/google/protobuf/repeated_field_unittest.cc
  2. 24
      src/google/protobuf/repeated_ptr_field.h

@ -1795,12 +1795,30 @@ TEST(RepeatedPtrField, Erase) {
} }
TEST(RepeatedPtrField, CopyConstruct) { TEST(RepeatedPtrField, CopyConstruct) {
auto token = internal::InternalVisibilityForTesting{};
RepeatedPtrField<std::string> source; RepeatedPtrField<std::string> source;
source.Add()->assign("1"); source.Add()->assign("1");
source.Add()->assign("2"); source.Add()->assign("2");
RepeatedPtrField<std::string> destination(source); RepeatedPtrField<std::string> destination1(source);
ASSERT_EQ(2, destination1.size());
EXPECT_EQ("1", destination1.Get(0));
EXPECT_EQ("2", destination1.Get(1));
RepeatedPtrField<std::string> destination2(token, nullptr, source);
ASSERT_EQ(2, destination2.size());
EXPECT_EQ("1", destination2.Get(0));
EXPECT_EQ("2", destination2.Get(1));
}
TEST(RepeatedPtrField, CopyConstructWithArena) {
auto token = internal::InternalVisibilityForTesting{};
RepeatedPtrField<std::string> source;
source.Add()->assign("1");
source.Add()->assign("2");
Arena arena;
RepeatedPtrField<std::string> destination(token, &arena, source);
ASSERT_EQ(2, destination.size()); ASSERT_EQ(2, destination.size());
EXPECT_EQ("1", destination.Get(0)); EXPECT_EQ("1", destination.Get(0));
EXPECT_EQ("2", destination.Get(1)); EXPECT_EQ("2", destination.Get(1));

@ -60,6 +60,7 @@
#include "google/protobuf/port.h" #include "google/protobuf/port.h"
#include "absl/base/attributes.h" #include "absl/base/attributes.h"
#include "absl/log/absl_check.h" #include "absl/log/absl_check.h"
#include "google/protobuf/internal_visibility.h"
#include "google/protobuf/message_lite.h" #include "google/protobuf/message_lite.h"
#include "google/protobuf/port.h" #include "google/protobuf/port.h"
@ -924,9 +925,18 @@ class RepeatedPtrField final : private internal::RepeatedPtrFieldBase {
public: public:
constexpr RepeatedPtrField(); constexpr RepeatedPtrField();
explicit RepeatedPtrField(Arena* arena); RepeatedPtrField(const RepeatedPtrField& other)
: RepeatedPtrField(nullptr, other) {}
// Arena enabled constructors: for internal use only.
RepeatedPtrField(internal::InternalVisibility, Arena* arena)
: RepeatedPtrField(arena) {}
RepeatedPtrField(internal::InternalVisibility, Arena* arena,
const RepeatedPtrField& rhs)
: RepeatedPtrField(arena, rhs) {}
RepeatedPtrField(const RepeatedPtrField& other); // TODO(b/290091828): make constructor private
explicit RepeatedPtrField(Arena* arena);
template <typename Iter, template <typename Iter,
typename = typename std::enable_if<std::is_constructible< typename = typename std::enable_if<std::is_constructible<
@ -1214,6 +1224,8 @@ class RepeatedPtrField final : private internal::RepeatedPtrFieldBase {
} }
private: private:
RepeatedPtrField(Arena* arena, const RepeatedPtrField& rhs);
// Note: RepeatedPtrField SHOULD NOT be subclassed by users. // Note: RepeatedPtrField SHOULD NOT be subclassed by users.
class TypeHandler; class TypeHandler;
@ -1267,11 +1279,11 @@ inline RepeatedPtrField<Element>::RepeatedPtrField(Arena* arena)
} }
template <typename Element> template <typename Element>
inline RepeatedPtrField<Element>::RepeatedPtrField( inline RepeatedPtrField<Element>::RepeatedPtrField(Arena* arena,
const RepeatedPtrField& other) const RepeatedPtrField& rhs)
: RepeatedPtrFieldBase() { : RepeatedPtrFieldBase(arena) {
StaticValidityCheck(); StaticValidityCheck();
MergeFrom(other); MergeFrom(rhs);
} }
template <typename Element> template <typename Element>

Loading…
Cancel
Save