Commit eb7de86
committed
Add
`enum` and `flag_enum` are implemented entirely using type metafunctions; they are not special hardwired language features, each is just a subset of the universe of general C++ classes. An `enum` is a `value`, and a `flag_enum` additionally has bitwise operations to set/test the flags in the usual way.
To specify each enumerator, just declare it as a member - the `:` is not required if you don't want to specify the underlying type or the enumerator's value. It will default to `public static constexpr`.
> Note: This is using the other recent [checkin that generalizes object aliases](9d86794) to allow `var :== value;` and `var: some_type = value;` to write `static constexpr` objects. Handy for enumerators.
The underlying type defaults to the minimum-sized signed integer that can represent the values, one of `i8`, `i16`, `i32`, or `i64`. To explicitly specify the underlying type, name it on the first member (only). For example: `color: @enum type = { red: u16 = 0; green; blue; }`. Enums larger than 64 bits are not currently supported.
When an enumerator's value is not specified, it defaults to the next consecutive value for `enum` and the next power-of-two value for `flag_enum`.
TODO: `is value` is working for `enum` but not yet for `flag_enum`
Pasting new feature test case `pure2-enum.cpp2`:
```
@@ -0,0 +1,81 @@
skat_game: @enum type = {
diamonds := 9;
hearts; // 10
spades; // 11
clubs; // 12
grand := 20;
null := 23;
}
rgb: @enum type = {
red; // 0
green; // 1
blue; // 2
}
file_attributes: @flag_enum type = {
cached; // 1
current; // 2
obsolete; // 4
}
main: () = {
// x : skat_game = 9; // error, can't construct skat_game from integer
x := skat_game::clubs;
// if x == 9 { } // error, can't compare skat_game and integer
// if x == rgb::red { } // error, can't compare skat_game and rgb color
std::cout << "with if else: ";
if x == skat_game::diamonds { // ok, can compare two skat_games
std::cout << "diamonds";
}
else if skat_game::hearts == x { // ok, in either order
std::cout << "hearts";
}
else if x is (skat_game::spades) { // ok, using is
std::cout << "spades";
}
else if skat_game::clubs is (x) { // ok, using is
std::cout << "clubs";
}
else {
std::cout << "not a suit";
}
std::cout << "\nwith inspect: " << inspect x -> std::string {
is (skat_game::diamonds) = "diamonds";
is (skat_game::hearts ) = "hearts";
is (skat_game::spades ) = "spades";
is (skat_game::clubs ) = "clubs";
is _ = "not a suit";
} << std::endl;
// x = 9; // error, can't assign skat_game from integer
// x = rgb::red; // error, can't assign skat_game from rgb color
x = skat_game::diamonds; // ok, can assign one skat_game from another
f := file_attributes::current | file_attributes::cached;
f &= file_attributes::cached | file_attributes::obsolete;
f |= file_attributes::current;
f2 := file_attributes::cached;
std::cout << "f as int is (f as int )$\n";
std::cout << "f2 as int is (f2 as int )$\n";
std::cout << "f == f2 is (f == f2 )$\n";
// The following are still in progress
// std::cout << "f is (f2) is (f is (f2))$\n";
// std::cout << "f2 is (f ) is (f2 is (f ))$\n";
// if f is (file_attributes::cached ) { std::cout << "cached "; }
// if f is (file_attributes::current ) { std::cout << "current "; }
// if f is (file_attributes::obsolete) { std::cout << "obsolete "; }
// if f is (file_attributes::none ) { std::cout << "none "; }
// std::cout << "\n";
}
```enum and flag_enum type metafunctions - initial checkin1 parent 9d86794 commit eb7de86
File tree
18 files changed
+755
-163
lines changed- include
- regression-tests
- test-results
- clang-12
- gcc-10
- gcc-13
- msvc-2022
- source
18 files changed
+755
-163
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
860 | 860 | | |
861 | 861 | | |
862 | 862 | | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
863 | 868 | | |
864 | | - | |
| 869 | + | |
865 | 870 | | |
866 | 871 | | |
867 | 872 | | |
868 | 873 | | |
869 | 874 | | |
870 | 875 | | |
871 | | - | |
| 876 | + | |
872 | 877 | | |
873 | 878 | | |
874 | 879 | | |
| |||
1534 | 1539 | | |
1535 | 1540 | | |
1536 | 1541 | | |
| 1542 | + | |
| 1543 | + | |
| 1544 | + | |
| 1545 | + | |
| 1546 | + | |
| 1547 | + | |
| 1548 | + | |
| 1549 | + | |
| 1550 | + | |
| 1551 | + | |
| 1552 | + | |
| 1553 | + | |
| 1554 | + | |
1537 | 1555 | | |
1538 | 1556 | | |
1539 | 1557 | | |
| |||
1550 | 1568 | | |
1551 | 1569 | | |
1552 | 1570 | | |
1553 | | - | |
| 1571 | + | |
1554 | 1572 | | |
1555 | | - | |
1556 | | - | |
| 1573 | + | |
1557 | 1574 | | |
1558 | | - | |
| 1575 | + | |
| 1576 | + | |
| 1577 | + | |
| 1578 | + | |
1559 | 1579 | | |
1560 | 1580 | | |
1561 | | - | |
| 1581 | + | |
1562 | 1582 | | |
1563 | 1583 | | |
1564 | 1584 | | |
| 1585 | + | |
| 1586 | + | |
| 1587 | + | |
| 1588 | + | |
| 1589 | + | |
| 1590 | + | |
| 1591 | + | |
| 1592 | + | |
| 1593 | + | |
| 1594 | + | |
1565 | 1595 | | |
1566 | 1596 | | |
1567 | 1597 | | |
| |||
1627 | 1657 | | |
1628 | 1658 | | |
1629 | 1659 | | |
1630 | | - | |
1631 | | - | |
1632 | | - | |
1633 | | - | |
1634 | | - | |
1635 | | - | |
1636 | | - | |
1637 | | - | |
1638 | | - | |
1639 | | - | |
1640 | | - | |
1641 | | - | |
1642 | | - | |
1643 | 1660 | | |
1644 | 1661 | | |
1645 | 1662 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
Whitespace-only changes.
Lines changed: 4 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
| 3 | + | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | | - | |
| 9 | + | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
Whitespace-only changes.
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
| 2 | + | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
Whitespace-only changes.
0 commit comments