MongoDB C++ Driver 4.4.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
41class oid {
42 public:
46 static constexpr std::size_t k_oid_length = 12;
47
48 private:
49 std::array<std::uint8_t, k_oid_length> _bytes;
50
51 public:
70
81 BSONCXX_ABI_EXPORT_CDECL() oid(std::uint8_t const* bytes, std::size_t len);
82
91 explicit BSONCXX_ABI_EXPORT_CDECL() oid(v1::stdx::string_view str);
92
96 static std::size_t size() {
97 return k_oid_length;
98 }
99
103 std::uint8_t const* bytes() const {
104 return _bytes.data();
105 }
106
111
116
120 BSONCXX_ABI_EXPORT_CDECL(int) compare(oid const& other) const;
121
125 friend bool operator==(oid const& lhs, oid const& rhs) {
126 return lhs.compare(rhs) == 0;
127 }
128
132 friend bool operator!=(oid const& lhs, oid const& rhs) {
133 return lhs.compare(rhs) != 0;
134 }
135
139 friend bool operator<(oid const& lhs, oid const& rhs) {
140 return lhs.compare(rhs) < 0;
141 }
142
146 friend bool operator<=(oid const& lhs, oid const& rhs) {
147 return lhs.compare(rhs) <= 0;
148 }
149
153 friend bool operator>(oid const& lhs, oid const& rhs) {
154 return lhs.compare(rhs) > 0;
155 }
156
160 friend bool operator>=(oid const& lhs, oid const& rhs) {
161 return lhs.compare(rhs) >= 0;
162 }
163
174
178 static BSONCXX_ABI_EXPORT_CDECL(std::error_category const&) error_category();
179
183 friend std::error_code make_error_code(errc v) {
184 return {static_cast<int>(v), error_category()};
185 }
186
187 class internal;
188
189 private:
190 struct for_overwrite_tag {};
191
192 /* explicit(false) */ oid(for_overwrite_tag) : _bytes{} {}
193};
194
195BSONCXX_PRIVATE_INLINE_CXX17 constexpr std::size_t oid::k_oid_length;
196
197} // namespace v1
198} // namespace bsoncxx
199
200template <>
201struct std::is_error_code_enum<bsoncxx::v1::oid::errc> : true_type {};
202
204
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:160
oid()
Initialize with a unique ObjectID.
static std::size_t size()
Return k_oid_length.
Definition oid.hpp:96
friend bool operator!=(oid const &lhs, oid const &rhs)
Equivalent to lhs.compare(rhs) != 0.
Definition oid.hpp:132
friend bool operator<(oid const &lhs, oid const &rhs)
Equivalent to lhs.compare(rhs) < 0.
Definition oid.hpp:139
friend bool operator==(oid const &lhs, oid const &rhs)
Equivalent to lhs.compare(rhs) == 0.
Definition oid.hpp:125
static constexpr std::size_t k_oid_length
The number of bytes required to represent an ObjectID.
Definition oid.hpp:46
friend bool operator<=(oid const &lhs, oid const &rhs)
Equivalent to lhs.compare(rhs) <= 0.
Definition oid.hpp:146
std::uint8_t const * bytes() const
Return a pointer to the byte representation.
Definition oid.hpp:103
friend std::error_code make_error_code(errc v)
Support implicit conversion to std::error_code.
Definition oid.hpp:183
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:167
@ invalid_length
Byte length must equal bsoncxx::v1::oid::k_oid_length.
Definition oid.hpp:170
@ invalid_string
String is not a valid ObjectID representation.
Definition oid.hpp:172
@ empty_string
String must not be empty.
Definition oid.hpp:171
@ zero
Zero.
Definition oid.hpp:168
@ null_bytes_ptr
Bytes pointer must not be null.
Definition oid.hpp:169
friend bool operator>(oid const &lhs, oid const &rhs)
Equivalent to lhs.compare(rhs) > 0.
Definition oid.hpp:153
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.