Merge pull request #9393 from pan-/fix-safe-enum-type-safety

BLE: Fix SafeEnum type safety
pull/9526/head
Cruz Monrreal 2019-01-28 10:31:52 -06:00 committed by GitHub
commit b49d949b50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 14 deletions

View File

@ -112,13 +112,15 @@ struct SafeEnum {
*/ */
typedef LayoutType representation_t; typedef LayoutType representation_t;
protected:
/** /**
* Construction of an enumeration value. * Construction of an enumeration value.
*/ */
SafeEnum(LayoutType value) : _value(value) { } explicit SafeEnum(LayoutType value) : _value(value) { }
public:
/** /**
* Equal to operator for SafeEnum instances. * Equal to operator for Target instances.
* *
* @param lhs left hand side of the comparison * @param lhs left hand side of the comparison
* @param rhs right hand side of the comparison * @param rhs right hand side of the comparison
@ -126,12 +128,12 @@ struct SafeEnum {
* @return true if the inner value of lhs and rhs are equal and false * @return true if the inner value of lhs and rhs are equal and false
* otherwise. * otherwise.
*/ */
friend bool operator==(SafeEnum lhs, SafeEnum rhs) { friend bool operator==(Target lhs, Target rhs) {
return lhs._value == rhs._value; return lhs._value == rhs._value;
} }
/** /**
* Not equal to operator for SafeEnum instances. * Not equal to operator for Target instances.
* *
* @param lhs left hand side of the comparison * @param lhs left hand side of the comparison
* @param rhs right hand side of the comparison * @param rhs right hand side of the comparison
@ -139,24 +141,24 @@ struct SafeEnum {
* @return true if the inner value of lhs and rhs are not equal and false * @return true if the inner value of lhs and rhs are not equal and false
* otherwise. * otherwise.
*/ */
friend bool operator!=(SafeEnum lhs, SafeEnum rhs) { friend bool operator!=(Target lhs, Target rhs) {
return !(lhs == rhs); return !(lhs == rhs);
} }
/** /**
* Less than operator for SafeEnum instances. * Less than operator for Target instances.
* *
* @param lhs left hand side of the comparison * @param lhs left hand side of the comparison
* @param rhs right hand side of the comparison * @param rhs right hand side of the comparison
* *
* @return true if the inner value of lhs is less than rhs and false otherwise. * @return true if the inner value of lhs is less than rhs and false otherwise.
*/ */
friend bool operator<(SafeEnum lhs, SafeEnum rhs) { friend bool operator<(Target lhs, Target rhs) {
return lhs.value() < rhs.value(); return lhs.value() < rhs.value();
} }
/** /**
* Less than or equal to operator for SafeEnum instances. * Less than or equal to operator for Target instances.
* *
* @param lhs left hand side of the comparison * @param lhs left hand side of the comparison
* @param rhs right hand side of the comparison * @param rhs right hand side of the comparison
@ -164,12 +166,12 @@ struct SafeEnum {
* @return true if the inner value of lhs is less than or equal to rhs and * @return true if the inner value of lhs is less than or equal to rhs and
* false otherwise. * false otherwise.
*/ */
friend bool operator<=(SafeEnum lhs, SafeEnum rhs) { friend bool operator<=(Target lhs, Target rhs) {
return lhs.value() < rhs.value() || lhs == rhs; return lhs.value() < rhs.value() || lhs == rhs;
} }
/** /**
* Greater than operator for SafeEnum instances. * Greater than operator for Target instances.
* *
* @param lhs left hand side of the comparison * @param lhs left hand side of the comparison
* @param rhs right hand side of the comparison * @param rhs right hand side of the comparison
@ -177,12 +179,12 @@ struct SafeEnum {
* @return true if the inner value of lhs is greater than rhs; false * @return true if the inner value of lhs is greater than rhs; false
* otherwise. * otherwise.
*/ */
friend bool operator>(SafeEnum lhs, SafeEnum rhs) { friend bool operator>(Target lhs, Target rhs) {
return !(lhs <= rhs); return !(lhs <= rhs);
} }
/** /**
* Greater than or equal to operator for SafeEnum instances. * Greater than or equal to operator for Target instances.
* *
* @param lhs left hand side of the comparison * @param lhs left hand side of the comparison
* @param rhs right hand side of the comparison * @param rhs right hand side of the comparison
@ -190,7 +192,7 @@ struct SafeEnum {
* @return true if the inner value of lhs is greater than or equal to rhs; * @return true if the inner value of lhs is greater than or equal to rhs;
* false otherwise. * false otherwise.
*/ */
friend bool operator>=(SafeEnum lhs, SafeEnum rhs) { friend bool operator>=(Target lhs, Target rhs) {
return !(lhs < rhs); return !(lhs < rhs);
} }

View File

@ -331,7 +331,7 @@ uint8_t *AdvertisingDataBuilder::findField(adv_data_type_t type)
for (uint8_t idx = 0; idx < _payload_length;) { for (uint8_t idx = 0; idx < _payload_length;) {
uint8_t fieldType = _buffer[idx + FIELD_TYPE_INDEX]; uint8_t fieldType = _buffer[idx + FIELD_TYPE_INDEX];
if (fieldType == type) { if (fieldType == type.value()) {
return _buffer.data() + idx; return _buffer.data() + idx;
} }