Skip to content

Commit

Permalink
clang format
Browse files Browse the repository at this point in the history
  • Loading branch information
dietmarkuehl committed Jan 19, 2025
1 parent e43c17f commit 4cd6405
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 36 deletions.
12 changes: 6 additions & 6 deletions examples/alloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ int main() {
}(std::allocator_arg, &resource));
std::cout << "ex::lazy<void, alloc_aware> with alloc done\n\n";

//std::cout << "running ex::lazy<void> with alloc\n";
//ex::sync_wait([](auto&&...) -> ex::lazy<void> {
// co_await ex::just(0);
// co_await ex::just(0);
//}(std::allocator_arg, &resource));
//std::cout << "ex::lazy<void> with alloc done\n\n";
// std::cout << "running ex::lazy<void> with alloc\n";
// ex::sync_wait([](auto&&...) -> ex::lazy<void> {
// co_await ex::just(0);
// co_await ex::just(0);
// }(std::allocator_arg, &resource));
// std::cout << "ex::lazy<void> with alloc done\n\n";

std::cout << "running ex::lazy<void, alloc_aware> extracting alloc\n";
ex::sync_wait([](auto&&, [[maybe_unused]] auto* res) -> ex::lazy<void, alloc_aware> {
Expand Down
4 changes: 2 additions & 2 deletions include/beman/lazy/detail/allocator_of.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ template <typename Context>
struct allocator_of<Context> {
using type = typename Context::allocator_type;
static_assert(
requires(type& a, std::size_t s, std::byte* ptr){
requires(type& a, std::size_t s, std::byte* ptr) {
{ a.allocate(s) } -> std::same_as<std::byte*>;
a.deallocate(ptr, s);
}, "The allocator_type needs to be an allocator of std::byte");
};
template <typename Context>
using allocator_of_t = typename allocator_of<Context>::type;
}
} // namespace beman::lazy::detail

// ----------------------------------------------------------------------------

Expand Down
12 changes: 8 additions & 4 deletions include/beman/lazy/detail/find_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,26 @@ Allocator find_allocator() {
}
template <typename Allocator>
Allocator find_allocator(const std::allocator_arg_t&) {
static_assert(requires{ { Allocator() } -> std::same_as<void>; }, "There needs to be an allocator argument following std::allocator_arg");
static_assert(
requires {
{ Allocator() } -> std::same_as<void>;
}, "There needs to be an allocator argument following std::allocator_arg");
return Allocator();
}

template <typename Allocator, typename Alloc, typename... A>
Allocator find_allocator(const std::allocator_arg_t&, const Alloc& alloc, const A&...) {
static_assert(requires(const Alloc& alloc) { Allocator(alloc); },
"The allocator needs to be constructible from the argument following std::allocator");
static_assert(
requires(const Alloc& alloc) { Allocator(alloc); },
"The allocator needs to be constructible from the argument following std::allocator");
return Allocator(alloc);
}
template <typename Allocator, typename A0, typename... A>
Allocator find_allocator(A0 const&, const A&... a) {
return ::beman::lazy::detail::find_allocator<Allocator>(a...);
}

}
} // namespace beman::lazy::detail

// ----------------------------------------------------------------------------

Expand Down
31 changes: 16 additions & 15 deletions tests/beman/lazy/allocator_of.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@
// ----------------------------------------------------------------------------

namespace {
struct no_allocator {};
struct defines_allocator {
using allocator_type = std::pmr::polymorphic_allocator<std::byte>;
};
struct defines_wrong_allocator {
using allocator_type = std::pmr::polymorphic_allocator<char>;
};
struct non_allocator {};
struct defines_non_allocator {
using allocator_type = non_allocator;
};
}
struct no_allocator {};
struct defines_allocator {
using allocator_type = std::pmr::polymorphic_allocator<std::byte>;
};
struct defines_wrong_allocator {
using allocator_type = std::pmr::polymorphic_allocator<char>;
};
struct non_allocator {};
struct defines_non_allocator {
using allocator_type = non_allocator;
};
} // namespace

int main() {
static_assert(std::same_as<std::allocator<std::byte>, beman::lazy::detail::allocator_of_t<no_allocator>>);
static_assert(std::same_as<std::pmr::polymorphic_allocator<std::byte>, beman::lazy::detail::allocator_of_t<defines_allocator>>);
//using type = beman::lazy::detail::allocator_of_t<defines_wrong_allocator>;
//using type = beman::lazy::detail::allocator_of_t<defines_non_allocator>;
static_assert(std::same_as<std::pmr::polymorphic_allocator<std::byte>,
beman::lazy::detail::allocator_of_t<defines_allocator>>);
// using type = beman::lazy::detail::allocator_of_t<defines_wrong_allocator>;
// using type = beman::lazy::detail::allocator_of_t<defines_non_allocator>;
}
20 changes: 11 additions & 9 deletions tests/beman/lazy/find_allocator.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
// ----------------------------------------------------------------------------

namespace {
template <typename Expect, typename... Args>
void test_find(Expect expect, Args... args) {
auto alloc{beman::lazy::detail::find_allocator<Expect>(std::forward<Args>(args)...)};
static_assert(std::same_as<Expect, decltype(alloc)>);
assert(expect == alloc);
}
template <typename Expect, typename... Args>
void test_find(Expect expect, Args... args) {
auto alloc{beman::lazy::detail::find_allocator<Expect>(std::forward<Args>(args)...)};
static_assert(std::same_as<Expect, decltype(alloc)>);
assert(expect == alloc);
}
} // namespace

int main() {
using std_alloc = std::allocator<char>;
using std_alloc = std::allocator<char>;
using poly_alloc = std::pmr::polymorphic_allocator<char>;

test_find(std_alloc{});
Expand All @@ -37,8 +37,10 @@ int main() {
test_find(poly_alloc{});
test_find(poly_alloc{}, std::allocator_arg, poly_alloc{}, 0, 1);
test_find(poly_alloc{}, std::allocator_arg, std::pmr::new_delete_resource(), 0, 1);
test_find(poly_alloc{std::pmr::null_memory_resource()}, std::allocator_arg, std::pmr::null_memory_resource(), 0, 1);
test_find(
poly_alloc{std::pmr::null_memory_resource()}, std::allocator_arg, std::pmr::null_memory_resource(), 0, 1);
test_find(poly_alloc{}, 0, std::allocator_arg, poly_alloc{}, 0, 1);
test_find(poly_alloc{}, 0, std::allocator_arg, std::pmr::new_delete_resource(), 0, 1);
test_find(poly_alloc{std::pmr::null_memory_resource()}, 0, std::allocator_arg, std::pmr::null_memory_resource(), 0, 1);
test_find(
poly_alloc{std::pmr::null_memory_resource()}, 0, std::allocator_arg, std::pmr::null_memory_resource(), 0, 1);
}

0 comments on commit 4cd6405

Please sign in to comment.