Skip to content

Commit

Permalink
Public PmrTypepmr_type; more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
serges147 committed Jun 5, 2024
1 parent 634fc25 commit 8066f01
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
26 changes: 26 additions & 0 deletions cetlvast/suites/unittest/test_unbounded_variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ TEST_F(TestPmrUnboundedVariant, bad_unbounded_variant_access_assignment)
TEST_F(TestPmrUnboundedVariant, cppref_example)
{
using ub_var = unbounded_variant<std::max(sizeof(int), sizeof(double))>;
static_assert(std::is_void<ub_var::pmr_type>::value, "pmr_type should be `void`");

ub_var a = 1;
EXPECT_THAT(get<int>(a), 1);
Expand Down Expand Up @@ -1208,6 +1209,7 @@ TEST_F(TestPmrUnboundedVariant, emplace_2_initializer_list)
TEST_F(TestPmrUnboundedVariant, pmr_only_ctor)
{
using ub_var = unbounded_variant<0 /*Footprint*/, true /*Copyable*/, true /*Movable*/, 1 /*Alignment*/, pmr>;
static_assert(std::is_same<ub_var::pmr_type, pmr>::value, "pmr_type should be `pmr`");

ub_var dst{get_default_mr()};
EXPECT_THAT(dst.has_value(), false);
Expand Down Expand Up @@ -1694,6 +1696,30 @@ TEST_F(TestPmrUnboundedVariant, pmr_make_unbounded_variant_2_list)
EXPECT_THAT(test.number_, 42);
}

TEST_F(TestPmrUnboundedVariant, pmr_use_mock_as_custom_mr_type)
{
using custom_mr_mock = StrictMock<cetlvast::MemoryResourceMock>;
custom_mr_mock mr_mock{};

const auto Alignment = alignof(std::max_align_t);
using ub_var = unbounded_variant<sizeof(int), true, true, Alignment, custom_mr_mock>;
static_assert(std::is_same<ub_var::pmr_type, custom_mr_mock>::value, "should be custom memory resource mock");

auto src = make_unbounded_variant<int, ub_var>(&mr_mock, 42);
EXPECT_THAT(get<int>(src), 42);

EXPECT_CALL(mr_mock, do_allocate(sizeof(double), Alignment))
.WillOnce(
[this](std::size_t size_bytes, std::size_t alignment) { return mr_.allocate(size_bytes, alignment); });
EXPECT_CALL(mr_mock, do_deallocate(_, sizeof(double), Alignment))
.WillOnce([this](void* p, std::size_t size_bytes, std::size_t alignment) {
mr_.deallocate(p, size_bytes, alignment);
});

src = 3.1415926;
EXPECT_THAT(get<double>(src), 3.1415926);
}

} // namespace

namespace cetl
Expand Down
20 changes: 9 additions & 11 deletions include/cetl/unbounded_variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,8 +998,7 @@ class unbounded_variant : detail::base_move<Pmr, Footprint, Copyable, Movable, A
///
/// `void` if PMR support is disabled.
///
using PmrType = Pmr;
using IsPmr = detail::IsPmr<Pmr>;
using pmr_type = Pmr;

using base::reset;
using base::has_value;
Expand Down Expand Up @@ -1488,7 +1487,7 @@ using unbounded_variant_like = unbounded_variant<sizeof(ValueType),
template <typename ValueType,
typename UnboundedVariant = unbounded_variant_like<ValueType>,
typename... Args,
typename = std::enable_if_t<!UnboundedVariant::IsPmr::value>>
typename = detail::EnableIfNotPmrT<typename UnboundedVariant::pmr_type>>
CETL_NODISCARD UnboundedVariant make_unbounded_variant(Args&&... args)
{
using in_place_type_t = typename UnboundedVariant::template in_place_type_t<ValueType>;
Expand All @@ -1512,7 +1511,7 @@ template <typename ValueType,
typename UnboundedVariant = unbounded_variant_like<ValueType>,
typename Up,
typename... Args,
typename = std::enable_if_t<!UnboundedVariant::IsPmr::value>>
typename = detail::EnableIfNotPmrT<typename UnboundedVariant::pmr_type>>
CETL_NODISCARD UnboundedVariant make_unbounded_variant(std::initializer_list<Up> list, Args&&... args)
{
using in_place_type_t = typename UnboundedVariant::template in_place_type_t<ValueType>;
Expand All @@ -1535,9 +1534,9 @@ CETL_NODISCARD UnboundedVariant make_unbounded_variant(std::initializer_list<Up>
template <typename ValueType,
typename UnboundedVariant = unbounded_variant_like<ValueType>,
typename... Args,
typename Pmr = typename UnboundedVariant::PmrType,
typename = std::enable_if_t<UnboundedVariant::IsPmr::value>>
CETL_NODISCARD UnboundedVariant make_unbounded_variant(Pmr* const mem_res, Args&&... args)
typename = detail::EnableIfPmrT<typename UnboundedVariant::pmr_type>>
CETL_NODISCARD UnboundedVariant make_unbounded_variant(typename UnboundedVariant::pmr_type* const mem_res,
Args&&... args)
{
using in_place_type_t = typename UnboundedVariant::template in_place_type_t<ValueType>;
return UnboundedVariant(mem_res, in_place_type_t{}, std::forward<Args>(args)...);
Expand All @@ -1561,10 +1560,9 @@ template <typename ValueType,
typename UnboundedVariant = unbounded_variant_like<ValueType>,
typename Up,
typename... Args,
typename Pmr = typename UnboundedVariant::PmrType,
typename = std::enable_if_t<UnboundedVariant::IsPmr::value>>
CETL_NODISCARD UnboundedVariant make_unbounded_variant(Pmr* const mem_res,
std::initializer_list<Up> list,
typename = detail::EnableIfPmrT<typename UnboundedVariant::pmr_type>>
CETL_NODISCARD UnboundedVariant make_unbounded_variant(typename UnboundedVariant::pmr_type* const mem_res,
std::initializer_list<Up> list,
Args&&... args)
{
using in_place_type_t = typename UnboundedVariant::template in_place_type_t<ValueType>;
Expand Down

0 comments on commit 8066f01

Please sign in to comment.