MongoDB C++ Driver 4.2.0
Loading...
Searching...
No Matches
oid.hpp
Go to the documentation of this file.
1// Copyright 2009-present 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
15#pragma once
16
17#include <bsoncxx/oid-fwd.hpp> // IWYU pragma: export
18
19//
20
21#include <bsoncxx/v1/detail/type_traits.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
22#include <bsoncxx/v1/oid.hpp> // IWYU pragma: export
23
24#include <array> // IWYU pragma: keep: backward compatibility, to be removed.
25#include <ctime>
26#include <string>
27
29
31
32namespace bsoncxx {
33namespace v_noabi {
34
41class oid {
42 private:
43 v1::oid _oid;
44
45 public:
46 static constexpr std::size_t k_oid_length = v1::oid::k_oid_length;
47
52
56 /* explicit(false) */ oid(v1::oid const& oid) noexcept : _oid{oid} {}
57
68 explicit BSONCXX_ABI_EXPORT_CDECL() oid(char const* bytes, std::size_t len);
69
79 explicit BSONCXX_ABI_EXPORT_CDECL() oid(v1::stdx::string_view const& str);
80
84 explicit operator v1::oid() const noexcept {
85 return _oid;
86 }
87
93 std::string to_string() const {
94 return _oid.to_string();
95 }
96
102 static std::size_t size() {
103 return k_oid_length;
104 }
105
112 std::time_t get_time_t() const {
113 return _oid.get_time_t();
114 }
115
121 char const* bytes() const {
122 return reinterpret_cast<char const*>(_oid.bytes());
123 }
124
128 int compare(oid const& other) const {
129 return _oid.compare(other._oid);
130 }
131
138 friend bool operator<(oid const& lhs, oid const& rhs) {
139 return lhs._oid < rhs._oid;
140 }
141
142 friend bool operator>(oid const& lhs, oid const& rhs) {
143 return lhs._oid > rhs._oid;
144 }
145
146 friend bool operator<=(oid const& lhs, oid const& rhs) {
147 return lhs._oid <= rhs._oid;
148 }
149
150 friend bool operator>=(oid const& lhs, oid const& rhs) {
151 return lhs._oid >= rhs._oid;
152 }
153
154 friend bool operator==(oid const& lhs, oid const& rhs) {
155 return lhs._oid == rhs._oid;
156 }
157
158 friend bool operator!=(oid const& lhs, oid const& rhs) {
159 return lhs._oid != rhs._oid;
160 }
161
163
164 private:
165 friend BSONCXX_ABI_EXPORT_CDECL(int) oid_compare(oid const& lhs, oid const& rhs);
166};
167
168BSONCXX_PRIVATE_INLINE_CXX17 constexpr std::size_t oid::k_oid_length;
169
173inline oid from_v1(v1::oid const& v) {
174 return {v};
175}
176
180inline v1::oid to_v1(v_noabi::oid const& v) {
181 return v1::oid{v};
182}
183
184} // namespace v_noabi
185} // namespace bsoncxx
186
188
#define BSONCXX_ABI_EXPORT_CDECL(...)
Equivalent to BSONCXX_ABI_EXPORT with BSONCXX_ABI_CDECL.
Definition export.hpp:52
The bsoncxx v_noabi macro guard postlude header.
The bsoncxx v_noabi macro guard prelude header.
oid()
Constructs an oid and initializes it to a newly generated ObjectId.
A BSON ObjectID.
Definition oid.hpp:43
static constexpr std::size_t k_oid_length
The number of bytes required to represent an ObjectID.
Definition oid.hpp:48
Represents a MongoDB BSON ObjectId.
Definition oid.hpp:41
std::string to_string() const
Converts this oid to a hexadecimal string.
Definition oid.hpp:93
friend bool operator>=(oid const &lhs, oid const &rhs)
Relational comparison operator.
Definition oid.hpp:150
int compare(oid const &other) const
Equivalent to bson_oid_compare.
Definition oid.hpp:128
static std::size_t size()
Returns the number of bytes in this ObjectId.
Definition oid.hpp:102
friend bool operator!=(oid const &lhs, oid const &rhs)
Relational comparison operator.
Definition oid.hpp:158
std::time_t get_time_t() const
Extracts the timestamp portion of the underlying ObjectId.
Definition oid.hpp:112
oid()
Constructs an oid and initializes it to a newly generated ObjectId.
friend bool operator<(oid const &lhs, oid const &rhs)
Relational comparison operator.
Definition oid.hpp:138
friend bool operator==(oid const &lhs, oid const &rhs)
Relational comparison operator.
Definition oid.hpp:154
friend bool operator<=(oid const &lhs, oid const &rhs)
Relational comparison operator.
Definition oid.hpp:146
char const * bytes() const
An accessor for the internal data buffer in the oid.
Definition oid.hpp:121
friend bool operator>(oid const &lhs, oid const &rhs)
Relational comparison operator.
Definition oid.hpp:142
Declares entities whose ABI stability is guaranteed for documented symbols.
Declares C++17 standard library polyfills.
Declares entities whose ABI stability is NOT guaranteed.
v1::element::view to_v1(v_noabi::array::element const &v)
Convert to the bsoncxx::v1 equivalent of v.
Definition element.hpp:132
v_noabi::array::value from_v1(v1::array::value const &v)
Convert from the bsoncxx::v1 equivalent of v.
Definition value.hpp:267
The top-level namespace within which all bsoncxx library entities are declared.
For internal use only!
Provides bsoncxx::v1::oid.
Declares bsoncxx::v_noabi::oid.
Provides std::string_view-related polyfills for library API usage.