Loading...
Searching...
No Matches
oid.hpp
Go to the documentation of this file.
// Copyright 2009-present MongoDB, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <bsoncxx/v1/oid-fwd.hpp> // IWYU pragma: export
//
#include <bsoncxx/v1/detail/prelude.hpp>
#include <bsoncxx/v1/config/export.hpp>
#include <bsoncxx/v1/detail/macros.hpp>
#include <bsoncxx/v1/stdx/string_view.hpp>
#include <array>
#include <cstddef>
#include <cstdint>
#include <ctime>
#include <string>
#include <system_error>
#include <type_traits>
namespace bsoncxx {
namespace v1 {
class oid {
public:
static constexpr std::size_t k_oid_length = 12;
private:
std::array<std::uint8_t, k_oid_length> _bytes;
public:
BSONCXX_ABI_EXPORT_CDECL() oid();
BSONCXX_ABI_EXPORT_CDECL() oid(std::uint8_t const* bytes, std::size_t len);
explicit BSONCXX_ABI_EXPORT_CDECL() oid(v1::stdx::string_view str);
static std::size_t size() {
return k_oid_length;
}
std::uint8_t const* bytes() const {
return _bytes.data();
}
BSONCXX_ABI_EXPORT_CDECL(std::string) to_string() const;
BSONCXX_ABI_EXPORT_CDECL(std::time_t) get_time_t() const;
BSONCXX_ABI_EXPORT_CDECL(int) compare(oid const& other) const;
friend bool operator==(oid const& lhs, oid const& rhs) {
return lhs.compare(rhs) == 0;
}
friend bool operator!=(oid const& lhs, oid const& rhs) {
return lhs.compare(rhs) != 0;
}
friend bool operator<(oid const& lhs, oid const& rhs) {
return lhs.compare(rhs) < 0;
}
friend bool operator<=(oid const& lhs, oid const& rhs) {
return lhs.compare(rhs) <= 0;
}
friend bool operator>(oid const& lhs, oid const& rhs) {
return lhs.compare(rhs) > 0;
}
friend bool operator>=(oid const& lhs, oid const& rhs) {
return lhs.compare(rhs) >= 0;
}
enum class errc {
zero,
null_bytes_ptr,
invalid_length,
empty_string,
invalid_string,
};
static BSONCXX_ABI_EXPORT_CDECL(std::error_category const&) error_category();
friend std::error_code make_error_code(errc v) {
return {static_cast<int>(v), error_category()};
}
class internal;
private:
struct for_overwrite_tag {};
/* explicit(false) */ oid(for_overwrite_tag) : _bytes{} {}
};
BSONCXX_PRIVATE_INLINE_CXX17 constexpr std::size_t oid::k_oid_length;
} // namespace v1
} // namespace bsoncxx
template <>
struct std::is_error_code_enum<bsoncxx::v1::oid::errc> : true_type {};
#include <bsoncxx/v1/detail/postlude.hpp>
export.hpp
Provides macros to control the set of symbols exported in the ABI.
BSONCXX_ABI_EXPORT_CDECL
#define BSONCXX_ABI_EXPORT_CDECL(...)
Equivalent to BSONCXX_ABI_EXPORT with BSONCXX_ABI_CDECL.
Definition export.hpp:52
macros.hpp
For internal use only!
postlude.hpp
The bsoncxx v1 macro guard postlude header.
prelude.hpp
The bsoncxx v1 macro guard prelude header.
bsoncxx::v1::oid::operator>=
friend bool operator>=(oid const &lhs, oid const &rhs)
Equivalent to lhs.compare(rhs) >= 0.
Definition oid.hpp:160
bsoncxx::v1::oid::oid
oid()
Initialize with a unique ObjectID.
bsoncxx::v1::oid::size
static std::size_t size()
Return k_oid_length.
Definition oid.hpp:96
bsoncxx::v1::oid::operator!=
friend bool operator!=(oid const &lhs, oid const &rhs)
Equivalent to lhs.compare(rhs) != 0.
Definition oid.hpp:132
bsoncxx::v1::oid::operator<
friend bool operator<(oid const &lhs, oid const &rhs)
Equivalent to lhs.compare(rhs) < 0.
Definition oid.hpp:139
bsoncxx::v1::oid::operator==
friend bool operator==(oid const &lhs, oid const &rhs)
Equivalent to lhs.compare(rhs) == 0.
Definition oid.hpp:125
bsoncxx::v1::oid::k_oid_length
static constexpr std::size_t k_oid_length
The number of bytes required to represent an ObjectID.
Definition oid.hpp:46
bsoncxx::v1::oid::operator<=
friend bool operator<=(oid const &lhs, oid const &rhs)
Equivalent to lhs.compare(rhs) <= 0.
Definition oid.hpp:146
bsoncxx::v1::oid::bytes
std::uint8_t const * bytes() const
Return a pointer to the byte representation.
Definition oid.hpp:103
bsoncxx::v1::oid::make_error_code
friend std::error_code make_error_code(errc v)
Support implicit conversion to std::error_code.
Definition oid.hpp:183
bsoncxx::v1::oid::get_time_t
std::time_t get_time_t() const
Return the timestamp component.
bsoncxx::v1::oid::error_category
static std::error_category const & error_category()
The error category for bsoncxx::v1::oid::errc.
bsoncxx::v1::oid::errc
errc
Errors codes may be returned by bsoncxx::v1::oid.
Definition oid.hpp:167
bsoncxx::v1::oid::errc::invalid_length
@ invalid_length
Byte length must equal bsoncxx::v1::oid::k_oid_length.
Definition oid.hpp:170
bsoncxx::v1::oid::errc::invalid_string
@ invalid_string
String is not a valid ObjectID representation.
Definition oid.hpp:172
bsoncxx::v1::oid::errc::empty_string
@ empty_string
String must not be empty.
Definition oid.hpp:171
bsoncxx::v1::oid::errc::zero
@ zero
Zero.
Definition oid.hpp:168
bsoncxx::v1::oid::errc::null_bytes_ptr
@ null_bytes_ptr
Bytes pointer must not be null.
Definition oid.hpp:169
bsoncxx::v1::oid::operator>
friend bool operator>(oid const &lhs, oid const &rhs)
Equivalent to lhs.compare(rhs) > 0.
Definition oid.hpp:153
bsoncxx::v1::oid::compare
int compare(oid const &other) const
Equivalent to bson_oid_compare.
bsoncxx::v1::oid::to_string
std::string to_string() const
Return the hexadecimal representation.
bsoncxx::v1::stdx
Declares C++17 standard library polyfills.
bsoncxx::v1
Declares entities whose ABI stability is guaranteed for documented symbols.
bsoncxx
The top-level namespace within which all bsoncxx library entities are declared.
oid-fwd.hpp
Declares bsoncxx::v1::oid.
string_view.hpp
Provides std::string_view-related polyfills for library API usage.