decimal128.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2016 MongoDB Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <cstdint>
  16. #include <string>
  17. #include <bsoncxx/stdx/string_view.hpp>
  18. #include <bsoncxx/config/prelude.hpp>
  19. namespace bsoncxx {
  20. BSONCXX_INLINE_NAMESPACE_BEGIN
  21. ///
  22. /// Represents an IEEE 754-2008 BSON Decimal128 value in a platform-independent way.
  23. ///
  24. class BSONCXX_API decimal128 {
  25. public:
  26. ///
  27. /// Constructs a BSON Decimal128 value representing zero.
  28. ///
  29. decimal128() = default;
  30. ///
  31. /// Constructs a BSON Decimal128 from high and low 64-bit big-endian parts.
  32. ///
  33. /// @param high
  34. /// The high 64-bits.
  35. /// @param low
  36. /// The low 64-bits.
  37. ///
  38. decimal128(uint64_t high, uint64_t low) noexcept : _high(high), _low(low) {}
  39. ///
  40. /// Constructs a BSON Decimal128 from a string.
  41. ///
  42. /// @param str
  43. /// A string representation of a decimal number.
  44. ///
  45. /// @throws bsoncxx::exception if the string isn't a valid BSON Decimal128
  46. /// representation.
  47. ///
  48. explicit decimal128(stdx::string_view str);
  49. ///
  50. /// Converts this decimal128 value to a string representation.
  51. ///
  52. /// @return A string representation of a IEEE 754-2008 decimal number.
  53. ///
  54. std::string to_string() const;
  55. ///
  56. /// @{
  57. ///
  58. /// Relational operators for decimal128
  59. ///
  60. /// @relates decimal128
  61. ///
  62. friend BSONCXX_API bool BSONCXX_CALL operator==(const decimal128& lhs, const decimal128& rhs);
  63. friend BSONCXX_API bool BSONCXX_CALL operator!=(const decimal128& lhs, const decimal128& rhs);
  64. ///
  65. /// @}
  66. ///
  67. ///
  68. /// Accessor for high 64 bits.
  69. ///
  70. BSONCXX_INLINE uint64_t high() const {
  71. return _high;
  72. }
  73. ///
  74. /// Accessor for low 64 bits.
  75. ///
  76. BSONCXX_INLINE uint64_t low() const {
  77. return _low;
  78. }
  79. private:
  80. uint64_t _high = 0;
  81. uint64_t _low = 0;
  82. };
  83. BSONCXX_INLINE_NAMESPACE_END
  84. } // namespace bsoncxx
  85. #include <bsoncxx/config/postlude.hpp>