Skip to content

Commit 9a3bb49

Browse files
samples: matter: Allow using enums as keys in the finite map
If the T1 type was an enum type, then the kInvalidKey gets 0 value instead of type::max(). To fix it, conditionally get the underlying type of T1 if it is an enum, and return T1 if not. Signed-off-by: Arkadiusz Balys <arkadiusz.balys@nordicsemi.no>
1 parent 3524638 commit 9a3bb49

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

samples/matter/common/src/util/finite_map.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,24 @@ namespace Nrf
3636
key.
3737
* T2 must have move semantics and bool()/==operators implemented
3838
*/
39+
40+
template <typename T, bool IsEnum> struct KeyTypeHelper {
41+
using type = T;
42+
};
43+
44+
template <typename T> struct KeyTypeHelper<T, true> {
45+
using type = typename std::underlying_type<T>::type;
46+
};
47+
3948
template <typename T1, typename T2, uint16_t N> struct FiniteMap {
4049
static_assert(std::is_trivial_v<T1>);
4150

42-
static constexpr T1 kInvalidKey{ std::numeric_limits<T1>::max() };
43-
static constexpr std::size_t kNoSlotsFound{ N + 1 };
51+
using KeyType = typename KeyTypeHelper<T1, std::is_enum_v<T1>>::type;
4452
using ElementCounterType = uint16_t;
4553

54+
static constexpr T1 kInvalidKey{ static_cast<T1>(std::numeric_limits<KeyType>::max()) };
55+
static constexpr std::size_t kNoSlotsFound{ N + 1 };
56+
4657
struct Item {
4758
/* Initialize with invalid key (0 is a valid key) */
4859
T1 key{ kInvalidKey };

0 commit comments

Comments
 (0)