field_comparator.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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: ksroka@google.com (Krzysztof Sroka)
  31. #include <google/protobuf/util/field_comparator.h>
  32. #include <string>
  33. #include <google/protobuf/descriptor.h>
  34. #include <google/protobuf/message.h>
  35. #include <google/protobuf/util/message_differencer.h>
  36. #include <google/protobuf/stubs/map_util.h>
  37. #include <google/protobuf/stubs/mathlimits.h>
  38. #include <google/protobuf/stubs/mathutil.h>
  39. namespace google {
  40. namespace protobuf {
  41. namespace util {
  42. FieldComparator::FieldComparator() {}
  43. FieldComparator::~FieldComparator() {}
  44. DefaultFieldComparator::DefaultFieldComparator()
  45. : float_comparison_(EXACT),
  46. treat_nan_as_equal_(false),
  47. has_default_tolerance_(false) {
  48. }
  49. DefaultFieldComparator::~DefaultFieldComparator() {}
  50. FieldComparator::ComparisonResult DefaultFieldComparator::Compare(
  51. const google::protobuf::Message& message_1,
  52. const google::protobuf::Message& message_2,
  53. const google::protobuf::FieldDescriptor* field,
  54. int index_1, int index_2,
  55. const google::protobuf::util::FieldContext* field_context) {
  56. const Reflection* reflection_1 = message_1.GetReflection();
  57. const Reflection* reflection_2 = message_2.GetReflection();
  58. switch (field->cpp_type()) {
  59. #define COMPARE_FIELD(METHOD) \
  60. if (field->is_repeated()) { \
  61. return ResultFromBoolean(Compare##METHOD( \
  62. *field, \
  63. reflection_1->GetRepeated##METHOD(message_1, field, index_1), \
  64. reflection_2->GetRepeated##METHOD(message_2, field, index_2))); \
  65. } else { \
  66. return ResultFromBoolean(Compare##METHOD( \
  67. *field, \
  68. reflection_1->Get##METHOD(message_1, field), \
  69. reflection_2->Get##METHOD(message_2, field))); \
  70. } \
  71. break; // Make sure no fall-through is introduced.
  72. case FieldDescriptor::CPPTYPE_BOOL:
  73. COMPARE_FIELD(Bool);
  74. case FieldDescriptor::CPPTYPE_DOUBLE:
  75. COMPARE_FIELD(Double);
  76. case FieldDescriptor::CPPTYPE_ENUM:
  77. COMPARE_FIELD(Enum);
  78. case FieldDescriptor::CPPTYPE_FLOAT:
  79. COMPARE_FIELD(Float);
  80. case FieldDescriptor::CPPTYPE_INT32:
  81. COMPARE_FIELD(Int32);
  82. case FieldDescriptor::CPPTYPE_INT64:
  83. COMPARE_FIELD(Int64);
  84. case FieldDescriptor::CPPTYPE_STRING:
  85. if (field->is_repeated()) {
  86. // Allocate scratch strings to store the result if a conversion is
  87. // needed.
  88. string scratch1;
  89. string scratch2;
  90. return ResultFromBoolean(
  91. CompareString(*field, reflection_1->GetRepeatedStringReference(
  92. message_1, field, index_1, &scratch1),
  93. reflection_2->GetRepeatedStringReference(
  94. message_2, field, index_2, &scratch2)));
  95. } else {
  96. // Allocate scratch strings to store the result if a conversion is
  97. // needed.
  98. string scratch1;
  99. string scratch2;
  100. return ResultFromBoolean(CompareString(
  101. *field,
  102. reflection_1->GetStringReference(message_1, field, &scratch1),
  103. reflection_2->GetStringReference(message_2, field, &scratch2)));
  104. }
  105. break;
  106. case FieldDescriptor::CPPTYPE_UINT32:
  107. COMPARE_FIELD(UInt32);
  108. case FieldDescriptor::CPPTYPE_UINT64:
  109. COMPARE_FIELD(UInt64);
  110. #undef COMPARE_FIELD
  111. case FieldDescriptor::CPPTYPE_MESSAGE:
  112. return RECURSE;
  113. default:
  114. GOOGLE_LOG(FATAL) << "No comparison code for field " << field->full_name()
  115. << " of CppType = " << field->cpp_type();
  116. return DIFFERENT;
  117. }
  118. }
  119. bool DefaultFieldComparator::Compare(
  120. MessageDifferencer* differencer,
  121. const Message& message1,
  122. const Message& message2,
  123. const google::protobuf::util::FieldContext* field_context) {
  124. return differencer->Compare(
  125. message1, message2, field_context->parent_fields());
  126. }
  127. void DefaultFieldComparator::SetDefaultFractionAndMargin(double fraction,
  128. double margin) {
  129. default_tolerance_ = Tolerance(fraction, margin);
  130. has_default_tolerance_ = true;
  131. }
  132. void DefaultFieldComparator::SetFractionAndMargin(const FieldDescriptor* field,
  133. double fraction,
  134. double margin) {
  135. GOOGLE_CHECK(FieldDescriptor::CPPTYPE_FLOAT == field->cpp_type() ||
  136. FieldDescriptor::CPPTYPE_DOUBLE == field->cpp_type())
  137. << "Field has to be float or double type. Field name is: "
  138. << field->full_name();
  139. map_tolerance_[field] = Tolerance(fraction, margin);
  140. }
  141. bool DefaultFieldComparator::CompareDouble(const FieldDescriptor& field,
  142. double value_1, double value_2) {
  143. return CompareDoubleOrFloat(field, value_1, value_2);
  144. }
  145. bool DefaultFieldComparator::CompareEnum(const FieldDescriptor& field,
  146. const EnumValueDescriptor* value_1,
  147. const EnumValueDescriptor* value_2) {
  148. return value_1->number() == value_2->number();
  149. }
  150. bool DefaultFieldComparator::CompareFloat(const FieldDescriptor& field,
  151. float value_1, float value_2) {
  152. return CompareDoubleOrFloat(field, value_1, value_2);
  153. }
  154. template<typename T>
  155. bool DefaultFieldComparator::CompareDoubleOrFloat(const FieldDescriptor& field,
  156. T value_1, T value_2) {
  157. if (value_1 == value_2) {
  158. // Covers +inf and -inf (which are not within margin or fraction of
  159. // themselves), and is a shortcut for finite values.
  160. return true;
  161. } else if (float_comparison_ == EXACT) {
  162. if (treat_nan_as_equal_ &&
  163. MathLimits<T>::IsNaN(value_1) && MathLimits<T>::IsNaN(value_2)) {
  164. return true;
  165. }
  166. return false;
  167. } else {
  168. if (treat_nan_as_equal_ &&
  169. MathLimits<T>::IsNaN(value_1) && MathLimits<T>::IsNaN(value_2)) {
  170. return true;
  171. }
  172. // float_comparison_ == APPROXIMATE covers two use cases.
  173. Tolerance* tolerance = FindOrNull(map_tolerance_, &field);
  174. if (tolerance == NULL && has_default_tolerance_) {
  175. tolerance = &default_tolerance_;
  176. }
  177. if (tolerance == NULL) {
  178. return MathUtil::AlmostEquals(value_1, value_2);
  179. } else {
  180. // Use user-provided fraction and margin. Since they are stored as
  181. // doubles, we explicitly cast them to types of values provided. This
  182. // is very likely to fail if provided values are not numeric.
  183. return MathUtil::WithinFractionOrMargin(
  184. value_1, value_2, static_cast<T>(tolerance->fraction),
  185. static_cast<T>(tolerance->margin));
  186. }
  187. }
  188. }
  189. FieldComparator::ComparisonResult DefaultFieldComparator::ResultFromBoolean(
  190. bool boolean_result) const {
  191. return boolean_result ? FieldComparator::SAME : FieldComparator::DIFFERENT;
  192. }
  193. } // namespace util
  194. } // namespace protobuf
  195. } // namespace google