Fix compiler warnings for C++ versions < 17

This commit is contained in:
h2zero 2024-11-30 15:18:55 -07:00 committed by h2zero
parent b4fe046c56
commit ecf1adc4d7
3 changed files with 202 additions and 8 deletions

View file

@ -84,8 +84,7 @@ class NimBLEAttValue {
* @param[in] len The size in bytes of the value to set.
* @param[in] max_len The max size in bytes that the value can be.
*/
NimBLEAttValue(const uint8_t *value, uint16_t len,
uint16_t max_len = BLE_ATT_ATTR_MAX_LEN);
NimBLEAttValue(const uint8_t* value, uint16_t len, uint16_t max_len = BLE_ATT_ATTR_MAX_LEN);
/**
* @brief Construct with an initial value from a const char string.
@ -221,14 +220,61 @@ class NimBLEAttValue {
/*********************** Template Functions ************************/
# if __cplusplus < 201703L
/**
* @brief Template to set value to the value of <type\>val.
* @param [in] v The <type\>value to set.
* @details Only used for types without a `c_str()` and `length()` or `data()` and `size()` method.
* <type\> size must be evaluatable by `sizeof()`.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<!std::is_pointer<T>::value && !Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
# endif
setValue(const T& v) {
return setValue(reinterpret_cast<const uint8_t*>(&v), sizeof(T));
}
/**
* @brief Template to set value to the value of <type\>val.
* @param [in] s The <type\>value to set.
* @note This function is only availabe if the type T is not a pointer.
* @details Only used if the <type\> has a `c_str()` method.
*/
template <typename T>
typename std::enable_if<!std::is_pointer<T>::value, bool>::type
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
# endif
setValue(const T& s) {
return setValue(reinterpret_cast<const uint8_t*>(s.c_str()), s.length());
}
/**
* @brief Template to set value to the value of <type\>val.
* @param [in] v The <type\>value to set.
* @details Only used if the <type\> has a `data()` and `size()` method.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<Has_data_size<T>::value, bool>::type
# endif
setValue(const T& v) {
return setValue(reinterpret_cast<const uint8_t*>(v.data()), v.size());
}
# else
/**
* @brief Template to set value to the value of <type\>val.
* @param [in] s The <type\>value to set.
* @note This function is only available if the type T is not a pointer.
*/
template <typename T>
typename std::enable_if<!std::is_pointer<T>::value, bool>::type setValue(const T& s) {
if constexpr (Has_data_size<T>::value) {
return setValue(reinterpret_cast<const uint8_t*>(s.data()), s.size());
} else if constexpr (Has_c_str_length<T>::value) {
@ -237,6 +283,7 @@ class NimBLEAttValue {
return setValue(reinterpret_cast<const uint8_t*>(&s), sizeof(s));
}
}
# endif
/**
* @brief Template to return the value as a <type\>.

View file

@ -72,6 +72,101 @@ class NimBLECharacteristic : public NimBLELocalValueAttribute {
/*********************** Template Functions ************************/
# if __cplusplus < 201703L
/**
* @brief Template to send a notification with a value from a struct or array.
* @param [in] v The value to send.
* @param [in] connHandle Optional, a connection handle to send the notification to.
* @details <type\> size must be evaluatable by `sizeof()`.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<!std::is_pointer<T>::value && !Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
# endif
notify(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
return notify(reinterpret_cast<const uint8_t*>(&v), sizeof(T), connHandle);
}
/**
* @brief Template to send a notification with a value from a class that has a c_str() and length() method.
* @param [in] s The value to send.
* @param [in] connHandle Optional, a connection handle to send the notification to.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
# endif
notify(const T& s, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
return notify(reinterpret_cast<const uint8_t*>(s.c_str()), s.length(), connHandle);
}
/**
* @brief Template to send a notification with a value from a class that has a data() and size() method.
* @param [in] v The value to send.
* @param [in] connHandle Optional, a connection handle to send the notification to.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<Has_data_size<T>::value, bool>::type
# endif
notify(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
return notify(reinterpret_cast<const uint8_t*>(v.data()), v.size(), connHandle);
}
/**
* @brief Template to send an indication with a value from a struct or array.
* @param [in] v The value to send.
* @param [in] connHandle Optional, a connection handle to send the notification to.
* @details <type\> size must be evaluatable by `sizeof()`.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<!std::is_pointer<T>::value && !Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
# endif
indicate(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
return indicate(reinterpret_cast<const uint8_t*>(&v), sizeof(T), connHandle);
}
/**
* @brief Template to send a indication with a value from a class that has a c_str() and length() method.
* @param [in] s The value to send.
* @param [in] connHandle Optional, a connection handle to send the notification to.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
# endif
indicate(const T& s, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
return indicate(reinterpret_cast<const uint8_t*>(s.c_str()), s.length(), connHandle);
}
/**
* @brief Template to send a indication with a value from a class that has a data() and size() method.
* @param [in] v The value to send.
* @param [in] connHandle Optional, a connection handle to send the notification to.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<Has_data_size<T>::value, bool>::type
# endif
indicate(const T& v, uint16_t connHandle = BLE_HS_CONN_HANDLE_NONE) const {
return indicate(reinterpret_cast<const uint8_t*>(v.data()), v.size(), connHandle);
}
# else
/**
* @brief Template to send a notification for classes which may have
* data()/size() or c_str()/length() methods. Falls back to sending
@ -115,6 +210,7 @@ class NimBLECharacteristic : public NimBLELocalValueAttribute {
return indicate(reinterpret_cast<const uint8_t*>(&value), sizeof(value), connHandle);
}
}
# endif
private:
friend class NimBLEServer;

View file

@ -74,6 +74,57 @@ class NimBLERemoteValueAttribute : public NimBLEAttribute {
return writeValue(reinterpret_cast<const uint8_t*>(str), length ? length : strlen(str), response);
}
# if __cplusplus < 201703L
/**
* @brief Template to set the remote characteristic value to <type\>val.
* @param [in] v The value to write.
* @param [in] response True == request write response.
* @details Only used for types without a `c_str()` and `length()` or `data()` and `size()` method.
* <type\> size must be evaluatable by `sizeof()` if no length is provided.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<!std::is_pointer<T>::value && !Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
# endif
writeValue(const T& v, bool response = false) const {
return writeValue(reinterpret_cast<const uint8_t*>(&v), sizeof(T), response);
}
/**
* @brief Template to set the remote characteristic value to <type\>val.
* @param [in] s The value to write.
* @param [in] response True == request write response.
* @details Only used if the <type\> has a `c_str()` and `length()` method.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<Has_c_str_length<T>::value && !Has_data_size<T>::value, bool>::type
# endif
writeValue(const T& s, bool response = false) const {
return writeValue(reinterpret_cast<const uint8_t*>(s.c_str()), s.length(), response);
}
/**
* @brief Template to set the remote characteristic value to <type\>val.
* @param [in] v The value to write.
* @param [in] response True == request write response.
* @details Only used if the <type\> has a `data()` and `size()` method.
*/
template <typename T>
# ifdef _DOXYGEN_
bool
# else
typename std::enable_if<Has_data_size<T>::value, bool>::type
# endif
writeValue(const T& v, bool response = false) const {
return writeValue(reinterpret_cast<const uint8_t*>(v.data()), v.size(), response);
}
# else
/**
* @brief Template to set the remote characteristic value to <type\>val.
* @param [in] s The value to write.
@ -81,8 +132,7 @@ class NimBLERemoteValueAttribute : public NimBLEAttribute {
* @note This function is only available if the type T is not a pointer.
*/
template <typename T>
typename std::enable_if<!std::is_pointer<T>::value, bool>::type
writeValue(const T& v, bool response = false) const {
typename std::enable_if<!std::is_pointer<T>::value, bool>::type writeValue(const T& v, bool response = false) const {
if constexpr (Has_data_size<T>::value) {
return writeValue(reinterpret_cast<const uint8_t*>(v.data()), v.size(), response);
} else if constexpr (Has_c_str_length<T>::value) {
@ -91,6 +141,7 @@ class NimBLERemoteValueAttribute : public NimBLEAttribute {
return writeValue(reinterpret_cast<const uint8_t*>(&v), sizeof(v), response);
}
}
# endif
/**
* @brief Template to convert the remote characteristic data to <type\>.