Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
kelbon committed Feb 18, 2024
1 parent 704454f commit c434fe7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 52 deletions.
89 changes: 44 additions & 45 deletions include/anyany/anyany.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,14 @@ constexpr inline bool is_const_method_v = method_traits<Method>::is_const;

// pseudomethod is just a value, which is stored in vtable
template <typename T>
concept pseudomethod = std::is_empty_v<T> && std::default_initializable<T> &&
requires {
typename T::value_type;
// T{}.template do_value<X>(),
// where 'do_value' is a
// consteval function template and X
// - some type for which 'do_value'
// exist(not substitution failure)
} &&
(!std::is_reference_v<typename T::value_type> &&
!std::is_function_v<typename T::value_type>);
concept pseudomethod = std::is_empty_v<T> && std::default_initializable<T> && requires {
typename T::value_type;
// T{}.template do_value<X>(),
// where 'do_value' is a
// consteval function template and X
// - some type for which 'do_value'
// exist(not substitution failure)
} && (!std::is_reference_v<typename T::value_type> && !std::is_function_v<typename T::value_type>);

template <typename T>
concept regular_method = std::is_empty_v<T> && std::default_initializable<T> &&
Expand Down Expand Up @@ -324,7 +321,7 @@ constexpr inline bool has_move =

// enables copy/copy assgin/move/move assign for any_with
// enables 'materialize' for references
template<typename Alloc = default_allocator, size_t SooS = default_any_soos>
template <typename Alloc = default_allocator, size_t SooS = default_any_soos>
using copy_with = noexport::copy_method<noexport::rebind_to_byte_t<Alloc>, noexport::soo_buffer_size(SooS)>;

