Skip to content

Commit

Permalink
refactor: Remove support for deprecated extended constructor of Enum
Browse files Browse the repository at this point in the history
  • Loading branch information
cwahn committed Mar 29, 2024
1 parent 7b2c850 commit 950c926
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 102 deletions.
116 changes: 58 additions & 58 deletions include/efp/enum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,71 +299,71 @@ namespace detail {
}

// ! Deprecated
// Count how many types in the pack are constructible with Args...
template<typename... Types>
struct ConstructibleCount {};

template<typename T>
struct ConstructibleCount<T> {
template<typename... Args>
using Type = Size<IsConstructible<T, Args...>::value ? 1 : 0>;
};

template<typename First, typename... Rest>
struct ConstructibleCount<First, Rest...> {
template<typename... Args>
using Type = Size<
IsConstructible<First, Args...>::value
? 1
: 0 + ConstructibleCount<Rest...>::template Type<Args...>::value>;
};

template<typename... Args>
using IsUniquelyConstructible =
Bool<ConstructibleCount<A, As...>::template Type<Args...>::value == 1>;

// Base case: no types are constructible
template<typename...>
struct FirstConstructible {
template<typename... Args>
using Type = void; // Fallback type if no constructible type is found
};

// Specialization for at least one type in the pack
template<typename First, typename... Rest>
struct FirstConstructible<First, Rest...> {
template<typename... Args>
using Type = typename std::conditional<
IsConstructible<First, Args...>::value,
First,
typename FirstConstructible<Rest...>::template Type<Args...>>::type;
};

template<typename... Args>
using DetermineAlt = EnableIf<
IsUniquelyConstructible<Args...>::value,
typename FirstConstructible<A, As...>::template Type<Args...>>;
// // Count how many types in the pack are constructible with Args...
// template<typename... Types>
// struct ConstructibleCount {};

// template<typename T>
// struct ConstructibleCount<T> {
// template<typename... Args>
// using Type = Size<IsConstructible<T, Args...>::value ? 1 : 0>;
// };

// template<typename First, typename... Rest>
// struct ConstructibleCount<First, Rest...> {
// template<typename... Args>
// using Type = Size<
// IsConstructible<First, Args...>::value
// ? 1
// : 0 + ConstructibleCount<Rest...>::template Type<Args...>::value>;
// };

// template<typename... Args>
// using IsUniquelyConstructible =
// Bool<ConstructibleCount<A, As...>::template Type<Args...>::value == 1>;

// // Base case: no types are constructible
// template<typename...>
// struct FirstConstructible {
// template<typename... Args>
// using Type = void; // Fallback type if no constructible type is found
// };

// // Specialization for at least one type in the pack
// template<typename First, typename... Rest>
// struct FirstConstructible<First, Rest...> {
// template<typename... Args>
// using Type = typename std::conditional<
// IsConstructible<First, Args...>::value,
// First,
// typename FirstConstructible<Rest...>::template Type<Args...>>::type;
// };

// template<typename... Args>
// using DetermineAlt = EnableIf<
// IsUniquelyConstructible<Args...>::value,
// typename FirstConstructible<A, As...>::template Type<Args...>>;

// ! End of deprecated

// ! Deprecated
// Extended constructor
// Templated constructor for forwarding arguments to the variants'
// constructors
template<
typename Head,
typename... Tail,
typename = EnableIf<
!(sizeof...(Tail) == 0 && Any<IsSame<As, Head>...>::value)
&& IsUniquelyConstructible<Head, Tail...>::value>>
EnumBase(Head&& head, Tail&&... args)
: _index(AltIndex<DetermineAlt<Head, Tail...>>::value) {
// Determine the appropriate variant type based on the argument
using Alt = DetermineAlt<Head, Tail...>; // Implement this based on your logic

// Construct the variant in place
new (reinterpret_cast<Alt*>(_storage)) Alt(forward<Head>(head), forward<Tail>(args)...);
}
// template<
// typename Head,
// typename... Tail,
// typename = EnableIf<
// !(sizeof...(Tail) == 0 && Any<IsSame<As, Head>...>::value)
// && IsUniquelyConstructible<Head, Tail...>::value>>
// EnumBase(Head&& head, Tail&&... args)
// : _index(AltIndex<DetermineAlt<Head, Tail...>>::value) {
// // Determine the appropriate variant type based on the argument
// using Alt = DetermineAlt<Head, Tail...>; // Implement this based on your logic

