Browse Source

Add move constructor for Reflection's SetString

reed-lau 6 years ago
parent
commit
6e0a6d1cbd
2 changed files with 34 additions and 0 deletions
  1. 32 0
      src/google/protobuf/generated_message_reflection.cc
  2. 2 0
      src/google/protobuf/message.h

+ 32 - 0
src/google/protobuf/generated_message_reflection.cc

@@ -1223,6 +1223,38 @@ void Reflection::SetString(Message* message, const FieldDescriptor* field,
 }
 
 
+void Reflection::SetString(Message* message, const FieldDescriptor* field,
+                           const std::string&& value) const {
+  USAGE_CHECK_ALL(SetString, SINGULAR, STRING);
+  if (field->is_extension()) {
+    return MutableExtensionSet(message)->SetString(field->number(),
+                                                   field->type(), value, field);
+  } else {
+    switch (field->options().ctype()) {
+      default:  // TODO(kenton):  Support other string reps.
+      case FieldOptions::STRING: {
+        if (IsInlined(field)) {
+          MutableField<InlinedStringField>(message, field)
+              ->SetNoArena(nullptr, value);
+          break;
+        }
+
+        const std::string* default_ptr =
+            &DefaultRaw<ArenaStringPtr>(field).Get();
+        if (field->containing_oneof() && !HasOneofField(*message, field)) {
+          ClearOneof(message, field->containing_oneof());
+          MutableField<ArenaStringPtr>(message, field)
+              ->UnsafeSetDefault(default_ptr);
+        }
+        *(MutableField<ArenaStringPtr>(message, field)
+            ->Mutable(default_ptr, GetArena(message))) = value;
+        break;
+      }
+    }
+  }
+}
+
+
 std::string Reflection::GetRepeatedString(const Message& message,
                                           const FieldDescriptor* field,
                                           int index) const {

+ 2 - 0
src/google/protobuf/message.h

@@ -540,6 +540,8 @@ class PROTOBUF_EXPORT Reflection final {
                bool value) const;
   void SetString(Message* message, const FieldDescriptor* field,
                  const std::string& value) const;
+  void SetString(Message* message, const FieldDescriptor* field,
+                 const std::string&& value) const;
   void SetEnum(Message* message, const FieldDescriptor* field,
                const EnumValueDescriptor* value) const;
   // Set an enum field's value with an integer rather than EnumValueDescriptor.