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/v1/oid-fwd.hpp> // IWYU pragma: export
18
19//
20
22
26
27#include <array>
28#include <cstddef>
29#include <cstdint>
30#include <ctime>
31#include <string>
32#include <system_error>
33#include <type_traits>
34
35namespace bsoncxx {
36namespace v1 {
37
43class oid {
44 public:
48 static constexpr std::size_t k_oid_length = 12;
49
50 private:
51 std::array<std::uint8_t, k_oid_length> _bytes;
52
53 public:
72
83 BSONCXX_ABI_EXPORT_CDECL() oid(std::uint8_t const* bytes, std::size_t len);
84
93 explicit BSONCXX_ABI_EXPORT_CDECL() oid(v1::stdx::string_view str);
94
98 static std::size_t size() {
99 return k_oid_length;
100 }
101
105 std::uint8_t const* bytes() const {
106 return _bytes.data();
107 }
108
113
118
122 BSONCXX_ABI_EXPORT_CDECL(int) compare(oid const& other) const;
123
127 friend bool operator==(oid const& lhs, oid const& rhs) {
128 return lhs.compare(rhs) == 0;
129 }
130
134 friend bool operator!=(oid const& lhs, oid const& rhs) {
135 return lhs.compare(rhs) != 0;
136 }
137
141 friend bool operator<(oid const& lhs, oid const& rhs) {
142 return lhs.compare(rhs) < 0;
143 }
144
148 friend bool operator<=(oid const& lhs, oid const& rhs) {
149 return lhs.compare(rhs) <= 0;
150 }
151
155 friend bool operator>(oid const& lhs, oid const& rhs) {
156 return lhs.compare(rhs) > 0;
157 }
158
162 friend bool operator>=(oid const& lhs, oid const& rhs) {
163 return lhs.compare(rhs) >= 0;
164 }
165
178
184 static BSONCXX_ABI_EXPORT_CDECL(std::error_category const&) error_category();
185
191 friend std::error_code make_error_code(errc v) {
192 return {static_cast<int>(v), error_category()};
193 }
194
195 class internal;
196
197 private:
198 struct for_overwrite_tag {};
199
200 /* explicit(false) */ oid(for_overwrite_tag) : _bytes{} {}
201};
202
203BSONCXX_PRIVATE_INLINE_CXX17 constexpr std::size_t oid::k_oid_length;
204
205} // namespace v1
206} // namespace bsoncxx
207
208template <>
209struct std::is_error_code_enum<bsoncxx::v1::oid::errc> : true_type {};
210
212
Provides macros to control the set of symbols exported in the ABI.
#define BSONCXX_ABI_EXPORT_CDECL(...)
Equivalent to BSONCXX_ABI_EXPORT with BSONCXX_ABI_CDECL.
Definition export.hpp:52
For internal use only!
The bsoncxx v1 macro guard postlude header.
The bsoncxx v1 macro guard prelude header.
friend bool operator>=(oid const &lhs, oid const &rhs)
Equivalent to lhs.compare(rhs) >= 0.
Definition oid.hpp:162
oid()
Initialize with a unique ObjectID.
static std::size_t size()
Return k_oid_length.
Definition oid.hpp:98
friend bool operator!=(oid const &lhs, oid const &rhs)
Equivalent to lhs.compare(rhs) != 0.
Definition oid.hpp:134
friend bool operator<(oid const &lhs, oid const &rhs)
Equivalent to lhs.compare(rhs) < 0.
Definition oid.hpp:141
friend bool operator==(oid const &lhs, oid const &rhs)
Equivalent to lhs.compare(rhs) == 0.
Definition oid.hpp:127
static constexpr std::size_t k_oid_length
The number of bytes required to represent an ObjectID.
Definition oid.hpp:48
friend bool operator<=(oid const &lhs, oid const &rhs)
Equivalent to lhs.compare(rhs) <= 0.
Definition oid.hpp:148
std::uint8_t const * bytes() const
Return a pointer to the byte representation.
Definition oid.hpp:105
friend std::error_code make_error_code(errc v)
Support implicit conversion to std::error_code.
Definition oid.hpp:191
std::time_t get_time_t() const
Return the timestamp component.
static std::error_category const & error_category()
The error category for bsoncxx::v1::oid::errc.
errc
Errors codes may be returned by bsoncxx::v1::oid.
Definition oid.hpp:171
@ invalid_length
Byte length must equal bsoncxx::v1::oid::k_oid_length.
Definition oid.hpp:174
@ invalid_string
String is not a valid ObjectID representation.
Definition oid.hpp:176
@ empty_string
String must not be empty.
Definition oid.hpp:175
@ zero
Zero.
Definition oid.hpp:172
@ null_bytes_ptr
Bytes pointer must not be null.
Definition oid.hpp:173
friend bool operator>(oid const &lhs, oid const &rhs)
Equivalent to lhs.compare(rhs) > 0.
Definition oid.hpp:155
int compare(oid const &other) const
Equivalent to bson_oid_compare.
std::string to_string() const
Return the hexadecimal representation.
Declares C++17 standard library polyfills.
Declares entities whose ABI stability is guaranteed for documented symbols.
The top-level namespace within which all bsoncxx library entities are declared.
Declares bsoncxx::v1::oid.
Provides std::string_view-related polyfills for library API usage.