诸暨麻将添加redis
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

345 righe
13 KiB

  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 <google/protobuf/reflection_ops.h>
  34. #include <string>
  35. #include <vector>
  36. #include <google/protobuf/stubs/logging.h>
  37. #include <google/protobuf/stubs/common.h>
  38. #include <google/protobuf/descriptor.pb.h>
  39. #include <google/protobuf/descriptor.h>
  40. #include <google/protobuf/map_field.h>
  41. #include <google/protobuf/map_field_inl.h>
  42. #include <google/protobuf/unknown_field_set.h>
  43. #include <google/protobuf/stubs/strutil.h>
  44. #include <google/protobuf/port_def.inc>
  45. namespace google {
  46. namespace protobuf {
  47. namespace internal {
  48. static const Reflection* GetReflectionOrDie(const Message& m) {
  49. const Reflection* r = m.GetReflection();
  50. if (r == nullptr) {
  51. const Descriptor* d = m.GetDescriptor();
  52. const std::string& mtype = d ? d->name() : "unknown";
  53. // RawMessage is one known type for which GetReflection() returns nullptr.
  54. GOOGLE_LOG(FATAL) << "Message does not support reflection (type " << mtype << ").";
  55. }
  56. return r;
  57. }
  58. void ReflectionOps::Copy(const Message& from, Message* to) {
  59. if (&from == to) return;
  60. Clear(to);
  61. Merge(from, to);
  62. }
  63. void ReflectionOps::Merge(const Message& from, Message* to) {
  64. GOOGLE_CHECK_NE(&from, to);
  65. const Descriptor* descriptor = from.GetDescriptor();
  66. GOOGLE_CHECK_EQ(to->GetDescriptor(), descriptor)
  67. << "Tried to merge messages of different types "
  68. << "(merge " << descriptor->full_name() << " to "
  69. << to->GetDescriptor()->full_name() << ")";
  70. const Reflection* from_reflection = GetReflectionOrDie(from);
  71. const Reflection* to_reflection = GetReflectionOrDie(*to);
  72. bool is_from_generated = (from_reflection->GetMessageFactory() ==
  73. google::protobuf::MessageFactory::generated_factory());
  74. bool is_to_generated = (to_reflection->GetMessageFactory() ==
  75. google::protobuf::MessageFactory::generated_factory());
  76. std::vector<const FieldDescriptor*> fields;
  77. from_reflection->ListFields(from, &fields);
  78. for (int i = 0; i < fields.size(); i++) {
  79. const FieldDescriptor* field = fields[i];
  80. if (field->is_repeated()) {
  81. // Use map reflection if both are in map status and have the
  82. // same map type to avoid sync with repeated field.
  83. // Note: As from and to messages have the same descriptor, the
  84. // map field types are the same if they are both generated
  85. // messages or both dynamic messages.
  86. if (is_from_generated == is_to_generated && field->is_map()) {
  87. const MapFieldBase* from_field =
  88. from_reflection->GetMapData(from, field);
  89. MapFieldBase* to_field = to_reflection->MutableMapData(to, field);
  90. if (to_field->IsMapValid() && from_field->IsMapValid()) {
  91. to_field->MergeFrom(*from_field);
  92. continue;
  93. }
  94. }
  95. int count = from_reflection->FieldSize(from, field);
  96. for (int j = 0; j < count; j++) {
  97. switch (field->cpp_type()) {
  98. #define HANDLE_TYPE(CPPTYPE, METHOD) \
  99. case FieldDescriptor::CPPTYPE_##CPPTYPE: \
  100. to_reflection->Add##METHOD( \
  101. to, field, from_reflection->GetRepeated##METHOD(from, field, j)); \
  102. break;
  103. HANDLE_TYPE(INT32, Int32);
  104. HANDLE_TYPE(INT64, Int64);
  105. HANDLE_TYPE(UINT32, UInt32);
  106. HANDLE_TYPE(UINT64, UInt64);
  107. HANDLE_TYPE(FLOAT, Float);
  108. HANDLE_TYPE(DOUBLE, Double);
  109. HANDLE_TYPE(BOOL, Bool);
  110. HANDLE_TYPE(STRING, String);
  111. HANDLE_TYPE(ENUM, Enum);
  112. #undef HANDLE_TYPE
  113. case FieldDescriptor::CPPTYPE_MESSAGE:
  114. to_reflection->AddMessage(to, field)->MergeFrom(
  115. from_reflection->GetRepeatedMessage(from, field, j));
  116. break;
  117. }
  118. }
  119. } else {
  120. switch (field->cpp_type()) {
  121. #define HANDLE_TYPE(CPPTYPE, METHOD) \
  122. case FieldDescriptor::CPPTYPE_##CPPTYPE: \
  123. to_reflection->Set##METHOD(to, field, \
  124. from_reflection->Get##METHOD(from, field)); \
  125. break;
  126. HANDLE_TYPE(INT32, Int32);
  127. HANDLE_TYPE(INT64, Int64);
  128. HANDLE_TYPE(UINT32, UInt32);
  129. HANDLE_TYPE(UINT64, UInt64);
  130. HANDLE_TYPE(FLOAT, Float);
  131. HANDLE_TYPE(DOUBLE, Double);
  132. HANDLE_TYPE(BOOL, Bool);
  133. HANDLE_TYPE(STRING, String);
  134. HANDLE_TYPE(ENUM, Enum);
  135. #undef HANDLE_TYPE
  136. case FieldDescriptor::CPPTYPE_MESSAGE:
  137. to_reflection->MutableMessage(to, field)->MergeFrom(
  138. from_reflection->GetMessage(from, field));
  139. break;
  140. }
  141. }
  142. }
  143. to_reflection->MutableUnknownFields(to)->MergeFrom(
  144. from_reflection->GetUnknownFields(from));
  145. }
  146. void ReflectionOps::Clear(Message* message) {
  147. const Reflection* reflection = GetReflectionOrDie(*message);
  148. std::vector<const FieldDescriptor*> fields;
  149. reflection->ListFields(*message, &fields);
  150. for (int i = 0; i < fields.size(); i++) {
  151. reflection->ClearField(message, fields[i]);
  152. }
  153. reflection->MutableUnknownFields(message)->Clear();
  154. }
  155. bool ReflectionOps::IsInitialized(const Message& message) {
  156. const Descriptor* descriptor = message.GetDescriptor();
  157. const Reflection* reflection = GetReflectionOrDie(message);
  158. // Check required fields of this message.
  159. for (int i = 0; i < descriptor->field_count(); i++) {
  160. if (descriptor->field(i)->is_required()) {
  161. if (!reflection->HasField(message, descriptor->field(i))) {
  162. return false;
  163. }
  164. }
  165. }
  166. // Check that sub-messages are initialized.
  167. std::vector<const FieldDescriptor*> fields;
  168. reflection->ListFields(message, &fields);
  169. for (int i = 0; i < fields.size(); i++) {
  170. const FieldDescriptor* field = fields[i];
  171. if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
  172. if (field->is_map()) {
  173. const FieldDescriptor* value_field = field->message_type()->field(1);
  174. if (value_field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
  175. const MapFieldBase* map_field =
  176. reflection->GetMapData(message, field);
  177. if (map_field->IsMapValid()) {
  178. MapIterator iter(const_cast<Message*>(&message), field);
  179. MapIterator end(const_cast<Message*>(&message), field);
  180. for (map_field->MapBegin(&iter), map_field->MapEnd(&end);
  181. iter != end; ++iter) {
  182. if (!iter.GetValueRef().GetMessageValue().IsInitialized()) {
  183. return false;
  184. }
  185. }
  186. continue;
  187. }
  188. } else {
  189. continue;
  190. }
  191. }
  192. if (field->is_repeated()) {
  193. int size = reflection->FieldSize(message, field);
  194. for (int j = 0; j < size; j++) {
  195. if (!reflection->GetRepeatedMessage(message, field, j)
  196. .IsInitialized()) {
  197. return false;
  198. }
  199. }
  200. } else {
  201. if (!reflection->GetMessage(message, field).IsInitialized()) {
  202. return false;
  203. }
  204. }
  205. }
  206. }
  207. return true;
  208. }
  209. static bool IsMapValueMessageTyped(const FieldDescriptor* map_field) {
  210. return map_field->message_type()->field(1)->cpp_type() ==
  211. FieldDescriptor::CPPTYPE_MESSAGE;
  212. }
  213. void ReflectionOps::DiscardUnknownFields(Message* message) {
  214. const Reflection* reflection = GetReflectionOrDie(*message);
  215. reflection->MutableUnknownFields(message)->Clear();
  216. // Walk through the fields of this message and DiscardUnknownFields on any
  217. // messages present.
  218. std::vector<const FieldDescriptor*> fields;
  219. reflection->ListFields(*message, &fields);
  220. for (int i = 0; i < fields.size(); i++) {
  221. const FieldDescriptor* field = fields[i];
  222. // Skip over non-message fields.
  223. if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) {
  224. continue;
  225. }
  226. // Discard the unknown fields in maps that contain message values.
  227. if (field->is_map() && IsMapValueMessageTyped(field)) {
  228. const MapFieldBase* map_field =
  229. reflection->MutableMapData(message, field);
  230. if (map_field->IsMapValid()) {
  231. MapIterator iter(message, field);
  232. MapIterator end(message, field);
  233. for (map_field->MapBegin(&iter), map_field->MapEnd(&end); iter != end;
  234. ++iter) {
  235. iter.MutableValueRef()->MutableMessageValue()->DiscardUnknownFields();
  236. }
  237. }
  238. // Discard every unknown field inside messages in a repeated field.
  239. } else if (field->is_repeated()) {
  240. int size = reflection->FieldSize(*message, field);
  241. for (int j = 0; j < size; j++) {
  242. reflection->MutableRepeatedMessage(message, field, j)
  243. ->DiscardUnknownFields();
  244. }
  245. // Discard the unknown fields inside an optional message.
  246. } else {
  247. reflection->MutableMessage(message, field)->DiscardUnknownFields();
  248. }
  249. }
  250. }
  251. static std::string SubMessagePrefix(const std::string& prefix,
  252. const FieldDescriptor* field, int index) {
  253. std::string result(prefix);
  254. if (field->is_extension()) {
  255. result.append("(");
  256. result.append(field->full_name());
  257. result.append(")");
  258. } else {
  259. result.append(field->name());
  260. }
  261. if (index != -1) {
  262. result.append("[");
  263. result.append(StrCat(index));
  264. result.append("]");
  265. }
  266. result.append(".");
  267. return result;
  268. }
  269. void ReflectionOps::FindInitializationErrors(const Message& message,
  270. const std::string& prefix,
  271. std::vector<std::string>* errors) {
  272. const Descriptor* descriptor = message.GetDescriptor();
  273. const Reflection* reflection = GetReflectionOrDie(message);
  274. // Check required fields of this message.
  275. for (int i = 0; i < descriptor->field_count(); i++) {
  276. if (descriptor->field(i)->is_required()) {
  277. if (!reflection->HasField(message, descriptor->field(i))) {
  278. errors->push_back(prefix + descriptor->field(i)->name());
  279. }
  280. }
  281. }
  282. // Check sub-messages.
  283. std::vector<const FieldDescriptor*> fields;
  284. reflection->ListFields(message, &fields);
  285. for (int i = 0; i < fields.size(); i++) {
  286. const FieldDescriptor* field = fields[i];
  287. if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
  288. if (field->is_repeated()) {
  289. int size = reflection->FieldSize(message, field);
  290. for (int j = 0; j < size; j++) {
  291. const Message& sub_message =
  292. reflection->GetRepeatedMessage(message, field, j);
  293. FindInitializationErrors(sub_message,
  294. SubMessagePrefix(prefix, field, j), errors);
  295. }
  296. } else {
  297. const Message& sub_message = reflection->GetMessage(message, field);
  298. FindInitializationErrors(sub_message,
  299. SubMessagePrefix(prefix, field, -1), errors);
  300. }
  301. }
  302. }
  303. }
  304. } // namespace internal
  305. } // namespace protobuf
  306. } // namespace google