summaryrefslogtreecommitdiff
path: root/src/libcamera/base
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2022-07-27 23:51:17 +0300
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2022-07-28 14:54:41 +0300
commit48c106429a197d9c35c30523d3f5c711f3137333 (patch)
treed188e436875e54b1764eb79babdc7c2dc36f632c /src/libcamera/base
parentf6d6181d3c91efa526b5027319331db9ac75f65b (diff)
libcamera: base: utils: Provide defopt to simplify std::optional::value_or() usage
The std::optional<T>::value_or(U &&default_value) function returns the contained value if available, or default_value if the std::optional has no value. If the desired default value is a default-constructed T, the obvious option is to call std::optional<T>::value_or(T{}). This approach has two drawbacks: - The \a default_value T{} is constructed even if the std::optional instance has a value, which impacts efficiency. - The T{} default constructor needs to be spelled out explicitly in the value_or() call, leading to long lines if the type is complex. Introduce a defopt variable that solves these issues by providing a value that can be passed to std::optional<T>::value_or() and get implicitly converted to a default-constructed T. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Umang Jain <umang.jain@ideasonboard.com> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
Diffstat (limited to 'src/libcamera/base')
-rw-r--r--src/libcamera/base/utils.cpp21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp
index 6a307940..9cd6cb19 100644
--- a/src/libcamera/base/utils.cpp
+++ b/src/libcamera/base/utils.cpp
@@ -463,6 +463,27 @@ std::string toAscii(const std::string &str)
* \a b
*/
+/**
+ * \var defopt
+ * \brief Constant used with std::optional::value_or() to create a
+ * default-constructed object
+ *
+ * The std::optional<T>::value_or(U &&default_value) function returns the
+ * contained value if available, or \a default_value if the std::optional has no
+ * value. If the desired default value is a default-constructed T, the obvious
+ * option is to call std::optional<T>::value_or(T{}). This approach has two
+ * drawbacks:
+ *
+ * * The \a default_value T{} is constructed even if the std::optional instance
+ * has a value, which impacts efficiency.
+ * * The T{} default constructor needs to be spelled out explicitly in the
+ * value_or() call, leading to long lines if the type is complex.
+ *
+ * The defopt variable solves these issues by providing a value that can be
+ * passed to std::optional<T>::value_or() and get implicitly converted to a
+ * default-constructed T.
+ */
+
} /* namespace utils */
#ifndef __DOXYGEN__