Skip to content

Commit 300f82e

Browse files
committed
added enum19 max_underlying_value_of
1 parent e93e05e commit 300f82e

4 files changed

Lines changed: 36 additions & 0 deletions

File tree

src/enum19.lib/enum19/Enum.max.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
#include "Enum.h"
3+
4+
namespace enum19 {
5+
6+
template<class T>
7+
constexpr auto max_underlying_value_of = []() -> std::underlying_type_t<T> {
8+
using V = std::underlying_type_t<T>;
9+
auto result = V{};
10+
for (auto& member : meta_enum_for<T>.members) {
11+
const auto v = static_cast<V>(member.value);
12+
if (result < v) result = v;
13+
}
14+
return result;
15+
}();
16+
17+
} // namespace enum19
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "Enum.max.h"
2+
3+
#include <gtest/gtest.h>
4+
5+
using namespace enum19;
6+
7+
namespace my_test {
8+
9+
ENUM19(Color, int, green = 0x0f0, red = 0xf00, brown = green + red);
10+
ENUM19(List, int, Base, Apple, Banana, Orange, Pinapple);
11+
12+
} // namespace my_test
13+
14+
TEST(Enum_max, example) {
15+
static_assert(enum19::max_underlying_value_of<my_test::Color> == 0xff0);
16+
static_assert(enum19::max_underlying_value_of<my_test::List> == 4);
17+
}

src/enum19.lib/enum19/enum19.qbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Product {
1515
"Enum.extras.ostream.h",
1616
"Enum.fmt.h",
1717
"Enum.h",
18+
"Enum.max.h",
1819
"Enum.names.h",
1920
"Enum.ostream.h",
2021
"visitEnumMemberNames.h",

src/enum19.lib/enum19/enum19.tests.qbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ Application {
1212
files: [
1313
"Enum.ostream.test.cpp",
1414
"Enum.test.cpp",
15+
"Enum.max.test.cpp",
1516
]
1617
}

0 commit comments

Comments
 (0)