Skip to content

Commit a92fdc8

Browse files
committed
Finish initial enum and flag_enum, and make the latter use unsigned underlying type
1 parent eb7de86 commit a92fdc8

15 files changed

+243
-235
lines changed

include/cpp2util.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@
201201
#include <type_traits>
202202
#include <new>
203203
#include <memory>
204+
#include <random>
204205
#include <tuple>
205206
#include <string>
206207
#include <string_view>
@@ -1566,7 +1567,7 @@ constexpr auto unsafe_narrow( X&& x ) noexcept -> decltype(auto)
15661567
//
15671568
//-----------------------------------------------------------------------
15681569
//
1569-
template <typename T, typename Tag>
1570+
template <typename T, typename Tag, bool BitwiseOps>
15701571
class strict_value {
15711572
T t = {};
15721573
public:
@@ -1583,17 +1584,23 @@ class strict_value {
15831584

15841585
auto operator<=>( strict_value const& ) const -> std::strong_ordering = default;
15851586

1586-
auto operator|=( strict_value const& that ) -> strict_value { t |= that.t; return *this; }
1587-
auto operator&=( strict_value const& that ) -> strict_value { t &= that.t; return *this; }
1588-
auto operator^=( strict_value const& that ) -> strict_value { t ^= that.t; return *this; }
1587+
auto operator|=( strict_value const& that ) -> strict_value requires BitwiseOps { t |= that.t; return *this; }
1588+
auto operator&=( strict_value const& that ) -> strict_value requires BitwiseOps { t &= that.t; return *this; }
1589+
auto operator^=( strict_value const& that ) -> strict_value requires BitwiseOps { t ^= that.t; return *this; }
15891590

1590-
auto operator|( strict_value const& that ) const -> strict_value { return strict_value(t | that.t); }
1591-
auto operator&( strict_value const& that ) const -> strict_value { return strict_value(t & that.t); }
1592-
auto operator^( strict_value const& that ) const -> strict_value { return strict_value(t ^ that.t); }
1591+
auto operator| ( strict_value const& that ) const -> strict_value requires BitwiseOps { return strict_value(t | that.t); }
1592+
auto operator& ( strict_value const& that ) const -> strict_value requires BitwiseOps { return strict_value(t & that.t); }
1593+
auto operator^ ( strict_value const& that ) const -> strict_value requires BitwiseOps { return strict_value(t ^ that.t); }
15931594

15941595
auto is_default_value() const -> bool { return t == T{}; }
15951596
};
15961597

1598+
template <typename T, typename Tag>
1599+
auto has_flags(strict_value<T, Tag, true> flags)
1600+
{
1601+
return [=](strict_value<T, Tag, true> value) { return (value & flags) == flags; };
1602+
}
1603+
15971604

15981605
//-----------------------------------------------------------------------
15991606
//

regression-tests/pure2-enum.cpp2

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,29 @@ main: () = {
6060

6161
f := file_attributes::current | file_attributes::cached;
6262
f &= file_attributes::cached | file_attributes::obsolete;
63-
f |= file_attributes::current;
6463

6564
f2 := file_attributes::cached;
6665

6766
std::cout << "f as int is (f as int )$\n";
6867
std::cout << "f2 as int is (f2 as int )$\n";
69-
std::cout << "f == f2 is (f == f2 )$\n";
7068

71-
// The following are still in progress
69+
std::cout << "f is (f2) is (f is (f2))$\n";
70+
std::cout << "f2 is (f ) is (f2 is (f ))$\n\n";
7271

73-
// std::cout << "f is (f2) is (f is (f2))$\n";
74-
// std::cout << "f2 is (f ) is (f2 is (f ))$\n";
72+
f |= file_attributes::current;
73+
f |= file_attributes::obsolete;
74+
f2 |= file_attributes::current;
7575

76-
// if f is (file_attributes::cached ) { std::cout << "cached "; }
77-
// if f is (file_attributes::current ) { std::cout << "current "; }
78-
// if f is (file_attributes::obsolete) { std::cout << "obsolete "; }
79-
// if f is (file_attributes::none ) { std::cout << "none "; }
80-
// std::cout << "\n";
76+
std::cout << "f as int is (f as int )$\n";
77+
std::cout << "f2 as int is (f2 as int )$\n";
78+
std::cout << "f == f2 is (f == f2 )$\n";
79+
std::cout << "f is (f2) is (f is (f2))$\n";
80+
std::cout << "f2 is (f ) is (f2 is (f ))$\n";
81+
std::cout << "(f & f2) == f2 is ((f & f2) == f2)$\n";
82+
83+
std::cout << "inspecting: " << inspect f -> std::string {
84+
is (file_attributes::current) = "exactly 'current'";
85+
is (cpp2::has_flags(f2)) = "includes all f2's flags ('cached' and 'current')";
86+
is _ = "no match";
87+
} << "\n";
8188
}
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
with if else: clubs
22
with inspect: clubs
3-
f as int is 3
3+
f as int is 1
44
f2 as int is 1
5+
f is (f2) is 1
6+
f2 is (f ) is 1
7+
8+
f as int is 7
9+
f2 as int is 3
510
f == f2 is 0
11+
f is (f2) is 0
12+
f2 is (f ) is 0
13+
(f & f2) == f2 is 1
14+
inspecting: includes all f2's flags ('cached' and 'current')

regression-tests/test-results/gcc-10/pure2-bugfix-for-requires-clause-in-forward-declaration.cpp.output

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp2:3:36: error: expected ‘;’ at end of member declaration
22
In file included from pure2-bugfix-for-requires-clause-in-forward-declaration.cpp:7:
3-
../../../include/cpp2util.h:1787:47: error: static assertion failed: GCC 11 or higher is required to support variables and type-scope functions that have a 'requires' clause. This includes a type-scope 'forward' parameter of non-wildcard type, such as 'func: (this, forward s: std::string)', which relies on being able to add a 'requires' clause - in that case, use 'forward s: _' instead if you need the result to compile with GCC 10.
4-
1787 | #define CPP2_REQUIRES_(...) static_assert(false, "GCC 11 or higher is required to support variables and type-scope functions that have a 'requires' clause. This includes a type-scope 'forward' parameter of non-wildcard type, such as 'func: (this, forward s: std::string)', which relies on being able to add a 'requires' clause - in that case, use 'forward s: _' instead if you need the result to compile with GCC 10.")
3+
../../../include/cpp2util.h:1794:47: error: static assertion failed: GCC 11 or higher is required to support variables and type-scope functions that have a 'requires' clause. This includes a type-scope 'forward' parameter of non-wildcard type, such as 'func: (this, forward s: std::string)', which relies on being able to add a 'requires' clause - in that case, use 'forward s: _' instead if you need the result to compile with GCC 10.
4+
1794 | #define CPP2_REQUIRES_(...) static_assert(false, "GCC 11 or higher is required to support variables and type-scope functions that have a 'requires' clause. This includes a type-scope 'forward' parameter of non-wildcard type, such as 'func: (this, forward s: std::string)', which relies on being able to add a 'requires' clause - in that case, use 'forward s: _' instead if you need the result to compile with GCC 10.")
55
| ^~~~~
66
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp2:4:1: note: in expansion of macro ‘CPP2_REQUIRES_’
77
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp2:3:46: error: expected ‘;’ at end of member declaration
88
In file included from pure2-bugfix-for-requires-clause-in-forward-declaration.cpp:7:
9-
../../../include/cpp2util.h:1787:47: error: static assertion failed: GCC 11 or higher is required to support variables and type-scope functions that have a 'requires' clause. This includes a type-scope 'forward' parameter of non-wildcard type, such as 'func: (this, forward s: std::string)', which relies on being able to add a 'requires' clause - in that case, use 'forward s: _' instead if you need the result to compile with GCC 10.
10-
1787 | #define CPP2_REQUIRES_(...) static_assert(false, "GCC 11 or higher is required to support variables and type-scope functions that have a 'requires' clause. This includes a type-scope 'forward' parameter of non-wildcard type, such as 'func: (this, forward s: std::string)', which relies on being able to add a 'requires' clause - in that case, use 'forward s: _' instead if you need the result to compile with GCC 10.")
9+
../../../include/cpp2util.h:1794:47: error: static assertion failed: GCC 11 or higher is required to support variables and type-scope functions that have a 'requires' clause. This includes a type-scope 'forward' parameter of non-wildcard type, such as 'func: (this, forward s: std::string)', which relies on being able to add a 'requires' clause - in that case, use 'forward s: _' instead if you need the result to compile with GCC 10.
10+
1794 | #define CPP2_REQUIRES_(...) static_assert(false, "GCC 11 or higher is required to support variables and type-scope functions that have a 'requires' clause. This includes a type-scope 'forward' parameter of non-wildcard type, such as 'func: (this, forward s: std::string)', which relies on being able to add a 'requires' clause - in that case, use 'forward s: _' instead if you need the result to compile with GCC 10.")
1111
| ^~~~~
1212
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp2:4:1: note: in expansion of macro ‘CPP2_REQUIRES_’
1313
pure2-bugfix-for-requires-clause-in-forward-declaration.cpp2:3:3: error: no declaration matches ‘element::element(auto:80&&) requires is_same_v<typename std::remove_cv<typename std::remove_reference<decltype(element::__ct ::n)>::type>::type, std::__cxx11::string>’
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
with if else: clubs
22
with inspect: clubs
3-
f as int is 3
3+
f as int is 1
44
f2 as int is 1
5+
f is (f2) is 1
6+
f2 is (f ) is 1
7+
8+
f as int is 7
9+
f2 as int is 3
510
f == f2 is 0
11+
f is (f2) is 0
12+
f2 is (f ) is 0
13+
(f & f2) == f2 is 1
14+
inspecting: includes all f2's flags ('cached' and 'current')
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
In file included from pure2-requires-clauses.cpp:7:
2-
../../../include/cpp2util.h:1787:33: error: expected unqualified-id before ‘static_assert’
3-
1787 | #define CPP2_REQUIRES_(...) static_assert(false, "GCC 11 or higher is required to support variables and type-scope functions that have a 'requires' clause. This includes a type-scope 'forward' parameter of non-wildcard type, such as 'func: (this, forward s: std::string)', which relies on being able to add a 'requires' clause - in that case, use 'forward s: _' instead if you need the result to compile with GCC 10.")
2+
../../../include/cpp2util.h:1794:33: error: expected unqualified-id before ‘static_assert’
3+
1794 | #define CPP2_REQUIRES_(...) static_assert(false, "GCC 11 or higher is required to support variables and type-scope functions that have a 'requires' clause. This includes a type-scope 'forward' parameter of non-wildcard type, such as 'func: (this, forward s: std::string)', which relies on being able to add a 'requires' clause - in that case, use 'forward s: _' instead if you need the result to compile with GCC 10.")
44
| ^~~~~~~~~~~~~
55
pure2-requires-clauses.cpp2:19:1: note: in expansion of macro ‘CPP2_REQUIRES_’
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
with if else: clubs
22
with inspect: clubs
3-
f as int is 3
3+
f as int is 1
44
f2 as int is 1
5+
f is (f2) is 1
6+
f2 is (f ) is 1
7+
8+
f as int is 7
9+
f2 as int is 3
510
f == f2 is 0
11+
f is (f2) is 0
12+
f2 is (f ) is 0
13+
(f & f2) == f2 is 1
14+
inspecting: includes all f2's flags ('cached' and 'current')
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
with if else: clubs
22
with inspect: clubs
3-
f as int is 3
3+
f as int is 1
44
f2 as int is 1
5+
f is (f2) is 1
6+
f2 is (f ) is 1
7+
8+
f as int is 7
9+
f2 as int is 3
510
f == f2 is 0
11+
f is (f2) is 0
12+
f2 is (f ) is 0
13+
(f & f2) == f2 is 1
14+
inspecting: includes all f2's flags ('cached' and 'current')
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pure2-bugfix-for-declaration-equal-error.cpp2...
2-
pure2-bugfix-for-declaration-equal-error.cpp2(1,10): error: missing semicolon at end of declaration or equal at start of initializer (at '{')
2+
pure2-bugfix-for-declaration-equal-error.cpp2(1,10): error: expected '=' before '{' - did you mean '= {' ? (at '{')
33
pure2-bugfix-for-declaration-equal-error.cpp2(1,1): error: unexpected text at end of Cpp2 code section (at 'main')
44
pure2-bugfix-for-declaration-equal-error.cpp2(1,0): error: parse failed for section starting here
55

0 commit comments

Comments
 (0)