oid.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2014 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 <array>
  16. #include <ctime>
  17. #include <string>
  18. #include <bsoncxx/stdx/string_view.hpp>
  19. #include <bsoncxx/config/prelude.hpp>
  20. namespace bsoncxx {
  21. BSONCXX_INLINE_NAMESPACE_BEGIN
  22. ///
  23. /// Represents a MongoDB ObjectId. As this BSON type is used within the MongoDB server
  24. /// as a primary key for each document, it is useful for representing a 'pointer'
  25. /// to another document.
  26. ///
  27. /// @note we use 'oid' to refer to this concrete class. We use 'ObjectId' to refer
  28. /// to the BSON type.
  29. ///
  30. /// @see https://docs.mongodb.com/master/reference/object-id/
  31. ///
  32. class BSONCXX_API oid {
  33. public:
  34. static constexpr std::size_t k_oid_length = 12;
  35. ///
  36. /// Constructs an oid and initializes it to a newly generated ObjectId.
  37. ///
  38. oid();
  39. ///
  40. /// Constructs an oid initializes it to the contents of the provided buffer.
  41. ///
  42. /// @param bytes
  43. /// A pointer a buffer containing a valid ObjectId.
  44. /// @param len
  45. /// The length of the buffer. Should be equal to oid::size().
  46. ///
  47. /// @throws bsoncxx::exception if the length is not equal to oid::size().
  48. ///
  49. explicit oid(const char* bytes, std::size_t len);
  50. ///
  51. /// Constructs an oid and initializes it from the provided hex string.
  52. ///
  53. /// @param str
  54. /// A string of a hexadecimal representation of a valid ObjectId.
  55. ///
  56. /// @throws bsoncxx::exception if the string isn't an OID-sized hex
  57. /// string.
  58. ///
  59. explicit oid(const bsoncxx::stdx::string_view& str);
  60. ///
  61. /// Converts this oid to a hexadecimal string.
  62. ///
  63. /// @return A hexadecimal string representation of this ObjectId.
  64. ///
  65. std::string to_string() const;
  66. ///
  67. /// Returns the number of bytes in this ObjectId.
  68. ///
  69. /// @return The length of this oid's buffer.
  70. ///
  71. static std::size_t size() {
  72. return k_oid_length;
  73. }
  74. ///
  75. /// @{
  76. ///
  77. /// Relational operators for OIDs
  78. ///
  79. /// @relates oid
  80. ///
  81. friend BSONCXX_API bool BSONCXX_CALL operator<(const oid& lhs, const oid& rhs);
  82. friend BSONCXX_API bool BSONCXX_CALL operator>(const oid& lhs, const oid& rhs);
  83. friend BSONCXX_API bool BSONCXX_CALL operator<=(const oid& lhs, const oid& rhs);
  84. friend BSONCXX_API bool BSONCXX_CALL operator>=(const oid& lhs, const oid& rhs);
  85. friend BSONCXX_API bool BSONCXX_CALL operator==(const oid& lhs, const oid& rhs);
  86. friend BSONCXX_API bool BSONCXX_CALL operator!=(const oid& lhs, const oid& rhs);
  87. ///
  88. /// @}
  89. ///
  90. ///
  91. /// Extracts the timestamp portion of the underlying ObjectId.
  92. ///
  93. /// @return A std::time_t initialized to the timestamp.
  94. ///
  95. std::time_t get_time_t() const;
  96. ///
  97. /// An accessor for the internal data buffer in the oid.
  98. ///
  99. /// @return A pointer to the internal buffer holding the oid bytes.
  100. ///
  101. const char* bytes() const;
  102. private:
  103. friend BSONCXX_PRIVATE int oid_compare(const oid& lhs, const oid& rhs);
  104. std::array<char, k_oid_length> _bytes;
  105. };
  106. BSONCXX_INLINE_NAMESPACE_END
  107. } // namespace bsoncxx
  108. #include <bsoncxx/config/postlude.hpp>