temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2 | // Copyright 2008 Google Inc. All rights reserved. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3 | // http://code.google.com/p/protobuf/ |
| 4 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 8 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 18 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 30 | |
| 31 | // Author: kenton@google.com (Kenton Varda) |
| 32 | // Based on original Protocol Buffers design by |
| 33 | // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 | |
| 35 | #include <algorithm> |
| 36 | #include <google/protobuf/generated_message_reflection.h> |
| 37 | #include <google/protobuf/descriptor.h> |
| 38 | #include <google/protobuf/descriptor.pb.h> |
| 39 | #include <google/protobuf/repeated_field.h> |
| 40 | #include <google/protobuf/extension_set.h> |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 41 | #include <google/protobuf/generated_message_util.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 42 | #include <google/protobuf/stubs/common.h> |
| 43 | |
| 44 | namespace google { |
| 45 | namespace protobuf { |
| 46 | namespace internal { |
| 47 | |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 48 | int StringSpaceUsedExcludingSelf(const string& str) { |
| 49 | const void* start = &str; |
| 50 | const void* end = &str + 1; |
| 51 | |
| 52 | if (start <= str.data() && str.data() <= end) { |
| 53 | // The string's data is stored inside the string object itself. |
| 54 | return 0; |
| 55 | } else { |
| 56 | return str.capacity(); |
| 57 | } |
| 58 | } |
| 59 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 60 | bool ParseNamedEnum(const EnumDescriptor* descriptor, |
| 61 | const string& name, |
| 62 | int* value) { |
| 63 | const EnumValueDescriptor* d = descriptor->FindValueByName(name); |
| 64 | if (d == NULL) return false; |
| 65 | *value = d->number(); |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | const string& NameOfEnum(const EnumDescriptor* descriptor, int value) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 70 | const EnumValueDescriptor* d = descriptor->FindValueByNumber(value); |
| 71 | return (d == NULL ? kEmptyString : d->name()); |
| 72 | } |
| 73 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 74 | // =================================================================== |
| 75 | // Helpers for reporting usage errors (e.g. trying to use GetInt32() on |
| 76 | // a string field). |
| 77 | |
| 78 | namespace { |
| 79 | |
| 80 | void ReportReflectionUsageError( |
| 81 | const Descriptor* descriptor, const FieldDescriptor* field, |
| 82 | const char* method, const char* description) { |
| 83 | GOOGLE_LOG(FATAL) |
| 84 | << "Protocol Buffer reflection usage error:\n" |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 85 | " Method : google::protobuf::Reflection::" << method << "\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 86 | " Message type: " << descriptor->full_name() << "\n" |
| 87 | " Field : " << field->full_name() << "\n" |
| 88 | " Problem : " << description; |
| 89 | } |
| 90 | |
| 91 | const char* cpptype_names_[FieldDescriptor::MAX_CPPTYPE + 1] = { |
| 92 | "INVALID_CPPTYPE", |
| 93 | "CPPTYPE_INT32", |
| 94 | "CPPTYPE_INT64", |
| 95 | "CPPTYPE_UINT32", |
| 96 | "CPPTYPE_UINT64", |
| 97 | "CPPTYPE_DOUBLE", |
| 98 | "CPPTYPE_FLOAT", |
| 99 | "CPPTYPE_BOOL", |
| 100 | "CPPTYPE_ENUM", |
| 101 | "CPPTYPE_STRING", |
| 102 | "CPPTYPE_MESSAGE" |
| 103 | }; |
| 104 | |
| 105 | static void ReportReflectionUsageTypeError( |
| 106 | const Descriptor* descriptor, const FieldDescriptor* field, |
| 107 | const char* method, |
| 108 | FieldDescriptor::CppType expected_type) { |
| 109 | GOOGLE_LOG(FATAL) |
| 110 | << "Protocol Buffer reflection usage error:\n" |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 111 | " Method : google::protobuf::Reflection::" << method << "\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 112 | " Message type: " << descriptor->full_name() << "\n" |
| 113 | " Field : " << field->full_name() << "\n" |
| 114 | " Problem : Field is not the right type for this message:\n" |
| 115 | " Expected : " << cpptype_names_[expected_type] << "\n" |
| 116 | " Field type: " << cpptype_names_[field->cpp_type()]; |
| 117 | } |
| 118 | |
| 119 | static void ReportReflectionUsageEnumTypeError( |
| 120 | const Descriptor* descriptor, const FieldDescriptor* field, |
| 121 | const char* method, const EnumValueDescriptor* value) { |
| 122 | GOOGLE_LOG(FATAL) |
| 123 | << "Protocol Buffer reflection usage error:\n" |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 124 | " Method : google::protobuf::Reflection::" << method << "\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 125 | " Message type: " << descriptor->full_name() << "\n" |
| 126 | " Field : " << field->full_name() << "\n" |
| 127 | " Problem : Enum value did not match field type:\n" |
| 128 | " Expected : " << field->enum_type()->full_name() << "\n" |
| 129 | " Actual : " << value->full_name(); |
| 130 | } |
| 131 | |
| 132 | #define USAGE_CHECK(CONDITION, METHOD, ERROR_DESCRIPTION) \ |
| 133 | if (!(CONDITION)) \ |
| 134 | ReportReflectionUsageError(descriptor_, field, #METHOD, ERROR_DESCRIPTION) |
| 135 | #define USAGE_CHECK_EQ(A, B, METHOD, ERROR_DESCRIPTION) \ |
| 136 | USAGE_CHECK((A) == (B), METHOD, ERROR_DESCRIPTION) |
| 137 | #define USAGE_CHECK_NE(A, B, METHOD, ERROR_DESCRIPTION) \ |
| 138 | USAGE_CHECK((A) != (B), METHOD, ERROR_DESCRIPTION) |
| 139 | |
| 140 | #define USAGE_CHECK_TYPE(METHOD, CPPTYPE) \ |
| 141 | if (field->cpp_type() != FieldDescriptor::CPPTYPE_##CPPTYPE) \ |
| 142 | ReportReflectionUsageTypeError(descriptor_, field, #METHOD, \ |
| 143 | FieldDescriptor::CPPTYPE_##CPPTYPE) |
| 144 | |
| 145 | #define USAGE_CHECK_ENUM_VALUE(METHOD) \ |
| 146 | if (value->type() != field->enum_type()) \ |
| 147 | ReportReflectionUsageEnumTypeError(descriptor_, field, #METHOD, value) |
| 148 | |
| 149 | #define USAGE_CHECK_MESSAGE_TYPE(METHOD) \ |
| 150 | USAGE_CHECK_EQ(field->containing_type(), descriptor_, \ |
| 151 | METHOD, "Field does not match message type."); |
| 152 | #define USAGE_CHECK_SINGULAR(METHOD) \ |
| 153 | USAGE_CHECK_NE(field->label(), FieldDescriptor::LABEL_REPEATED, METHOD, \ |
| 154 | "Field is repeated; the method requires a singular field.") |
| 155 | #define USAGE_CHECK_REPEATED(METHOD) \ |
| 156 | USAGE_CHECK_EQ(field->label(), FieldDescriptor::LABEL_REPEATED, METHOD, \ |
| 157 | "Field is singular; the method requires a repeated field.") |
| 158 | |
| 159 | #define USAGE_CHECK_ALL(METHOD, LABEL, CPPTYPE) \ |
| 160 | USAGE_CHECK_MESSAGE_TYPE(METHOD); \ |
| 161 | USAGE_CHECK_##LABEL(METHOD); \ |
| 162 | USAGE_CHECK_TYPE(METHOD, CPPTYPE) |
| 163 | |
| 164 | } // namespace |
| 165 | |
| 166 | // =================================================================== |
| 167 | |
| 168 | GeneratedMessageReflection::GeneratedMessageReflection( |
| 169 | const Descriptor* descriptor, |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 170 | const Message* default_instance, |
| 171 | const int offsets[], |
| 172 | int has_bits_offset, |
| 173 | int unknown_fields_offset, |
| 174 | int extensions_offset, |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 175 | const DescriptorPool* descriptor_pool, |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 176 | MessageFactory* factory, |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 177 | int object_size) |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 178 | : descriptor_ (descriptor), |
| 179 | default_instance_ (default_instance), |
| 180 | offsets_ (offsets), |
| 181 | has_bits_offset_ (has_bits_offset), |
| 182 | unknown_fields_offset_(unknown_fields_offset), |
| 183 | extensions_offset_(extensions_offset), |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 184 | object_size_ (object_size), |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 185 | descriptor_pool_ ((descriptor_pool == NULL) ? |
| 186 | DescriptorPool::generated_pool() : |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 187 | descriptor_pool), |
| 188 | message_factory_ (factory) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | GeneratedMessageReflection::~GeneratedMessageReflection() {} |
| 192 | |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 193 | const UnknownFieldSet& GeneratedMessageReflection::GetUnknownFields( |
| 194 | const Message& message) const { |
| 195 | const void* ptr = reinterpret_cast<const uint8*>(&message) + |
| 196 | unknown_fields_offset_; |
| 197 | return *reinterpret_cast<const UnknownFieldSet*>(ptr); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 198 | } |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 199 | UnknownFieldSet* GeneratedMessageReflection::MutableUnknownFields( |
| 200 | Message* message) const { |
| 201 | void* ptr = reinterpret_cast<uint8*>(message) + unknown_fields_offset_; |
| 202 | return reinterpret_cast<UnknownFieldSet*>(ptr); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 203 | } |
| 204 | |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 205 | int GeneratedMessageReflection::SpaceUsed(const Message& message) const { |
| 206 | // object_size_ already includes the in-memory representation of each field |
| 207 | // in the message, so we only need to account for additional memory used by |
| 208 | // the fields. |
| 209 | int total_size = object_size_; |
| 210 | |
| 211 | total_size += GetUnknownFields(message).SpaceUsedExcludingSelf(); |
| 212 | |
| 213 | if (extensions_offset_ != -1) { |
| 214 | total_size += GetExtensionSet(message).SpaceUsedExcludingSelf(); |
| 215 | } |
| 216 | |
| 217 | for (int i = 0; i < descriptor_->field_count(); i++) { |
| 218 | const FieldDescriptor* field = descriptor_->field(i); |
| 219 | |
| 220 | if (field->is_repeated()) { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 221 | switch (field->cpp_type()) { |
| 222 | #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ |
| 223 | case FieldDescriptor::CPPTYPE_##UPPERCASE : \ |
| 224 | total_size += GetRaw<RepeatedField<LOWERCASE> >(message, field) \ |
| 225 | .SpaceUsedExcludingSelf(); \ |
| 226 | break |
| 227 | |
| 228 | HANDLE_TYPE( INT32, int32); |
| 229 | HANDLE_TYPE( INT64, int64); |
| 230 | HANDLE_TYPE(UINT32, uint32); |
| 231 | HANDLE_TYPE(UINT64, uint64); |
| 232 | HANDLE_TYPE(DOUBLE, double); |
| 233 | HANDLE_TYPE( FLOAT, float); |
| 234 | HANDLE_TYPE( BOOL, bool); |
| 235 | HANDLE_TYPE( ENUM, int); |
| 236 | #undef HANDLE_TYPE |
| 237 | |
| 238 | case FieldDescriptor::CPPTYPE_STRING: |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 239 | switch (field->options().ctype()) { |
| 240 | default: // TODO(kenton): Support other string reps. |
| 241 | case FieldOptions::STRING: |
| 242 | total_size += GetRaw<RepeatedPtrField<string> >(message, field) |
| 243 | .SpaceUsedExcludingSelf(); |
| 244 | break; |
| 245 | } |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 246 | break; |
| 247 | |
| 248 | case FieldDescriptor::CPPTYPE_MESSAGE: |
| 249 | // We don't know which subclass of RepeatedPtrFieldBase the type is, |
| 250 | // so we use RepeatedPtrFieldBase directly. |
| 251 | total_size += |
| 252 | GetRaw<RepeatedPtrFieldBase>(message, field) |
| 253 | .SpaceUsedExcludingSelf<GenericTypeHandler<Message> >(); |
| 254 | break; |
| 255 | } |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 256 | } else { |
| 257 | switch (field->cpp_type()) { |
| 258 | case FieldDescriptor::CPPTYPE_INT32 : |
| 259 | case FieldDescriptor::CPPTYPE_INT64 : |
| 260 | case FieldDescriptor::CPPTYPE_UINT32: |
| 261 | case FieldDescriptor::CPPTYPE_UINT64: |
| 262 | case FieldDescriptor::CPPTYPE_DOUBLE: |
| 263 | case FieldDescriptor::CPPTYPE_FLOAT : |
| 264 | case FieldDescriptor::CPPTYPE_BOOL : |
| 265 | case FieldDescriptor::CPPTYPE_ENUM : |
| 266 | // Field is inline, so we've already counted it. |
| 267 | break; |
| 268 | |
| 269 | case FieldDescriptor::CPPTYPE_STRING: { |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 270 | switch (field->options().ctype()) { |
| 271 | default: // TODO(kenton): Support other string reps. |
| 272 | case FieldOptions::STRING: { |
| 273 | const string* ptr = GetField<const string*>(message, field); |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 274 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 275 | // Initially, the string points to the default value stored in |
| 276 | // the prototype. Only count the string if it has been changed |
| 277 | // from the default value. |
| 278 | const string* default_ptr = DefaultRaw<const string*>(field); |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 279 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 280 | if (ptr != default_ptr) { |
| 281 | // string fields are represented by just a pointer, so also |
| 282 | // include sizeof(string) as well. |
| 283 | total_size += sizeof(*ptr) + StringSpaceUsedExcludingSelf(*ptr); |
| 284 | } |
| 285 | break; |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 286 | } |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 287 | } |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 288 | break; |
| 289 | } |
| 290 | |
| 291 | case FieldDescriptor::CPPTYPE_MESSAGE: |
| 292 | if (&message == default_instance_) { |
| 293 | // For singular fields, the prototype just stores a pointer to the |
| 294 | // external type's prototype, so there is no extra memory usage. |
| 295 | } else { |
| 296 | const Message* sub_message = GetRaw<const Message*>(message, field); |
| 297 | if (sub_message != NULL) { |
| 298 | total_size += sub_message->SpaceUsed(); |
| 299 | } |
| 300 | } |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | return total_size; |
| 307 | } |
| 308 | |
kenton@google.com | ceb561d | 2009-06-25 19:05:36 +0000 | [diff] [blame] | 309 | void GeneratedMessageReflection::Swap( |
| 310 | Message* message1, |
| 311 | Message* message2) const { |
| 312 | if (message1 == message2) return; |
| 313 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 314 | // TODO(kenton): Other Reflection methods should probably check this too. |
kenton@google.com | ceb561d | 2009-06-25 19:05:36 +0000 | [diff] [blame] | 315 | GOOGLE_CHECK_EQ(message1->GetReflection(), this) |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 316 | << "First argument to Swap() (of type \"" |
| 317 | << message1->GetDescriptor()->full_name() |
| 318 | << "\") is not compatible with this reflection object (which is for type \"" |
| 319 | << descriptor_->full_name() |
| 320 | << "\"). Note that the exact same class is required; not just the same " |
| 321 | "descriptor."; |
kenton@google.com | ceb561d | 2009-06-25 19:05:36 +0000 | [diff] [blame] | 322 | GOOGLE_CHECK_EQ(message2->GetReflection(), this) |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 323 | << "Second argument to Swap() (of type \"" |
| 324 | << message1->GetDescriptor()->full_name() |
| 325 | << "\") is not compatible with this reflection object (which is for type \"" |
| 326 | << descriptor_->full_name() |
| 327 | << "\"). Note that the exact same class is required; not just the same " |
| 328 | "descriptor."; |
kenton@google.com | ceb561d | 2009-06-25 19:05:36 +0000 | [diff] [blame] | 329 | |
| 330 | uint32* has_bits1 = MutableHasBits(message1); |
| 331 | uint32* has_bits2 = MutableHasBits(message2); |
| 332 | int has_bits_size = (descriptor_->field_count() + 31) / 32; |
| 333 | |
| 334 | for (int i = 0; i < has_bits_size; i++) { |
kenton@google.com | 7fb9ae9 | 2009-09-02 02:42:56 +0000 | [diff] [blame] | 335 | std::swap(has_bits1[i], has_bits2[i]); |
kenton@google.com | ceb561d | 2009-06-25 19:05:36 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | for (int i = 0; i < descriptor_->field_count(); i++) { |
| 339 | const FieldDescriptor* field = descriptor_->field(i); |
| 340 | if (field->is_repeated()) { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 341 | switch (field->cpp_type()) { |
| 342 | #define SWAP_ARRAYS(CPPTYPE, TYPE) \ |
| 343 | case FieldDescriptor::CPPTYPE_##CPPTYPE: \ |
| 344 | MutableRaw<RepeatedField<TYPE> >(message1, field)->Swap( \ |
| 345 | MutableRaw<RepeatedField<TYPE> >(message2, field)); \ |
| 346 | break; |
| 347 | |
| 348 | SWAP_ARRAYS(INT32 , int32 ); |
| 349 | SWAP_ARRAYS(INT64 , int64 ); |
| 350 | SWAP_ARRAYS(UINT32, uint32); |
| 351 | SWAP_ARRAYS(UINT64, uint64); |
| 352 | SWAP_ARRAYS(FLOAT , float ); |
| 353 | SWAP_ARRAYS(DOUBLE, double); |
| 354 | SWAP_ARRAYS(BOOL , bool ); |
| 355 | SWAP_ARRAYS(ENUM , int ); |
| 356 | #undef SWAP_ARRAYS |
| 357 | |
| 358 | case FieldDescriptor::CPPTYPE_STRING: |
| 359 | case FieldDescriptor::CPPTYPE_MESSAGE: |
| 360 | MutableRaw<RepeatedPtrFieldBase>(message1, field)->Swap( |
| 361 | MutableRaw<RepeatedPtrFieldBase>(message2, field)); |
| 362 | break; |
| 363 | |
| 364 | default: |
| 365 | GOOGLE_LOG(FATAL) << "Unimplemented type: " << field->cpp_type(); |
| 366 | } |
kenton@google.com | ceb561d | 2009-06-25 19:05:36 +0000 | [diff] [blame] | 367 | } else { |
| 368 | switch (field->cpp_type()) { |
| 369 | #define SWAP_VALUES(CPPTYPE, TYPE) \ |
| 370 | case FieldDescriptor::CPPTYPE_##CPPTYPE: \ |
kenton@google.com | 7fb9ae9 | 2009-09-02 02:42:56 +0000 | [diff] [blame] | 371 | std::swap(*MutableRaw<TYPE>(message1, field), \ |
| 372 | *MutableRaw<TYPE>(message2, field)); \ |
kenton@google.com | ceb561d | 2009-06-25 19:05:36 +0000 | [diff] [blame] | 373 | break; |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 374 | |
kenton@google.com | ceb561d | 2009-06-25 19:05:36 +0000 | [diff] [blame] | 375 | SWAP_VALUES(INT32 , int32 ); |
| 376 | SWAP_VALUES(INT64 , int64 ); |
| 377 | SWAP_VALUES(UINT32, uint32); |
| 378 | SWAP_VALUES(UINT64, uint64); |
| 379 | SWAP_VALUES(FLOAT , float ); |
| 380 | SWAP_VALUES(DOUBLE, double); |
| 381 | SWAP_VALUES(BOOL , bool ); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 382 | SWAP_VALUES(ENUM , int ); |
kenton@google.com | ceb561d | 2009-06-25 19:05:36 +0000 | [diff] [blame] | 383 | SWAP_VALUES(MESSAGE, Message*); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 384 | #undef SWAP_VALUES |
| 385 | |
| 386 | case FieldDescriptor::CPPTYPE_STRING: |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 387 | switch (field->options().ctype()) { |
| 388 | default: // TODO(kenton): Support other string reps. |
| 389 | case FieldOptions::STRING: |
| 390 | std::swap(*MutableRaw<string*>(message1, field), |
| 391 | *MutableRaw<string*>(message2, field)); |
| 392 | break; |
| 393 | } |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 394 | break; |
| 395 | |
kenton@google.com | ceb561d | 2009-06-25 19:05:36 +0000 | [diff] [blame] | 396 | default: |
| 397 | GOOGLE_LOG(FATAL) << "Unimplemented type: " << field->cpp_type(); |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | if (extensions_offset_ != -1) { |
| 403 | MutableExtensionSet(message1)->Swap(MutableExtensionSet(message2)); |
| 404 | } |
| 405 | |
| 406 | MutableUnknownFields(message1)->Swap(MutableUnknownFields(message2)); |
| 407 | } |
| 408 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 409 | // ------------------------------------------------------------------- |
| 410 | |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 411 | bool GeneratedMessageReflection::HasField(const Message& message, |
| 412 | const FieldDescriptor* field) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 413 | USAGE_CHECK_MESSAGE_TYPE(HasField); |
| 414 | USAGE_CHECK_SINGULAR(HasField); |
| 415 | |
| 416 | if (field->is_extension()) { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 417 | return GetExtensionSet(message).Has(field->number()); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 418 | } else { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 419 | return HasBit(message, field); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 423 | int GeneratedMessageReflection::FieldSize(const Message& message, |
| 424 | const FieldDescriptor* field) const { |
kenton@google.com | ceb561d | 2009-06-25 19:05:36 +0000 | [diff] [blame] | 425 | USAGE_CHECK_MESSAGE_TYPE(FieldSize); |
| 426 | USAGE_CHECK_REPEATED(FieldSize); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 427 | |
| 428 | if (field->is_extension()) { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 429 | return GetExtensionSet(message).ExtensionSize(field->number()); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 430 | } else { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 431 | switch (field->cpp_type()) { |
| 432 | #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ |
| 433 | case FieldDescriptor::CPPTYPE_##UPPERCASE : \ |
| 434 | return GetRaw<RepeatedField<LOWERCASE> >(message, field).size() |
| 435 | |
| 436 | HANDLE_TYPE( INT32, int32); |
| 437 | HANDLE_TYPE( INT64, int64); |
| 438 | HANDLE_TYPE(UINT32, uint32); |
| 439 | HANDLE_TYPE(UINT64, uint64); |
| 440 | HANDLE_TYPE(DOUBLE, double); |
| 441 | HANDLE_TYPE( FLOAT, float); |
| 442 | HANDLE_TYPE( BOOL, bool); |
| 443 | HANDLE_TYPE( ENUM, int); |
| 444 | #undef HANDLE_TYPE |
| 445 | |
| 446 | case FieldDescriptor::CPPTYPE_STRING: |
| 447 | case FieldDescriptor::CPPTYPE_MESSAGE: |
| 448 | return GetRaw<RepeatedPtrFieldBase>(message, field).size(); |
| 449 | } |
| 450 | |
| 451 | GOOGLE_LOG(FATAL) << "Can't get here."; |
| 452 | return 0; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 456 | void GeneratedMessageReflection::ClearField( |
| 457 | Message* message, const FieldDescriptor* field) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 458 | USAGE_CHECK_MESSAGE_TYPE(ClearField); |
| 459 | |
| 460 | if (field->is_extension()) { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 461 | MutableExtensionSet(message)->ClearExtension(field->number()); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 462 | } else if (!field->is_repeated()) { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 463 | if (HasBit(*message, field)) { |
| 464 | ClearBit(message, field); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 465 | |
| 466 | // We need to set the field back to its default value. |
| 467 | switch (field->cpp_type()) { |
| 468 | #define CLEAR_TYPE(CPPTYPE, TYPE) \ |
| 469 | case FieldDescriptor::CPPTYPE_##CPPTYPE: \ |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 470 | *MutableRaw<TYPE>(message, field) = \ |
| 471 | field->default_value_##TYPE(); \ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 472 | break; |
| 473 | |
| 474 | CLEAR_TYPE(INT32 , int32 ); |
| 475 | CLEAR_TYPE(INT64 , int64 ); |
| 476 | CLEAR_TYPE(UINT32, uint32); |
| 477 | CLEAR_TYPE(UINT64, uint64); |
| 478 | CLEAR_TYPE(FLOAT , float ); |
| 479 | CLEAR_TYPE(DOUBLE, double); |
| 480 | CLEAR_TYPE(BOOL , bool ); |
| 481 | #undef CLEAR_TYPE |
| 482 | |
| 483 | case FieldDescriptor::CPPTYPE_ENUM: |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 484 | *MutableRaw<int>(message, field) = |
| 485 | field->default_value_enum()->number(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 486 | break; |
| 487 | |
| 488 | case FieldDescriptor::CPPTYPE_STRING: { |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 489 | switch (field->options().ctype()) { |
| 490 | default: // TODO(kenton): Support other string reps. |
| 491 | case FieldOptions::STRING: |
| 492 | const string* default_ptr = DefaultRaw<const string*>(field); |
| 493 | string** value = MutableRaw<string*>(message, field); |
| 494 | if (*value != default_ptr) { |
| 495 | if (field->has_default_value()) { |
| 496 | (*value)->assign(field->default_value_string()); |
| 497 | } else { |
| 498 | (*value)->clear(); |
| 499 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 500 | } |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 501 | break; |
| 502 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 503 | break; |
| 504 | } |
| 505 | |
| 506 | case FieldDescriptor::CPPTYPE_MESSAGE: |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 507 | (*MutableRaw<Message*>(message, field))->Clear(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 508 | break; |
| 509 | } |
| 510 | } |
| 511 | } else { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 512 | switch (field->cpp_type()) { |
| 513 | #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ |
| 514 | case FieldDescriptor::CPPTYPE_##UPPERCASE : \ |
| 515 | MutableRaw<RepeatedField<LOWERCASE> >(message, field)->Clear(); \ |
| 516 | break |
| 517 | |
| 518 | HANDLE_TYPE( INT32, int32); |
| 519 | HANDLE_TYPE( INT64, int64); |
| 520 | HANDLE_TYPE(UINT32, uint32); |
| 521 | HANDLE_TYPE(UINT64, uint64); |
| 522 | HANDLE_TYPE(DOUBLE, double); |
| 523 | HANDLE_TYPE( FLOAT, float); |
| 524 | HANDLE_TYPE( BOOL, bool); |
| 525 | HANDLE_TYPE( ENUM, int); |
| 526 | #undef HANDLE_TYPE |
| 527 | |
| 528 | case FieldDescriptor::CPPTYPE_STRING: { |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 529 | switch (field->options().ctype()) { |
| 530 | default: // TODO(kenton): Support other string reps. |
| 531 | case FieldOptions::STRING: |
| 532 | MutableRaw<RepeatedPtrField<string> >(message, field)->Clear(); |
| 533 | break; |
| 534 | } |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 535 | break; |
| 536 | } |
| 537 | |
| 538 | case FieldDescriptor::CPPTYPE_MESSAGE: { |
| 539 | // We don't know which subclass of RepeatedPtrFieldBase the type is, |
| 540 | // so we use RepeatedPtrFieldBase directly. |
| 541 | MutableRaw<RepeatedPtrFieldBase>(message, field) |
| 542 | ->Clear<GenericTypeHandler<Message> >(); |
| 543 | break; |
| 544 | } |
| 545 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
kenton@google.com | ceb561d | 2009-06-25 19:05:36 +0000 | [diff] [blame] | 549 | void GeneratedMessageReflection::RemoveLast( |
| 550 | Message* message, |
| 551 | const FieldDescriptor* field) const { |
| 552 | USAGE_CHECK_MESSAGE_TYPE(RemoveLast); |
| 553 | USAGE_CHECK_REPEATED(RemoveLast); |
| 554 | |
| 555 | if (field->is_extension()) { |
| 556 | MutableExtensionSet(message)->RemoveLast(field->number()); |
| 557 | } else { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 558 | switch (field->cpp_type()) { |
| 559 | #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ |
| 560 | case FieldDescriptor::CPPTYPE_##UPPERCASE : \ |
| 561 | MutableRaw<RepeatedField<LOWERCASE> >(message, field)->RemoveLast(); \ |
| 562 | break |
| 563 | |
| 564 | HANDLE_TYPE( INT32, int32); |
| 565 | HANDLE_TYPE( INT64, int64); |
| 566 | HANDLE_TYPE(UINT32, uint32); |
| 567 | HANDLE_TYPE(UINT64, uint64); |
| 568 | HANDLE_TYPE(DOUBLE, double); |
| 569 | HANDLE_TYPE( FLOAT, float); |
| 570 | HANDLE_TYPE( BOOL, bool); |
| 571 | HANDLE_TYPE( ENUM, int); |
| 572 | #undef HANDLE_TYPE |
| 573 | |
| 574 | case FieldDescriptor::CPPTYPE_STRING: |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 575 | switch (field->options().ctype()) { |
| 576 | default: // TODO(kenton): Support other string reps. |
| 577 | case FieldOptions::STRING: |
| 578 | MutableRaw<RepeatedPtrField<string> >(message, field)->RemoveLast(); |
| 579 | break; |
| 580 | } |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 581 | break; |
| 582 | |
| 583 | case FieldDescriptor::CPPTYPE_MESSAGE: |
| 584 | MutableRaw<RepeatedPtrFieldBase>(message, field) |
| 585 | ->RemoveLast<GenericTypeHandler<Message> >(); |
| 586 | break; |
| 587 | } |
kenton@google.com | ceb561d | 2009-06-25 19:05:36 +0000 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | |
| 591 | void GeneratedMessageReflection::SwapElements( |
| 592 | Message* message, |
| 593 | const FieldDescriptor* field, |
| 594 | int index1, |
| 595 | int index2) const { |
| 596 | USAGE_CHECK_MESSAGE_TYPE(Swap); |
| 597 | USAGE_CHECK_REPEATED(Swap); |
| 598 | |
| 599 | if (field->is_extension()) { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 600 | MutableExtensionSet(message)->SwapElements(field->number(), index1, index2); |
kenton@google.com | ceb561d | 2009-06-25 19:05:36 +0000 | [diff] [blame] | 601 | } else { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 602 | switch (field->cpp_type()) { |
| 603 | #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ |
| 604 | case FieldDescriptor::CPPTYPE_##UPPERCASE : \ |
| 605 | MutableRaw<RepeatedField<LOWERCASE> >(message, field) \ |
| 606 | ->SwapElements(index1, index2); \ |
| 607 | break |
| 608 | |
| 609 | HANDLE_TYPE( INT32, int32); |
| 610 | HANDLE_TYPE( INT64, int64); |
| 611 | HANDLE_TYPE(UINT32, uint32); |
| 612 | HANDLE_TYPE(UINT64, uint64); |
| 613 | HANDLE_TYPE(DOUBLE, double); |
| 614 | HANDLE_TYPE( FLOAT, float); |
| 615 | HANDLE_TYPE( BOOL, bool); |
| 616 | HANDLE_TYPE( ENUM, int); |
| 617 | #undef HANDLE_TYPE |
| 618 | |
| 619 | case FieldDescriptor::CPPTYPE_STRING: |
| 620 | case FieldDescriptor::CPPTYPE_MESSAGE: |
| 621 | MutableRaw<RepeatedPtrFieldBase>(message, field) |
| 622 | ->SwapElements(index1, index2); |
| 623 | break; |
| 624 | } |
kenton@google.com | ceb561d | 2009-06-25 19:05:36 +0000 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 628 | namespace { |
| 629 | // Comparison functor for sorting FieldDescriptors by field number. |
| 630 | struct FieldNumberSorter { |
| 631 | bool operator()(const FieldDescriptor* left, |
| 632 | const FieldDescriptor* right) const { |
| 633 | return left->number() < right->number(); |
| 634 | } |
| 635 | }; |
| 636 | } // namespace |
| 637 | |
| 638 | void GeneratedMessageReflection::ListFields( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 639 | const Message& message, |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 640 | vector<const FieldDescriptor*>* output) const { |
| 641 | output->clear(); |
| 642 | |
| 643 | // Optimization: The default instance never has any fields set. |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 644 | if (&message == default_instance_) return; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 645 | |
| 646 | for (int i = 0; i < descriptor_->field_count(); i++) { |
| 647 | const FieldDescriptor* field = descriptor_->field(i); |
| 648 | if (field->is_repeated()) { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 649 | if (FieldSize(message, field) > 0) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 650 | output->push_back(field); |
| 651 | } |
| 652 | } else { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 653 | if (HasBit(message, field)) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 654 | output->push_back(field); |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 659 | if (extensions_offset_ != -1) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 660 | GetExtensionSet(message).AppendToList(descriptor_, descriptor_pool_, |
| 661 | output); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | // ListFields() must sort output by field number. |
| 665 | sort(output->begin(), output->end(), FieldNumberSorter()); |
| 666 | } |
| 667 | |
| 668 | // ------------------------------------------------------------------- |
| 669 | |
| 670 | #undef DEFINE_PRIMITIVE_ACCESSORS |
| 671 | #define DEFINE_PRIMITIVE_ACCESSORS(TYPENAME, TYPE, PASSTYPE, CPPTYPE) \ |
| 672 | PASSTYPE GeneratedMessageReflection::Get##TYPENAME( \ |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 673 | const Message& message, const FieldDescriptor* field) const { \ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 674 | USAGE_CHECK_ALL(Get##TYPENAME, SINGULAR, CPPTYPE); \ |
| 675 | if (field->is_extension()) { \ |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 676 | return GetExtensionSet(message).Get##TYPENAME( \ |
| 677 | field->number(), field->default_value_##PASSTYPE()); \ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 678 | } else { \ |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 679 | return GetField<TYPE>(message, field); \ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 680 | } \ |
| 681 | } \ |
| 682 | \ |
| 683 | void GeneratedMessageReflection::Set##TYPENAME( \ |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 684 | Message* message, const FieldDescriptor* field, \ |
| 685 | PASSTYPE value) const { \ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 686 | USAGE_CHECK_ALL(Set##TYPENAME, SINGULAR, CPPTYPE); \ |
| 687 | if (field->is_extension()) { \ |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 688 | return MutableExtensionSet(message)->Set##TYPENAME( \ |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 689 | field->number(), field->type(), value, field); \ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 690 | } else { \ |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 691 | SetField<TYPE>(message, field, value); \ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 692 | } \ |
| 693 | } \ |
| 694 | \ |
| 695 | PASSTYPE GeneratedMessageReflection::GetRepeated##TYPENAME( \ |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 696 | const Message& message, \ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 697 | const FieldDescriptor* field, int index) const { \ |
| 698 | USAGE_CHECK_ALL(GetRepeated##TYPENAME, REPEATED, CPPTYPE); \ |
| 699 | if (field->is_extension()) { \ |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 700 | return GetExtensionSet(message).GetRepeated##TYPENAME( \ |
| 701 | field->number(), index); \ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 702 | } else { \ |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 703 | return GetRepeatedField<TYPE>(message, field, index); \ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 704 | } \ |
| 705 | } \ |
| 706 | \ |
| 707 | void GeneratedMessageReflection::SetRepeated##TYPENAME( \ |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 708 | Message* message, const FieldDescriptor* field, \ |
| 709 | int index, PASSTYPE value) const { \ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 710 | USAGE_CHECK_ALL(SetRepeated##TYPENAME, REPEATED, CPPTYPE); \ |
| 711 | if (field->is_extension()) { \ |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 712 | MutableExtensionSet(message)->SetRepeated##TYPENAME( \ |
| 713 | field->number(), index, value); \ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 714 | } else { \ |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 715 | SetRepeatedField<TYPE>(message, field, index, value); \ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 716 | } \ |
| 717 | } \ |
| 718 | \ |
| 719 | void GeneratedMessageReflection::Add##TYPENAME( \ |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 720 | Message* message, const FieldDescriptor* field, \ |
| 721 | PASSTYPE value) const { \ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 722 | USAGE_CHECK_ALL(Add##TYPENAME, REPEATED, CPPTYPE); \ |
| 723 | if (field->is_extension()) { \ |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 724 | MutableExtensionSet(message)->Add##TYPENAME( \ |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 725 | field->number(), field->type(), field->options().packed(), value, \ |
| 726 | field); \ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 727 | } else { \ |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 728 | AddField<TYPE>(message, field, value); \ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 729 | } \ |
| 730 | } |
| 731 | |
| 732 | DEFINE_PRIMITIVE_ACCESSORS(Int32 , int32 , int32 , INT32 ) |
| 733 | DEFINE_PRIMITIVE_ACCESSORS(Int64 , int64 , int64 , INT64 ) |
| 734 | DEFINE_PRIMITIVE_ACCESSORS(UInt32, uint32, uint32, UINT32) |
| 735 | DEFINE_PRIMITIVE_ACCESSORS(UInt64, uint64, uint64, UINT64) |
| 736 | DEFINE_PRIMITIVE_ACCESSORS(Float , float , float , FLOAT ) |
| 737 | DEFINE_PRIMITIVE_ACCESSORS(Double, double, double, DOUBLE) |
| 738 | DEFINE_PRIMITIVE_ACCESSORS(Bool , bool , bool , BOOL ) |
| 739 | #undef DEFINE_PRIMITIVE_ACCESSORS |
| 740 | |
| 741 | // ------------------------------------------------------------------- |
| 742 | |
| 743 | string GeneratedMessageReflection::GetString( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 744 | const Message& message, const FieldDescriptor* field) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 745 | USAGE_CHECK_ALL(GetString, SINGULAR, STRING); |
| 746 | if (field->is_extension()) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 747 | return GetExtensionSet(message).GetString(field->number(), |
| 748 | field->default_value_string()); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 749 | } else { |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 750 | switch (field->options().ctype()) { |
| 751 | default: // TODO(kenton): Support other string reps. |
| 752 | case FieldOptions::STRING: |
| 753 | return *GetField<const string*>(message, field); |
| 754 | } |
| 755 | |
| 756 | GOOGLE_LOG(FATAL) << "Can't get here."; |
| 757 | return kEmptyString; // Make compiler happy. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | |
| 761 | const string& GeneratedMessageReflection::GetStringReference( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 762 | const Message& message, |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 763 | const FieldDescriptor* field, string* scratch) const { |
| 764 | USAGE_CHECK_ALL(GetStringReference, SINGULAR, STRING); |
| 765 | if (field->is_extension()) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 766 | return GetExtensionSet(message).GetString(field->number(), |
| 767 | field->default_value_string()); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 768 | } else { |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 769 | switch (field->options().ctype()) { |
| 770 | default: // TODO(kenton): Support other string reps. |
| 771 | case FieldOptions::STRING: |
| 772 | return *GetField<const string*>(message, field); |
| 773 | } |
| 774 | |
| 775 | GOOGLE_LOG(FATAL) << "Can't get here."; |
| 776 | return kEmptyString; // Make compiler happy. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | |
| 780 | |
| 781 | void GeneratedMessageReflection::SetString( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 782 | Message* message, const FieldDescriptor* field, |
| 783 | const string& value) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 784 | USAGE_CHECK_ALL(SetString, SINGULAR, STRING); |
| 785 | if (field->is_extension()) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 786 | return MutableExtensionSet(message)->SetString(field->number(), |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 787 | field->type(), value, field); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 788 | } else { |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 789 | switch (field->options().ctype()) { |
| 790 | default: // TODO(kenton): Support other string reps. |
| 791 | case FieldOptions::STRING: { |
| 792 | string** ptr = MutableField<string*>(message, field); |
| 793 | if (*ptr == DefaultRaw<const string*>(field)) { |
| 794 | *ptr = new string(value); |
| 795 | } else { |
| 796 | (*ptr)->assign(value); |
| 797 | } |
| 798 | break; |
| 799 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | |
| 805 | string GeneratedMessageReflection::GetRepeatedString( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 806 | const Message& message, const FieldDescriptor* field, int index) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 807 | USAGE_CHECK_ALL(GetRepeatedString, REPEATED, STRING); |
| 808 | if (field->is_extension()) { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 809 | return GetExtensionSet(message).GetRepeatedString(field->number(), index); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 810 | } else { |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 811 | switch (field->options().ctype()) { |
| 812 | default: // TODO(kenton): Support other string reps. |
| 813 | case FieldOptions::STRING: |
| 814 | return GetRepeatedPtrField<string>(message, field, index); |
| 815 | } |
| 816 | |
| 817 | GOOGLE_LOG(FATAL) << "Can't get here."; |
| 818 | return kEmptyString; // Make compiler happy. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | |
| 822 | const string& GeneratedMessageReflection::GetRepeatedStringReference( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 823 | const Message& message, const FieldDescriptor* field, |
| 824 | int index, string* scratch) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 825 | USAGE_CHECK_ALL(GetRepeatedStringReference, REPEATED, STRING); |
| 826 | if (field->is_extension()) { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 827 | return GetExtensionSet(message).GetRepeatedString(field->number(), index); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 828 | } else { |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 829 | switch (field->options().ctype()) { |
| 830 | default: // TODO(kenton): Support other string reps. |
| 831 | case FieldOptions::STRING: |
| 832 | return GetRepeatedPtrField<string>(message, field, index); |
| 833 | } |
| 834 | |
| 835 | GOOGLE_LOG(FATAL) << "Can't get here."; |
| 836 | return kEmptyString; // Make compiler happy. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
| 840 | |
| 841 | void GeneratedMessageReflection::SetRepeatedString( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 842 | Message* message, const FieldDescriptor* field, |
| 843 | int index, const string& value) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 844 | USAGE_CHECK_ALL(SetRepeatedString, REPEATED, STRING); |
| 845 | if (field->is_extension()) { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 846 | MutableExtensionSet(message)->SetRepeatedString( |
| 847 | field->number(), index, value); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 848 | } else { |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 849 | switch (field->options().ctype()) { |
| 850 | default: // TODO(kenton): Support other string reps. |
| 851 | case FieldOptions::STRING: |
| 852 | *MutableRepeatedField<string>(message, field, index) = value; |
| 853 | break; |
| 854 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 855 | } |
| 856 | } |
| 857 | |
| 858 | |
| 859 | void GeneratedMessageReflection::AddString( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 860 | Message* message, const FieldDescriptor* field, |
| 861 | const string& value) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 862 | USAGE_CHECK_ALL(AddString, REPEATED, STRING); |
| 863 | if (field->is_extension()) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 864 | MutableExtensionSet(message)->AddString(field->number(), |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 865 | field->type(), value, field); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 866 | } else { |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 867 | switch (field->options().ctype()) { |
| 868 | default: // TODO(kenton): Support other string reps. |
| 869 | case FieldOptions::STRING: |
| 870 | *AddField<string>(message, field) = value; |
| 871 | break; |
| 872 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | |
| 876 | |
| 877 | // ------------------------------------------------------------------- |
| 878 | |
| 879 | const EnumValueDescriptor* GeneratedMessageReflection::GetEnum( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 880 | const Message& message, const FieldDescriptor* field) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 881 | USAGE_CHECK_ALL(GetEnum, SINGULAR, ENUM); |
| 882 | |
| 883 | int value; |
| 884 | if (field->is_extension()) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 885 | value = GetExtensionSet(message).GetEnum( |
| 886 | field->number(), field->default_value_enum()->number()); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 887 | } else { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 888 | value = GetField<int>(message, field); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 889 | } |
| 890 | const EnumValueDescriptor* result = |
| 891 | field->enum_type()->FindValueByNumber(value); |
| 892 | GOOGLE_CHECK(result != NULL); |
| 893 | return result; |
| 894 | } |
| 895 | |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 896 | void GeneratedMessageReflection::SetEnum( |
| 897 | Message* message, const FieldDescriptor* field, |
| 898 | const EnumValueDescriptor* value) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 899 | USAGE_CHECK_ALL(SetEnum, SINGULAR, ENUM); |
| 900 | USAGE_CHECK_ENUM_VALUE(SetEnum); |
| 901 | |
| 902 | if (field->is_extension()) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 903 | MutableExtensionSet(message)->SetEnum(field->number(), field->type(), |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 904 | value->number(), field); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 905 | } else { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 906 | SetField<int>(message, field, value->number()); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
| 910 | const EnumValueDescriptor* GeneratedMessageReflection::GetRepeatedEnum( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 911 | const Message& message, const FieldDescriptor* field, int index) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 912 | USAGE_CHECK_ALL(GetRepeatedEnum, REPEATED, ENUM); |
| 913 | |
| 914 | int value; |
| 915 | if (field->is_extension()) { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 916 | value = GetExtensionSet(message).GetRepeatedEnum(field->number(), index); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 917 | } else { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 918 | value = GetRepeatedField<int>(message, field, index); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 919 | } |
| 920 | const EnumValueDescriptor* result = |
| 921 | field->enum_type()->FindValueByNumber(value); |
| 922 | GOOGLE_CHECK(result != NULL); |
| 923 | return result; |
| 924 | } |
| 925 | |
| 926 | void GeneratedMessageReflection::SetRepeatedEnum( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 927 | Message* message, |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 928 | const FieldDescriptor* field, int index, |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 929 | const EnumValueDescriptor* value) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 930 | USAGE_CHECK_ALL(SetRepeatedEnum, REPEATED, ENUM); |
| 931 | USAGE_CHECK_ENUM_VALUE(SetRepeatedEnum); |
| 932 | |
| 933 | if (field->is_extension()) { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 934 | MutableExtensionSet(message)->SetRepeatedEnum( |
| 935 | field->number(), index, value->number()); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 936 | } else { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 937 | SetRepeatedField<int>(message, field, index, value->number()); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 938 | } |
| 939 | } |
| 940 | |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 941 | void GeneratedMessageReflection::AddEnum( |
| 942 | Message* message, const FieldDescriptor* field, |
| 943 | const EnumValueDescriptor* value) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 944 | USAGE_CHECK_ALL(AddEnum, REPEATED, ENUM); |
| 945 | USAGE_CHECK_ENUM_VALUE(AddEnum); |
| 946 | |
| 947 | if (field->is_extension()) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 948 | MutableExtensionSet(message)->AddEnum(field->number(), field->type(), |
| 949 | field->options().packed(), |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 950 | value->number(), field); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 951 | } else { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 952 | AddField<int>(message, field, value->number()); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | |
| 956 | // ------------------------------------------------------------------- |
| 957 | |
| 958 | const Message& GeneratedMessageReflection::GetMessage( |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 959 | const Message& message, const FieldDescriptor* field, |
| 960 | MessageFactory* factory) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 961 | USAGE_CHECK_ALL(GetMessage, SINGULAR, MESSAGE); |
| 962 | |
| 963 | if (field->is_extension()) { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 964 | return static_cast<const Message&>( |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 965 | GetExtensionSet(message).GetMessage( |
| 966 | field->number(), field->message_type(), |
| 967 | factory == NULL ? message_factory_ : factory)); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 968 | } else { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 969 | const Message* result = GetRaw<const Message*>(message, field); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 970 | if (result == NULL) { |
| 971 | result = DefaultRaw<const Message*>(field); |
| 972 | } |
| 973 | return *result; |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | Message* GeneratedMessageReflection::MutableMessage( |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 978 | Message* message, const FieldDescriptor* field, |
| 979 | MessageFactory* factory) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 980 | USAGE_CHECK_ALL(MutableMessage, SINGULAR, MESSAGE); |
| 981 | |
| 982 | if (field->is_extension()) { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 983 | return static_cast<Message*>( |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 984 | MutableExtensionSet(message)->MutableMessage(field, |
| 985 | factory == NULL ? message_factory_ : factory)); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 986 | } else { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 987 | Message** result = MutableField<Message*>(message, field); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 988 | if (*result == NULL) { |
| 989 | const Message* default_message = DefaultRaw<const Message*>(field); |
| 990 | *result = default_message->New(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 991 | } |
| 992 | return *result; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | const Message& GeneratedMessageReflection::GetRepeatedMessage( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 997 | const Message& message, const FieldDescriptor* field, int index) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 998 | USAGE_CHECK_ALL(GetRepeatedMessage, REPEATED, MESSAGE); |
| 999 | |
| 1000 | if (field->is_extension()) { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1001 | return static_cast<const Message&>( |
| 1002 | GetExtensionSet(message).GetRepeatedMessage(field->number(), index)); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1003 | } else { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1004 | return GetRaw<RepeatedPtrFieldBase>(message, field) |
| 1005 | .Get<GenericTypeHandler<Message> >(index); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | Message* GeneratedMessageReflection::MutableRepeatedMessage( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1010 | Message* message, const FieldDescriptor* field, int index) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1011 | USAGE_CHECK_ALL(MutableRepeatedMessage, REPEATED, MESSAGE); |
| 1012 | |
| 1013 | if (field->is_extension()) { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1014 | return static_cast<Message*>( |
| 1015 | MutableExtensionSet(message)->MutableRepeatedMessage( |
| 1016 | field->number(), index)); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1017 | } else { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1018 | return MutableRaw<RepeatedPtrFieldBase>(message, field) |
| 1019 | ->Mutable<GenericTypeHandler<Message> >(index); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1023 | Message* GeneratedMessageReflection::AddMessage( |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 1024 | Message* message, const FieldDescriptor* field, |
| 1025 | MessageFactory* factory) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1026 | USAGE_CHECK_ALL(AddMessage, REPEATED, MESSAGE); |
| 1027 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 1028 | if (factory == NULL) factory = message_factory_; |
| 1029 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1030 | if (field->is_extension()) { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1031 | return static_cast<Message*>( |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 1032 | MutableExtensionSet(message)->AddMessage(field, factory)); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1033 | } else { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1034 | // We can't use AddField<Message>() because RepeatedPtrFieldBase doesn't |
| 1035 | // know how to allocate one. |
| 1036 | RepeatedPtrFieldBase* repeated = |
| 1037 | MutableRaw<RepeatedPtrFieldBase>(message, field); |
| 1038 | Message* result = repeated->AddFromCleared<GenericTypeHandler<Message> >(); |
| 1039 | if (result == NULL) { |
| 1040 | // We must allocate a new object. |
| 1041 | const Message* prototype; |
| 1042 | if (repeated->size() == 0) { |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 1043 | prototype = factory->GetPrototype(field->message_type()); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1044 | } else { |
| 1045 | prototype = &repeated->Get<GenericTypeHandler<Message> >(0); |
| 1046 | } |
| 1047 | result = prototype->New(); |
| 1048 | repeated->AddAllocated<GenericTypeHandler<Message> >(result); |
| 1049 | } |
| 1050 | return result; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | // ------------------------------------------------------------------- |
| 1055 | |
| 1056 | const FieldDescriptor* GeneratedMessageReflection::FindKnownExtensionByName( |
| 1057 | const string& name) const { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1058 | if (extensions_offset_ == -1) return NULL; |
| 1059 | |
| 1060 | const FieldDescriptor* result = descriptor_pool_->FindExtensionByName(name); |
| 1061 | if (result != NULL && result->containing_type() == descriptor_) { |
| 1062 | return result; |
| 1063 | } |
| 1064 | |
| 1065 | if (descriptor_->options().message_set_wire_format()) { |
| 1066 | // MessageSet extensions may be identified by type name. |
| 1067 | const Descriptor* type = descriptor_pool_->FindMessageTypeByName(name); |
| 1068 | if (type != NULL) { |
| 1069 | // Look for a matching extension in the foreign type's scope. |
| 1070 | for (int i = 0; i < type->extension_count(); i++) { |
| 1071 | const FieldDescriptor* extension = type->extension(i); |
| 1072 | if (extension->containing_type() == descriptor_ && |
| 1073 | extension->type() == FieldDescriptor::TYPE_MESSAGE && |
| 1074 | extension->is_optional() && |
| 1075 | extension->message_type() == type) { |
| 1076 | // Found it. |
| 1077 | return extension; |
| 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | return NULL; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | const FieldDescriptor* GeneratedMessageReflection::FindKnownExtensionByNumber( |
| 1087 | int number) const { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1088 | if (extensions_offset_ == -1) return NULL; |
| 1089 | return descriptor_pool_->FindExtensionByNumber(descriptor_, number); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | // =================================================================== |
| 1093 | // Some private helpers. |
| 1094 | |
| 1095 | // These simple template accessors obtain pointers (or references) to |
| 1096 | // the given field. |
| 1097 | template <typename Type> |
| 1098 | inline const Type& GeneratedMessageReflection::GetRaw( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1099 | const Message& message, const FieldDescriptor* field) const { |
| 1100 | const void* ptr = reinterpret_cast<const uint8*>(&message) + |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1101 | offsets_[field->index()]; |
| 1102 | return *reinterpret_cast<const Type*>(ptr); |
| 1103 | } |
| 1104 | |
| 1105 | template <typename Type> |
| 1106 | inline Type* GeneratedMessageReflection::MutableRaw( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1107 | Message* message, const FieldDescriptor* field) const { |
| 1108 | void* ptr = reinterpret_cast<uint8*>(message) + offsets_[field->index()]; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1109 | return reinterpret_cast<Type*>(ptr); |
| 1110 | } |
| 1111 | |
| 1112 | template <typename Type> |
| 1113 | inline const Type& GeneratedMessageReflection::DefaultRaw( |
| 1114 | const FieldDescriptor* field) const { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1115 | const void* ptr = reinterpret_cast<const uint8*>(default_instance_) + |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1116 | offsets_[field->index()]; |
| 1117 | return *reinterpret_cast<const Type*>(ptr); |
| 1118 | } |
| 1119 | |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1120 | inline const uint32* GeneratedMessageReflection::GetHasBits( |
| 1121 | const Message& message) const { |
| 1122 | const void* ptr = reinterpret_cast<const uint8*>(&message) + has_bits_offset_; |
| 1123 | return reinterpret_cast<const uint32*>(ptr); |
| 1124 | } |
| 1125 | inline uint32* GeneratedMessageReflection::MutableHasBits( |
| 1126 | Message* message) const { |
| 1127 | void* ptr = reinterpret_cast<uint8*>(message) + has_bits_offset_; |
| 1128 | return reinterpret_cast<uint32*>(ptr); |
| 1129 | } |
| 1130 | |
| 1131 | inline const ExtensionSet& GeneratedMessageReflection::GetExtensionSet( |
| 1132 | const Message& message) const { |
| 1133 | GOOGLE_DCHECK_NE(extensions_offset_, -1); |
| 1134 | const void* ptr = reinterpret_cast<const uint8*>(&message) + |
| 1135 | extensions_offset_; |
| 1136 | return *reinterpret_cast<const ExtensionSet*>(ptr); |
| 1137 | } |
| 1138 | inline ExtensionSet* GeneratedMessageReflection::MutableExtensionSet( |
| 1139 | Message* message) const { |
| 1140 | GOOGLE_DCHECK_NE(extensions_offset_, -1); |
| 1141 | void* ptr = reinterpret_cast<uint8*>(message) + extensions_offset_; |
| 1142 | return reinterpret_cast<ExtensionSet*>(ptr); |
| 1143 | } |
| 1144 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1145 | // Simple accessors for manipulating has_bits_. |
| 1146 | inline bool GeneratedMessageReflection::HasBit( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1147 | const Message& message, const FieldDescriptor* field) const { |
| 1148 | return GetHasBits(message)[field->index() / 32] & |
| 1149 | (1 << (field->index() % 32)); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | inline void GeneratedMessageReflection::SetBit( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1153 | Message* message, const FieldDescriptor* field) const { |
| 1154 | MutableHasBits(message)[field->index() / 32] |= (1 << (field->index() % 32)); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | inline void GeneratedMessageReflection::ClearBit( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1158 | Message* message, const FieldDescriptor* field) const { |
| 1159 | MutableHasBits(message)[field->index() / 32] &= ~(1 << (field->index() % 32)); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | // Template implementations of basic accessors. Inline because each |
| 1163 | // template instance is only called from one location. These are |
| 1164 | // used for all types except messages. |
| 1165 | template <typename Type> |
| 1166 | inline const Type& GeneratedMessageReflection::GetField( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1167 | const Message& message, const FieldDescriptor* field) const { |
| 1168 | return GetRaw<Type>(message, field); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | template <typename Type> |
| 1172 | inline void GeneratedMessageReflection::SetField( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1173 | Message* message, const FieldDescriptor* field, const Type& value) const { |
| 1174 | *MutableRaw<Type>(message, field) = value; |
| 1175 | SetBit(message, field); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | template <typename Type> |
| 1179 | inline Type* GeneratedMessageReflection::MutableField( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1180 | Message* message, const FieldDescriptor* field) const { |
| 1181 | SetBit(message, field); |
| 1182 | return MutableRaw<Type>(message, field); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | template <typename Type> |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 1186 | inline const Type& GeneratedMessageReflection::GetRepeatedField( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1187 | const Message& message, const FieldDescriptor* field, int index) const { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1188 | return GetRaw<RepeatedField<Type> >(message, field).Get(index); |
| 1189 | } |
| 1190 | |
| 1191 | template <typename Type> |
| 1192 | inline const Type& GeneratedMessageReflection::GetRepeatedPtrField( |
| 1193 | const Message& message, const FieldDescriptor* field, int index) const { |
| 1194 | return GetRaw<RepeatedPtrField<Type> >(message, field).Get(index); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | template <typename Type> |
| 1198 | inline void GeneratedMessageReflection::SetRepeatedField( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1199 | Message* message, const FieldDescriptor* field, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1200 | int index, Type value) const { |
| 1201 | MutableRaw<RepeatedField<Type> >(message, field)->Set(index, value); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | template <typename Type> |
| 1205 | inline Type* GeneratedMessageReflection::MutableRepeatedField( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1206 | Message* message, const FieldDescriptor* field, int index) const { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1207 | RepeatedPtrField<Type>* repeated = |
| 1208 | MutableRaw<RepeatedPtrField<Type> >(message, field); |
| 1209 | return repeated->Mutable(index); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | template <typename Type> |
| 1213 | inline void GeneratedMessageReflection::AddField( |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 1214 | Message* message, const FieldDescriptor* field, const Type& value) const { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1215 | MutableRaw<RepeatedField<Type> >(message, field)->Add(value); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | template <typename Type> |
| 1219 | inline Type* GeneratedMessageReflection::AddField( |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1220 | Message* message, const FieldDescriptor* field) const { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1221 | RepeatedPtrField<Type>* repeated = |
| 1222 | MutableRaw<RepeatedPtrField<Type> >(message, field); |
| 1223 | return repeated->Add(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1226 | } // namespace internal |
| 1227 | } // namespace protobuf |
| 1228 | } // namespace google |