| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // http://code.google.com/p/protobuf/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| 11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
| 12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
| 13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 // FindFieldByName("numbers"); | 91 // FindFieldByName("numbers"); |
| 92 // assert(numbers_field != NULL); | 92 // assert(numbers_field != NULL); |
| 93 // assert(numbers_field->type() == FieldDescriptor::TYPE_INT32); | 93 // assert(numbers_field->type() == FieldDescriptor::TYPE_INT32); |
| 94 // assert(numbers_field->label() == FieldDescriptor::LABEL_REPEATED); | 94 // assert(numbers_field->label() == FieldDescriptor::LABEL_REPEATED); |
| 95 // | 95 // |
| 96 // // Parse the message. | 96 // // Parse the message. |
| 97 // foo->ParseFromString(data); | 97 // foo->ParseFromString(data); |
| 98 // | 98 // |
| 99 // // Use the reflection interface to examine the contents. | 99 // // Use the reflection interface to examine the contents. |
| 100 // const Reflection* reflection = foo->GetReflection(); | 100 // const Reflection* reflection = foo->GetReflection(); |
| 101 // assert(reflection->GetString(foo, text_field) == "Hello World!"); | 101 // assert(reflection->GetString(*foo, text_field) == "Hello World!"); |
| 102 // assert(reflection->FieldSize(foo, numbers_field) == 3); | 102 // assert(reflection->FieldSize(*foo, numbers_field) == 3); |
| 103 // assert(reflection->GetRepeatedInt32(foo, numbers_field, 0) == 1); | 103 // assert(reflection->GetRepeatedInt32(*foo, numbers_field, 0) == 1); |
| 104 // assert(reflection->GetRepeatedInt32(foo, numbers_field, 1) == 5); | 104 // assert(reflection->GetRepeatedInt32(*foo, numbers_field, 1) == 5); |
| 105 // assert(reflection->GetRepeatedInt32(foo, numbers_field, 2) == 42); | 105 // assert(reflection->GetRepeatedInt32(*foo, numbers_field, 2) == 42); |
| 106 // | 106 // |
| 107 // delete foo; | 107 // delete foo; |
| 108 // } | 108 // } |
| 109 | 109 |
| 110 #ifndef GOOGLE_PROTOBUF_MESSAGE_H__ | 110 #ifndef GOOGLE_PROTOBUF_MESSAGE_H__ |
| 111 #define GOOGLE_PROTOBUF_MESSAGE_H__ | 111 #define GOOGLE_PROTOBUF_MESSAGE_H__ |
| 112 | 112 |
| 113 #include <iosfwd> |
| 114 #include <string> |
| 115 #include <google/protobuf/stubs/type_traits.h> |
| 113 #include <vector> | 116 #include <vector> |
| 114 #include <string> | |
| 115 | 117 |
| 116 #ifdef __DECCXX | 118 #include <google/protobuf/arena.h> |
| 117 // HP C++'s iosfwd doesn't work. | |
| 118 #include <iostream> | |
| 119 #else | |
| 120 #include <iosfwd> | |
| 121 #endif | |
| 122 | |
| 123 #include <google/protobuf/message_lite.h> | 119 #include <google/protobuf/message_lite.h> |
| 124 | 120 |
| 125 #include <google/protobuf/stubs/common.h> | 121 #include <google/protobuf/stubs/common.h> |
| 126 #include <google/protobuf/descriptor.h> | 122 #include <google/protobuf/descriptor.h> |
| 127 | 123 |
| 128 | 124 |
| 125 #define GOOGLE_PROTOBUF_HAS_ONEOF |
| 126 #define GOOGLE_PROTOBUF_HAS_ARENAS |
| 127 |
| 129 namespace google { | 128 namespace google { |
| 130 namespace protobuf { | 129 namespace protobuf { |
| 131 | 130 |
| 132 // Defined in this file. | 131 // Defined in this file. |
| 133 class Message; | 132 class Message; |
| 134 class Reflection; | 133 class Reflection; |
| 135 class MessageFactory; | 134 class MessageFactory; |
| 136 | 135 |
| 137 // Defined in other files. | 136 // Defined in other files. |
| 137 class MapKey; |
| 138 class MapValueRef; |
| 139 class MapIterator; |
| 140 class MapReflectionTester; |
| 141 |
| 142 namespace internal { |
| 143 class MapFieldBase; |
| 144 } |
| 138 class UnknownFieldSet; // unknown_field_set.h | 145 class UnknownFieldSet; // unknown_field_set.h |
| 139 namespace io { | 146 namespace io { |
| 140 class ZeroCopyInputStream; // zero_copy_stream.h | 147 class ZeroCopyInputStream; // zero_copy_stream.h |
| 141 class ZeroCopyOutputStream; // zero_copy_stream.h | 148 class ZeroCopyOutputStream; // zero_copy_stream.h |
| 142 class CodedInputStream; // coded_stream.h | 149 class CodedInputStream; // coded_stream.h |
| 143 class CodedOutputStream; // coded_stream.h | 150 class CodedOutputStream; // coded_stream.h |
| 151 } |
| 152 namespace python { |
| 153 class MapReflectionFriend; // scalar_map_container.h |
| 144 } | 154 } |
| 145 | 155 |
| 146 | 156 |
| 147 template<typename T> | 157 template<typename T> |
| 148 class RepeatedField; // repeated_field.h | 158 class RepeatedField; // repeated_field.h |
| 149 | 159 |
| 150 template<typename T> | 160 template<typename T> |
| 151 class RepeatedPtrField; // repeated_field.h | 161 class RepeatedPtrField; // repeated_field.h |
| 152 | 162 |
| 153 // A container to hold message metadata. | 163 // A container to hold message metadata. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 171 inline Message() {} | 181 inline Message() {} |
| 172 virtual ~Message(); | 182 virtual ~Message(); |
| 173 | 183 |
| 174 // Basic Operations ------------------------------------------------ | 184 // Basic Operations ------------------------------------------------ |
| 175 | 185 |
| 176 // Construct a new instance of the same type. Ownership is passed to the | 186 // Construct a new instance of the same type. Ownership is passed to the |
| 177 // caller. (This is also defined in MessageLite, but is defined again here | 187 // caller. (This is also defined in MessageLite, but is defined again here |
| 178 // for return-type covariance.) | 188 // for return-type covariance.) |
| 179 virtual Message* New() const = 0; | 189 virtual Message* New() const = 0; |
| 180 | 190 |
| 191 // Construct a new instance on the arena. Ownership is passed to the caller |
| 192 // if arena is a NULL. Default implementation allows for API compatibility |
| 193 // during the Arena transition. |
| 194 virtual Message* New(::google::protobuf::Arena* arena) const { |
| 195 Message* message = New(); |
| 196 if (arena != NULL) { |
| 197 arena->Own(message); |
| 198 } |
| 199 return message; |
| 200 } |
| 201 |
| 181 // Make this message into a copy of the given message. The given message | 202 // Make this message into a copy of the given message. The given message |
| 182 // must have the same descriptor, but need not necessarily be the same class. | 203 // must have the same descriptor, but need not necessarily be the same class. |
| 183 // By default this is just implemented as "Clear(); MergeFrom(from);". | 204 // By default this is just implemented as "Clear(); MergeFrom(from);". |
| 184 virtual void CopyFrom(const Message& from); | 205 virtual void CopyFrom(const Message& from); |
| 185 | 206 |
| 186 // Merge the fields from the given message into this message. Singular | 207 // Merge the fields from the given message into this message. Singular |
| 187 // fields will be overwritten, except for embedded messages which will | 208 // fields will be overwritten, if specified in from, except for embedded |
| 188 // be merged. Repeated fields will be concatenated. The given message | 209 // messages which will be merged. Repeated fields will be concatenated. |
| 189 // must be of the same type as this message (i.e. the exact same class). | 210 // The given message must be of the same type as this message (i.e. the |
| 211 // exact same class). |
| 190 virtual void MergeFrom(const Message& from); | 212 virtual void MergeFrom(const Message& from); |
| 191 | 213 |
| 192 // Verifies that IsInitialized() returns true. GOOGLE_CHECK-fails otherwise,
with | 214 // Verifies that IsInitialized() returns true. GOOGLE_CHECK-fails otherwise,
with |
| 193 // a nice error message. | 215 // a nice error message. |
| 194 void CheckInitialized() const; | 216 void CheckInitialized() const; |
| 195 | 217 |
| 196 // Slowly build a list of all required fields that are not set. | 218 // Slowly build a list of all required fields that are not set. |
| 197 // This is much, much slower than IsInitialized() as it is implemented | 219 // This is much, much slower than IsInitialized() as it is implemented |
| 198 // purely via reflection. Generally, you should not call this unless you | 220 // purely via reflection. Generally, you should not call this unless you |
| 199 // have already determined that an error exists by calling IsInitialized(). | 221 // have already determined that an error exists by calling IsInitialized(). |
| 200 void FindInitializationErrors(vector<string>* errors) const; | 222 void FindInitializationErrors(std::vector<string>* errors) const; |
| 201 | 223 |
| 202 // Like FindInitializationErrors, but joins all the strings, delimited by | 224 // Like FindInitializationErrors, but joins all the strings, delimited by |
| 203 // commas, and returns them. | 225 // commas, and returns them. |
| 204 string InitializationErrorString() const; | 226 string InitializationErrorString() const; |
| 205 | 227 |
| 206 // Clears all unknown fields from this message and all embedded messages. | 228 // Clears all unknown fields from this message and all embedded messages. |
| 207 // Normally, if unknown tag numbers are encountered when parsing a message, | 229 // Normally, if unknown tag numbers are encountered when parsing a message, |
| 208 // the tag and value are stored in the message's UnknownFieldSet and | 230 // the tag and value are stored in the message's UnknownFieldSet and |
| 209 // then written back out when the message is serialized. This allows servers | 231 // then written back out when the message is serialized. This allows servers |
| 210 // which simply route messages to other servers to pass through messages | 232 // which simply route messages to other servers to pass through messages |
| 211 // that have new field definitions which they don't yet know about. However, | 233 // that have new field definitions which they don't yet know about. However, |
| 212 // this behavior can have security implications. To avoid it, call this | 234 // this behavior can have security implications. To avoid it, call this |
| 213 // method after parsing. | 235 // method after parsing. |
| 214 // | 236 // |
| 215 // See Reflection::GetUnknownFields() for more on unknown fields. | 237 // See Reflection::GetUnknownFields() for more on unknown fields. |
| 216 virtual void DiscardUnknownFields(); | 238 virtual void DiscardUnknownFields(); |
| 217 | 239 |
| 218 // Computes (an estimate of) the total number of bytes currently used for | 240 // Computes (an estimate of) the total number of bytes currently used for |
| 219 // storing the message in memory. The default implementation calls the | 241 // storing the message in memory. The default implementation calls the |
| 220 // Reflection object's SpaceUsed() method. | 242 // Reflection object's SpaceUsed() method. |
| 243 // |
| 244 // SpaceUsed() is noticeably slower than ByteSize(), as it is implemented |
| 245 // using reflection (rather than the generated code implementation for |
| 246 // ByteSize()). Like ByteSize(), its CPU time is linear in the number of |
| 247 // fields defined for the proto. |
| 221 virtual int SpaceUsed() const; | 248 virtual int SpaceUsed() const; |
| 222 | 249 |
| 223 // Debugging & Testing---------------------------------------------- | 250 // Debugging & Testing---------------------------------------------- |
| 224 | 251 |
| 225 // Generates a human readable form of this message, useful for debugging | 252 // Generates a human readable form of this message, useful for debugging |
| 226 // and other purposes. | 253 // and other purposes. |
| 227 string DebugString() const; | 254 string DebugString() const; |
| 228 // Like DebugString(), but with less whitespace. | 255 // Like DebugString(), but with less whitespace. |
| 229 string ShortDebugString() const; | 256 string ShortDebugString() const; |
| 230 // Like DebugString(), but do not escape UTF-8 byte sequences. | 257 // Like DebugString(), but do not escape UTF-8 byte sequences. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 // Get a struct containing the metadata for the Message. Most subclasses only | 336 // Get a struct containing the metadata for the Message. Most subclasses only |
| 310 // need to implement this method, rather than the GetDescriptor() and | 337 // need to implement this method, rather than the GetDescriptor() and |
| 311 // GetReflection() wrappers. | 338 // GetReflection() wrappers. |
| 312 virtual Metadata GetMetadata() const = 0; | 339 virtual Metadata GetMetadata() const = 0; |
| 313 | 340 |
| 314 | 341 |
| 315 private: | 342 private: |
| 316 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Message); | 343 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Message); |
| 317 }; | 344 }; |
| 318 | 345 |
| 346 namespace internal { |
| 347 // Forward-declare interfaces used to implement RepeatedFieldRef. |
| 348 // These are protobuf internals that users shouldn't care about. |
| 349 class RepeatedFieldAccessor; |
| 350 } // namespace internal |
| 351 |
| 352 // Forward-declare RepeatedFieldRef templates. The second type parameter is |
| 353 // used for SFINAE tricks. Users should ignore it. |
| 354 template<typename T, typename Enable = void> |
| 355 class RepeatedFieldRef; |
| 356 |
| 357 template<typename T, typename Enable = void> |
| 358 class MutableRepeatedFieldRef; |
| 359 |
| 319 // This interface contains methods that can be used to dynamically access | 360 // This interface contains methods that can be used to dynamically access |
| 320 // and modify the fields of a protocol message. Their semantics are | 361 // and modify the fields of a protocol message. Their semantics are |
| 321 // similar to the accessors the protocol compiler generates. | 362 // similar to the accessors the protocol compiler generates. |
| 322 // | 363 // |
| 323 // To get the Reflection for a given Message, call Message::GetReflection(). | 364 // To get the Reflection for a given Message, call Message::GetReflection(). |
| 324 // | 365 // |
| 325 // This interface is separate from Message only for efficiency reasons; | 366 // This interface is separate from Message only for efficiency reasons; |
| 326 // the vast majority of implementations of Message will share the same | 367 // the vast majority of implementations of Message will share the same |
| 327 // implementation of Reflection (GeneratedMessageReflection, | 368 // implementation of Reflection (GeneratedMessageReflection, |
| 328 // defined in generated_message.h), and all Messages of a particular class | 369 // defined in generated_message.h), and all Messages of a particular class |
| (...skipping 28 matching lines...) Expand all Loading... |
| 357 // | 398 // |
| 358 // TODO(kenton): Create a utility class which callers can use to read and | 399 // TODO(kenton): Create a utility class which callers can use to read and |
| 359 // write fields from a Reflection without paying attention to the type. | 400 // write fields from a Reflection without paying attention to the type. |
| 360 class LIBPROTOBUF_EXPORT Reflection { | 401 class LIBPROTOBUF_EXPORT Reflection { |
| 361 public: | 402 public: |
| 362 inline Reflection() {} | 403 inline Reflection() {} |
| 363 virtual ~Reflection(); | 404 virtual ~Reflection(); |
| 364 | 405 |
| 365 // Get the UnknownFieldSet for the message. This contains fields which | 406 // Get the UnknownFieldSet for the message. This contains fields which |
| 366 // were seen when the Message was parsed but were not recognized according | 407 // were seen when the Message was parsed but were not recognized according |
| 367 // to the Message's definition. | 408 // to the Message's definition. For proto3 protos, this method will always |
| 409 // return an empty UnknownFieldSet. |
| 368 virtual const UnknownFieldSet& GetUnknownFields( | 410 virtual const UnknownFieldSet& GetUnknownFields( |
| 369 const Message& message) const = 0; | 411 const Message& message) const = 0; |
| 370 // Get a mutable pointer to the UnknownFieldSet for the message. This | 412 // Get a mutable pointer to the UnknownFieldSet for the message. This |
| 371 // contains fields which were seen when the Message was parsed but were not | 413 // contains fields which were seen when the Message was parsed but were not |
| 372 // recognized according to the Message's definition. | 414 // recognized according to the Message's definition. For proto3 protos, this |
| 415 // method will return a valid mutable UnknownFieldSet pointer but modifying |
| 416 // it won't affect the serialized bytes of the message. |
| 373 virtual UnknownFieldSet* MutableUnknownFields(Message* message) const = 0; | 417 virtual UnknownFieldSet* MutableUnknownFields(Message* message) const = 0; |
| 374 | 418 |
| 375 // Estimate the amount of memory used by the message object. | 419 // Estimate the amount of memory used by the message object. |
| 376 virtual int SpaceUsed(const Message& message) const = 0; | 420 virtual int SpaceUsed(const Message& message) const = 0; |
| 377 | 421 |
| 378 // Check if the given non-repeated field is set. | 422 // Check if the given non-repeated field is set. |
| 379 virtual bool HasField(const Message& message, | 423 virtual bool HasField(const Message& message, |
| 380 const FieldDescriptor* field) const = 0; | 424 const FieldDescriptor* field) const = 0; |
| 381 | 425 |
| 382 // Get the number of elements of a repeated field. | 426 // Get the number of elements of a repeated field. |
| 383 virtual int FieldSize(const Message& message, | 427 virtual int FieldSize(const Message& message, |
| 384 const FieldDescriptor* field) const = 0; | 428 const FieldDescriptor* field) const = 0; |
| 385 | 429 |
| 386 // Clear the value of a field, so that HasField() returns false or | 430 // Clear the value of a field, so that HasField() returns false or |
| 387 // FieldSize() returns zero. | 431 // FieldSize() returns zero. |
| 388 virtual void ClearField(Message* message, | 432 virtual void ClearField(Message* message, |
| 389 const FieldDescriptor* field) const = 0; | 433 const FieldDescriptor* field) const = 0; |
| 390 | 434 |
| 435 // Check if the oneof is set. Returns true if any field in oneof |
| 436 // is set, false otherwise. |
| 437 // TODO(jieluo) - make it pure virtual after updating all |
| 438 // the subclasses. |
| 439 virtual bool HasOneof(const Message& /*message*/, |
| 440 const OneofDescriptor* /*oneof_descriptor*/) const { |
| 441 return false; |
| 442 } |
| 443 |
| 444 virtual void ClearOneof(Message* /*message*/, |
| 445 const OneofDescriptor* /*oneof_descriptor*/) const {} |
| 446 |
| 447 // Returns the field descriptor if the oneof is set. NULL otherwise. |
| 448 // TODO(jieluo) - make it pure virtual. |
| 449 virtual const FieldDescriptor* GetOneofFieldDescriptor( |
| 450 const Message& /*message*/, |
| 451 const OneofDescriptor* /*oneof_descriptor*/) const { |
| 452 return NULL; |
| 453 } |
| 454 |
| 391 // Removes the last element of a repeated field. | 455 // Removes the last element of a repeated field. |
| 392 // We don't provide a way to remove any element other than the last | 456 // We don't provide a way to remove any element other than the last |
| 393 // because it invites inefficient use, such as O(n^2) filtering loops | 457 // because it invites inefficient use, such as O(n^2) filtering loops |
| 394 // that should have been O(n). If you want to remove an element other | 458 // that should have been O(n). If you want to remove an element other |
| 395 // than the last, the best way to do it is to re-arrange the elements | 459 // than the last, the best way to do it is to re-arrange the elements |
| 396 // (using Swap()) so that the one you want removed is at the end, then | 460 // (using Swap()) so that the one you want removed is at the end, then |
| 397 // call RemoveLast(). | 461 // call RemoveLast(). |
| 398 virtual void RemoveLast(Message* message, | 462 virtual void RemoveLast(Message* message, |
| 399 const FieldDescriptor* field) const = 0; | 463 const FieldDescriptor* field) const = 0; |
| 400 // Removes the last element of a repeated message field, and returns the | 464 // Removes the last element of a repeated message field, and returns the |
| 401 // pointer to the caller. Caller takes ownership of the returned pointer. | 465 // pointer to the caller. Caller takes ownership of the returned pointer. |
| 402 virtual Message* ReleaseLast(Message* message, | 466 virtual Message* ReleaseLast(Message* message, |
| 403 const FieldDescriptor* field) const = 0; | 467 const FieldDescriptor* field) const = 0; |
| 404 | 468 |
| 405 // Swap the complete contents of two messages. | 469 // Swap the complete contents of two messages. |
| 406 virtual void Swap(Message* message1, Message* message2) const = 0; | 470 virtual void Swap(Message* message1, Message* message2) const = 0; |
| 407 | 471 |
| 472 // Swap fields listed in fields vector of two messages. |
| 473 virtual void SwapFields(Message* message1, |
| 474 Message* message2, |
| 475 const std::vector<const FieldDescriptor*>& fields) |
| 476 const = 0; |
| 477 |
| 408 // Swap two elements of a repeated field. | 478 // Swap two elements of a repeated field. |
| 409 virtual void SwapElements(Message* message, | 479 virtual void SwapElements(Message* message, |
| 410 const FieldDescriptor* field, | 480 const FieldDescriptor* field, |
| 411 int index1, | 481 int index1, |
| 412 int index2) const = 0; | 482 int index2) const = 0; |
| 413 | 483 |
| 414 // List all fields of the message which are currently set. This includes | 484 // List all fields of the message which are currently set. This includes |
| 415 // extensions. Singular fields will only be listed if HasField(field) would | 485 // extensions. Singular fields will only be listed if HasField(field) would |
| 416 // return true and repeated fields will only be listed if FieldSize(field) | 486 // return true and repeated fields will only be listed if FieldSize(field) |
| 417 // would return non-zero. Fields (both normal fields and extension fields) | 487 // would return non-zero. Fields (both normal fields and extension fields) |
| 418 // will be listed ordered by field number. | 488 // will be listed ordered by field number. |
| 419 virtual void ListFields(const Message& message, | 489 virtual void ListFields( |
| 420 vector<const FieldDescriptor*>* output) const = 0; | 490 const Message& message, |
| 491 std::vector<const FieldDescriptor*>* output) const = 0; |
| 421 | 492 |
| 422 // Singular field getters ------------------------------------------ | 493 // Singular field getters ------------------------------------------ |
| 423 // These get the value of a non-repeated field. They return the default | 494 // These get the value of a non-repeated field. They return the default |
| 424 // value for fields that aren't set. | 495 // value for fields that aren't set. |
| 425 | 496 |
| 426 virtual int32 GetInt32 (const Message& message, | 497 virtual int32 GetInt32 (const Message& message, |
| 427 const FieldDescriptor* field) const = 0; | 498 const FieldDescriptor* field) const = 0; |
| 428 virtual int64 GetInt64 (const Message& message, | 499 virtual int64 GetInt64 (const Message& message, |
| 429 const FieldDescriptor* field) const = 0; | 500 const FieldDescriptor* field) const = 0; |
| 430 virtual uint32 GetUInt32(const Message& message, | 501 virtual uint32 GetUInt32(const Message& message, |
| 431 const FieldDescriptor* field) const = 0; | 502 const FieldDescriptor* field) const = 0; |
| 432 virtual uint64 GetUInt64(const Message& message, | 503 virtual uint64 GetUInt64(const Message& message, |
| 433 const FieldDescriptor* field) const = 0; | 504 const FieldDescriptor* field) const = 0; |
| 434 virtual float GetFloat (const Message& message, | 505 virtual float GetFloat (const Message& message, |
| 435 const FieldDescriptor* field) const = 0; | 506 const FieldDescriptor* field) const = 0; |
| 436 virtual double GetDouble(const Message& message, | 507 virtual double GetDouble(const Message& message, |
| 437 const FieldDescriptor* field) const = 0; | 508 const FieldDescriptor* field) const = 0; |
| 438 virtual bool GetBool (const Message& message, | 509 virtual bool GetBool (const Message& message, |
| 439 const FieldDescriptor* field) const = 0; | 510 const FieldDescriptor* field) const = 0; |
| 440 virtual string GetString(const Message& message, | 511 virtual string GetString(const Message& message, |
| 441 const FieldDescriptor* field) const = 0; | 512 const FieldDescriptor* field) const = 0; |
| 442 virtual const EnumValueDescriptor* GetEnum( | 513 virtual const EnumValueDescriptor* GetEnum( |
| 443 const Message& message, const FieldDescriptor* field) const = 0; | 514 const Message& message, const FieldDescriptor* field) const = 0; |
| 515 |
| 516 // GetEnumValue() returns an enum field's value as an integer rather than |
| 517 // an EnumValueDescriptor*. If the integer value does not correspond to a |
| 518 // known value descriptor, a new value descriptor is created. (Such a value |
| 519 // will only be present when the new unknown-enum-value semantics are enabled |
| 520 // for a message.) |
| 521 virtual int GetEnumValue( |
| 522 const Message& message, const FieldDescriptor* field) const; |
| 523 |
| 444 // See MutableMessage() for the meaning of the "factory" parameter. | 524 // See MutableMessage() for the meaning of the "factory" parameter. |
| 445 virtual const Message& GetMessage(const Message& message, | 525 virtual const Message& GetMessage(const Message& message, |
| 446 const FieldDescriptor* field, | 526 const FieldDescriptor* field, |
| 447 MessageFactory* factory = NULL) const = 0; | 527 MessageFactory* factory = NULL) const = 0; |
| 448 | 528 |
| 449 // Get a string value without copying, if possible. | 529 // Get a string value without copying, if possible. |
| 450 // | 530 // |
| 451 // GetString() necessarily returns a copy of the string. This can be | 531 // GetString() necessarily returns a copy of the string. This can be |
| 452 // inefficient when the string is already stored in a string object in the | 532 // inefficient when the string is already stored in a string object in the |
| 453 // underlying message. GetStringReference() will return a reference to the | 533 // underlying message. GetStringReference() will return a reference to the |
| 454 // underlying string in this case. Otherwise, it will copy the string into | 534 // underlying string in this case. Otherwise, it will copy the string into |
| 455 // *scratch and return that. | 535 // *scratch and return that. |
| 456 // | 536 // |
| 457 // Note: It is perfectly reasonable and useful to write code like: | 537 // Note: It is perfectly reasonable and useful to write code like: |
| 458 // str = reflection->GetStringReference(field, &str); | 538 // str = reflection->GetStringReference(field, &str); |
| 459 // This line would ensure that only one copy of the string is made | 539 // This line would ensure that only one copy of the string is made |
| 460 // regardless of the field's underlying representation. When initializing | 540 // regardless of the field's underlying representation. When initializing |
| 461 // a newly-constructed string, though, it's just as fast and more readable | 541 // a newly-constructed string, though, it's just as fast and more readable |
| 462 // to use code like: | 542 // to use code like: |
| 463 // string str = reflection->GetString(field); | 543 // string str = reflection->GetString(message, field); |
| 464 virtual const string& GetStringReference(const Message& message, | 544 virtual const string& GetStringReference(const Message& message, |
| 465 const FieldDescriptor* field, | 545 const FieldDescriptor* field, |
| 466 string* scratch) const = 0; | 546 string* scratch) const = 0; |
| 467 | 547 |
| 468 | 548 |
| 469 // Singular field mutators ----------------------------------------- | 549 // Singular field mutators ----------------------------------------- |
| 470 // These mutate the value of a non-repeated field. | 550 // These mutate the value of a non-repeated field. |
| 471 | 551 |
| 472 virtual void SetInt32 (Message* message, | 552 virtual void SetInt32 (Message* message, |
| 473 const FieldDescriptor* field, int32 value) const = 0; | 553 const FieldDescriptor* field, int32 value) const = 0; |
| 474 virtual void SetInt64 (Message* message, | 554 virtual void SetInt64 (Message* message, |
| 475 const FieldDescriptor* field, int64 value) const = 0; | 555 const FieldDescriptor* field, int64 value) const = 0; |
| 476 virtual void SetUInt32(Message* message, | 556 virtual void SetUInt32(Message* message, |
| 477 const FieldDescriptor* field, uint32 value) const = 0; | 557 const FieldDescriptor* field, uint32 value) const = 0; |
| 478 virtual void SetUInt64(Message* message, | 558 virtual void SetUInt64(Message* message, |
| 479 const FieldDescriptor* field, uint64 value) const = 0; | 559 const FieldDescriptor* field, uint64 value) const = 0; |
| 480 virtual void SetFloat (Message* message, | 560 virtual void SetFloat (Message* message, |
| 481 const FieldDescriptor* field, float value) const = 0; | 561 const FieldDescriptor* field, float value) const = 0; |
| 482 virtual void SetDouble(Message* message, | 562 virtual void SetDouble(Message* message, |
| 483 const FieldDescriptor* field, double value) const = 0; | 563 const FieldDescriptor* field, double value) const = 0; |
| 484 virtual void SetBool (Message* message, | 564 virtual void SetBool (Message* message, |
| 485 const FieldDescriptor* field, bool value) const = 0; | 565 const FieldDescriptor* field, bool value) const = 0; |
| 486 virtual void SetString(Message* message, | 566 virtual void SetString(Message* message, |
| 487 const FieldDescriptor* field, | 567 const FieldDescriptor* field, |
| 488 const string& value) const = 0; | 568 const string& value) const = 0; |
| 489 virtual void SetEnum (Message* message, | 569 virtual void SetEnum (Message* message, |
| 490 const FieldDescriptor* field, | 570 const FieldDescriptor* field, |
| 491 const EnumValueDescriptor* value) const = 0; | 571 const EnumValueDescriptor* value) const = 0; |
| 572 // Set an enum field's value with an integer rather than EnumValueDescriptor. |
| 573 // If the value does not correspond to a known enum value, either behavior is |
| 574 // undefined (for proto2 messages), or the value is accepted silently for |
| 575 // messages with new unknown-enum-value semantics. |
| 576 virtual void SetEnumValue(Message* message, |
| 577 const FieldDescriptor* field, |
| 578 int value) const; |
| 579 |
| 492 // Get a mutable pointer to a field with a message type. If a MessageFactory | 580 // Get a mutable pointer to a field with a message type. If a MessageFactory |
| 493 // is provided, it will be used to construct instances of the sub-message; | 581 // is provided, it will be used to construct instances of the sub-message; |
| 494 // otherwise, the default factory is used. If the field is an extension that | 582 // otherwise, the default factory is used. If the field is an extension that |
| 495 // does not live in the same pool as the containing message's descriptor (e.g. | 583 // does not live in the same pool as the containing message's descriptor (e.g. |
| 496 // it lives in an overlay pool), then a MessageFactory must be provided. | 584 // it lives in an overlay pool), then a MessageFactory must be provided. |
| 497 // If you have no idea what that meant, then you probably don't need to worry | 585 // If you have no idea what that meant, then you probably don't need to worry |
| 498 // about it (don't provide a MessageFactory). WARNING: If the | 586 // about it (don't provide a MessageFactory). WARNING: If the |
| 499 // FieldDescriptor is for a compiled-in extension, then | 587 // FieldDescriptor is for a compiled-in extension, then |
| 500 // factory->GetPrototype(field->message_type() MUST return an instance of the | 588 // factory->GetPrototype(field->message_type() MUST return an instance of the |
| 501 // compiled-in class for this type, NOT DynamicMessage. | 589 // compiled-in class for this type, NOT DynamicMessage. |
| 502 virtual Message* MutableMessage(Message* message, | 590 virtual Message* MutableMessage(Message* message, |
| 503 const FieldDescriptor* field, | 591 const FieldDescriptor* field, |
| 504 MessageFactory* factory = NULL) const = 0; | 592 MessageFactory* factory = NULL) const = 0; |
| 593 // Replaces the message specified by 'field' with the already-allocated object |
| 594 // sub_message, passing ownership to the message. If the field contained a |
| 595 // message, that message is deleted. If sub_message is NULL, the field is |
| 596 // cleared. |
| 597 virtual void SetAllocatedMessage(Message* message, |
| 598 Message* sub_message, |
| 599 const FieldDescriptor* field) const = 0; |
| 505 // Releases the message specified by 'field' and returns the pointer, | 600 // Releases the message specified by 'field' and returns the pointer, |
| 506 // ReleaseMessage() will return the message the message object if it exists. | 601 // ReleaseMessage() will return the message the message object if it exists. |
| 507 // Otherwise, it may or may not return NULL. In any case, if the return value | 602 // Otherwise, it may or may not return NULL. In any case, if the return value |
| 508 // is non-NULL, the caller takes ownership of the pointer. | 603 // is non-NULL, the caller takes ownership of the pointer. |
| 509 // If the field existed (HasField() is true), then the returned pointer will | 604 // If the field existed (HasField() is true), then the returned pointer will |
| 510 // be the same as the pointer returned by MutableMessage(). | 605 // be the same as the pointer returned by MutableMessage(). |
| 511 // This function has the same effect as ClearField(). | 606 // This function has the same effect as ClearField(). |
| 512 virtual Message* ReleaseMessage(Message* message, | 607 virtual Message* ReleaseMessage(Message* message, |
| 513 const FieldDescriptor* field, | 608 const FieldDescriptor* field, |
| 514 MessageFactory* factory = NULL) const = 0; | 609 MessageFactory* factory = NULL) const = 0; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 537 int index) const = 0; | 632 int index) const = 0; |
| 538 virtual bool GetRepeatedBool (const Message& message, | 633 virtual bool GetRepeatedBool (const Message& message, |
| 539 const FieldDescriptor* field, | 634 const FieldDescriptor* field, |
| 540 int index) const = 0; | 635 int index) const = 0; |
| 541 virtual string GetRepeatedString(const Message& message, | 636 virtual string GetRepeatedString(const Message& message, |
| 542 const FieldDescriptor* field, | 637 const FieldDescriptor* field, |
| 543 int index) const = 0; | 638 int index) const = 0; |
| 544 virtual const EnumValueDescriptor* GetRepeatedEnum( | 639 virtual const EnumValueDescriptor* GetRepeatedEnum( |
| 545 const Message& message, | 640 const Message& message, |
| 546 const FieldDescriptor* field, int index) const = 0; | 641 const FieldDescriptor* field, int index) const = 0; |
| 642 // GetRepeatedEnumValue() returns an enum field's value as an integer rather |
| 643 // than an EnumValueDescriptor*. If the integer value does not correspond to a |
| 644 // known value descriptor, a new value descriptor is created. (Such a value |
| 645 // will only be present when the new unknown-enum-value semantics are enabled |
| 646 // for a message.) |
| 647 virtual int GetRepeatedEnumValue( |
| 648 const Message& message, |
| 649 const FieldDescriptor* field, int index) const; |
| 547 virtual const Message& GetRepeatedMessage( | 650 virtual const Message& GetRepeatedMessage( |
| 548 const Message& message, | 651 const Message& message, |
| 549 const FieldDescriptor* field, int index) const = 0; | 652 const FieldDescriptor* field, int index) const = 0; |
| 550 | 653 |
| 551 // See GetStringReference(), above. | 654 // See GetStringReference(), above. |
| 552 virtual const string& GetRepeatedStringReference( | 655 virtual const string& GetRepeatedStringReference( |
| 553 const Message& message, const FieldDescriptor* field, | 656 const Message& message, const FieldDescriptor* field, |
| 554 int index, string* scratch) const = 0; | 657 int index, string* scratch) const = 0; |
| 555 | 658 |
| 556 | 659 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 577 int index, double value) const = 0; | 680 int index, double value) const = 0; |
| 578 virtual void SetRepeatedBool (Message* message, | 681 virtual void SetRepeatedBool (Message* message, |
| 579 const FieldDescriptor* field, | 682 const FieldDescriptor* field, |
| 580 int index, bool value) const = 0; | 683 int index, bool value) const = 0; |
| 581 virtual void SetRepeatedString(Message* message, | 684 virtual void SetRepeatedString(Message* message, |
| 582 const FieldDescriptor* field, | 685 const FieldDescriptor* field, |
| 583 int index, const string& value) const = 0; | 686 int index, const string& value) const = 0; |
| 584 virtual void SetRepeatedEnum(Message* message, | 687 virtual void SetRepeatedEnum(Message* message, |
| 585 const FieldDescriptor* field, int index, | 688 const FieldDescriptor* field, int index, |
| 586 const EnumValueDescriptor* value) const = 0; | 689 const EnumValueDescriptor* value) const = 0; |
| 690 // Set an enum field's value with an integer rather than EnumValueDescriptor. |
| 691 // If the value does not correspond to a known enum value, either behavior is |
| 692 // undefined (for proto2 messages), or the value is accepted silently for |
| 693 // messages with new unknown-enum-value semantics. |
| 694 virtual void SetRepeatedEnumValue(Message* message, |
| 695 const FieldDescriptor* field, int index, |
| 696 int value) const; |
| 587 // Get a mutable pointer to an element of a repeated field with a message | 697 // Get a mutable pointer to an element of a repeated field with a message |
| 588 // type. | 698 // type. |
| 589 virtual Message* MutableRepeatedMessage( | 699 virtual Message* MutableRepeatedMessage( |
| 590 Message* message, const FieldDescriptor* field, int index) const = 0; | 700 Message* message, const FieldDescriptor* field, int index) const = 0; |
| 591 | 701 |
| 592 | 702 |
| 593 // Repeated field adders ------------------------------------------- | 703 // Repeated field adders ------------------------------------------- |
| 594 // These add an element to a repeated field. | 704 // These add an element to a repeated field. |
| 595 | 705 |
| 596 virtual void AddInt32 (Message* message, | 706 virtual void AddInt32 (Message* message, |
| 597 const FieldDescriptor* field, int32 value) const = 0; | 707 const FieldDescriptor* field, int32 value) const = 0; |
| 598 virtual void AddInt64 (Message* message, | 708 virtual void AddInt64 (Message* message, |
| 599 const FieldDescriptor* field, int64 value) const = 0; | 709 const FieldDescriptor* field, int64 value) const = 0; |
| 600 virtual void AddUInt32(Message* message, | 710 virtual void AddUInt32(Message* message, |
| 601 const FieldDescriptor* field, uint32 value) const = 0; | 711 const FieldDescriptor* field, uint32 value) const = 0; |
| 602 virtual void AddUInt64(Message* message, | 712 virtual void AddUInt64(Message* message, |
| 603 const FieldDescriptor* field, uint64 value) const = 0; | 713 const FieldDescriptor* field, uint64 value) const = 0; |
| 604 virtual void AddFloat (Message* message, | 714 virtual void AddFloat (Message* message, |
| 605 const FieldDescriptor* field, float value) const = 0; | 715 const FieldDescriptor* field, float value) const = 0; |
| 606 virtual void AddDouble(Message* message, | 716 virtual void AddDouble(Message* message, |
| 607 const FieldDescriptor* field, double value) const = 0; | 717 const FieldDescriptor* field, double value) const = 0; |
| 608 virtual void AddBool (Message* message, | 718 virtual void AddBool (Message* message, |
| 609 const FieldDescriptor* field, bool value) const = 0; | 719 const FieldDescriptor* field, bool value) const = 0; |
| 610 virtual void AddString(Message* message, | 720 virtual void AddString(Message* message, |
| 611 const FieldDescriptor* field, | 721 const FieldDescriptor* field, |
| 612 const string& value) const = 0; | 722 const string& value) const = 0; |
| 613 virtual void AddEnum (Message* message, | 723 virtual void AddEnum (Message* message, |
| 614 const FieldDescriptor* field, | 724 const FieldDescriptor* field, |
| 615 const EnumValueDescriptor* value) const = 0; | 725 const EnumValueDescriptor* value) const = 0; |
| 726 // Set an enum field's value with an integer rather than EnumValueDescriptor. |
| 727 // If the value does not correspond to a known enum value, either behavior is |
| 728 // undefined (for proto2 messages), or the value is accepted silently for |
| 729 // messages with new unknown-enum-value semantics. |
| 730 virtual void AddEnumValue(Message* message, |
| 731 const FieldDescriptor* field, |
| 732 int value) const; |
| 616 // See MutableMessage() for comments on the "factory" parameter. | 733 // See MutableMessage() for comments on the "factory" parameter. |
| 617 virtual Message* AddMessage(Message* message, | 734 virtual Message* AddMessage(Message* message, |
| 618 const FieldDescriptor* field, | 735 const FieldDescriptor* field, |
| 619 MessageFactory* factory = NULL) const = 0; | 736 MessageFactory* factory = NULL) const = 0; |
| 620 | 737 |
| 738 // Appends an already-allocated object 'new_entry' to the repeated field |
| 739 // specifyed by 'field' passing ownership to the message. |
| 740 // TODO(tmarek): Make virtual after all subclasses have been |
| 741 // updated. |
| 742 virtual void AddAllocatedMessage(Message* message, |
| 743 const FieldDescriptor* field, |
| 744 Message* new_entry) const {} |
| 621 | 745 |
| 746 |
| 747 // Get a RepeatedFieldRef object that can be used to read the underlying |
| 748 // repeated field. The type parameter T must be set according to the |
| 749 // field's cpp type. The following table shows the mapping from cpp type |
| 750 // to acceptable T. |
| 751 // |
| 752 // field->cpp_type() T |
| 753 // CPPTYPE_INT32 int32 |
| 754 // CPPTYPE_UINT32 uint32 |
| 755 // CPPTYPE_INT64 int64 |
| 756 // CPPTYPE_UINT64 uint64 |
| 757 // CPPTYPE_DOUBLE double |
| 758 // CPPTYPE_FLOAT float |
| 759 // CPPTYPE_BOOL bool |
| 760 // CPPTYPE_ENUM generated enum type or int32 |
| 761 // CPPTYPE_STRING string |
| 762 // CPPTYPE_MESSAGE generated message type or google::protobuf::Message |
| 763 // |
| 764 // A RepeatedFieldRef object can be copied and the resulted object will point |
| 765 // to the same repeated field in the same message. The object can be used as |
| 766 // long as the message is not destroyed. |
| 767 // |
| 768 // Note that to use this method users need to include the header file |
| 769 // "google/protobuf/reflection.h" (which defines the RepeatedFieldRef |
| 770 // class templates). |
| 771 template<typename T> |
| 772 RepeatedFieldRef<T> GetRepeatedFieldRef( |
| 773 const Message& message, const FieldDescriptor* field) const; |
| 774 |
| 775 // Like GetRepeatedFieldRef() but return an object that can also be used |
| 776 // manipulate the underlying repeated field. |
| 777 template<typename T> |
| 778 MutableRepeatedFieldRef<T> GetMutableRepeatedFieldRef( |
| 779 Message* message, const FieldDescriptor* field) const; |
| 780 |
| 781 // DEPRECATED. Please use Get(Mutable)RepeatedFieldRef() for repeated field |
| 782 // access. The following repeated field accesors will be removed in the |
| 783 // future. |
| 784 // |
| 622 // Repeated field accessors ------------------------------------------------- | 785 // Repeated field accessors ------------------------------------------------- |
| 623 // The methods above, e.g. GetRepeatedInt32(msg, fd, index), provide singular | 786 // The methods above, e.g. GetRepeatedInt32(msg, fd, index), provide singular |
| 624 // access to the data in a RepeatedField. The methods below provide aggregate | 787 // access to the data in a RepeatedField. The methods below provide aggregate |
| 625 // access by exposing the RepeatedField object itself with the Message. | 788 // access by exposing the RepeatedField object itself with the Message. |
| 626 // Applying these templates to inappropriate types will lead to an undefined | 789 // Applying these templates to inappropriate types will lead to an undefined |
| 627 // reference at link time (e.g. GetRepeatedField<***double>), or possibly a | 790 // reference at link time (e.g. GetRepeatedField<***double>), or possibly a |
| 628 // template matching error at compile time (e.g. GetRepeatedPtrField<File>). | 791 // template matching error at compile time (e.g. GetRepeatedPtrField<File>). |
| 629 // | 792 // |
| 630 // Usage example: my_doubs = refl->GetRepeatedField<double>(msg, fd); | 793 // Usage example: my_doubs = refl->GetRepeatedField<double>(msg, fd); |
| 631 | 794 |
| 795 // DEPRECATED. Please use GetRepeatedFieldRef(). |
| 796 // |
| 632 // for T = Cord and all protobuf scalar types except enums. | 797 // for T = Cord and all protobuf scalar types except enums. |
| 633 template<typename T> | 798 template<typename T> |
| 634 const RepeatedField<T>& GetRepeatedField( | 799 const RepeatedField<T>& GetRepeatedField( |
| 635 const Message&, const FieldDescriptor*) const; | 800 const Message&, const FieldDescriptor*) const; |
| 636 | 801 |
| 802 // DEPRECATED. Please use GetMutableRepeatedFieldRef(). |
| 803 // |
| 637 // for T = Cord and all protobuf scalar types except enums. | 804 // for T = Cord and all protobuf scalar types except enums. |
| 638 template<typename T> | 805 template<typename T> |
| 639 RepeatedField<T>* MutableRepeatedField( | 806 RepeatedField<T>* MutableRepeatedField( |
| 640 Message*, const FieldDescriptor*) const; | 807 Message*, const FieldDescriptor*) const; |
| 641 | 808 |
| 809 // DEPRECATED. Please use GetRepeatedFieldRef(). |
| 810 // |
| 642 // for T = string, google::protobuf::internal::StringPieceField | 811 // for T = string, google::protobuf::internal::StringPieceField |
| 643 // google::protobuf::Message & descendants. | 812 // google::protobuf::Message & descendants. |
| 644 template<typename T> | 813 template<typename T> |
| 645 const RepeatedPtrField<T>& GetRepeatedPtrField( | 814 const RepeatedPtrField<T>& GetRepeatedPtrField( |
| 646 const Message&, const FieldDescriptor*) const; | 815 const Message&, const FieldDescriptor*) const; |
| 647 | 816 |
| 817 // DEPRECATED. Please use GetMutableRepeatedFieldRef(). |
| 818 // |
| 648 // for T = string, google::protobuf::internal::StringPieceField | 819 // for T = string, google::protobuf::internal::StringPieceField |
| 649 // google::protobuf::Message & descendants. | 820 // google::protobuf::Message & descendants. |
| 650 template<typename T> | 821 template<typename T> |
| 651 RepeatedPtrField<T>* MutableRepeatedPtrField( | 822 RepeatedPtrField<T>* MutableRepeatedPtrField( |
| 652 Message*, const FieldDescriptor*) const; | 823 Message*, const FieldDescriptor*) const; |
| 653 | 824 |
| 654 // Extensions ---------------------------------------------------------------- | 825 // Extensions ---------------------------------------------------------------- |
| 655 | 826 |
| 656 // Try to find an extension of this message type by fully-qualified field | 827 // Try to find an extension of this message type by fully-qualified field |
| 657 // name. Returns NULL if no extension is known for this name or number. | 828 // name. Returns NULL if no extension is known for this name or number. |
| 658 virtual const FieldDescriptor* FindKnownExtensionByName( | 829 virtual const FieldDescriptor* FindKnownExtensionByName( |
| 659 const string& name) const = 0; | 830 const string& name) const = 0; |
| 660 | 831 |
| 661 // Try to find an extension of this message type by field number. | 832 // Try to find an extension of this message type by field number. |
| 662 // Returns NULL if no extension is known for this name or number. | 833 // Returns NULL if no extension is known for this name or number. |
| 663 virtual const FieldDescriptor* FindKnownExtensionByNumber( | 834 virtual const FieldDescriptor* FindKnownExtensionByNumber( |
| 664 int number) const = 0; | 835 int number) const = 0; |
| 665 | 836 |
| 837 // Feature Flags ------------------------------------------------------------- |
| 838 |
| 839 // Does this message support storing arbitrary integer values in enum fields? |
| 840 // If |true|, GetEnumValue/SetEnumValue and associated repeated-field versions |
| 841 // take arbitrary integer values, and the legacy GetEnum() getter will |
| 842 // dynamically create an EnumValueDescriptor for any integer value without |
| 843 // one. If |false|, setting an unknown enum value via the integer-based |
| 844 // setters results in undefined behavior (in practice, GOOGLE_DCHECK-fails). |
| 845 // |
| 846 // Generic code that uses reflection to handle messages with enum fields |
| 847 // should check this flag before using the integer-based setter, and either |
| 848 // downgrade to a compatible value or use the UnknownFieldSet if not. For |
| 849 // example: |
| 850 // |
| 851 // int new_value = GetValueFromApplicationLogic(); |
| 852 // if (reflection->SupportsUnknownEnumValues()) { |
| 853 // reflection->SetEnumValue(message, field, new_value); |
| 854 // } else { |
| 855 // if (field_descriptor->enum_type()-> |
| 856 // FindValueByNumver(new_value) != NULL) { |
| 857 // reflection->SetEnumValue(message, field, new_value); |
| 858 // } else if (emit_unknown_enum_values) { |
| 859 // reflection->MutableUnknownFields(message)->AddVarint( |
| 860 // field->number(), |
| 861 // new_value); |
| 862 // } else { |
| 863 // // convert value to a compatible/default value. |
| 864 // new_value = CompatibleDowngrade(new_value); |
| 865 // reflection->SetEnumValue(message, field, new_value); |
| 866 // } |
| 867 // } |
| 868 virtual bool SupportsUnknownEnumValues() const { return false; } |
| 869 |
| 870 // Returns the MessageFactory associated with this message. This can be |
| 871 // useful for determining if a message is a generated message or not, for |
| 872 // example: |
| 873 // |
| 874 // if (message->GetReflection()->GetMessageFactory() == |
| 875 // google::protobuf::MessageFactory::generated_factory()) { |
| 876 // // This is a generated message. |
| 877 // } |
| 878 // |
| 879 // It can also be used to create more messages of this type, though |
| 880 // Message::New() is an easier way to accomplish this. |
| 881 virtual MessageFactory* GetMessageFactory() const; |
| 882 |
| 666 // --------------------------------------------------------------------------- | 883 // --------------------------------------------------------------------------- |
| 667 | 884 |
| 668 protected: | 885 protected: |
| 669 // Obtain a pointer to a Repeated Field Structure and do some type checking: | 886 // Obtain a pointer to a Repeated Field Structure and do some type checking: |
| 670 // on field->cpp_type(), | 887 // on field->cpp_type(), |
| 671 // on field->field_option().ctype() (if ctype >= 0) | 888 // on field->field_option().ctype() (if ctype >= 0) |
| 672 // of field->message_type() (if message_type != NULL). | 889 // of field->message_type() (if message_type != NULL). |
| 673 // We use 1 routine rather than 4 (const vs mutable) x (scalar vs pointer). | 890 // We use 2 routine rather than 4 (const vs mutable) x (scalar vs pointer). |
| 674 virtual void* MutableRawRepeatedField( | 891 virtual void* MutableRawRepeatedField( |
| 675 Message* message, const FieldDescriptor* field, FieldDescriptor::CppType, | 892 Message* message, const FieldDescriptor* field, FieldDescriptor::CppType, |
| 676 int ctype, const Descriptor* message_type) const = 0; | 893 int ctype, const Descriptor* message_type) const = 0; |
| 677 | 894 |
| 895 // TODO(jieluo) - make it pure virtual after updating all the subclasses. |
| 896 virtual const void* GetRawRepeatedField( |
| 897 const Message& message, const FieldDescriptor* field, |
| 898 FieldDescriptor::CppType cpptype, int ctype, |
| 899 const Descriptor* message_type) const { |
| 900 return MutableRawRepeatedField( |
| 901 const_cast<Message*>(&message), field, cpptype, ctype, message_type); |
| 902 } |
| 903 |
| 904 // The following methods are used to implement (Mutable)RepeatedFieldRef. |
| 905 // A Ref object will store a raw pointer to the repeated field data (obtained |
| 906 // from RepeatedFieldData()) and a pointer to a Accessor (obtained from |
| 907 // RepeatedFieldAccessor) which will be used to access the raw data. |
| 908 // |
| 909 // TODO(xiaofeng): Make these methods pure-virtual. |
| 910 |
| 911 // Returns a raw pointer to the repeated field |
| 912 // |
| 913 // "cpp_type" and "message_type" are decuded from the type parameter T passed |
| 914 // to Get(Mutable)RepeatedFieldRef. If T is a generated message type, |
| 915 // "message_type" should be set to its descriptor. Otherwise "message_type" |
| 916 // should be set to NULL. Implementations of this method should check whether |
| 917 // "cpp_type"/"message_type" is consistent with the actual type of the field. |
| 918 // We use 1 routine rather than 2 (const vs mutable) because it is protected |
| 919 // and it doesn't change the message. |
| 920 virtual void* RepeatedFieldData( |
| 921 Message* message, const FieldDescriptor* field, |
| 922 FieldDescriptor::CppType cpp_type, |
| 923 const Descriptor* message_type) const; |
| 924 |
| 925 // The returned pointer should point to a singleton instance which implements |
| 926 // the RepeatedFieldAccessor interface. |
| 927 virtual const internal::RepeatedFieldAccessor* RepeatedFieldAccessor( |
| 928 const FieldDescriptor* field) const; |
| 929 |
| 678 private: | 930 private: |
| 931 template<typename T, typename Enable> |
| 932 friend class RepeatedFieldRef; |
| 933 template<typename T, typename Enable> |
| 934 friend class MutableRepeatedFieldRef; |
| 935 friend class ::google::protobuf::python::MapReflectionFriend; |
| 936 |
| 679 // Special version for specialized implementations of string. We can't call | 937 // Special version for specialized implementations of string. We can't call |
| 680 // MutableRawRepeatedField directly here because we don't have access to | 938 // MutableRawRepeatedField directly here because we don't have access to |
| 681 // FieldOptions::* which are defined in descriptor.pb.h. Including that | 939 // FieldOptions::* which are defined in descriptor.pb.h. Including that |
| 682 // file here is not possible because it would cause a circular include cycle. | 940 // file here is not possible because it would cause a circular include cycle. |
| 941 // We use 1 routine rather than 2 (const vs mutable) because it is private |
| 942 // and mutable a repeated string field doesn't change the message. |
| 683 void* MutableRawRepeatedString( | 943 void* MutableRawRepeatedString( |
| 684 Message* message, const FieldDescriptor* field, bool is_string) const; | 944 Message* message, const FieldDescriptor* field, bool is_string) const; |
| 685 | 945 |
| 946 friend class MapReflectionTester; |
| 947 // TODO(jieluo) - make the map APIs pure virtual after updating |
| 948 // all the subclasses. |
| 949 // Returns true if key is in map. Returns false if key is not in map field. |
| 950 virtual bool ContainsMapKey(const Message& message, |
| 951 const FieldDescriptor* field, |
| 952 const MapKey& key) const { |
| 953 return false; |
| 954 } |
| 955 |
| 956 // If key is in map field: Saves the value pointer to val and returns |
| 957 // false. If key in not in map field: Insert the key into map, saves |
| 958 // value pointer to val and retuns true. |
| 959 virtual bool InsertOrLookupMapValue(Message* message, |
| 960 const FieldDescriptor* field, |
| 961 const MapKey& key, |
| 962 MapValueRef* val) const { |
| 963 return false; |
| 964 } |
| 965 |
| 966 // Delete and returns true if key is in the map field. Returns false |
| 967 // otherwise. |
| 968 virtual bool DeleteMapValue(Message* message, |
| 969 const FieldDescriptor* field, |
| 970 const MapKey& key) const { |
| 971 return false; |
| 972 } |
| 973 |
| 974 // Returns a MapIterator referring to the first element in the map field. |
| 975 // If the map field is empty, this function returns the same as |
| 976 // reflection::MapEnd. Mutation to the field may invalidate the iterator. |
| 977 virtual MapIterator MapBegin( |
| 978 Message* message, |
| 979 const FieldDescriptor* field) const; |
| 980 |
| 981 // Returns a MapIterator referring to the theoretical element that would |
| 982 // follow the last element in the map field. It does not point to any |
| 983 // real element. Mutation to the field may invalidate the iterator. |
| 984 virtual MapIterator MapEnd( |
| 985 Message* message, |
| 986 const FieldDescriptor* field) const; |
| 987 |
| 988 // Get the number of <key, value> pair of a map field. The result may be |
| 989 // different from FieldSize which can have duplicate keys. |
| 990 virtual int MapSize(const Message& message, |
| 991 const FieldDescriptor* field) const { |
| 992 return 0; |
| 993 } |
| 994 |
| 995 // Help method for MapIterator. |
| 996 friend class MapIterator; |
| 997 virtual internal::MapFieldBase* MapData( |
| 998 Message* message, const FieldDescriptor* field) const { |
| 999 return NULL; |
| 1000 } |
| 1001 |
| 686 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Reflection); | 1002 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Reflection); |
| 687 }; | 1003 }; |
| 688 | 1004 |
| 689 // Abstract interface for a factory for message objects. | 1005 // Abstract interface for a factory for message objects. |
| 690 class LIBPROTOBUF_EXPORT MessageFactory { | 1006 class LIBPROTOBUF_EXPORT MessageFactory { |
| 691 public: | 1007 public: |
| 692 inline MessageFactory() {} | 1008 inline MessageFactory() {} |
| 693 virtual ~MessageFactory(); | 1009 virtual ~MessageFactory(); |
| 694 | 1010 |
| 695 // Given a Descriptor, gets or constructs the default (prototype) Message | 1011 // Given a Descriptor, gets or constructs the default (prototype) Message |
| 696 // of that type. You can then call that message's New() method to construct | 1012 // of that type. You can then call that message's New() method to construct |
| 697 // a mutable message of that type. | 1013 // a mutable message of that type. |
| 698 // | 1014 // |
| 699 // Calling this method twice with the same Descriptor returns the same | 1015 // Calling this method twice with the same Descriptor returns the same |
| 700 // object. The returned object remains property of the factory. Also, any | 1016 // object. The returned object remains property of the factory. Also, any |
| 701 // objects created by calling the prototype's New() method share some data | 1017 // objects created by calling the prototype's New() method share some data |
| 702 // with the prototype, so these must be destoyed before the MessageFactory | 1018 // with the prototype, so these must be destroyed before the MessageFactory |
| 703 // is destroyed. | 1019 // is destroyed. |
| 704 // | 1020 // |
| 705 // The given descriptor must outlive the returned message, and hence must | 1021 // The given descriptor must outlive the returned message, and hence must |
| 706 // outlive the MessageFactory. | 1022 // outlive the MessageFactory. |
| 707 // | 1023 // |
| 708 // Some implementations do not support all types. GetPrototype() will | 1024 // Some implementations do not support all types. GetPrototype() will |
| 709 // return NULL if the descriptor passed in is not supported. | 1025 // return NULL if the descriptor passed in is not supported. |
| 710 // | 1026 // |
| 711 // This method may or may not be thread-safe depending on the implementation. | 1027 // This method may or may not be thread-safe depending on the implementation. |
| 712 // Each implementation should document its own degree thread-safety. | 1028 // Each implementation should document its own degree thread-safety. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 791 return static_cast<RepeatedPtrField<string>* >( | 1107 return static_cast<RepeatedPtrField<string>* >( |
| 792 MutableRawRepeatedString(message, field, true)); | 1108 MutableRawRepeatedString(message, field, true)); |
| 793 } | 1109 } |
| 794 | 1110 |
| 795 | 1111 |
| 796 // ----- | 1112 // ----- |
| 797 | 1113 |
| 798 template<> | 1114 template<> |
| 799 inline const RepeatedPtrField<Message>& Reflection::GetRepeatedPtrField( | 1115 inline const RepeatedPtrField<Message>& Reflection::GetRepeatedPtrField( |
| 800 const Message& message, const FieldDescriptor* field) const { | 1116 const Message& message, const FieldDescriptor* field) const { |
| 801 return *static_cast<RepeatedPtrField<Message>* >( | 1117 return *static_cast<const RepeatedPtrField<Message>* >( |
| 802 MutableRawRepeatedField(const_cast<Message*>(&message), field, | 1118 GetRawRepeatedField(message, field, FieldDescriptor::CPPTYPE_MESSAGE, |
| 803 FieldDescriptor::CPPTYPE_MESSAGE, -1, | 1119 -1, NULL)); |
| 804 NULL)); | |
| 805 } | 1120 } |
| 806 | 1121 |
| 807 template<> | 1122 template<> |
| 808 inline RepeatedPtrField<Message>* Reflection::MutableRepeatedPtrField( | 1123 inline RepeatedPtrField<Message>* Reflection::MutableRepeatedPtrField( |
| 809 Message* message, const FieldDescriptor* field) const { | 1124 Message* message, const FieldDescriptor* field) const { |
| 810 return static_cast<RepeatedPtrField<Message>* >( | 1125 return static_cast<RepeatedPtrField<Message>* >( |
| 811 MutableRawRepeatedField(message, field, | 1126 MutableRawRepeatedField(message, field, |
| 812 FieldDescriptor::CPPTYPE_MESSAGE, -1, | 1127 FieldDescriptor::CPPTYPE_MESSAGE, -1, |
| 813 NULL)); | 1128 NULL)); |
| 814 } | 1129 } |
| 815 | 1130 |
| 816 template<typename PB> | 1131 template<typename PB> |
| 817 inline const RepeatedPtrField<PB>& Reflection::GetRepeatedPtrField( | 1132 inline const RepeatedPtrField<PB>& Reflection::GetRepeatedPtrField( |
| 818 const Message& message, const FieldDescriptor* field) const { | 1133 const Message& message, const FieldDescriptor* field) const { |
| 819 return *static_cast<RepeatedPtrField<PB>* >( | 1134 return *static_cast<const RepeatedPtrField<PB>* >( |
| 820 MutableRawRepeatedField(const_cast<Message*>(&message), field, | 1135 GetRawRepeatedField(message, field, FieldDescriptor::CPPTYPE_MESSAGE, |
| 821 FieldDescriptor::CPPTYPE_MESSAGE, -1, | 1136 -1, PB::default_instance().GetDescriptor())); |
| 822 PB::default_instance().GetDescriptor())); | |
| 823 } | 1137 } |
| 824 | 1138 |
| 825 template<typename PB> | 1139 template<typename PB> |
| 826 inline RepeatedPtrField<PB>* Reflection::MutableRepeatedPtrField( | 1140 inline RepeatedPtrField<PB>* Reflection::MutableRepeatedPtrField( |
| 827 Message* message, const FieldDescriptor* field) const { | 1141 Message* message, const FieldDescriptor* field) const { |
| 828 return static_cast<RepeatedPtrField<PB>* >( | 1142 return static_cast<RepeatedPtrField<PB>* >( |
| 829 MutableRawRepeatedField(message, field, | 1143 MutableRawRepeatedField(message, field, |
| 830 FieldDescriptor::CPPTYPE_MESSAGE, -1, | 1144 FieldDescriptor::CPPTYPE_MESSAGE, -1, |
| 831 PB::default_instance().GetDescriptor())); | 1145 PB::default_instance().GetDescriptor())); |
| 832 } | 1146 } |
| 833 | |
| 834 } // namespace protobuf | 1147 } // namespace protobuf |
| 835 | 1148 |
| 836 } // namespace google | 1149 } // namespace google |
| 837 #endif // GOOGLE_PROTOBUF_MESSAGE_H__ | 1150 #endif // GOOGLE_PROTOBUF_MESSAGE_H__ |
| OLD | NEW |