Skip to content

Commit

Permalink
update handle template
Browse files Browse the repository at this point in the history
  • Loading branch information
rzblue committed Aug 27, 2024
1 parent f4c3223 commit 7399d37
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion hal/src/main/native/include/hal/I2CTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace hal {
* A move-only C++ wrapper around HAL_I2CPort.
* Does not ensure destruction.
*/
using I2CPort = Handle<HAL_I2CPort, nullptr, HAL_I2C_kInvalid>;
using I2CPort = Handle<HAL_I2CPort, [](auto){}, HAL_I2C_kInvalid>;

} // namespace hal
#endif
Expand Down
2 changes: 1 addition & 1 deletion hal/src/main/native/include/hal/SPITypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace hal {
* A move-only C++ wrapper around HAL_SPIPort.
* Does not ensure destruction.
*/
using SPIPort = Handle<HAL_SPIPort, nullptr, HAL_SPI_kInvalid>;
using SPIPort = Handle<HAL_SPIPort, [](auto){}, HAL_SPI_kInvalid>;

} // namespace hal
#endif
Expand Down
8 changes: 3 additions & 5 deletions hal/src/main/native/include/hal/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ namespace hal {
* A move-only C++ wrapper around a HAL handle.
* Will free the handle if FreeFunction is provided
*/
template <typename CType, void (*FreeFunction)(CType) = nullptr,
template <typename CType, auto FreeFunction = [](CType) {},
int32_t CInvalid = HAL_kInvalidHandle>
class Handle {
public:
Expand All @@ -111,10 +111,8 @@ class Handle {
}

~Handle() {
if constexpr (FreeFunction != nullptr) {
if (m_handle != CInvalid) {
FreeFunction(m_handle);
}
if (m_handle != CInvalid) {
FreeFunction(m_handle);
}
}

Expand Down

0 comments on commit 7399d37

Please sign in to comment.