Skip to content

Commit 50860b1

Browse files
feat: improve hal::Can::Id (#786)
* Add equality comparators * Make hal::Can::Id creation constexpr
1 parent d3b1218 commit 50860b1

File tree

3 files changed

+54
-18
lines changed

3 files changed

+54
-18
lines changed

hal/interfaces/Can.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,6 @@
22

33
namespace hal
44
{
5-
Can::Id::Id(uint32_t id)
6-
: id(id)
7-
{}
8-
9-
Can::Id Can::Id::Create11BitId(uint32_t id)
10-
{
11-
return Can::Id(id);
12-
}
13-
14-
Can::Id Can::Id::Create29BitId(uint32_t id)
15-
{
16-
return Can::Id(id | indicator29Bit);
17-
}
18-
195
bool Can::Id::Is11BitId() const
206
{
217
return (id & indicator29Bit) == 0;
@@ -39,4 +25,14 @@ namespace hal
3925

4026
return id ^ indicator29Bit;
4127
}
28+
29+
bool Can::Id::operator==(const Id& other) const
30+
{
31+
return id == other.id;
32+
}
33+
34+
bool Can::Id::operator!=(const Id& other) const
35+
{
36+
return !(*this == other);
37+
}
4238
}

hal/interfaces/Can.hpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#ifndef HAL_CAN_HPP
22
#define HAL_CAN_HPP
33

4-
#include "hal/interfaces/Gpio.hpp"
54
#include "infra/util/BoundedVector.hpp"
5+
#include "infra/util/Function.hpp"
66
#include <cstdint>
77

88
namespace hal
@@ -13,17 +13,29 @@ namespace hal
1313
class Id
1414
{
1515
public:
16-
static Id Create11BitId(uint32_t id);
17-
static Id Create29BitId(uint32_t id);
16+
static constexpr Id Create11BitId(uint32_t id)
17+
{
18+
return Can::Id(id);
19+
}
20+
21+
static constexpr Id Create29BitId(uint32_t id)
22+
{
23+
return Can::Id(id | indicator29Bit);
24+
}
1825

1926
bool Is11BitId() const;
2027
bool Is29BitId() const;
2128

2229
uint32_t Get11BitId() const;
2330
uint32_t Get29BitId() const;
2431

32+
bool operator==(const Id& other) const;
33+
bool operator!=(const Id& other) const;
34+
2535
private:
26-
explicit Id(uint32_t id);
36+
constexpr explicit Id(uint32_t id)
37+
: id(id)
38+
{}
2739

2840
private:
2941
static const uint32_t indicator29Bit = static_cast<uint32_t>(1) << 31;

hal/interfaces/test/TestCan.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,31 @@ TEST(CanTest, generate_29_bit_id)
1616
EXPECT_TRUE(id.Is29BitId());
1717
EXPECT_EQ(0, id.Get29BitId());
1818
}
19+
20+
TEST(CanTest, test_equality_mixed)
21+
{
22+
auto id11_0 = hal::Can::Id::Create11BitId(0);
23+
auto id11_1 = hal::Can::Id::Create11BitId(1);
24+
auto id29_0 = hal::Can::Id::Create29BitId(0);
25+
auto id29_1 = hal::Can::Id::Create29BitId(1);
26+
27+
EXPECT_TRUE(id11_0 == hal::Can::Id::Create11BitId(0));
28+
EXPECT_TRUE(id11_0 != id11_1);
29+
EXPECT_TRUE(id11_0 != id29_0);
30+
EXPECT_TRUE(id11_0 != id29_1);
31+
32+
EXPECT_TRUE(id11_1 != id11_0);
33+
EXPECT_TRUE(id11_1 == hal::Can::Id::Create11BitId(1));
34+
EXPECT_TRUE(id11_1 != id29_0);
35+
EXPECT_TRUE(id11_1 != id29_1);
36+
37+
EXPECT_TRUE(id29_0 != id11_0);
38+
EXPECT_TRUE(id29_0 != id11_1);
39+
EXPECT_TRUE(id29_0 == hal::Can::Id::Create29BitId(0));
40+
EXPECT_TRUE(id29_0 != id29_1);
41+
42+
EXPECT_TRUE(id29_1 != id11_0);
43+
EXPECT_TRUE(id29_1 != id11_1);
44+
EXPECT_TRUE(id29_1 != id29_0);
45+
EXPECT_TRUE(id29_1 == hal::Can::Id::Create29BitId(1));
46+
}

0 commit comments

Comments
 (0)