Skip to content

Commit

Permalink
non default constructible allocs (#68)
Browse files Browse the repository at this point in the history
* allow non default constructible allocs
  • Loading branch information
kelbon authored Feb 2, 2024
1 parent 5568bee commit 278c272
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
11 changes: 6 additions & 5 deletions include/anyany/anyany.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,7 @@ struct basic_any : construct_interface<basic_any<Alloc, SooS, Methods...>, Metho
constexpr size_t sizeof_now() const noexcept {
if (!has_value())
return 0;
return memory_allocated() ? allocated_size() : SooS;//HMM
return memory_allocated() ? allocated_size() : SooS;
}

private:
Expand Down Expand Up @@ -1618,15 +1618,15 @@ template <typename Alloc, size_t SooS, anyany_simple_method_concept... Methods>
auto insert_into_basic_any(type_list<Methods...>) {
// if user provides 'destroy' Method, then use it
if constexpr (noexport::contains_v<destroy, Methods...>)
return basic_any<Alloc, SooS, Methods...>{};
return noexport::type_identity<basic_any<Alloc, SooS, Methods...>>{};
else
return basic_any<Alloc, SooS, destroy, Methods...>{};
return noexport::type_identity<basic_any<Alloc, SooS, destroy, Methods...>>{};
}
// if user provides 'destroy' as first Method, then i need to duplicate it
// (so basic any do not removes it as utility Method)
template <typename Alloc, size_t SooS, anyany_simple_method_concept... Methods>
auto insert_into_basic_any(type_list<destroy, Methods...>)
-> basic_any<Alloc, SooS, destroy, destroy, Methods...>;
-> noexport::type_identity<basic_any<Alloc, SooS, destroy, destroy, Methods...>>;

template <typename Alloc, size_t SooS, anyany_method_concept... Methods>
auto flatten_into_basic_any(type_list<Methods...>) {
Expand All @@ -1645,7 +1645,8 @@ auto get_interface_of(const basic_any<Alloc, SooS, destroy, Methods...>&) -> run
} // namespace noexport

template <typename Alloc, size_t SooS, anyany_method_concept... Methods>
using basic_any_with = decltype(noexport::flatten_into_basic_any<Alloc, SooS>(type_list<Methods...>{}));
using basic_any_with =
typename decltype(noexport::flatten_into_basic_any<Alloc, SooS>(type_list<Methods...>{}))::type;

template <anyany_method_concept... Methods>
using any_with = basic_any_with<default_allocator, default_any_soos, Methods...>;
Expand Down
28 changes: 27 additions & 1 deletion tests/test_anyany.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,31 @@ TEST(always_allocated_any) {
error_if(!y.is_always_stable_pointers);
return error_count;
}
struct non_default_constructible_alloc : aa::default_allocator {
int x;
non_default_constructible_alloc() = delete;
non_default_constructible_alloc(int x) : x(x) {
}
};
struct non_default_constructible_alloc_empty : aa::default_allocator {
non_default_constructible_alloc_empty() = delete;
non_default_constructible_alloc_empty(int) {
}
};

TEST(non_default_constructible_allocs) {
using test_t = aa::basic_any_with<non_default_constructible_alloc, aa::default_any_soos, aa::move>;
test_t a(std::allocator_arg, non_default_constructible_alloc{5});
a = 5;
auto b = std::move(a);
(void)b.get_allocator();
using test_t2 = aa::basic_any_with<non_default_constructible_alloc_empty, aa::default_any_soos, aa::move>;
test_t2 a2(std::allocator_arg, non_default_constructible_alloc_empty{5});
a2 = 5;
auto b2 = std::move(a2);
(void)b2.get_allocator();
return 0;
}

int main() {
fwd_declare(5);
Expand Down Expand Up @@ -1545,5 +1570,6 @@ int main() {
return TESTconstructors() + TESTany_cast() + TESTany_cast2() + TESTinvoke() + TESTcompare() +
TESTtype_descriptor_and_plugins_interaction() + TESTspecial_member_functions() + TESTptr_behavior() +
TESTtransmutate_ctors() + TESTstateful() + TESTsubtable_ptr() + TESTmaterialize() +
TESTruntime_reflection() + TESTcustom_unique_ptr() + TESTstrange_allocs() + TESTalways_allocated_any();
TESTruntime_reflection() + TESTcustom_unique_ptr() + TESTstrange_allocs() +
TESTalways_allocated_any() + TESTnon_default_constructible_allocs();
}

0 comments on commit 278c272

Please sign in to comment.