blob: d62ca79c80cb7d49cdf4a2dc814db293560f5f81 [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// Protocol Buffers - Google's data interchange format
kenton@google.com24bf56f2008-09-24 20:31:01 +00002// Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07003// https://developers.google.com/protocol-buffers/
temporal40ee5512008-07-10 02:12:20 +00004//
kenton@google.com24bf56f2008-09-24 20:31:01 +00005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
temporal40ee5512008-07-10 02:12:20 +00008//
kenton@google.com24bf56f2008-09-24 20:31:01 +00009// * 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.
temporal40ee5512008-07-10 02:12:20 +000018//
kenton@google.com24bf56f2008-09-24 20:31:01 +000019// 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.
temporal40ee5512008-07-10 02:12:20 +000030
31// Author: kenton@google.com (Kenton Varda)
32// Based on original Protocol Buffers design by
33// Sanjay Ghemawat, Jeff Dean, and others.
34
jieluo@google.com3b547d32014-07-18 23:19:21 +000035#include <iostream>
temporal40ee5512008-07-10 02:12:20 +000036#include <stack>
37#include <google/protobuf/stubs/hash.h>
38
39#include <google/protobuf/message.h>
40
Feng Xiaoeee38b02015-08-22 18:25:48 -070041#include <google/protobuf/stubs/logging.h>
temporal40ee5512008-07-10 02:12:20 +000042#include <google/protobuf/stubs/common.h>
Feng Xiaoeee38b02015-08-22 18:25:48 -070043#include <google/protobuf/stubs/mutex.h>
kenton@google.com1a34c922009-08-06 22:17:26 +000044#include <google/protobuf/stubs/once.h>
Feng Xiao6ef984a2014-11-10 17:34:54 -080045#include <google/protobuf/reflection_internal.h>
temporal40ee5512008-07-10 02:12:20 +000046#include <google/protobuf/io/coded_stream.h>
47#include <google/protobuf/io/zero_copy_stream_impl.h>
48#include <google/protobuf/descriptor.pb.h>
Feng Xiaoeee38b02015-08-22 18:25:48 -070049#include <google/protobuf/map_field.h>
temporal40ee5512008-07-10 02:12:20 +000050#include <google/protobuf/descriptor.h>
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000051#include <google/protobuf/generated_message_util.h>
temporal40ee5512008-07-10 02:12:20 +000052#include <google/protobuf/reflection_ops.h>
53#include <google/protobuf/wire_format.h>
54#include <google/protobuf/stubs/strutil.h>
jieluo@google.com4de8f552014-07-18 00:47:59 +000055#include <google/protobuf/stubs/map_util.h>
Feng Xiao6ef984a2014-11-10 17:34:54 -080056#include <google/protobuf/stubs/singleton.h>
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000057#include <google/protobuf/stubs/stl_util.h>
temporal40ee5512008-07-10 02:12:20 +000058
59namespace google {
60namespace protobuf {
61
62using internal::WireFormat;
63using internal::ReflectionOps;
64
temporal40ee5512008-07-10 02:12:20 +000065Message::~Message() {}
temporal40ee5512008-07-10 02:12:20 +000066
67void Message::MergeFrom(const Message& from) {
68 const Descriptor* descriptor = GetDescriptor();
69 GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor)
70 << ": Tried to merge from a message with a different type. "
71 "to: " << descriptor->full_name() << ", "
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070072 "from: " << from.GetDescriptor()->full_name();
temporal779f61c2008-08-13 03:15:00 +000073 ReflectionOps::Merge(from, this);
temporal40ee5512008-07-10 02:12:20 +000074}
75
kenton@google.com80b1d622009-07-29 01:13:20 +000076void Message::CheckTypeAndMergeFrom(const MessageLite& other) {
77 MergeFrom(*down_cast<const Message*>(&other));
78}
79
temporal40ee5512008-07-10 02:12:20 +000080void Message::CopyFrom(const Message& from) {
81 const Descriptor* descriptor = GetDescriptor();
82 GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor)
jieluo@google.com4de8f552014-07-18 00:47:59 +000083 << ": Tried to copy from a message with a different type. "
temporal40ee5512008-07-10 02:12:20 +000084 "to: " << descriptor->full_name() << ", "
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070085 "from: " << from.GetDescriptor()->full_name();
temporal779f61c2008-08-13 03:15:00 +000086 ReflectionOps::Copy(from, this);
temporal40ee5512008-07-10 02:12:20 +000087}
88
kenton@google.com80b1d622009-07-29 01:13:20 +000089string Message::GetTypeName() const {
90 return GetDescriptor()->full_name();
91}
92
temporal40ee5512008-07-10 02:12:20 +000093void Message::Clear() {
temporal779f61c2008-08-13 03:15:00 +000094 ReflectionOps::Clear(this);
temporal40ee5512008-07-10 02:12:20 +000095}
96
97bool Message::IsInitialized() const {
temporal779f61c2008-08-13 03:15:00 +000098 return ReflectionOps::IsInitialized(*this);
temporal40ee5512008-07-10 02:12:20 +000099}
100
101void Message::FindInitializationErrors(vector<string>* errors) const {
temporal779f61c2008-08-13 03:15:00 +0000102 return ReflectionOps::FindInitializationErrors(*this, "", errors);
temporal40ee5512008-07-10 02:12:20 +0000103}
104
105string Message::InitializationErrorString() const {
106 vector<string> errors;
107 FindInitializationErrors(&errors);
jieluo@google.com4de8f552014-07-18 00:47:59 +0000108 return Join(errors, ", ");
temporal40ee5512008-07-10 02:12:20 +0000109}
110
111void Message::CheckInitialized() const {
112 GOOGLE_CHECK(IsInitialized())
113 << "Message of type \"" << GetDescriptor()->full_name()
114 << "\" is missing required fields: " << InitializationErrorString();
115}
116
117void Message::DiscardUnknownFields() {
temporal779f61c2008-08-13 03:15:00 +0000118 return ReflectionOps::DiscardUnknownFields(this);
temporal40ee5512008-07-10 02:12:20 +0000119}
120
121bool Message::MergePartialFromCodedStream(io::CodedInputStream* input) {
temporal779f61c2008-08-13 03:15:00 +0000122 return WireFormat::ParseAndMergePartial(input, this);
temporal40ee5512008-07-10 02:12:20 +0000123}
124
temporal40ee5512008-07-10 02:12:20 +0000125bool Message::ParseFromFileDescriptor(int file_descriptor) {
126 io::FileInputStream input(file_descriptor);
127 return ParseFromZeroCopyStream(&input) && input.GetErrno() == 0;
128}
129
130bool Message::ParsePartialFromFileDescriptor(int file_descriptor) {
131 io::FileInputStream input(file_descriptor);
132 return ParsePartialFromZeroCopyStream(&input) && input.GetErrno() == 0;
133}
134
135bool Message::ParseFromIstream(istream* input) {
136 io::IstreamInputStream zero_copy_input(input);
137 return ParseFromZeroCopyStream(&zero_copy_input) && input->eof();
138}
139
140bool Message::ParsePartialFromIstream(istream* input) {
141 io::IstreamInputStream zero_copy_input(input);
142 return ParsePartialFromZeroCopyStream(&zero_copy_input) && input->eof();
143}
144
145
kenton@google.comd37d46d2009-04-25 02:53:47 +0000146void Message::SerializeWithCachedSizes(
temporal40ee5512008-07-10 02:12:20 +0000147 io::CodedOutputStream* output) const {
kenton@google.comd37d46d2009-04-25 02:53:47 +0000148 WireFormat::SerializeWithCachedSizes(*this, GetCachedSize(), output);
149}
150
temporal40ee5512008-07-10 02:12:20 +0000151int Message::ByteSize() const {
temporal779f61c2008-08-13 03:15:00 +0000152 int size = WireFormat::ByteSize(*this);
temporal40ee5512008-07-10 02:12:20 +0000153 SetCachedSize(size);
154 return size;
155}
156
liujisi@google.comc5553a32014-05-28 21:48:28 +0000157void Message::SetCachedSize(int /* size */) const {
temporal40ee5512008-07-10 02:12:20 +0000158 GOOGLE_LOG(FATAL) << "Message class \"" << GetDescriptor()->full_name()
159 << "\" implements neither SetCachedSize() nor ByteSize(). "
160 "Must implement one or the other.";
161}
162
kenton@google.com26bd9ee2008-11-21 00:06:27 +0000163int Message::SpaceUsed() const {
164 return GetReflection()->SpaceUsed(*this);
165}
166
temporal40ee5512008-07-10 02:12:20 +0000167bool Message::SerializeToFileDescriptor(int file_descriptor) const {
168 io::FileOutputStream output(file_descriptor);
169 return SerializeToZeroCopyStream(&output);
170}
171
172bool Message::SerializePartialToFileDescriptor(int file_descriptor) const {
173 io::FileOutputStream output(file_descriptor);
174 return SerializePartialToZeroCopyStream(&output);
175}
176
177bool Message::SerializeToOstream(ostream* output) const {
kenton@google.comd6e84b32009-12-22 19:43:41 +0000178 {
179 io::OstreamOutputStream zero_copy_output(output);
180 if (!SerializeToZeroCopyStream(&zero_copy_output)) return false;
181 }
182 return output->good();
temporal40ee5512008-07-10 02:12:20 +0000183}
184
185bool Message::SerializePartialToOstream(ostream* output) const {
186 io::OstreamOutputStream zero_copy_output(output);
187 return SerializePartialToZeroCopyStream(&zero_copy_output);
188}
189
190
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000191// =============================================================================
192// Reflection and associated Template Specializations
193
temporal779f61c2008-08-13 03:15:00 +0000194Reflection::~Reflection() {}
195
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000196#define HANDLE_TYPE(TYPE, CPPTYPE, CTYPE) \
liujisi@google.com691f6da2013-01-15 02:19:41 +0000197template<> \
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000198const RepeatedField<TYPE>& Reflection::GetRepeatedField<TYPE>( \
199 const Message& message, const FieldDescriptor* field) const { \
200 return *static_cast<RepeatedField<TYPE>* >( \
201 MutableRawRepeatedField(const_cast<Message*>(&message), \
202 field, CPPTYPE, CTYPE, NULL)); \
203} \
204 \
liujisi@google.com691f6da2013-01-15 02:19:41 +0000205template<> \
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000206RepeatedField<TYPE>* Reflection::MutableRepeatedField<TYPE>( \
207 Message* message, const FieldDescriptor* field) const { \
208 return static_cast<RepeatedField<TYPE>* >( \
209 MutableRawRepeatedField(message, field, CPPTYPE, CTYPE, NULL)); \
210}
211
212HANDLE_TYPE(int32, FieldDescriptor::CPPTYPE_INT32, -1);
213HANDLE_TYPE(int64, FieldDescriptor::CPPTYPE_INT64, -1);
214HANDLE_TYPE(uint32, FieldDescriptor::CPPTYPE_UINT32, -1);
215HANDLE_TYPE(uint64, FieldDescriptor::CPPTYPE_UINT64, -1);
216HANDLE_TYPE(float, FieldDescriptor::CPPTYPE_FLOAT, -1);
217HANDLE_TYPE(double, FieldDescriptor::CPPTYPE_DOUBLE, -1);
218HANDLE_TYPE(bool, FieldDescriptor::CPPTYPE_BOOL, -1);
219
220
221#undef HANDLE_TYPE
222
223void* Reflection::MutableRawRepeatedString(
jieluo@google.com4de8f552014-07-18 00:47:59 +0000224 Message* message, const FieldDescriptor* field, bool is_string) const {
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000225 return MutableRawRepeatedField(message, field,
226 FieldDescriptor::CPPTYPE_STRING, FieldOptions::STRING, NULL);
227}
228
229
Feng Xiao6ef984a2014-11-10 17:34:54 -0800230// Default EnumValue API implementations. Real reflection implementations should
231// override these. However, there are several legacy implementations that do
232// not, and cannot easily be changed at the same time as the Reflection API, so
233// we provide these for now.
234// TODO: Remove these once all Reflection implementations are updated.
235int Reflection::GetEnumValue(const Message& message,
236 const FieldDescriptor* field) const {
237 GOOGLE_LOG(FATAL) << "Unimplemented EnumValue API.";
238 return 0;
239}
240void Reflection::SetEnumValue(Message* message,
241 const FieldDescriptor* field,
242 int value) const {
243 GOOGLE_LOG(FATAL) << "Unimplemented EnumValue API.";
244}
245int Reflection::GetRepeatedEnumValue(
246 const Message& message,
247 const FieldDescriptor* field, int index) const {
248 GOOGLE_LOG(FATAL) << "Unimplemented EnumValue API.";
249 return 0;
250}
251void Reflection::SetRepeatedEnumValue(Message* message,
252 const FieldDescriptor* field, int index,
253 int value) const {
254 GOOGLE_LOG(FATAL) << "Unimplemented EnumValue API.";
255}
256void Reflection::AddEnumValue(Message* message,
257 const FieldDescriptor* field,
258 int value) const {
259 GOOGLE_LOG(FATAL) << "Unimplemented EnumValue API.";
260}
261
Feng Xiaoeee38b02015-08-22 18:25:48 -0700262MapIterator Reflection::MapBegin(
263 Message* message,
264 const FieldDescriptor* field) const {
265 GOOGLE_LOG(FATAL) << "Unimplemented Map Reflection API.";
266 MapIterator iter(message, field);
267 return iter;
268}
269
270MapIterator Reflection::MapEnd(
271 Message* message,
272 const FieldDescriptor* field) const {
273 GOOGLE_LOG(FATAL) << "Unimplemented Map Reflection API.";
274 MapIterator iter(message, field);
275 return iter;
276}
277
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000278// =============================================================================
temporal40ee5512008-07-10 02:12:20 +0000279// MessageFactory
280
281MessageFactory::~MessageFactory() {}
282
283namespace {
284
285class GeneratedMessageFactory : public MessageFactory {
286 public:
287 GeneratedMessageFactory();
288 ~GeneratedMessageFactory();
289
290 static GeneratedMessageFactory* singleton();
291
kenton@google.com80b1d622009-07-29 01:13:20 +0000292 typedef void RegistrationFunc(const string&);
kenton@google.comd37d46d2009-04-25 02:53:47 +0000293 void RegisterFile(const char* file, RegistrationFunc* registration_func);
temporal40ee5512008-07-10 02:12:20 +0000294 void RegisterType(const Descriptor* descriptor, const Message* prototype);
295
296 // implements MessageFactory ---------------------------------------
297 const Message* GetPrototype(const Descriptor* type);
298
299 private:
kenton@google.comd37d46d2009-04-25 02:53:47 +0000300 // Only written at static init time, so does not require locking.
301 hash_map<const char*, RegistrationFunc*,
302 hash<const char*>, streq> file_map_;
303
304 // Initialized lazily, so requires locking.
305 Mutex mutex_;
temporal40ee5512008-07-10 02:12:20 +0000306 hash_map<const Descriptor*, const Message*> type_map_;
307};
308
kenton@google.com1a34c922009-08-06 22:17:26 +0000309GeneratedMessageFactory* generated_message_factory_ = NULL;
temporalbdbb8632009-12-18 08:21:00 +0000310GOOGLE_PROTOBUF_DECLARE_ONCE(generated_message_factory_once_init_);
kenton@google.com1a34c922009-08-06 22:17:26 +0000311
312void ShutdownGeneratedMessageFactory() {
313 delete generated_message_factory_;
314}
315
316void InitGeneratedMessageFactory() {
317 generated_message_factory_ = new GeneratedMessageFactory;
318 internal::OnShutdown(&ShutdownGeneratedMessageFactory);
319}
320
temporal40ee5512008-07-10 02:12:20 +0000321GeneratedMessageFactory::GeneratedMessageFactory() {}
322GeneratedMessageFactory::~GeneratedMessageFactory() {}
323
324GeneratedMessageFactory* GeneratedMessageFactory::singleton() {
kenton@google.comfccb1462009-12-18 02:11:36 +0000325 ::google::protobuf::GoogleOnceInit(&generated_message_factory_once_init_,
kenton@google.com1a34c922009-08-06 22:17:26 +0000326 &InitGeneratedMessageFactory);
327 return generated_message_factory_;
temporal40ee5512008-07-10 02:12:20 +0000328}
329
kenton@google.comd37d46d2009-04-25 02:53:47 +0000330void GeneratedMessageFactory::RegisterFile(
331 const char* file, RegistrationFunc* registration_func) {
332 if (!InsertIfNotPresent(&file_map_, file, registration_func)) {
333 GOOGLE_LOG(FATAL) << "File is already registered: " << file;
334 }
335}
336
temporal40ee5512008-07-10 02:12:20 +0000337void GeneratedMessageFactory::RegisterType(const Descriptor* descriptor,
338 const Message* prototype) {
339 GOOGLE_DCHECK_EQ(descriptor->file()->pool(), DescriptorPool::generated_pool())
340 << "Tried to register a non-generated type with the generated "
341 "type registry.";
342
kenton@google.comd37d46d2009-04-25 02:53:47 +0000343 // This should only be called as a result of calling a file registration
344 // function during GetPrototype(), in which case we already have locked
345 // the mutex.
346 mutex_.AssertHeld();
temporal40ee5512008-07-10 02:12:20 +0000347 if (!InsertIfNotPresent(&type_map_, descriptor, prototype)) {
348 GOOGLE_LOG(DFATAL) << "Type is already registered: " << descriptor->full_name();
349 }
350}
351
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000352
temporal40ee5512008-07-10 02:12:20 +0000353const Message* GeneratedMessageFactory::GetPrototype(const Descriptor* type) {
kenton@google.comd37d46d2009-04-25 02:53:47 +0000354 {
355 ReaderMutexLock lock(&mutex_);
356 const Message* result = FindPtrOrNull(type_map_, type);
357 if (result != NULL) return result;
358 }
359
360 // If the type is not in the generated pool, then we can't possibly handle
361 // it.
362 if (type->file()->pool() != DescriptorPool::generated_pool()) return NULL;
363
364 // Apparently the file hasn't been registered yet. Let's do that now.
365 RegistrationFunc* registration_func =
366 FindPtrOrNull(file_map_, type->file()->name().c_str());
367 if (registration_func == NULL) {
368 GOOGLE_LOG(DFATAL) << "File appears to be in generated pool but wasn't "
369 "registered: " << type->file()->name();
370 return NULL;
371 }
372
373 WriterMutexLock lock(&mutex_);
374
375 // Check if another thread preempted us.
376 const Message* result = FindPtrOrNull(type_map_, type);
377 if (result == NULL) {
378 // Nope. OK, register everything.
kenton@google.com80b1d622009-07-29 01:13:20 +0000379 registration_func(type->file()->name());
kenton@google.comd37d46d2009-04-25 02:53:47 +0000380 // Should be here now.
381 result = FindPtrOrNull(type_map_, type);
382 }
383
384 if (result == NULL) {
385 GOOGLE_LOG(DFATAL) << "Type appears to be in generated pool but wasn't "
386 << "registered: " << type->full_name();
387 }
388
389 return result;
temporal40ee5512008-07-10 02:12:20 +0000390}
391
392} // namespace
393
394MessageFactory* MessageFactory::generated_factory() {
395 return GeneratedMessageFactory::singleton();
396}
397
kenton@google.comd37d46d2009-04-25 02:53:47 +0000398void MessageFactory::InternalRegisterGeneratedFile(
kenton@google.com80b1d622009-07-29 01:13:20 +0000399 const char* filename, void (*register_messages)(const string&)) {
kenton@google.comd37d46d2009-04-25 02:53:47 +0000400 GeneratedMessageFactory::singleton()->RegisterFile(filename,
401 register_messages);
402}
403
temporal40ee5512008-07-10 02:12:20 +0000404void MessageFactory::InternalRegisterGeneratedMessage(
405 const Descriptor* descriptor, const Message* prototype) {
406 GeneratedMessageFactory::singleton()->RegisterType(descriptor, prototype);
407}
408
409
Feng Xiao6ef984a2014-11-10 17:34:54 -0800410MessageFactory* Reflection::GetMessageFactory() const {
411 GOOGLE_LOG(FATAL) << "Not implemented.";
412 return NULL;
413}
414
415void* Reflection::RepeatedFieldData(
416 Message* message, const FieldDescriptor* field,
417 FieldDescriptor::CppType cpp_type,
418 const Descriptor* message_type) const {
419 GOOGLE_LOG(FATAL) << "Not implemented.";
420 return NULL;
421}
422
423namespace internal {
424RepeatedFieldAccessor::~RepeatedFieldAccessor() {
425}
426} // namespace internal
427
428const internal::RepeatedFieldAccessor* Reflection::RepeatedFieldAccessor(
429 const FieldDescriptor* field) const {
430 GOOGLE_CHECK(field->is_repeated());
431 switch (field->cpp_type()) {
432#define HANDLE_PRIMITIVE_TYPE(TYPE, type) \
433 case FieldDescriptor::CPPTYPE_ ## TYPE: \
434 return internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<type> >::get();
435 HANDLE_PRIMITIVE_TYPE(INT32, int32)
436 HANDLE_PRIMITIVE_TYPE(UINT32, uint32)
437 HANDLE_PRIMITIVE_TYPE(INT64, int64)
438 HANDLE_PRIMITIVE_TYPE(UINT64, uint64)
439 HANDLE_PRIMITIVE_TYPE(FLOAT, float)
440 HANDLE_PRIMITIVE_TYPE(DOUBLE, double)
441 HANDLE_PRIMITIVE_TYPE(BOOL, bool)
442 HANDLE_PRIMITIVE_TYPE(ENUM, int32)
443#undef HANDLE_PRIMITIVE_TYPE
444 case FieldDescriptor::CPPTYPE_STRING:
445 switch (field->options().ctype()) {
446 default:
447 case FieldOptions::STRING:
448 return internal::Singleton<internal::RepeatedPtrFieldStringAccessor>::get();
449 }
450 break;
451 case FieldDescriptor::CPPTYPE_MESSAGE:
Feng Xiaof157a562014-11-14 11:50:31 -0800452 if (field->is_map()) {
453 return internal::Singleton<internal::MapFieldAccessor>::get();
454 } else {
455 return internal::Singleton<internal::RepeatedPtrFieldMessageAccessor>::get();
456 }
Feng Xiao6ef984a2014-11-10 17:34:54 -0800457 }
458 GOOGLE_LOG(FATAL) << "Should not reach here.";
459 return NULL;
460}
461
462namespace internal {
Jisi Liu7a00a1e2015-02-21 17:28:51 -0800463namespace {
464void ShutdownRepeatedFieldAccessor() {
Jisi Liu885b6122015-02-28 14:51:22 -0800465 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<int32> >::ShutDown();
466 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<uint32> >::ShutDown();
467 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<int64> >::ShutDown();
468 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<uint64> >::ShutDown();
469 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<float> >::ShutDown();
470 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<double> >::ShutDown();
471 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<bool> >::ShutDown();
472 internal::Singleton<internal::RepeatedPtrFieldStringAccessor>::ShutDown();
473 internal::Singleton<internal::RepeatedPtrFieldMessageAccessor>::ShutDown();
474 internal::Singleton<internal::MapFieldAccessor>::ShutDown();
475}
Jisi Liu7a00a1e2015-02-21 17:28:51 -0800476
477struct ShutdownRepeatedFieldRegister {
478 ShutdownRepeatedFieldRegister() {
479 OnShutdown(&ShutdownRepeatedFieldAccessor);
480 }
481} shutdown_;
482
Jisi Liu885b6122015-02-28 14:51:22 -0800483} // namespace
Jisi Liu7a00a1e2015-02-21 17:28:51 -0800484} // namespace internal
485
486namespace internal {
Bo Yang5db21732015-05-21 14:28:59 -0700487template<>
Douglas Heriot5021c4d2015-08-22 02:05:40 +1000488#if defined(_MSC_VER) && (_MSC_VER >= 1900)
489// Note: force noinline to workaround MSVC 2015 compiler bug, issue #240
490GOOGLE_ATTRIBUTE_NOINLINE
491#endif
Bo Yang5db21732015-05-21 14:28:59 -0700492Message* GenericTypeHandler<Message>::NewFromPrototype(
493 const Message* prototype, google::protobuf::Arena* arena) {
494 return prototype->New(arena);
495}
496template<>
0xAAEd41a3d62015-09-15 01:46:28 +0300497#if defined(_MSC_VER) && (_MSC_VER >= 1900)
498// Note: force noinline to workaround MSVC 2015 compiler bug, issue #240
499GOOGLE_ATTRIBUTE_NOINLINE
500#endif
Bo Yang5db21732015-05-21 14:28:59 -0700501google::protobuf::Arena* GenericTypeHandler<Message>::GetArena(
502 Message* value) {
503 return value->GetArena();
504}
505template<>
0xAAEd41a3d62015-09-15 01:46:28 +0300506#if defined(_MSC_VER) && (_MSC_VER >= 1900)
507// Note: force noinline to workaround MSVC 2015 compiler bug, issue #240
508GOOGLE_ATTRIBUTE_NOINLINE
509#endif
Bo Yang5db21732015-05-21 14:28:59 -0700510void* GenericTypeHandler<Message>::GetMaybeArenaPointer(
511 Message* value) {
512 return value->GetMaybeArenaPointer();
513}
Feng Xiao6ef984a2014-11-10 17:34:54 -0800514} // namespace internal
515
temporal40ee5512008-07-10 02:12:20 +0000516} // namespace protobuf
517} // namespace google