using copy = copy_with<>;
Expand Down Expand Up @@ -1244,11 +1241,11 @@ struct basic_any : construct_interface<basic_any<Alloc, SooS, Methods...>, Metho
} else { // not propagatable alloc
destroy_value();
if (alloc != other.alloc) {
if (other.has_value() && other.memory_allocated() &&
memory_allocated() && size_allocated >= other.size_allocated) {
if (other.has_value() && other.memory_allocated() && memory_allocated() &&
size_allocated >= other.size_allocated) {
// reuse allocated memory
// if exception thrown here, *this is empty
other.get_move_fn() (other.value_ptr, value_ptr);
other.get_move_fn()(other.value_ptr, value_ptr);
vtable_ptr = std::exchange(other.vtable_ptr, nullptr);
return *this;
}
Expand Down Expand Up @@ -1281,8 +1278,8 @@ struct basic_any : construct_interface<basic_any<Alloc, SooS, Methods...>, Metho
// if you really need this, use .emplace
// * returns reference to emplaced value (not *this!)
// for cases like any1 = any2 = 5; (any1 = int is better then any1 = any)
template <typename V, std::enable_if_t<
std::conjunction_v<is_not_polymorphic<V>, exist_for<V, Methods...>>, int> = 0>
template <typename V,
std::enable_if_t<std::conjunction_v<is_not_polymorphic<V>, exist_for<V, Methods...>>, int> = 0>
std::decay_t<V>& operator=(V&& val) ANYANY_LIFETIMEBOUND {
return emplace<V>(std::forward<V>(val));
}
Expand All @@ -1292,42 +1289,44 @@ struct basic_any : construct_interface<basic_any<Alloc, SooS, Methods...>, Metho
// postcondition: has_value() == true, *this is empty if exception thrown
template <typename T, typename... Args, std::enable_if_t<exist_for<T, Methods...>::value, int> = 0>
std::decay_t<T>& emplace(Args&&... args) noexcept(
std::is_nothrow_constructible_v<std::decay_t<T>, Args&&...>&& any_is_small_for<std::decay_t<T>>)
ANYANY_LIFETIMEBOUND {
std::is_nothrow_constructible_v<std::decay_t<T>, Args&&...> &&
any_is_small_for<std::decay_t<T>>) ANYANY_LIFETIMEBOUND {
// decay T, so less function instantiated (emplace<int | int& | const int|> same fn)
return emplace_decayed<std::decay_t<T>>(std::forward<Args>(args)...);
}
template <typename T, typename U, typename... Args,
std::enable_if_t<exist_for<T, Methods...>::value, int> = 0>
std::decay_t<T>& emplace(std::initializer_list<U> list, Args&&... args) noexcept(
std::is_nothrow_constructible_v<std::decay_t<T>, std::initializer_list<U>, Args&&...>&&
any_is_small_for<std::decay_t<T>>) ANYANY_LIFETIMEBOUND {
std::is_nothrow_constructible_v<std::decay_t<T>, std::initializer_list<U>, Args&&...> &&
any_is_small_for<std::decay_t<T>>) ANYANY_LIFETIMEBOUND {
return emplace<std::decay_t<T>, std::initializer_list<U>, Args...>(std::move(list),
std::forward<Args>(args)...);
}
template <typename T, typename... Args, std::enable_if_t<exist_for<T, Methods...>::value, int> = 0>
basic_any(std::in_place_type_t<T>, Args&&... args) noexcept(
std::is_nothrow_constructible_v<std::decay_t<T>, Args&&...>&& any_is_small_for<std::decay_t<T>>) {
basic_any(std::in_place_type_t<T>,
Args&&... args) noexcept(std::is_nothrow_constructible_v<std::decay_t<T>, Args&&...> &&
any_is_small_for<std::decay_t<T>>) {
emplace_in_empty<std::decay_t<T>>(std::forward<Args>(args)...);
}
template <typename T, typename U, typename... Args,
std::enable_if_t<exist_for<T, Methods...>::value, int> = 0>
basic_any(std::in_place_type_t<T>, std::initializer_list<U> list, Args&&... args) noexcept(
std::is_nothrow_constructible_v<std::decay_t<T>, std::initializer_list<U>, Args&&...>&&
any_is_small_for<std::decay_t<T>>) {
std::is_nothrow_constructible_v<std::decay_t<T>, std::initializer_list<U>, Args&&...> &&
any_is_small_for<std::decay_t<T>>) {
emplace_in_empty<std::decay_t<T>>(list, std::forward<Args>(args)...);
}
template <typename T,
std::enable_if_t<std::conjunction_v<is_not_polymorphic<T>, exist_for<T, Methods...>>, int> = 0>
basic_any(T&& value) noexcept(
std::is_nothrow_constructible_v<std::decay_t<T>, T&&>&& any_is_small_for<std::decay_t<T>>)
basic_any(T&& value) noexcept(std::is_nothrow_constructible_v<std::decay_t<T>, T&&> &&
any_is_small_for<std::decay_t<T>>)
: basic_any(std::in_place_type<std::decay_t<T>>, std::forward<T>(value)) {
}
constexpr basic_any(std::allocator_arg_t, Alloc alloc) noexcept : alloc(std::move(alloc)) {
}
template <typename T, std::enable_if_t<exist_for<T, Methods...>::value, int> = 0>
basic_any(std::allocator_arg_t, Alloc alloc, T&& value) noexcept(
std::is_nothrow_constructible_v<std::decay_t<T>, T&&>&& any_is_small_for<std::decay_t<T>>)
basic_any(std::allocator_arg_t, Alloc alloc,
T&& value) noexcept(std::is_nothrow_constructible_v<std::decay_t<T>, T&&> &&
any_is_small_for<std::decay_t<T>>)
: alloc(std::move(alloc)) {
emplace_in_empty<std::decay_t<T>>(std::forward<T>(value));
}
Expand Down Expand Up @@ -1360,22 +1359,24 @@ struct basic_any : construct_interface<basic_any<Alloc, SooS, Methods...>, Metho
// force allocate versions

template <typename T, typename... Args, std::enable_if_t<exist_for<T, Methods...>::value, int> = 0>
basic_any(force_stable_pointers_t, std::in_place_type_t<T>, Args&&... args) noexcept(
std::is_nothrow_constructible_v<std::decay_t<T>, Args&&...>&& any_is_small_for<std::decay_t<T>>) {
basic_any(force_stable_pointers_t, std::in_place_type_t<T>,
Args&&... args) noexcept(std::is_nothrow_constructible_v<std::decay_t<T>, Args&&...> &&
any_is_small_for<std::decay_t<T>>) {
emplace_in_empty<std::decay_t<T>, force_stable_pointers_t>(std::forward<Args>(args)...);
}
template <typename T, typename U, typename... Args,
std::enable_if_t<exist_for<T, Methods...>::value, int> = 0>
basic_any(force_stable_pointers_t, std::in_place_type_t<T>, std::initializer_list<U> list,
Args&&... args) noexcept(std::is_nothrow_constructible_v<std::decay_t<T>,
std::initializer_list<U>, Args&&...>&&
any_is_small_for<std::decay_t<T>>) {
std::initializer_list<U>, Args&&...> &&
any_is_small_for<std::decay_t<T>>) {
emplace_in_empty<std::decay_t<T>, force_stable_pointers_t>(list, std::forward<Args>(args)...);
}
template <typename T,
std::enable_if_t<std::conjunction_v<is_not_polymorphic<T>, exist_for<T, Methods...>>, int> = 0>
basic_any(force_stable_pointers_t, T&& value) noexcept(
std::is_nothrow_constructible_v<std::decay_t<T>, T&&>&& any_is_small_for<std::decay_t<T>>)
basic_any(force_stable_pointers_t,
T&& value) noexcept(std::is_nothrow_constructible_v<std::decay_t<T>, T&&> &&
any_is_small_for<std::decay_t<T>>)
: basic_any(force_stable_pointers, std::in_place_type<std::decay_t<T>>, std::forward<T>(value)) {
}

Expand Down Expand Up @@ -1445,15 +1446,15 @@ struct basic_any : construct_interface<basic_any<Alloc, SooS, Methods...>, Metho

// postcondition: has_value() == true, *this is empty if exception thrown
template <typename T, typename... Args, std::enable_if_t<exist_for<T, Methods...>::value, int> = 0>
T& emplace_decayed(Args&&... args) noexcept(
std::is_nothrow_constructible_v<T, Args&&...> && any_is_small_for<T>) {
T& emplace_decayed(Args&&... args) noexcept(std::is_nothrow_constructible_v<T, Args&&...> &&
any_is_small_for<T>) {
static_assert(std::is_same_v<T, std::decay_t<T>>);
destroy_value();
if (!memory_allocated())
goto do_emplace_in_empty;
if (size_allocated < sizeof(T)) {
deallocate_memory();
do_emplace_in_empty:
do_emplace_in_empty:
emplace_in_empty<T>(std::forward<Args>(args)...);
} else {
construct_value<T>(std::forward<Args>(args)...);
Expand All @@ -1462,7 +1463,7 @@ struct basic_any : construct_interface<basic_any<Alloc, SooS, Methods...>, Metho
}

// postcondition: has_value()
template<typename T, typename... Args>
template <typename T, typename... Args>
constexpr void construct_value(Args&&... args) noexcept(std::is_nothrow_constructible_v<T, Args&&...>) {
static_assert(std::is_same_v<T, std::decay_t<T>>);
alloc_traits::construct(alloc, static_cast<T*>(value_ptr), std::forward<Args>(args)...);
Expand Down Expand Up @@ -1536,7 +1537,7 @@ auto materialize(const_poly_ref<Methods...> ref, Alloc alloc = Alloc{})
#define AA_DECLARE_MATERIALIZE(TEMPLATE, TRANSFORM) \
template <typename Alloc = default_allocator, size_t SooS = default_any_soos, typename... Methods> \
AA_ALWAYS_INLINE auto materialize(const TEMPLATE<Methods...>& value, Alloc alloc = Alloc{}) \
->decltype(materialize<Alloc, SooS, Methods...>(TRANSFORM, std::move(alloc))) { \
-> decltype(materialize<Alloc, SooS, Methods...>(TRANSFORM, std::move(alloc))) { \
return materialize<Alloc, SooS, Methods...>(TRANSFORM, std::move(alloc)); \
}
AA_DECLARE_MATERIALIZE(poly_ref, const_poly_ref(value))
Expand Down Expand Up @@ -1609,7 +1610,7 @@ struct any_cast_fn<T, anyany_poly_traits> {
}

template <typename U, std::enable_if_t<is_any<U>::value, int> = 0>
decltype(auto) operator()(U&& any) const {
decltype(auto) operator()(U && any) const {
auto* ptr = (*this)(std::addressof(any));
if (!ptr)
throw aa::bad_cast{};
Expand Down Expand Up @@ -1708,10 +1709,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 =
noexport::flatten_into_basic_any_t<noexport::rebind_to_byte_t<Alloc>,
noexport::soo_buffer_size(SooS),
Methods...>;
using basic_any_with = noexport::flatten_into_basic_any_t<noexport::rebind_to_byte_t<Alloc>,
noexport::soo_buffer_size(SooS), Methods...>;

template <anyany_method_concept... Methods>
using any_with = basic_any_with<default_allocator, default_any_soos, Methods...>;
Expand Down
17 changes: 10 additions & 7 deletions tests/test_anyany.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,12 @@ void noallocate_test() {
#if __cplusplus >= 202002L
using any_compare = aa::any_with<aa::copy, aa::equal_to, aa::spaceship, aa::move>;
static_assert(
std::is_same_v<any_compare::ref, aa::poly_ref<aa::destroy, aa::copy, aa::equal_to, aa::spaceship, aa::move>> &&
std::is_same_v<any_compare::ref,
aa::poly_ref<aa::destroy, aa::copy, aa::equal_to, aa::spaceship, aa::move>> &&
std::is_same_v<any_compare::const_ref,
aa::const_poly_ref<aa::destroy, aa::copy, aa::equal_to, aa::spaceship, aa::move>> &&
std::is_same_v<any_compare::ptr, aa::poly_ptr<aa::destroy, aa::copy, aa::equal_to, aa::spaceship, aa::move>> &&
std::is_same_v<any_compare::ptr,
aa::poly_ptr<aa::destroy, aa::copy, aa::equal_to, aa::spaceship, aa::move>> &&
std::is_same_v<any_compare::const_ptr,
aa::const_poly_ptr<aa::destroy, aa::copy, aa::equal_to, aa::spaceship, aa::move>>);
using any_equal = aa::any_with<aa::equal_to, aa::equal_to, aa::spaceship, aa::spaceship, aa::move>;
Expand Down Expand Up @@ -980,8 +982,9 @@ void anyany_interface_alias_tests() {
static_assert(std::is_same_v<aa::any_with<a, b, c>,
aa::basic_any<aa::default_allocator, aa::default_any_soos, aa::destroy,
aa::destroy, aa::type_info, aa::destroy, aa::type_info>>);
static_assert(std::is_same_v<aa::any_with<a, b, c>::ref,
aa::poly_ref<aa::destroy, aa::destroy, aa::type_info, aa::destroy, aa::type_info>>);
static_assert(
std::is_same_v<aa::any_with<a, b, c>::ref,
aa::poly_ref<aa::destroy, aa::destroy, aa::type_info, aa::destroy, aa::type_info>>);
#if __cplusplus >= 202002L
static_assert(aa::compound_method<a>);
static_assert(aa::compound_method<b>);
Expand Down Expand Up @@ -1310,7 +1313,7 @@ TEST(zero_sized_any) {

TEST(memory_reuse) {
// create any
auto do_test = [&] (auto a) {
auto do_test = [&](auto a) {
a.replace_with_bytes(200);
error_if(a.capacity() < 200);
auto expect = [&](auto x) {
Expand Down Expand Up @@ -1627,6 +1630,6 @@ int main() {
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() + TESTnon_default_constructible_allocs() + TESTzero_sized_any()
+ TESTmemory_reuse();
TESTalways_allocated_any() + TESTnon_default_constructible_allocs() + TESTzero_sized_any() +
TESTmemory_reuse();
}

0 comments on commit c434fe7

Please sign in to comment.