[v2,2/4] libcamera: Replace iterators with range-based for loops
diff mbox series

Message ID 20260212151238.3736234-3-laurent.pinchart@ideasonboard.com
State Accepted
Headers show
Series
  • libcamera: Clean up iterators
Related show

Commit Message

Laurent Pinchart Feb. 12, 2026, 3:12 p.m. UTC
Use range-based for loops instead of iterators when iterating over a
container. This improves readability.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 include/libcamera/base/utils.h       |  8 ++---
 src/gstreamer/gstlibcamera-utils.cpp | 50 +++++++++++-----------------
 src/ipa/rpi/controller/rpi/agc.cpp   |  4 +--
 3 files changed, 26 insertions(+), 36 deletions(-)

Comments

Barnabás Pőcze Feb. 13, 2026, 9:15 a.m. UTC | #1
2026. 02. 12. 16:12 keltezéssel, Laurent Pinchart írta:
> Use range-based for loops instead of iterators when iterating over a
> container. This improves readability.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---

Reviewed-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>


>   include/libcamera/base/utils.h       |  8 ++---
>   src/gstreamer/gstlibcamera-utils.cpp | 50 +++++++++++-----------------
>   src/ipa/rpi/controller/rpi/agc.cpp   |  4 +--
>   3 files changed, 26 insertions(+), 36 deletions(-)
> 
> diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h
> index 4373bc20d42e..37f0d065f82f 100644
> --- a/include/libcamera/base/utils.h
> +++ b/include/libcamera/base/utils.h
> @@ -110,13 +110,13 @@ std::string join(const Container &items, const std::string &sep, UnaryOp op)
>   	std::ostringstream ss;
>   	bool first = true;
>   
> -	for (auto it = std::begin(items); it != std::end(items); ++it) {
> +	for (const auto &item : items) {
>   		if (!first)
>   			ss << sep;
>   		else
>   			first = false;
>   
> -		ss << op(*it);
> +		ss << op(item);
>   	}
>   
>   	return ss.str();
> @@ -128,13 +128,13 @@ std::string join(const Container &items, const std::string &sep)
>   	std::ostringstream ss;
>   	bool first = true;
>   
> -	for (auto it = std::begin(items); it != std::end(items); ++it) {
> +	for (const auto &item : items) {
>   		if (!first)
>   			ss << sep;
>   		else
>   			first = false;
>   
> -		ss << *it;
> +		ss << item;
>   	}
>   
>   	return ss.str();
> diff --git a/src/gstreamer/gstlibcamera-utils.cpp b/src/gstreamer/gstlibcamera-utils.cpp
> index bfb094c94641..6541d478c83d 100644
> --- a/src/gstreamer/gstlibcamera-utils.cpp
> +++ b/src/gstreamer/gstlibcamera-utils.cpp
> @@ -749,10 +749,9 @@ int gst_libcamera_set_structure_field(GstStructure *structure, const ControlId *
>   	switch (value.type()) {
>   	case ControlTypeBool:
>   		if (is_array) {
> -			Span<const bool> data = value.get<Span<const bool>>();
> -			for (auto it = data.begin(); it != data.end(); ++it) {
> +			for (const auto &item : value.get<Span<const bool>>()) {
>   				g_value_init(&x, type);
> -				g_value_set_boolean(&x, *it);
> +				g_value_set_boolean(&x, item);
>   				gst_value_array_append_and_take_value(&v, &x);
>   			}
>   		} else {
> @@ -762,10 +761,9 @@ int gst_libcamera_set_structure_field(GstStructure *structure, const ControlId *
>   		break;
>   	case ControlTypeByte:
>   		if (is_array) {
> -			Span<const uint8_t> data = value.get<Span<const uint8_t>>();
> -			for (auto it = data.begin(); it != data.end(); ++it) {
> +			for (const auto &item : value.get<Span<const uint8_t>>()) {
>   				g_value_init(&x, type);
> -				g_value_set_uint(&x, *it);
> +				g_value_set_uint(&x, item);
>   				gst_value_array_append_and_take_value(&v, &x);
>   			}
>   		} else {
> @@ -775,10 +773,9 @@ int gst_libcamera_set_structure_field(GstStructure *structure, const ControlId *
>   		break;
>   	case ControlTypeUnsigned16:
>   		if (is_array) {
> -			Span<const uint16_t> data = value.get<Span<const uint16_t>>();
> -			for (auto it = data.begin(); it != data.end(); ++it) {
> +			for (const auto &item : value.get<Span<const uint16_t>>()) {
>   				g_value_init(&x, type);
> -				g_value_set_uint(&x, *it);
> +				g_value_set_uint(&x, item);
>   				gst_value_array_append_and_take_value(&v, &x);
>   			}
>   		} else {
> @@ -788,10 +785,9 @@ int gst_libcamera_set_structure_field(GstStructure *structure, const ControlId *
>   		break;
>   	case ControlTypeUnsigned32:
>   		if (is_array) {
> -			Span<const uint32_t> data = value.get<Span<const uint32_t>>();
> -			for (auto it = data.begin(); it != data.end(); ++it) {
> +			for (const auto &item : value.get<Span<const uint32_t>>()) {
>   				g_value_init(&x, type);
> -				g_value_set_uint(&x, *it);
> +				g_value_set_uint(&x, item);
>   				gst_value_array_append_and_take_value(&v, &x);
>   			}
>   		} else {
> @@ -801,10 +797,9 @@ int gst_libcamera_set_structure_field(GstStructure *structure, const ControlId *
>   		break;
>   	case ControlTypeInteger32:
>   		if (is_array) {
> -			Span<const int32_t> data = value.get<Span<const int32_t>>();
> -			for (auto it = data.begin(); it != data.end(); ++it) {
> +			for (const auto &item : value.get<Span<const int32_t>>()) {
>   				g_value_init(&x, type);
> -				g_value_set_int(&x, *it);
> +				g_value_set_int(&x, item);
>   				gst_value_array_append_and_take_value(&v, &x);
>   			}
>   		} else {
> @@ -827,10 +822,9 @@ int gst_libcamera_set_structure_field(GstStructure *structure, const ControlId *
>   		break;
>   	case ControlTypeInteger64:
>   		if (is_array) {
> -			Span<const int64_t> data = value.get<Span<const int64_t>>();
> -			for (auto it = data.begin(); it != data.end(); ++it) {
> +			for (const auto &item : value.get<Span<const int64_t>>()) {
>   				g_value_init(&x, type);
> -				g_value_set_int64(&x, *it);
> +				g_value_set_int64(&x, item);
>   				gst_value_array_append_and_take_value(&v, &x);
>   			}
>   		} else {
> @@ -840,10 +834,9 @@ int gst_libcamera_set_structure_field(GstStructure *structure, const ControlId *
>   		break;
>   	case ControlTypeFloat:
>   		if (is_array) {
> -			Span<const float> data = value.get<Span<const float>>();
> -			for (auto it = data.begin(); it != data.end(); ++it) {
> +			for (const auto &item : value.get<Span<const float>>()) {
>   				g_value_init(&x, type);
> -				g_value_set_float(&x, *it);
> +				g_value_set_float(&x, item);
>   				gst_value_array_append_and_take_value(&v, &x);
>   			}
>   		} else {
> @@ -862,27 +855,24 @@ int gst_libcamera_set_structure_field(GstStructure *structure, const ControlId *
>   		break;
>   	case ControlTypeSize:
>   		if (is_array) {
> -			Span<const Size> data = value.get<Span<const Size>>();
> -			for (auto it = data.begin(); it != data.end(); ++it)
> -				gst_libcamera_gvalue_set_size(&v, *it);
> +			for (const auto &item : value.get<Span<const Size>>())
> +				gst_libcamera_gvalue_set_size(&v, item);
>   		} else {
>   			gst_libcamera_gvalue_set_size(&v, value.get<const Size>());
>   		}
>   		break;
>   	case ControlTypePoint:
>   		if (is_array) {
> -			Span<const Point> data = value.get<Span<const Point>>();
> -			for (auto it = data.begin(); it != data.end(); ++it)
> -				gst_libcamera_gvalue_set_point(&v, *it);
> +			for (const auto &item : value.get<Span<const Point>>())
> +				gst_libcamera_gvalue_set_point(&v, item);
>   		} else {
>   			gst_libcamera_gvalue_set_point(&v, value.get<const Point>());
>   		}
>   		break;
>   	case ControlTypeRectangle:
>   		if (is_array) {
> -			Span<const Rectangle> data = value.get<Span<const Rectangle>>();
> -			for (auto it = data.begin(); it != data.end(); ++it)
> -				gst_libcamera_gvalue_set_rectangle(&v, *it);
> +			for (const auto &item : value.get<Span<const Rectangle>>())
> +				gst_libcamera_gvalue_set_rectangle(&v, item);
>   		} else {
>   			gst_libcamera_gvalue_set_rectangle(&v, value.get<const Rectangle>());
>   		}
> diff --git a/src/ipa/rpi/controller/rpi/agc.cpp b/src/ipa/rpi/controller/rpi/agc.cpp
> index afda2e364f64..a8d91f43df29 100644
> --- a/src/ipa/rpi/controller/rpi/agc.cpp
> +++ b/src/ipa/rpi/controller/rpi/agc.cpp
> @@ -45,10 +45,10 @@ int Agc::read(const libcamera::YamlObject &params)
>   	}
>   
>   	const auto &channels = params["channels"].asList();
> -	for (auto ch = channels.begin(); ch != channels.end(); ch++) {
> +	for (const auto &ch : channels) {
>   		LOG(RPiAgc, Debug) << "Read AGC channel";
>   		channelData_.emplace_back();
> -		int ret = channelData_.back().channel.read(*ch, getHardwareConfig());
> +		int ret = channelData_.back().channel.read(ch, getHardwareConfig());
>   		if (ret)
>   			return ret;
>   	}
Isaac Scott March 2, 2026, 5:41 p.m. UTC | #2
Hi Laurent,

Thank you for the patch!

Quoting Laurent Pinchart (2026-02-12 15:12:36)
> Use range-based for loops instead of iterators when iterating over a
> container. This improves readability.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
>  include/libcamera/base/utils.h       |  8 ++---
>  src/gstreamer/gstlibcamera-utils.cpp | 50 +++++++++++-----------------
>  src/ipa/rpi/controller/rpi/agc.cpp   |  4 +--
>  3 files changed, 26 insertions(+), 36 deletions(-)
> 
> diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h
> index 4373bc20d42e..37f0d065f82f 100644
> --- a/include/libcamera/base/utils.h
> +++ b/include/libcamera/base/utils.h

<snip>

> @@ -862,27 +855,24 @@ int gst_libcamera_set_structure_field(GstStructure *structure, const ControlId *
>                 break;
>         case ControlTypeSize:
>                 if (is_array) {
> -                       Span<const Size> data = value.get<Span<const Size>>();
> -                       for (auto it = data.begin(); it != data.end(); ++it)
> -                               gst_libcamera_gvalue_set_size(&v, *it);
> +                       for (const auto &item : value.get<Span<const Size>>())
> +                               gst_libcamera_gvalue_set_size(&v, item);
>                 } else {
>                         gst_libcamera_gvalue_set_size(&v, value.get<const Size>());
>                 }
>                 break;
>         case ControlTypePoint:
>                 if (is_array) {
> -                       Span<const Point> data = value.get<Span<const Point>>();
> -                       for (auto it = data.begin(); it != data.end(); ++it)
> -                               gst_libcamera_gvalue_set_point(&v, *it);
> +                       for (const auto &item : value.get<Span<const Point>>())
> +                               gst_libcamera_gvalue_set_point(&v, item);
>                 } else {
>                         gst_libcamera_gvalue_set_point(&v, value.get<const Point>());
>                 }
>                 break;
>         case ControlTypeRectangle:
>                 if (is_array) {
> -                       Span<const Rectangle> data = value.get<Span<const Rectangle>>();
> -                       for (auto it = data.begin(); it != data.end(); ++it)
> -                               gst_libcamera_gvalue_set_rectangle(&v, *it);
> +                       for (const auto &item : value.get<Span<const Rectangle>>())
> +                               gst_libcamera_gvalue_set_rectangle(&v, item);
>                 } else {
>                         gst_libcamera_gvalue_set_rectangle(&v, value.get<const Rectangle>());
>                 }
> diff --git a/src/ipa/rpi/controller/rpi/agc.cpp b/src/ipa/rpi/controller/rpi/agc.cpp
> index afda2e364f64..a8d91f43df29 100644
> --- a/src/ipa/rpi/controller/rpi/agc.cpp
> +++ b/src/ipa/rpi/controller/rpi/agc.cpp
> @@ -45,10 +45,10 @@ int Agc::read(const libcamera::YamlObject &params)
>         }
>  
>         const auto &channels = params["channels"].asList();
> -       for (auto ch = channels.begin(); ch != channels.end(); ch++) {
> +       for (const auto &ch : channels) {
>                 LOG(RPiAgc, Debug) << "Read AGC channel";
>                 channelData_.emplace_back();
> -               int ret = channelData_.back().channel.read(*ch, getHardwareConfig());
> +               int ret = channelData_.back().channel.read(ch, getHardwareConfig());

Reviewed-by: Isaac Scott <isaac.scott@ideasonboard.com>

Best wishes,
Isaac

>                 if (ret)
>                         return ret;
>         }
> -- 
> Regards,
> 
> Laurent Pinchart
>

Patch
diff mbox series

diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h
index 4373bc20d42e..37f0d065f82f 100644
--- a/include/libcamera/base/utils.h
+++ b/include/libcamera/base/utils.h
@@ -110,13 +110,13 @@  std::string join(const Container &items, const std::string &sep, UnaryOp op)
 	std::ostringstream ss;
 	bool first = true;
 
-	for (auto it = std::begin(items); it != std::end(items); ++it) {
+	for (const auto &item : items) {
 		if (!first)
 			ss << sep;
 		else
 			first = false;
 
-		ss << op(*it);
+		ss << op(item);
 	}
 
 	return ss.str();
@@ -128,13 +128,13 @@  std::string join(const Container &items, const std::string &sep)
 	std::ostringstream ss;
 	bool first = true;
 
-	for (auto it = std::begin(items); it != std::end(items); ++it) {
+	for (const auto &item : items) {
 		if (!first)
 			ss << sep;
 		else
 			first = false;
 
-		ss << *it;
+		ss << item;
 	}
 
 	return ss.str();
diff --git a/src/gstreamer/gstlibcamera-utils.cpp b/src/gstreamer/gstlibcamera-utils.cpp
index bfb094c94641..6541d478c83d 100644
--- a/src/gstreamer/gstlibcamera-utils.cpp
+++ b/src/gstreamer/gstlibcamera-utils.cpp
@@ -749,10 +749,9 @@  int gst_libcamera_set_structure_field(GstStructure *structure, const ControlId *
 	switch (value.type()) {
 	case ControlTypeBool:
 		if (is_array) {
-			Span<const bool> data = value.get<Span<const bool>>();
-			for (auto it = data.begin(); it != data.end(); ++it) {
+			for (const auto &item : value.get<Span<const bool>>()) {
 				g_value_init(&x, type);
-				g_value_set_boolean(&x, *it);
+				g_value_set_boolean(&x, item);
 				gst_value_array_append_and_take_value(&v, &x);
 			}
 		} else {
@@ -762,10 +761,9 @@  int gst_libcamera_set_structure_field(GstStructure *structure, const ControlId *
 		break;
 	case ControlTypeByte:
 		if (is_array) {
-			Span<const uint8_t> data = value.get<Span<const uint8_t>>();
-			for (auto it = data.begin(); it != data.end(); ++it) {
+			for (const auto &item : value.get<Span<const uint8_t>>()) {
 				g_value_init(&x, type);
-				g_value_set_uint(&x, *it);
+				g_value_set_uint(&x, item);
 				gst_value_array_append_and_take_value(&v, &x);
 			}
 		} else {
@@ -775,10 +773,9 @@  int gst_libcamera_set_structure_field(GstStructure *structure, const ControlId *
 		break;
 	case ControlTypeUnsigned16:
 		if (is_array) {
-			Span<const uint16_t> data = value.get<Span<const uint16_t>>();
-			for (auto it = data.begin(); it != data.end(); ++it) {
+			for (const auto &item : value.get<Span<const uint16_t>>()) {
 				g_value_init(&x, type);
-				g_value_set_uint(&x, *it);
+				g_value_set_uint(&x, item);
 				gst_value_array_append_and_take_value(&v, &x);
 			}
 		} else {
@@ -788,10 +785,9 @@  int gst_libcamera_set_structure_field(GstStructure *structure, const ControlId *
 		break;
 	case ControlTypeUnsigned32:
 		if (is_array) {
-			Span<const uint32_t> data = value.get<Span<const uint32_t>>();
-			for (auto it = data.begin(); it != data.end(); ++it) {
+			for (const auto &item : value.get<Span<const uint32_t>>()) {
 				g_value_init(&x, type);
-				g_value_set_uint(&x, *it);
+				g_value_set_uint(&x, item);
 				gst_value_array_append_and_take_value(&v, &x);
 			}
 		} else {
@@ -801,10 +797,9 @@  int gst_libcamera_set_structure_field(GstStructure *structure, const ControlId *
 		break;
 	case ControlTypeInteger32:
 		if (is_array) {
-			Span<const int32_t> data = value.get<Span<const int32_t>>();
-			for (auto it = data.begin(); it != data.end(); ++it) {
+			for (const auto &item : value.get<Span<const int32_t>>()) {
 				g_value_init(&x, type);
-				g_value_set_int(&x, *it);
+				g_value_set_int(&x, item);
 				gst_value_array_append_and_take_value(&v, &x);
 			}
 		} else {
@@ -827,10 +822,9 @@  int gst_libcamera_set_structure_field(GstStructure *structure, const ControlId *
 		break;
 	case ControlTypeInteger64:
 		if (is_array) {
-			Span<const int64_t> data = value.get<Span<const int64_t>>();
-			for (auto it = data.begin(); it != data.end(); ++it) {
+			for (const auto &item : value.get<Span<const int64_t>>()) {
 				g_value_init(&x, type);
-				g_value_set_int64(&x, *it);
+				g_value_set_int64(&x, item);
 				gst_value_array_append_and_take_value(&v, &x);
 			}
 		} else {
@@ -840,10 +834,9 @@  int gst_libcamera_set_structure_field(GstStructure *structure, const ControlId *
 		break;
 	case ControlTypeFloat:
 		if (is_array) {
-			Span<const float> data = value.get<Span<const float>>();
-			for (auto it = data.begin(); it != data.end(); ++it) {
+			for (const auto &item : value.get<Span<const float>>()) {
 				g_value_init(&x, type);
-				g_value_set_float(&x, *it);
+				g_value_set_float(&x, item);
 				gst_value_array_append_and_take_value(&v, &x);
 			}
 		} else {
@@ -862,27 +855,24 @@  int gst_libcamera_set_structure_field(GstStructure *structure, const ControlId *
 		break;
 	case ControlTypeSize:
 		if (is_array) {
-			Span<const Size> data = value.get<Span<const Size>>();
-			for (auto it = data.begin(); it != data.end(); ++it)
-				gst_libcamera_gvalue_set_size(&v, *it);
+			for (const auto &item : value.get<Span<const Size>>())
+				gst_libcamera_gvalue_set_size(&v, item);
 		} else {
 			gst_libcamera_gvalue_set_size(&v, value.get<const Size>());
 		}
 		break;
 	case ControlTypePoint:
 		if (is_array) {
-			Span<const Point> data = value.get<Span<const Point>>();
-			for (auto it = data.begin(); it != data.end(); ++it)
-				gst_libcamera_gvalue_set_point(&v, *it);
+			for (const auto &item : value.get<Span<const Point>>())
+				gst_libcamera_gvalue_set_point(&v, item);
 		} else {
 			gst_libcamera_gvalue_set_point(&v, value.get<const Point>());
 		}
 		break;
 	case ControlTypeRectangle:
 		if (is_array) {
-			Span<const Rectangle> data = value.get<Span<const Rectangle>>();
-			for (auto it = data.begin(); it != data.end(); ++it)
-				gst_libcamera_gvalue_set_rectangle(&v, *it);
+			for (const auto &item : value.get<Span<const Rectangle>>())
+				gst_libcamera_gvalue_set_rectangle(&v, item);
 		} else {
 			gst_libcamera_gvalue_set_rectangle(&v, value.get<const Rectangle>());
 		}
diff --git a/src/ipa/rpi/controller/rpi/agc.cpp b/src/ipa/rpi/controller/rpi/agc.cpp
index afda2e364f64..a8d91f43df29 100644
--- a/src/ipa/rpi/controller/rpi/agc.cpp
+++ b/src/ipa/rpi/controller/rpi/agc.cpp
@@ -45,10 +45,10 @@  int Agc::read(const libcamera::YamlObject &params)
 	}
 
 	const auto &channels = params["channels"].asList();
-	for (auto ch = channels.begin(); ch != channels.end(); ch++) {
+	for (const auto &ch : channels) {
 		LOG(RPiAgc, Debug) << "Read AGC channel";
 		channelData_.emplace_back();
-		int ret = channelData_.back().channel.read(*ch, getHardwareConfig());
+		int ret = channelData_.back().channel.read(ch, getHardwareConfig());
 		if (ret)
 			return ret;
 	}