Skip to content

CXX-2745 add immortal.hh and type_traits.hh #1402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/bsoncxx/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,13 @@ set_dist_list(src_bsoncxx_lib_DIST
bsoncxx/private/convert.hh
bsoncxx/private/export.hh
bsoncxx/private/helpers.hh
bsoncxx/private/immortal.hh
bsoncxx/private/itoa.hh
bsoncxx/private/bson.hh
bsoncxx/private/make_unique.hh
bsoncxx/private/stack.hh
bsoncxx/private/suppress_deprecation_warnings.hh
bsoncxx/private/type_traits.hh
bsoncxx/private/version.hh
bsoncxx/v_noabi/bsoncxx/types/bson_value/value.hh
bsoncxx/v1/config/config.hpp.in
Expand Down
62 changes: 62 additions & 0 deletions src/bsoncxx/lib/bsoncxx/private/immortal.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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/detail/macros.hpp>

#include <new>

namespace bsoncxx {

//
// Used to construct an object whose destructor is never invoked (hence, "immortal").
//
// This is primarily to avoid generating exit-time destructors for error category objects:
//
// ```cpp
// std::error_category const& name() noexcept {
// class type final : public std::error_category { ... };
// static bsoncxx::immortal<type> const instance;
// return instance.value();
// }
// ```
//
template <typename T>
class immortal {
Comment on lines +36 to +37
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a doc comment on what this class template does any why it might want to be used, and how it should not be used (e.g. declaring a non-static immortal<T> is potentially bad).

private:
alignas(T) unsigned char _storage[sizeof(T)];

public:
~immortal() = default;
immortal(immortal&&) = delete;
immortal& operator=(immortal&&) = delete;
immortal(immortal const&) = delete;
immortal& operator=(immortal const&) = delete;

template <typename... Args>
explicit immortal(Args&&... args) {
new (_storage) T(BSONCXX_PRIVATE_FWD(args)...);
}

T const& value() const {
return *reinterpret_cast<T const*>(_storage);
}

T& value() {
return *reinterpret_cast<T*>(_storage);
}
};

} // namespace bsoncxx
78 changes: 78 additions & 0 deletions src/bsoncxx/lib/bsoncxx/private/type_traits.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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/detail/type_traits.hpp>

namespace bsoncxx {

template <typename LHS, typename RHS>
using is_assignable_from_expr = decltype(std::declval<LHS>() = std::declval<RHS>());

// Approximates the `assignable_from` concept in C++20.
template <typename LHS, typename RHS>
struct is_assignable_from : detail::conjunction<
std::is_lvalue_reference<LHS>,
std::is_same<detail::detected_t<is_assignable_from_expr, LHS, RHS>, LHS>> {};

// Equivalent to `is_assignable_from` but also requires noexceptness.
template <typename LHS, typename RHS>
struct is_nothrow_assignable_from
: detail::bool_constant<
is_assignable_from<LHS, RHS>::value && noexcept(std::declval<LHS>() = std::declval<RHS>())> {};

// Approximates the `movable` concept in C++20.
template <typename T>
struct is_movable : detail::conjunction<
std::is_object<T>,
std::is_move_constructible<T>,
is_assignable_from<T&, T>,
detail::is_swappable<T>> {};

// Equivalent to `is_moveable` but additionally requires noexceptness.
template <typename T>
struct is_nothrow_moveable : detail::conjunction<
std::is_object<T>,
std::is_nothrow_move_constructible<T>,
is_nothrow_assignable_from<T&, T>,
detail::is_nothrow_swappable<T>> {};

// Approximates the `copyable` concept in C++20.
template <typename T>
struct is_copyable : detail::conjunction<
std::is_copy_constructible<T>,
is_movable<T>,
is_assignable_from<T&, T&>,
is_assignable_from<T&, T const&>,
is_assignable_from<T&, T const>> {};

// Approximates the `semiregular` concept in C++20.
template <typename T>
struct is_semiregular : detail::conjunction<is_copyable<T>, std::is_default_constructible<T>> {};

// Approximates the `regular` concept in C++20.
template <typename T>
struct is_regular : detail::conjunction<is_semiregular<T>, detail::is_equality_comparable<T>> {};

// Equivalent to `is_trivial` without requiring trivial default constructibility.
template <typename T>
struct is_semitrivial : detail::conjunction<
std::is_trivially_destructible<T>,
std::is_trivially_move_constructible<T>,
std::is_trivially_move_assignable<T>,
std::is_trivially_copy_constructible<T>,
std::is_trivially_copy_assignable<T>> {};
Comment on lines +69 to +76
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is "semitrivial" a (proposed) standard trait? I've not heard the term, but it is reasonably useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, not as far as I am aware.


} // namespace bsoncxx