[libcamera-devel] libcamera: utils: Only enable utils::hex() for integer arguments
diff mbox series

Message ID 20210625215558.32615-1-laurent.pinchart@ideasonboard.com
State Accepted
Commit d3fef998444c21142fa12b9bf698a73ee5706fd6
Headers show
Series
  • [libcamera-devel] libcamera: utils: Only enable utils::hex() for integer arguments
Related show

Commit Message

Laurent Pinchart June 25, 2021, 9:55 p.m. UTC
The utils::hex() function is defined as a function template that has
implementations for integer arguments only. When given a different
argument type, the compiler will not catch the issue, but linking will
fail:

src/libcamera/libcamera.so.p/camera_sensor.cpp.o: in function `libcamera::CameraSensor::validateSensorDriver()':
camera_sensor.cpp:(.text+0x1e6b): undefined reference to `libcamera::utils::_hex libcamera::utils::hex<libcamera::ControlId const*>(libcamera::ControlId const*, unsigned int)'

Move the failure to compilation time by enabling the function for
integer arguments only. This provides better diagnostics:

../../src/libcamera/camera_sensor.cpp: In member function ‘int libcamera::CameraSensor::validateSensorDriver()’:
../../src/libcamera/camera_sensor.cpp:199:77: error: no matching function for call to ‘hex(const libcamera::ControlId*&)’

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 include/libcamera/base/utils.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Kieran Bingham June 27, 2021, 11:50 a.m. UTC | #1
Hi Laurent,

On 25/06/2021 22:55, Laurent Pinchart wrote:
> The utils::hex() function is defined as a function template that has
> implementations for integer arguments only. When given a different
> argument type, the compiler will not catch the issue, but linking will
> fail:
> 
> src/libcamera/libcamera.so.p/camera_sensor.cpp.o: in function `libcamera::CameraSensor::validateSensorDriver()':
> camera_sensor.cpp:(.text+0x1e6b): undefined reference to `libcamera::utils::_hex libcamera::utils::hex<libcamera::ControlId const*>(libcamera::ControlId const*, unsigned int)'
> 
> Move the failure to compilation time by enabling the function for
> integer arguments only. This provides better diagnostics:
> 
> ../../src/libcamera/camera_sensor.cpp: In member function ‘int libcamera::CameraSensor::validateSensorDriver()’:
> ../../src/libcamera/camera_sensor.cpp:199:77: error: no matching function for call to ‘hex(const libcamera::ControlId*&)’
> 

Sounds better indeed.

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>


> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
>  include/libcamera/base/utils.h | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h
> index 07685045aa05..52301254c2eb 100644
> --- a/include/libcamera/base/utils.h
> +++ b/include/libcamera/base/utils.h
> @@ -16,6 +16,7 @@
>  #include <string>
>  #include <string.h>
>  #include <sys/time.h>
> +#include <type_traits>
>  #include <utility>
>  #include <vector>
>  
> @@ -84,7 +85,8 @@ std::basic_ostream<char, std::char_traits<char>> &
>  operator<<(std::basic_ostream<char, std::char_traits<char>> &stream, const _hex &h);
>  #endif
>  
> -template<typename T>
> +template<typename T,
> +	 std::enable_if_t<std::is_integral<T>::value> * = nullptr>
>  _hex hex(T value, unsigned int width = 0);
>  
>  #ifndef __DOXYGEN__
>
Paul Elder June 28, 2021, 5 a.m. UTC | #2
Hi Laurent,

On Sat, Jun 26, 2021 at 12:55:58AM +0300, Laurent Pinchart wrote:
> The utils::hex() function is defined as a function template that has
> implementations for integer arguments only. When given a different
> argument type, the compiler will not catch the issue, but linking will
> fail:
> 
> src/libcamera/libcamera.so.p/camera_sensor.cpp.o: in function `libcamera::CameraSensor::validateSensorDriver()':
> camera_sensor.cpp:(.text+0x1e6b): undefined reference to `libcamera::utils::_hex libcamera::utils::hex<libcamera::ControlId const*>(libcamera::ControlId const*, unsigned int)'
> 
> Move the failure to compilation time by enabling the function for
> integer arguments only. This provides better diagnostics:
> 
> ../../src/libcamera/camera_sensor.cpp: In member function ‘int libcamera::CameraSensor::validateSensorDriver()’:
> ../../src/libcamera/camera_sensor.cpp:199:77: error: no matching function for call to ‘hex(const libcamera::ControlId*&)’
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>

> ---
>  include/libcamera/base/utils.h | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h
> index 07685045aa05..52301254c2eb 100644
> --- a/include/libcamera/base/utils.h
> +++ b/include/libcamera/base/utils.h
> @@ -16,6 +16,7 @@
>  #include <string>
>  #include <string.h>
>  #include <sys/time.h>
> +#include <type_traits>
>  #include <utility>
>  #include <vector>
>  
> @@ -84,7 +85,8 @@ std::basic_ostream<char, std::char_traits<char>> &
>  operator<<(std::basic_ostream<char, std::char_traits<char>> &stream, const _hex &h);
>  #endif
>  
> -template<typename T>
> +template<typename T,
> +	 std::enable_if_t<std::is_integral<T>::value> * = nullptr>
>  _hex hex(T value, unsigned int width = 0);
>  
>  #ifndef __DOXYGEN__
> -- 
> Regards,
> 
> Laurent Pinchart
>

Patch
diff mbox series

diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h
index 07685045aa05..52301254c2eb 100644
--- a/include/libcamera/base/utils.h
+++ b/include/libcamera/base/utils.h
@@ -16,6 +16,7 @@ 
 #include <string>
 #include <string.h>
 #include <sys/time.h>
+#include <type_traits>
 #include <utility>
 #include <vector>
 
@@ -84,7 +85,8 @@  std::basic_ostream<char, std::char_traits<char>> &
 operator<<(std::basic_ostream<char, std::char_traits<char>> &stream, const _hex &h);
 #endif
 
-template<typename T>
+template<typename T,
+	 std::enable_if_t<std::is_integral<T>::value> * = nullptr>
 _hex hex(T value, unsigned int width = 0);
 
 #ifndef __DOXYGEN__