message.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  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.
  18. //
  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.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <iostream>
  34. #include <stack>
  35. #include <google/protobuf/stubs/hash.h>
  36. #include <google/protobuf/message.h>
  37. #include <google/protobuf/stubs/casts.h>
  38. #include <google/protobuf/stubs/logging.h>
  39. #include <google/protobuf/stubs/common.h>
  40. #include <google/protobuf/stubs/mutex.h>
  41. #include <google/protobuf/stubs/once.h>
  42. #include <google/protobuf/descriptor.pb.h>
  43. #include <google/protobuf/reflection_internal.h>
  44. #include <google/protobuf/io/coded_stream.h>
  45. #include <google/protobuf/io/zero_copy_stream_impl.h>
  46. #include <google/protobuf/descriptor.h>
  47. #include <google/protobuf/generated_message_util.h>
  48. #include <google/protobuf/map_field.h>
  49. #include <google/protobuf/reflection_ops.h>
  50. #include <google/protobuf/wire_format.h>
  51. #include <google/protobuf/stubs/strutil.h>
  52. #include <google/protobuf/stubs/map_util.h>
  53. #include <google/protobuf/stubs/singleton.h>
  54. #include <google/protobuf/stubs/stl_util.h>
  55. namespace google {
  56. namespace protobuf {
  57. using internal::WireFormat;
  58. using internal::ReflectionOps;
  59. void Message::MergeFrom(const Message& from) {
  60. const Descriptor* descriptor = GetDescriptor();
  61. GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor)
  62. << ": Tried to merge from a message with a different type. "
  63. "to: " << descriptor->full_name() << ", "
  64. "from: " << from.GetDescriptor()->full_name();
  65. ReflectionOps::Merge(from, this);
  66. }
  67. void Message::CheckTypeAndMergeFrom(const MessageLite& other) {
  68. MergeFrom(*down_cast<const Message*>(&other));
  69. }
  70. void Message::CopyFrom(const Message& from) {
  71. const Descriptor* descriptor = GetDescriptor();
  72. GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor)
  73. << ": Tried to copy from a message with a different type. "
  74. "to: " << descriptor->full_name() << ", "
  75. "from: " << from.GetDescriptor()->full_name();
  76. ReflectionOps::Copy(from, this);
  77. }
  78. string Message::GetTypeName() const {
  79. return GetDescriptor()->full_name();
  80. }
  81. void Message::Clear() {
  82. ReflectionOps::Clear(this);
  83. }
  84. bool Message::IsInitialized() const {
  85. return ReflectionOps::IsInitialized(*this);
  86. }
  87. void Message::FindInitializationErrors(std::vector<string>* errors) const {
  88. return ReflectionOps::FindInitializationErrors(*this, "", errors);
  89. }
  90. string Message::InitializationErrorString() const {
  91. std::vector<string> errors;
  92. FindInitializationErrors(&errors);
  93. return Join(errors, ", ");
  94. }
  95. void Message::CheckInitialized() const {
  96. GOOGLE_CHECK(IsInitialized())
  97. << "Message of type \"" << GetDescriptor()->full_name()
  98. << "\" is missing required fields: " << InitializationErrorString();
  99. }
  100. void Message::DiscardUnknownFields() {
  101. return ReflectionOps::DiscardUnknownFields(this);
  102. }
  103. bool Message::MergePartialFromCodedStream(io::CodedInputStream* input) {
  104. return WireFormat::ParseAndMergePartial(input, this);
  105. }
  106. bool Message::ParseFromFileDescriptor(int file_descriptor) {
  107. io::FileInputStream input(file_descriptor);
  108. return ParseFromZeroCopyStream(&input) && input.GetErrno() == 0;
  109. }
  110. bool Message::ParsePartialFromFileDescriptor(int file_descriptor) {
  111. io::FileInputStream input(file_descriptor);
  112. return ParsePartialFromZeroCopyStream(&input) && input.GetErrno() == 0;
  113. }
  114. bool Message::ParseFromIstream(std::istream* input) {
  115. io::IstreamInputStream zero_copy_input(input);
  116. return ParseFromZeroCopyStream(&zero_copy_input) && input->eof();
  117. }
  118. bool Message::ParsePartialFromIstream(std::istream* input) {
  119. io::IstreamInputStream zero_copy_input(input);
  120. return ParsePartialFromZeroCopyStream(&zero_copy_input) && input->eof();
  121. }
  122. void Message::SerializeWithCachedSizes(
  123. io::CodedOutputStream* output) const {
  124. const internal::SerializationTable* table =
  125. static_cast<const internal::SerializationTable*>(InternalGetTable());
  126. if (table == 0) {
  127. WireFormat::SerializeWithCachedSizes(*this, GetCachedSize(), output);
  128. } else {
  129. internal::TableSerialize(*this, table, output);
  130. }
  131. }
  132. size_t Message::ByteSizeLong() const {
  133. size_t size = WireFormat::ByteSize(*this);
  134. SetCachedSize(internal::ToCachedSize(size));
  135. return size;
  136. }
  137. void Message::SetCachedSize(int /* size */) const {
  138. GOOGLE_LOG(FATAL) << "Message class \"" << GetDescriptor()->full_name()
  139. << "\" implements neither SetCachedSize() nor ByteSize(). "
  140. "Must implement one or the other.";
  141. }
  142. size_t Message::SpaceUsedLong() const {
  143. return GetReflection()->SpaceUsedLong(*this);
  144. }
  145. bool Message::SerializeToFileDescriptor(int file_descriptor) const {
  146. io::FileOutputStream output(file_descriptor);
  147. return SerializeToZeroCopyStream(&output) && output.Flush();
  148. }
  149. bool Message::SerializePartialToFileDescriptor(int file_descriptor) const {
  150. io::FileOutputStream output(file_descriptor);
  151. return SerializePartialToZeroCopyStream(&output) && output.Flush();
  152. }
  153. bool Message::SerializeToOstream(std::ostream* output) const {
  154. {
  155. io::OstreamOutputStream zero_copy_output(output);
  156. if (!SerializeToZeroCopyStream(&zero_copy_output)) return false;
  157. }
  158. return output->good();
  159. }
  160. bool Message::SerializePartialToOstream(std::ostream* output) const {
  161. io::OstreamOutputStream zero_copy_output(output);
  162. return SerializePartialToZeroCopyStream(&zero_copy_output);
  163. }
  164. // =============================================================================
  165. // Reflection and associated Template Specializations
  166. Reflection::~Reflection() {}
  167. void Reflection::AddAllocatedMessage(Message* /* message */,
  168. const FieldDescriptor* /*field */,
  169. Message* /* new_entry */) const {}
  170. #define HANDLE_TYPE(TYPE, CPPTYPE, CTYPE) \
  171. template<> \
  172. const RepeatedField<TYPE>& Reflection::GetRepeatedField<TYPE>( \
  173. const Message& message, const FieldDescriptor* field) const { \
  174. return *static_cast<RepeatedField<TYPE>* >( \
  175. MutableRawRepeatedField(const_cast<Message*>(&message), \
  176. field, CPPTYPE, CTYPE, NULL)); \
  177. } \
  178. \
  179. template<> \
  180. RepeatedField<TYPE>* Reflection::MutableRepeatedField<TYPE>( \
  181. Message* message, const FieldDescriptor* field) const { \
  182. return static_cast<RepeatedField<TYPE>* >( \
  183. MutableRawRepeatedField(message, field, CPPTYPE, CTYPE, NULL)); \
  184. }
  185. HANDLE_TYPE(int32, FieldDescriptor::CPPTYPE_INT32, -1);
  186. HANDLE_TYPE(int64, FieldDescriptor::CPPTYPE_INT64, -1);
  187. HANDLE_TYPE(uint32, FieldDescriptor::CPPTYPE_UINT32, -1);
  188. HANDLE_TYPE(uint64, FieldDescriptor::CPPTYPE_UINT64, -1);
  189. HANDLE_TYPE(float, FieldDescriptor::CPPTYPE_FLOAT, -1);
  190. HANDLE_TYPE(double, FieldDescriptor::CPPTYPE_DOUBLE, -1);
  191. HANDLE_TYPE(bool, FieldDescriptor::CPPTYPE_BOOL, -1);
  192. #undef HANDLE_TYPE
  193. void* Reflection::MutableRawRepeatedString(
  194. Message* message, const FieldDescriptor* field, bool is_string) const {
  195. return MutableRawRepeatedField(message, field,
  196. FieldDescriptor::CPPTYPE_STRING, FieldOptions::STRING, NULL);
  197. }
  198. MapIterator Reflection::MapBegin(
  199. Message* message,
  200. const FieldDescriptor* field) const {
  201. GOOGLE_LOG(FATAL) << "Unimplemented Map Reflection API.";
  202. MapIterator iter(message, field);
  203. return iter;
  204. }
  205. MapIterator Reflection::MapEnd(
  206. Message* message,
  207. const FieldDescriptor* field) const {
  208. GOOGLE_LOG(FATAL) << "Unimplemented Map Reflection API.";
  209. MapIterator iter(message, field);
  210. return iter;
  211. }
  212. // =============================================================================
  213. // MessageFactory
  214. MessageFactory::~MessageFactory() {}
  215. namespace {
  216. class GeneratedMessageFactory : public MessageFactory {
  217. public:
  218. static GeneratedMessageFactory* singleton();
  219. typedef void RegistrationFunc(const string&);
  220. void RegisterFile(const char* file, RegistrationFunc* registration_func);
  221. void RegisterType(const Descriptor* descriptor, const Message* prototype);
  222. // implements MessageFactory ---------------------------------------
  223. const Message* GetPrototype(const Descriptor* type);
  224. private:
  225. // Only written at static init time, so does not require locking.
  226. hash_map<const char*, RegistrationFunc*,
  227. hash<const char*>, streq> file_map_;
  228. Mutex mutex_;
  229. // Initialized lazily, so requires locking.
  230. hash_map<const Descriptor*, const Message*> type_map_;
  231. };
  232. GeneratedMessageFactory* GeneratedMessageFactory::singleton() {
  233. static auto instance = internal::OnShutdownDelete(new GeneratedMessageFactory);
  234. return instance;
  235. }
  236. void GeneratedMessageFactory::RegisterFile(
  237. const char* file, RegistrationFunc* registration_func) {
  238. if (!InsertIfNotPresent(&file_map_, file, registration_func)) {
  239. GOOGLE_LOG(FATAL) << "File is already registered: " << file;
  240. }
  241. }
  242. void GeneratedMessageFactory::RegisterType(const Descriptor* descriptor,
  243. const Message* prototype) {
  244. GOOGLE_DCHECK_EQ(descriptor->file()->pool(), DescriptorPool::generated_pool())
  245. << "Tried to register a non-generated type with the generated "
  246. "type registry.";
  247. // This should only be called as a result of calling a file registration
  248. // function during GetPrototype(), in which case we already have locked
  249. // the mutex.
  250. mutex_.AssertHeld();
  251. if (!InsertIfNotPresent(&type_map_, descriptor, prototype)) {
  252. GOOGLE_LOG(DFATAL) << "Type is already registered: " << descriptor->full_name();
  253. }
  254. }
  255. const Message* GeneratedMessageFactory::GetPrototype(const Descriptor* type) {
  256. {
  257. ReaderMutexLock lock(&mutex_);
  258. const Message* result = FindPtrOrNull(type_map_, type);
  259. if (result != NULL) return result;
  260. }
  261. // If the type is not in the generated pool, then we can't possibly handle
  262. // it.
  263. if (type->file()->pool() != DescriptorPool::generated_pool()) return NULL;
  264. // Apparently the file hasn't been registered yet. Let's do that now.
  265. RegistrationFunc* registration_func =
  266. FindPtrOrNull(file_map_, type->file()->name().c_str());
  267. if (registration_func == NULL) {
  268. GOOGLE_LOG(DFATAL) << "File appears to be in generated pool but wasn't "
  269. "registered: " << type->file()->name();
  270. return NULL;
  271. }
  272. WriterMutexLock lock(&mutex_);
  273. // Check if another thread preempted us.
  274. const Message* result = FindPtrOrNull(type_map_, type);
  275. if (result == NULL) {
  276. // Nope. OK, register everything.
  277. registration_func(type->file()->name());
  278. // Should be here now.
  279. result = FindPtrOrNull(type_map_, type);
  280. }
  281. if (result == NULL) {
  282. GOOGLE_LOG(DFATAL) << "Type appears to be in generated pool but wasn't "
  283. << "registered: " << type->full_name();
  284. }
  285. return result;
  286. }
  287. } // namespace
  288. MessageFactory* MessageFactory::generated_factory() {
  289. return GeneratedMessageFactory::singleton();
  290. }
  291. void MessageFactory::InternalRegisterGeneratedFile(
  292. const char* filename, void (*register_messages)(const string&)) {
  293. GeneratedMessageFactory::singleton()->RegisterFile(filename,
  294. register_messages);
  295. }
  296. void MessageFactory::InternalRegisterGeneratedMessage(
  297. const Descriptor* descriptor, const Message* prototype) {
  298. GeneratedMessageFactory::singleton()->RegisterType(descriptor, prototype);
  299. }
  300. MessageFactory* Reflection::GetMessageFactory() const {
  301. GOOGLE_LOG(FATAL) << "Not implemented.";
  302. return NULL;
  303. }
  304. void* Reflection::RepeatedFieldData(
  305. Message* message, const FieldDescriptor* field,
  306. FieldDescriptor::CppType cpp_type,
  307. const Descriptor* message_type) const {
  308. GOOGLE_LOG(FATAL) << "Not implemented.";
  309. return NULL;
  310. }
  311. namespace internal {
  312. RepeatedFieldAccessor::~RepeatedFieldAccessor() {
  313. }
  314. } // namespace internal
  315. const internal::RepeatedFieldAccessor* Reflection::RepeatedFieldAccessor(
  316. const FieldDescriptor* field) const {
  317. GOOGLE_CHECK(field->is_repeated());
  318. switch (field->cpp_type()) {
  319. #define HANDLE_PRIMITIVE_TYPE(TYPE, type) \
  320. case FieldDescriptor::CPPTYPE_ ## TYPE: \
  321. return internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<type> >::get();
  322. HANDLE_PRIMITIVE_TYPE(INT32, int32)
  323. HANDLE_PRIMITIVE_TYPE(UINT32, uint32)
  324. HANDLE_PRIMITIVE_TYPE(INT64, int64)
  325. HANDLE_PRIMITIVE_TYPE(UINT64, uint64)
  326. HANDLE_PRIMITIVE_TYPE(FLOAT, float)
  327. HANDLE_PRIMITIVE_TYPE(DOUBLE, double)
  328. HANDLE_PRIMITIVE_TYPE(BOOL, bool)
  329. HANDLE_PRIMITIVE_TYPE(ENUM, int32)
  330. #undef HANDLE_PRIMITIVE_TYPE
  331. case FieldDescriptor::CPPTYPE_STRING:
  332. switch (field->options().ctype()) {
  333. default:
  334. case FieldOptions::STRING:
  335. return internal::Singleton<internal::RepeatedPtrFieldStringAccessor>::get();
  336. }
  337. break;
  338. case FieldDescriptor::CPPTYPE_MESSAGE:
  339. if (field->is_map()) {
  340. return internal::Singleton<internal::MapFieldAccessor>::get();
  341. } else {
  342. return internal::Singleton<internal::RepeatedPtrFieldMessageAccessor>::get();
  343. }
  344. }
  345. GOOGLE_LOG(FATAL) << "Should not reach here.";
  346. return NULL;
  347. }
  348. namespace internal {
  349. namespace {
  350. void ShutdownRepeatedFieldAccessor() {
  351. internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<int32> >::ShutDown();
  352. internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<uint32> >::ShutDown();
  353. internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<int64> >::ShutDown();
  354. internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<uint64> >::ShutDown();
  355. internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<float> >::ShutDown();
  356. internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<double> >::ShutDown();
  357. internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<bool> >::ShutDown();
  358. internal::Singleton<internal::RepeatedPtrFieldStringAccessor>::ShutDown();
  359. internal::Singleton<internal::RepeatedPtrFieldMessageAccessor>::ShutDown();
  360. internal::Singleton<internal::MapFieldAccessor>::ShutDown();
  361. }
  362. struct ShutdownRepeatedFieldRegister {
  363. ShutdownRepeatedFieldRegister() {
  364. OnShutdown(&ShutdownRepeatedFieldAccessor);
  365. }
  366. } shutdown_;
  367. } // namespace
  368. } // namespace internal
  369. namespace internal {
  370. template<>
  371. #if defined(_MSC_VER) && (_MSC_VER >= 1800)
  372. // Note: force noinline to workaround MSVC compiler bug with /Zc:inline, issue #240
  373. GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE
  374. #endif
  375. Message* GenericTypeHandler<Message>::NewFromPrototype(
  376. const Message* prototype, google::protobuf::Arena* arena) {
  377. return prototype->New(arena);
  378. }
  379. template<>
  380. #if defined(_MSC_VER) && (_MSC_VER >= 1800)
  381. // Note: force noinline to workaround MSVC compiler bug with /Zc:inline, issue #240
  382. GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE
  383. #endif
  384. google::protobuf::Arena* GenericTypeHandler<Message>::GetArena(
  385. Message* value) {
  386. return value->GetArena();
  387. }
  388. template<>
  389. #if defined(_MSC_VER) && (_MSC_VER >= 1800)
  390. // Note: force noinline to workaround MSVC compiler bug with /Zc:inline, issue #240
  391. GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE
  392. #endif
  393. void* GenericTypeHandler<Message>::GetMaybeArenaPointer(
  394. Message* value) {
  395. return value->GetMaybeArenaPointer();
  396. }
  397. } // namespace internal
  398. } // namespace protobuf
  399. } // namespace google