[1/2] libcamera: utils: Add to_underlying() helper function
diff mbox series

Message ID 20240225164348.10073-2-laurent.pinchart@ideasonboard.com
State Accepted
Commit 6a50c960be774b44a32f566f1f485cf800cc0527
Headers show
Series
  • libcamera: Add utils::to_underlying() helper
Related show

Commit Message

Laurent Pinchart Feb. 25, 2024, 4:43 p.m. UTC
C++23 has a std::to_underlying() helper function that converts an
enumeration value to its underlying type. Add a compatible
implementation to the libcamera::utils namespace.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 include/libcamera/base/utils.h |  6 ++++++
 src/libcamera/base/utils.cpp   | 10 ++++++++++
 2 files changed, 16 insertions(+)

Comments

Kieran Bingham Feb. 25, 2024, 8 p.m. UTC | #1
Quoting Laurent Pinchart (2024-02-25 16:43:46)
> C++23 has a std::to_underlying() helper function that converts an
> enumeration value to its underlying type. Add a compatible
> implementation to the libcamera::utils namespace.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
>  include/libcamera/base/utils.h |  6 ++++++
>  src/libcamera/base/utils.cpp   | 10 ++++++++++
>  2 files changed, 16 insertions(+)
> 
> diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h
> index 37d9af609ec7..922e4dfaf9fa 100644
> --- a/include/libcamera/base/utils.h
> +++ b/include/libcamera/base/utils.h
> @@ -369,6 +369,12 @@ decltype(auto) abs_diff(const T &a, const T &b)
>  
>  double strtod(const char *__restrict nptr, char **__restrict endptr);
>  
> +template<class Enum>
> +constexpr std::underlying_type_t<Enum> to_underlying(Enum e) noexcept
> +{
> +       return static_cast<std::underlying_type_t<Enum>>(e);
> +}

Looks good compared to my understanding from:
https://en.cppreference.com/w/cpp/utility/to_underlying

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

> +
>  } /* namespace utils */
>  
>  #ifndef __DOXYGEN__
> diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp
> index 3b73b442260a..2f4c3177ac13 100644
> --- a/src/libcamera/base/utils.cpp
> +++ b/src/libcamera/base/utils.cpp
> @@ -521,6 +521,16 @@ double strtod(const char *__restrict nptr, char **__restrict endptr)
>  #endif
>  }
>  
> +/**
> + * \fn to_underlying(Enum e)
> + * \brief Convert an enumeration to its underlygin type
> + * \param[in] e Enumeration value to convert
> + *
> + * This function is equivalent to the C++23 std::to_underlying().
> + *
> + * \return The value of e converted to its underlying type
> + */
> +
>  } /* namespace utils */
>  
>  #ifndef __DOXYGEN__
> -- 
> Regards,
> 
> Laurent Pinchart
>

Patch
diff mbox series

diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h
index 37d9af609ec7..922e4dfaf9fa 100644
--- a/include/libcamera/base/utils.h
+++ b/include/libcamera/base/utils.h
@@ -369,6 +369,12 @@  decltype(auto) abs_diff(const T &a, const T &b)
 
 double strtod(const char *__restrict nptr, char **__restrict endptr);
 
+template<class Enum>
+constexpr std::underlying_type_t<Enum> to_underlying(Enum e) noexcept
+{
+	return static_cast<std::underlying_type_t<Enum>>(e);
+}
+
 } /* namespace utils */
 
 #ifndef __DOXYGEN__
diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp
index 3b73b442260a..2f4c3177ac13 100644
--- a/src/libcamera/base/utils.cpp
+++ b/src/libcamera/base/utils.cpp
@@ -521,6 +521,16 @@  double strtod(const char *__restrict nptr, char **__restrict endptr)
 #endif
 }
 
+/**
+ * \fn to_underlying(Enum e)
+ * \brief Convert an enumeration to its underlygin type
+ * \param[in] e Enumeration value to convert
+ *
+ * This function is equivalent to the C++23 std::to_underlying().
+ *
+ * \return The value of e converted to its underlying type
+ */
+
 } /* namespace utils */
 
 #ifndef __DOXYGEN__