Skip to content

Commit

Permalink
Just testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Quuxplusone committed Feb 21, 2025
1 parent af5477f commit eb9ce5e
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 48 deletions.
4 changes: 2 additions & 2 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ cc_library(
srcs = ["indirect.cc"],
hdrs = ["indirect.h"],
copts = ["-Iexternal/value_types/"],
defines = ["XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION"],
defines = [],
visibility = ["//visibility:public"],
deps = ["feature_check"],
)
Expand Down Expand Up @@ -92,7 +92,7 @@ cc_library(
srcs = ["polymorphic.cc"],
hdrs = ["polymorphic.h"],
copts = ["-Iexternal/value_types/"],
defines = ["XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION"],
defines = [],
visibility = ["//visibility:public"],
)

Expand Down
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ target_sources(xyz_value_types
xyz_add_library(
NAME indirect
ALIAS xyz_value_types::indirect
DEFINITIONS XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
)
target_sources(indirect
INTERFACE
Expand Down Expand Up @@ -76,7 +75,6 @@ target_sources(indirect_cxx17
xyz_add_library(
NAME polymorphic
ALIAS xyz_value_types::polymorphic
DEFINITIONS XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
)
target_sources(polymorphic
INTERFACE
Expand Down
32 changes: 21 additions & 11 deletions indirect.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

namespace xyz {

#ifndef XYZ_TYPE_IDENTITY_DEFINED
#define XYZ_TYPE_IDENTITY_DEFINED
#ifdef XYZ_HAS_STD_TYPE_IDENTITY
using std::type_identity_t;
#else
template <class T>
struct type_identity {
using type = T;
};
template <class T>
using type_identity_t = typename type_identity<T>::type;
#endif // XYZ_HAS_STD_TYPE_IDENTITY
#endif // XYZ_TYPE_IDENTITY_DEFINED

#ifndef XYZ_UNREACHABLE_DEFINED
#define XYZ_UNREACHABLE_DEFINED

Expand Down Expand Up @@ -182,7 +196,7 @@ class indirect {
p_ = construct_from(alloc_, ilist, std::forward<Us>(us)...);
}

constexpr indirect(std::allocator_arg_t, const std::type_identity_t<A>& alloc,
constexpr indirect(std::allocator_arg_t, const xyz::type_identity_t<A>& alloc,
const indirect& other)
: alloc_(alloc) {
static_assert(std::copy_constructible<T>);
Expand All @@ -195,7 +209,7 @@ class indirect {
}

constexpr indirect(
std::allocator_arg_t, const std::type_identity_t<A>& alloc,
std::allocator_arg_t, const xyz::type_identity_t<A>& alloc,
indirect&& other) noexcept(allocator_traits::is_always_equal::value)
: p_(nullptr), alloc_(alloc) {
static_assert(std::move_constructible<T>);
Expand Down Expand Up @@ -465,16 +479,12 @@ concept is_hashable = requires(T t) { std::hash<T>{}(t); };
template <typename Value>
indirect(Value) -> indirect<Value>;

template <typename Alloc, typename Value,
typename std::enable_if_t<!is_indirect_v<Value>, int> = 0>
indirect(std::allocator_arg_t, Alloc, Value) -> indirect<
Value, typename std::allocator_traits<Alloc>::template rebind_alloc<Value>>;

#ifdef XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
template <typename Alloc, typename Value>
indirect(std::allocator_arg_t, std::type_identity_t<Alloc>,
indirect<Value, Alloc>) -> indirect<Value, Alloc>;
#endif // XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
indirect(std::allocator_arg_t, Alloc, Value) -> indirect<Value, Alloc>;

template <typename Alloc, typename Alloc2, typename Value>
indirect(std::allocator_arg_t, Alloc2, indirect<Value, Alloc>)
-> indirect<Value, Alloc>;

} // namespace xyz

Expand Down
21 changes: 19 additions & 2 deletions indirect_cxx14.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ namespace xyz {
struct in_place_t {};
#endif // XYZ_IN_PLACE_DEFINED

#ifndef XYZ_TYPE_IDENTITY_DEFINED
#define XYZ_TYPE_IDENTITY_DEFINED
#ifdef XYZ_HAS_STD_TYPE_IDENTITY
using std::type_identity_t;
#else
template <class T>
struct type_identity {
using type = T;
};
template <class T>
using type_identity_t = typename type_identity<T>::type;
#endif // XYZ_HAS_STD_TYPE_IDENTITY
#endif // XYZ_TYPE_IDENTITY_DEFINED

#ifndef XYZ_UNREACHABLE_DEFINED
#define XYZ_UNREACHABLE_DEFINED

Expand Down Expand Up @@ -575,8 +589,11 @@ template <typename Value>
indirect(Value) -> indirect<Value>;

template <typename Alloc, typename Value>
indirect(std::allocator_arg_t, Alloc, Value) -> indirect<
Value, typename std::allocator_traits<Alloc>::template rebind_alloc<Value>>;
indirect(std::allocator_arg_t, Alloc, Value) -> indirect<Value, Alloc>;

template <typename Alloc, typename Alloc2, typename Value>
indirect(std::allocator_arg_t, Alloc2, indirect<Value, Alloc>)
-> indirect<Value, Alloc>;
#endif // XYZ_HAS_TEMPLATE_ARGUMENT_DEDUCTION

} // namespace xyz
Expand Down
6 changes: 1 addition & 5 deletions indirect_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,13 @@ TEST(IndirectTest, TemplateArgumentDeductionCopy) {
}

TEST(IndirectTest, TemplateArgumentDeductionWithAllocator) {
xyz::indirect i(std::allocator_arg, xyz::TaggedAllocator<void>(1), 42);
xyz::indirect i(std::allocator_arg, xyz::TaggedAllocator<int>(1), 42);

static_assert(
std::is_same_v<decltype(i.get_allocator()), xyz::TaggedAllocator<int>>);
EXPECT_EQ(*i, 42);
}

#ifdef XYZ_HAS_STD_TYPE_IDENTITY
#ifdef XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
TEST(IndirectTest, TemplateArgumentDeductionWithDeducedAllocatorAndCopy) {
xyz::indirect i(std::allocator_arg, xyz::TaggedAllocator<int>(1), 42);
xyz::indirect ii(std::allocator_arg, 2, i);
Expand All @@ -158,8 +156,6 @@ TEST(IndirectTest, TemplateArgumentDeductionWithDeducedAllocatorAndMove) {
EXPECT_EQ(*ii, 42);
EXPECT_EQ(ii.get_allocator().tag, 2);
}
#endif // XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
#endif // XYZ_HAS_STD_TYPE_IDENTITY
#endif // XYZ_HAS_TEMPLATE_ARGUMENT_DEDUCTION

template <typename Allocator = std::allocator<void>>
Expand Down
40 changes: 23 additions & 17 deletions polymorphic.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

namespace xyz {

#ifndef XYZ_TYPE_IDENTITY_DEFINED
#define XYZ_TYPE_IDENTITY_DEFINED
#ifdef XYZ_HAS_STD_TYPE_IDENTITY
using std::type_identity_t;
#else
template <class T>
struct type_identity {
using type = T;
};
template <class T>
using type_identity_t = typename type_identity<T>::type;
#endif // XYZ_HAS_STD_TYPE_IDENTITY
#endif // XYZ_TYPE_IDENTITY_DEFINED

#ifndef XYZ_UNREACHABLE_DEFINED
#define XYZ_UNREACHABLE_DEFINED

Expand Down Expand Up @@ -119,12 +133,6 @@ class direct_control_block final : public control_block<T, A> {
template <class T, class A>
class polymorphic;

template <class>
inline constexpr bool is_polymorphic_v = false;

template <class T, class A>
inline constexpr bool is_polymorphic_v<polymorphic<T, A>> = true;

template <class T, class A = std::allocator<T>>
class polymorphic {
using cblock_t = detail::control_block<T, A>;
Expand Down Expand Up @@ -251,7 +259,7 @@ class polymorphic {
}

constexpr polymorphic(std::allocator_arg_t,
const std::type_identity_t<A>& alloc,
const xyz::type_identity_t<A>& alloc,
const polymorphic& other)
: alloc_(alloc) {
if (!other.valueless_after_move()) {
Expand All @@ -262,7 +270,7 @@ class polymorphic {
}

constexpr polymorphic(
std::allocator_arg_t, const std::type_identity_t<A>& alloc,
std::allocator_arg_t, const xyz::type_identity_t<A>& alloc,
polymorphic&& other) noexcept(allocator_traits::is_always_equal::value)
: alloc_(alloc) {
if constexpr (allocator_traits::is_always_equal::value) {
Expand Down Expand Up @@ -414,19 +422,17 @@ class polymorphic {
}
}
};
#ifdef XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION

template <typename Value>
polymorphic(Value) -> polymorphic<Value>;

template <typename Alloc, typename Value,
typename std::enable_if_t<!is_polymorphic_v<Value>, int> = 0>
polymorphic(std::allocator_arg_t, Alloc, Value) -> polymorphic<
Value, typename std::allocator_traits<Alloc>::template rebind_alloc<Value>>;

template <typename Alloc, typename Value>
polymorphic(std::allocator_arg_t, std::type_identity_t<Alloc>,
polymorphic<Value, Alloc>) -> polymorphic<Value, Alloc>;
#endif // XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
polymorphic(std::allocator_arg_t, Alloc, Value) -> polymorphic<Value, Alloc>;

template <typename Alloc, typename Alloc2, typename Value>
polymorphic(std::allocator_arg_t, Alloc2, polymorphic<Value, Alloc>)
-> polymorphic<Value, Alloc>;

} // namespace xyz

#endif // XYZ_POLYMORPHIC_H_
35 changes: 33 additions & 2 deletions polymorphic_cxx14.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <type_traits>
#include <utility>

#include "feature_check.h"

#ifndef XYZ_POLYMORPHIC_HAS_EXTENDED_CONSTRUCTORS
#define XYZ_POLYMORPHIC_HAS_EXTENDED_CONSTRUCTORS 1
#endif // XYZ_POLYMORPHIC_HAS_EXTENDED_CONSTRUCTORS
Expand All @@ -40,6 +42,22 @@ struct in_place_type_t {};
} // namespace xyz
#endif // XYZ_IN_PLACE_TYPE_DEFINED

#ifndef XYZ_TYPE_IDENTITY_DEFINED
#define XYZ_TYPE_IDENTITY_DEFINED
namespace xyz {
#ifdef XYZ_HAS_STD_TYPE_IDENTITY
using std::type_identity_t;
#else
template <class T>
struct type_identity {
using type = T;
};
template <class T>
using type_identity_t = typename type_identity<T>::type;
#endif // XYZ_HAS_STD_TYPE_IDENTITY
} // namespace xyz
#endif // XYZ_TYPE_IDENTITY_DEFINED

#ifndef XYZ_UNREACHABLE_DEFINED
#define XYZ_UNREACHABLE_DEFINED

Expand Down Expand Up @@ -319,7 +337,8 @@ class polymorphic : private detail::empty_base_optimization<A> {
typename std::remove_reference<U>::type>::type>{},
std::forward<U>(u)) {}

polymorphic(std::allocator_arg_t, const A& alloc, const polymorphic& other)
polymorphic(std::allocator_arg_t, const xyz::type_identity_t<A>& alloc,
const polymorphic& other)
: alloc_base(alloc) {
if (!other.valueless_after_move()) {
cb_ = other.cb_->clone(alloc_base::get());
Expand All @@ -335,7 +354,7 @@ class polymorphic : private detail::empty_base_optimization<A> {
other) {}

polymorphic(
std::allocator_arg_t, const A& alloc,
std::allocator_arg_t, const xyz::type_identity_t<A>& alloc,
polymorphic&& other) noexcept(allocator_traits::is_always_equal::value)
: alloc_base(alloc) {
if (allocator_traits::propagate_on_container_copy_assignment::value) {
Expand Down Expand Up @@ -476,6 +495,18 @@ class polymorphic : private detail::empty_base_optimization<A> {
}
};

#ifdef XYZ_HAS_TEMPLATE_ARGUMENT_DEDUCTION
template <typename Value>
polymorphic(Value) -> polymorphic<Value>;

template <typename Alloc, typename Value>
polymorphic(std::allocator_arg_t, Alloc, Value) -> polymorphic<Value, Alloc>;

template <typename Alloc, typename Alloc2, typename Value>
polymorphic(std::allocator_arg_t, Alloc2, polymorphic<Value, Alloc>)
-> polymorphic<Value, Alloc>;
#endif // XYZ_HAS_TEMPLATE_ARGUMENT_DEDUCTION

} // namespace xyz

#endif // XYZ_POLYMORPHIC_H_
10 changes: 3 additions & 7 deletions polymorphic_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -846,8 +846,6 @@ TEST(PolymorphicTest, TaggedAllocatorsNotEqualMoveConstructFromValueless) {
}

#ifdef XYZ_HAS_TEMPLATE_ARGUMENT_DEDUCTION
#ifdef XYZ_HAS_STD_TYPE_IDENTITY
#ifdef XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
TEST(PolymorphicTest, TemplateArgumentDeduction) {
xyz::polymorphic p(Derived(4));

Expand All @@ -864,15 +862,15 @@ TEST(PolymorphicTest, TemplateArgumentDeductionCopy) {
}

TEST(PolymorphicTest, TemplateArgumentDeductionWithAllocator) {
xyz::TaggedAllocator<int> a(1);
xyz::TaggedAllocator<Derived> a(1);
xyz::polymorphic p(std::allocator_arg, a, Derived(4));

EXPECT_EQ(p->value(), 4);
EXPECT_EQ(p.get_allocator().tag, 1);
}

TEST(PolymorphicTest, TemplateArgumentDeductionWithDeducedAllocatorAndCopy) {
xyz::TaggedAllocator<int> a(1);
xyz::TaggedAllocator<Derived> a(1);
xyz::polymorphic p(std::allocator_arg, a, Derived(4));
xyz::polymorphic pp(std::allocator_arg, 2, p);

Expand All @@ -881,16 +879,14 @@ TEST(PolymorphicTest, TemplateArgumentDeductionWithDeducedAllocatorAndCopy) {
}

TEST(PolymorphicTest, TemplateArgumentDeductionWithDeducedAllocatorAndMove) {
xyz::TaggedAllocator<int> a(1);
xyz::TaggedAllocator<Derived> a(1);
xyz::polymorphic p(std::allocator_arg, a, Derived(4));
xyz::polymorphic pp(std::allocator_arg, 2, std::move(p));

EXPECT_EQ(pp->value(), 4);
EXPECT_EQ(pp.get_allocator().tag, 2);
}

#endif // XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION
#endif // XYZ_HAS_STD_TYPE_IDENTITY
#endif // XYZ_HAS_TEMPLATE_ARGUMENT_DEDUCTION

} // namespace

0 comments on commit eb9ce5e

Please sign in to comment.