// // Construct the variant in place
// new (reinterpret_cast<Alt*>(_storage)) Alt(forward<Head>(head), forward<Tail>(args)...);
// }

bool operator==(const EnumBase& other) const {
if (_index != other._index) {
Expand Down
89 changes: 45 additions & 44 deletions test/efp/enum_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,72 +184,73 @@ TEST_CASE("enum_type") {
}

// ! Deprecated
TEST_CASE("Enum Exteneded Constructor") {
SECTION("Explicit constructor") {
struct A {
A(bool arg) : value(arg) {}
// TEST_CASE("Enum Exteneded Constructor") {
// SECTION("Explicit constructor") {
// struct A {
// A(bool arg) : value(arg) {}

bool value;
};
// bool value;
// };

struct B {};
// struct B {};

using SomeEnum = Enum<A, B>;
// using SomeEnum = Enum<A, B>;

CHECK(IsConstructible<bool, bool>::value);
CHECK(IsConstructible<A, bool>::value);
// CHECK(IsConstructible<bool, bool>::value);
// CHECK(IsConstructible<A, bool>::value);

CHECK(SomeEnum::IsUniquelyConstructible<bool>::value);
// CHECK(SomeEnum::IsUniquelyConstructible<bool>::value);

int idx_0 = (int)SomeEnum(true).index();
int idx_1 = (int)SomeEnum {A {true}}.index();
// int idx_0 = (int)SomeEnum(true).index();
// int idx_1 = (int)SomeEnum {A {true}}.index();

CHECK(idx_0 == idx_1);
// CHECK(idx_0 == idx_1);

// CHECK((int)SomeEnum{true}.index() == (int)SomeEnum{A{true}}.index());
}
// // CHECK((int)SomeEnum{true}.index() == (int)SomeEnum{A{true}}.index());
// }

// todo Make it works for aggregate construction as well
// SECTION("Non-explicit constructor") {
// struct A {
// bool value;
// };
// struct B {};
// todo Make it works for aggregate construction as well
// SECTION("Non-explicit constructor") {
// struct A {
// bool value;
// };
// struct B {};

// using SomeEnum = Enum<A, B>;
// using SomeEnum = Enum<A, B>;

// CHECK(IsConstructible<bool, bool>::value);
// CHECK(IsConstructible<A, bool>::value);
// CHECK(IsConstructible<bool, bool>::value);
// CHECK(IsConstructible<A, bool>::value);

// CHECK(SomeEnum::IsUniquelyConstructible<bool>::value);
// CHECK(SomeEnum::IsUniquelyConstructible<bool>::value);

// int idx_0 = (int)SomeEnum(true).index();
// int idx_1 = (int)SomeEnum{A{true}}.index();
// int idx_0 = (int)SomeEnum(true).index();
// int idx_1 = (int)SomeEnum{A{true}}.index();

// CHECK(idx_0 == idx_1);
// CHECK(idx_0 == idx_1);

// // CHECK((int)SomeEnum{true}.index() == (int)SomeEnum{A{true}}.index());
// }
// // CHECK((int)SomeEnum{true}.index() == (int)SomeEnum{A{true}}.index());
// }

SECTION("Nested Enum") {
struct A {};
// ! Deprecated. Nested Enum does not support any shortcutss
// SECTION("Nested Enum") {
// struct A {};

struct B {};
// struct B {};

using Enum0 = Enum<A, B>;
// using Enum0 = Enum<A, B>;

struct C {};
// struct C {};

using NestedEnum = Enum<C, Enum0>;
// using NestedEnum = Enum<C, Enum0>;

CHECK(NestedEnum {A {}}.index() == NestedEnum {Enum0 {A {}}}.index());
}
// CHECK(NestedEnum {A {}}.index() == NestedEnum {Enum0 {A {}}}.index());
// }

// SECTION("Self referencing Enum") {
// List<int> empty_list = Nil{};
// List<int> one_list = Cons<int>{42, Nil{}};
// }
}
// SECTION("Self referencing Enum") {
// List<int> empty_list = Nil{};
// List<int> one_list = Cons<int>{42, Nil{}};
// }
// }

// SECTION("Self referencing Enum") {
// List<int> empty_list = Nil{};
Expand Down

0 comments on commit 950c926

Please sign in to comment.