123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- #ifndef REALM_OBJECT_ID_HPP
- #define REALM_OBJECT_ID_HPP
- #include <array>
- #include <cstdint>
- #include <cstring>
- #include <realm/timestamp.hpp>
- namespace realm {
- class ObjectId {
- public:
- static constexpr size_t num_bytes = 12;
- using ObjectIdBytes = std::array<uint8_t, num_bytes>;
-
- ObjectId() noexcept = default;
-
- static bool is_valid_str(StringData) noexcept;
-
- ObjectId(const char* init) noexcept;
-
- ObjectId(const ObjectIdBytes& init) noexcept;
-
- ObjectId(Timestamp d, int machine_id, int process_id) noexcept;
-
- static ObjectId gen();
- bool operator==(const ObjectId& other) const
- {
- return m_bytes == other.m_bytes;
- }
- bool operator!=(const ObjectId& other) const
- {
- return m_bytes != other.m_bytes;
- }
- bool operator>(const ObjectId& other) const
- {
- return m_bytes > other.m_bytes;
- }
- bool operator<(const ObjectId& other) const
- {
- return m_bytes < other.m_bytes;
- }
- bool operator>=(const ObjectId& other) const
- {
- return m_bytes >= other.m_bytes;
- }
- bool operator<=(const ObjectId& other) const
- {
- return m_bytes <= other.m_bytes;
- }
- explicit operator Timestamp() const
- {
- return get_timestamp();
- }
- Timestamp get_timestamp() const;
- std::string to_string() const;
- ObjectIdBytes to_bytes() const;
- size_t hash() const noexcept;
- private:
- ObjectIdBytes m_bytes = {};
- };
- inline std::ostream& operator<<(std::ostream& ostr, const ObjectId& id)
- {
- ostr << id.to_string();
- return ostr;
- }
- }
- namespace std {
- template <>
- struct hash<realm::ObjectId> {
- size_t operator()(const realm::ObjectId& oid) const noexcept
- {
- return oid.hash();
- }
- };
- }
- #endif
|