Skip to content

Commit 853d5c7

Browse files
chris-durandrleh
authored andcommitted
[architecture] Fix compiler warnings in can message, more constexpr
Fix deprecated warning about implicitly-declared copy constructor with custom assignment operator.
1 parent d772940 commit 853d5c7

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

src/modm/architecture/interface/can_message.hpp.in

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,90 +26,95 @@ namespace modm::can
2626
/// @ingroup modm_architecture_can
2727
struct Message
2828
{
29-
inline Message(uint32_t inIdentifier = 0, uint8_t inLength = 0) :
29+
constexpr Message(uint32_t inIdentifier = 0, uint8_t inLength = 0) :
3030
identifier(inIdentifier), flags()
3131
{
3232
setLength(inLength);
3333
}
3434

35-
inline Message(uint32_t inIdentifier, uint8_t inLength, const uint64_t &inData, bool extended = false) :
35+
constexpr Message(uint32_t inIdentifier, uint8_t inLength, const uint64_t &inData, bool extended = false) :
3636
Message(inIdentifier, std::min(inLength, uint8_t(8)))
3737
{
3838
flags.extended = extended;
39-
const uint8_t *inDataB = reinterpret_cast<const uint8_t *>(&inData);
4039
for (uint8_t ii = 0; ii < length; ++ii)
41-
data[ii] = inDataB[length - ii - 1];
40+
data[ii] = inData >> ((7 - ii - (8 - length)) * 8);
4241
}
4342

44-
inline Message(uint32_t inIdentifier, uint8_t inLength, const uint8_t *inData, bool extended = false) :
43+
constexpr Message(uint32_t inIdentifier, uint8_t inLength, const uint8_t *inData, bool extended = false) :
4544
Message(inIdentifier, inLength)
4645
{
4746
flags.extended = extended;
4847
for (uint8_t ii = 0; ii < length; ++ii)
4948
data[ii] = inData[ii];
5049
}
5150

52-
inline uint32_t
51+
constexpr Message(const modm::can::Message& rhs)
52+
: identifier{rhs.identifier}, flags{rhs.flags}, dlc{rhs.dlc}, length{rhs.length}
53+
{
54+
std::copy(std::begin(rhs.data), std::end(rhs.data), std::begin(data));
55+
}
56+
57+
constexpr uint32_t
5358
getIdentifier() const
5459
{
5560
return identifier;
5661
}
5762

58-
inline void
63+
constexpr void
5964
setIdentifier(uint32_t id)
6065
{
6166
identifier = id;
6267
}
6368

64-
inline constexpr uint8_t
69+
constexpr uint8_t
6570
getCapacity() const
6671
{
6772
return capacity;
6873
}
6974

70-
inline void
75+
constexpr void
7176
setFlexibleData(bool fd = true)
7277
{
7378
flags.fd = fd;
7479
}
7580

76-
inline bool
81+
constexpr bool
7782
isFlexibleData() const
7883
{
7984
return (flags.fd != 0);
8085
}
8186

82-
inline bool
87+
constexpr bool
8388
isBitRateSwitching() const
8489
{
8590
return (flags.brs != 0);
8691
}
8792

88-
inline void
93+
constexpr void
8994
setExtended(bool extended = true)
9095
{
9196
flags.extended = (extended) ? 1 : 0;
9297
}
9398

94-
inline bool
99+
constexpr bool
95100
isExtended() const
96101
{
97102
return (flags.extended != 0);
98103
}
99104

100-
inline void
105+
constexpr void
101106
setRemoteTransmitRequest(bool rtr = true)
102107
{
103108
flags.rtr = (rtr) ? 1 : 0;
104109
}
105110

106-
inline bool
111+
constexpr bool
107112
isRemoteTransmitRequest() const
108113
{
109114
return (flags.rtr != 0);
110115
}
111116

112-
inline void
117+
constexpr void
113118
setDataLengthCode(uint8_t inDlc)
114119
{
115120
while (dlcConversionTable[inDlc] > capacity) inDlc--;
@@ -119,7 +124,7 @@ struct Message
119124
if (dlc > 8) setFlexibleData();
120125
}
121126

122-
inline void
127+
constexpr void
123128
setLength(uint8_t inLength)
124129
{
125130
if constexpr (capacity <= 8)
@@ -134,13 +139,13 @@ struct Message
134139
}
135140
}
136141

137-
inline uint8_t
142+
constexpr uint8_t
138143
getLength() const
139144
{
140145
return length;
141146
}
142147

143-
inline uint8_t
148+
constexpr uint8_t
144149
getDataLengthCode() const
145150
{
146151
return dlc;
@@ -153,7 +158,7 @@ public:
153158
uint32_t identifier;
154159
struct Flags
155160
{
156-
Flags() :
161+
constexpr Flags() :
157162
fd(0), brs(0), rtr(0), extended(1)
158163
{
159164
}
@@ -165,10 +170,10 @@ public:
165170
} flags;
166171
uint8_t dlc;
167172
uint8_t length;
168-
uint8_t modm_aligned(4) data[capacity];
173+
uint8_t modm_aligned(4) data[capacity]{};
169174

170175
public:
171-
inline bool
176+
constexpr bool
172177
operator == (const modm::can::Message& rhs) const
173178
{
174179
return ((this->identifier == rhs.identifier) and
@@ -181,7 +186,7 @@ public:
181186
std::equal(data, data + getLength(), rhs.data));
182187
}
183188

184-
inline void
189+
constexpr modm::can::Message&
185190
operator = (const modm::can::Message& rhs)
186191
{
187192
this->identifier = rhs.identifier;
@@ -192,9 +197,10 @@ public:
192197
this->flags.rtr = rhs.flags.rtr;
193198
this->flags.extended = rhs.flags.extended;
194199
std::copy(std::begin(rhs.data), std::end(rhs.data), std::begin(this->data));
200+
return *this;
195201
}
196202

197-
inline bool
203+
constexpr bool
198204
operator < (const modm::can::Message& rhs) const
199205
{
200206
return (this->identifier << (this->flags.extended ? 0 : 18))

0 commit comments

Comments
 (0)