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 // https://developers.google.com/protocol-buffers/ | 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. |
(...skipping 3212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3223 reflection->SetRepeatedFloat (message, F("packed_float" ), 1, 811); | 3223 reflection->SetRepeatedFloat (message, F("packed_float" ), 1, 811); |
3224 reflection->SetRepeatedDouble(message, F("packed_double" ), 1, 812); | 3224 reflection->SetRepeatedDouble(message, F("packed_double" ), 1, 812); |
3225 reflection->SetRepeatedBool (message, F("packed_bool" ), 1, true); | 3225 reflection->SetRepeatedBool (message, F("packed_bool" ), 1, true); |
3226 reflection->SetRepeatedEnum (message, F("packed_enum" ), 1, foreign_foo_); | 3226 reflection->SetRepeatedEnum (message, F("packed_enum" ), 1, foreign_foo_); |
3227 } | 3227 } |
3228 | 3228 |
3229 void TestUtil::ReflectionTester::RemoveLastRepeatedsViaReflection( | 3229 void TestUtil::ReflectionTester::RemoveLastRepeatedsViaReflection( |
3230 Message* message) { | 3230 Message* message) { |
3231 const Reflection* reflection = message->GetReflection(); | 3231 const Reflection* reflection = message->GetReflection(); |
3232 | 3232 |
3233 std::vector<const FieldDescriptor*> output; | 3233 vector<const FieldDescriptor*> output; |
3234 reflection->ListFields(*message, &output); | 3234 reflection->ListFields(*message, &output); |
3235 for (int i=0; i<output.size(); ++i) { | 3235 for (int i=0; i<output.size(); ++i) { |
3236 const FieldDescriptor* field = output[i]; | 3236 const FieldDescriptor* field = output[i]; |
3237 if (!field->is_repeated()) continue; | 3237 if (!field->is_repeated()) continue; |
3238 | 3238 |
3239 reflection->RemoveLast(message, field); | 3239 reflection->RemoveLast(message, field); |
3240 } | 3240 } |
3241 } | 3241 } |
3242 | 3242 |
3243 void TestUtil::ReflectionTester::ReleaseLastRepeatedsViaReflection( | 3243 void TestUtil::ReflectionTester::ReleaseLastRepeatedsViaReflection( |
3244 Message* message, bool expect_extensions_notnull) { | 3244 Message* message, bool expect_extensions_notnull) { |
3245 const Reflection* reflection = message->GetReflection(); | 3245 const Reflection* reflection = message->GetReflection(); |
3246 | 3246 |
3247 std::vector<const FieldDescriptor*> output; | 3247 vector<const FieldDescriptor*> output; |
3248 reflection->ListFields(*message, &output); | 3248 reflection->ListFields(*message, &output); |
3249 for (int i=0; i<output.size(); ++i) { | 3249 for (int i=0; i<output.size(); ++i) { |
3250 const FieldDescriptor* field = output[i]; | 3250 const FieldDescriptor* field = output[i]; |
3251 if (!field->is_repeated()) continue; | 3251 if (!field->is_repeated()) continue; |
3252 if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) continue; | 3252 if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) continue; |
3253 | 3253 |
3254 Message* released = reflection->ReleaseLast(message, field); | 3254 Message* released = reflection->ReleaseLast(message, field); |
3255 if (!field->is_extension() || expect_extensions_notnull) { | 3255 if (!field->is_extension() || expect_extensions_notnull) { |
3256 ASSERT_TRUE(released != NULL) << "ReleaseLast returned NULL for: " | 3256 ASSERT_TRUE(released != NULL) << "ReleaseLast returned NULL for: " |
3257 << field->name(); | 3257 << field->name(); |
3258 } | 3258 } |
3259 delete released; | 3259 delete released; |
3260 } | 3260 } |
3261 } | 3261 } |
3262 | 3262 |
3263 void TestUtil::ReflectionTester::SwapRepeatedsViaReflection(Message* message) { | 3263 void TestUtil::ReflectionTester::SwapRepeatedsViaReflection(Message* message) { |
3264 const Reflection* reflection = message->GetReflection(); | 3264 const Reflection* reflection = message->GetReflection(); |
3265 | 3265 |
3266 std::vector<const FieldDescriptor*> output; | 3266 vector<const FieldDescriptor*> output; |
3267 reflection->ListFields(*message, &output); | 3267 reflection->ListFields(*message, &output); |
3268 for (int i=0; i<output.size(); ++i) { | 3268 for (int i=0; i<output.size(); ++i) { |
3269 const FieldDescriptor* field = output[i]; | 3269 const FieldDescriptor* field = output[i]; |
3270 if (!field->is_repeated()) continue; | 3270 if (!field->is_repeated()) continue; |
3271 | 3271 |
3272 reflection->SwapElements(message, field, 0, 1); | 3272 reflection->SwapElements(message, field, 0, 1); |
3273 } | 3273 } |
3274 } | 3274 } |
3275 | 3275 |
3276 void TestUtil::ReflectionTester:: | 3276 void TestUtil::ReflectionTester:: |
3277 SetAllocatedOptionalMessageFieldsToNullViaReflection( | 3277 SetAllocatedOptionalMessageFieldsToNullViaReflection( |
3278 Message* message) { | 3278 Message* message) { |
3279 const Reflection* reflection = message->GetReflection(); | 3279 const Reflection* reflection = message->GetReflection(); |
3280 | 3280 |
3281 std::vector<const FieldDescriptor*> fields; | 3281 vector<const FieldDescriptor*> fields; |
3282 reflection->ListFields(*message, &fields); | 3282 reflection->ListFields(*message, &fields); |
3283 | 3283 |
3284 for (int i = 0; i < fields.size(); ++i) { | 3284 for (int i = 0; i < fields.size(); ++i) { |
3285 const FieldDescriptor* field = fields[i]; | 3285 const FieldDescriptor* field = fields[i]; |
3286 if (!field->is_optional() || | 3286 if (!field->is_optional() || |
3287 field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) continue; | 3287 field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) continue; |
3288 | 3288 |
3289 reflection->SetAllocatedMessage(message, NULL, field); | 3289 reflection->SetAllocatedMessage(message, NULL, field); |
3290 } | 3290 } |
3291 } | 3291 } |
3292 | 3292 |
3293 void TestUtil::ReflectionTester:: | 3293 void TestUtil::ReflectionTester:: |
3294 SetAllocatedOptionalMessageFieldsToMessageViaReflection( | 3294 SetAllocatedOptionalMessageFieldsToMessageViaReflection( |
3295 Message* from_message, | 3295 Message* from_message, |
3296 Message* to_message) { | 3296 Message* to_message) { |
3297 EXPECT_EQ(from_message->GetDescriptor(), to_message->GetDescriptor()); | 3297 EXPECT_EQ(from_message->GetDescriptor(), to_message->GetDescriptor()); |
3298 const Reflection* from_reflection = from_message->GetReflection(); | 3298 const Reflection* from_reflection = from_message->GetReflection(); |
3299 const Reflection* to_reflection = to_message->GetReflection(); | 3299 const Reflection* to_reflection = to_message->GetReflection(); |
3300 | 3300 |
3301 std::vector<const FieldDescriptor*> fields; | 3301 vector<const FieldDescriptor*> fields; |
3302 from_reflection->ListFields(*from_message, &fields); | 3302 from_reflection->ListFields(*from_message, &fields); |
3303 | 3303 |
3304 for (int i = 0; i < fields.size(); ++i) { | 3304 for (int i = 0; i < fields.size(); ++i) { |
3305 const FieldDescriptor* field = fields[i]; | 3305 const FieldDescriptor* field = fields[i]; |
3306 if (!field->is_optional() || | 3306 if (!field->is_optional() || |
3307 field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) continue; | 3307 field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) continue; |
3308 | 3308 |
3309 Message* sub_message = | 3309 Message* sub_message = |
3310 from_reflection->ReleaseMessage(from_message, field); | 3310 from_reflection->ReleaseMessage(from_message, field); |
3311 to_reflection->SetAllocatedMessage(to_message, sub_message, field); | 3311 to_reflection->SetAllocatedMessage(to_message, sub_message, field); |
(...skipping 13 matching lines...) Expand all Loading... |
3325 }; | 3325 }; |
3326 for (int i = 0; i < GOOGLE_ARRAYSIZE(fields); i++) { | 3326 for (int i = 0; i < GOOGLE_ARRAYSIZE(fields); i++) { |
3327 const Message& sub_message = reflection->GetMessage(*message, F(fields[i])); | 3327 const Message& sub_message = reflection->GetMessage(*message, F(fields[i])); |
3328 Message* released = reflection->ReleaseMessage(message, F(fields[i])); | 3328 Message* released = reflection->ReleaseMessage(message, F(fields[i])); |
3329 switch (expected_release_state) { | 3329 switch (expected_release_state) { |
3330 case IS_NULL: | 3330 case IS_NULL: |
3331 EXPECT_TRUE(released == NULL); | 3331 EXPECT_TRUE(released == NULL); |
3332 break; | 3332 break; |
3333 case NOT_NULL: | 3333 case NOT_NULL: |
3334 EXPECT_TRUE(released != NULL); | 3334 EXPECT_TRUE(released != NULL); |
3335 if (message->GetArena() == NULL) { | 3335 EXPECT_EQ(&sub_message, released); |
3336 // released message must be same as sub_message if source message is | |
3337 // not on arena. | |
3338 EXPECT_EQ(&sub_message, released); | |
3339 } | |
3340 break; | 3336 break; |
3341 case CAN_BE_NULL: | 3337 case CAN_BE_NULL: |
3342 break; | 3338 break; |
3343 } | 3339 } |
3344 delete released; | 3340 delete released; |
3345 EXPECT_FALSE(reflection->HasField(*message, F(fields[i]))); | 3341 EXPECT_FALSE(reflection->HasField(*message, F(fields[i]))); |
3346 } | 3342 } |
3347 } | 3343 } |
3348 | 3344 |
3349 } // namespace protobuf | 3345 } // namespace protobuf |
3350 } // namespace google | 3346 } // namespace google |
OLD | NEW |