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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
49 changes: 49 additions & 0 deletions src/bsoncxx/lib/bsoncxx/private/immortal.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 {

template <typename T>
class immortal {
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>
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>> {};

} // namespace bsoncxx