[libcamera-devel,5/5] libcamera: geometry: Make Size and Rectangle usable as constexpr

Message ID 20200714234009.16596-6-laurent.pinchart@ideasonboard.com
State Accepted
Commit b5f3b9915ca8bc049e1eec3e918c234330991a0e
Headers show
Series
  • libcamera: Various improvements to geometry classes
Related show

Commit Message

Laurent Pinchart July 14, 2020, 11:40 p.m. UTC
There are use cases for declaring constexpr Size and Rectangle
instances. Make it possible.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 include/libcamera/geometry.h | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

Comments

Niklas Söderlund July 15, 2020, 6:35 a.m. UTC | #1
Hi Laurent,

Thanks for your patch.

On 2020-07-15 02:40:09 +0300, Laurent Pinchart wrote:
> There are use cases for declaring constexpr Size and Rectangle
> instances. Make it possible.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>

> ---
>  include/libcamera/geometry.h | 20 +++++++++++---------
>  1 file changed, 11 insertions(+), 9 deletions(-)
> 
> diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h
> index 16c94a4861d6..30aaa7a30fe2 100644
> --- a/include/libcamera/geometry.h
> +++ b/include/libcamera/geometry.h
> @@ -16,12 +16,12 @@ namespace libcamera {
>  class Size
>  {
>  public:
> -	Size()
> +	constexpr Size()
>  		: Size(0, 0)
>  	{
>  	}
>  
> -	Size(unsigned int w, unsigned int h)
> +	constexpr Size(unsigned int w, unsigned int h)
>  		: width(w), height(h)
>  	{
>  	}
> @@ -32,7 +32,8 @@ public:
>  	bool isNull() const { return !width && !height; }
>  	const std::string toString() const;
>  
> -	Size alignedDownTo(unsigned int hAlignment, unsigned int vAlignment) const
> +	constexpr Size alignedDownTo(unsigned int hAlignment,
> +				     unsigned int vAlignment) const
>  	{
>  		return {
>  			width / hAlignment * hAlignment,
> @@ -40,7 +41,8 @@ public:
>  		};
>  	}
>  
> -	Size alignedUpTo(unsigned int hAlignment, unsigned int vAlignment) const
> +	constexpr Size alignedUpTo(unsigned int hAlignment,
> +				   unsigned int vAlignment) const
>  	{
>  		return {
>  			(width + hAlignment - 1) / hAlignment * hAlignment,
> @@ -48,7 +50,7 @@ public:
>  		};
>  	}
>  
> -	Size boundedTo(const Size &bound) const
> +	constexpr Size boundedTo(const Size &bound) const
>  	{
>  		return {
>  			std::min(width, bound.width),
> @@ -56,7 +58,7 @@ public:
>  		};
>  	}
>  
> -	Size expandedTo(const Size &expand) const
> +	constexpr Size expandedTo(const Size &expand) const
>  	{
>  		return {
>  			std::max(width, expand.width),
> @@ -131,17 +133,17 @@ static inline bool operator!=(const SizeRange &lhs, const SizeRange &rhs)
>  class Rectangle
>  {
>  public:
> -	Rectangle()
> +	constexpr Rectangle()
>  		: Rectangle(0, 0, 0, 0)
>  	{
>  	}
>  
> -	Rectangle(int xpos, int ypos, const Size &size)
> +	constexpr Rectangle(int xpos, int ypos, const Size &size)
>  		: x(xpos), y(ypos), width(size.width), height(size.height)
>  	{
>  	}
>  
> -	Rectangle(int xpos, int ypos, unsigned int w, unsigned int h)
> +	constexpr Rectangle(int xpos, int ypos, unsigned int w, unsigned int h)
>  		: x(xpos), y(ypos), width(w), height(h)
>  	{
>  	}
> -- 
> Regards,
> 
> Laurent Pinchart
> 
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel

Patch

diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h
index 16c94a4861d6..30aaa7a30fe2 100644
--- a/include/libcamera/geometry.h
+++ b/include/libcamera/geometry.h
@@ -16,12 +16,12 @@  namespace libcamera {
 class Size
 {
 public:
-	Size()
+	constexpr Size()
 		: Size(0, 0)
 	{
 	}
 
-	Size(unsigned int w, unsigned int h)
+	constexpr Size(unsigned int w, unsigned int h)
 		: width(w), height(h)
 	{
 	}
@@ -32,7 +32,8 @@  public:
 	bool isNull() const { return !width && !height; }
 	const std::string toString() const;
 
-	Size alignedDownTo(unsigned int hAlignment, unsigned int vAlignment) const
+	constexpr Size alignedDownTo(unsigned int hAlignment,
+				     unsigned int vAlignment) const
 	{
 		return {
 			width / hAlignment * hAlignment,
@@ -40,7 +41,8 @@  public:
 		};
 	}
 
-	Size alignedUpTo(unsigned int hAlignment, unsigned int vAlignment) const
+	constexpr Size alignedUpTo(unsigned int hAlignment,
+				   unsigned int vAlignment) const
 	{
 		return {
 			(width + hAlignment - 1) / hAlignment * hAlignment,
@@ -48,7 +50,7 @@  public:
 		};
 	}
 
-	Size boundedTo(const Size &bound) const
+	constexpr Size boundedTo(const Size &bound) const
 	{
 		return {
 			std::min(width, bound.width),
@@ -56,7 +58,7 @@  public:
 		};
 	}
 
-	Size expandedTo(const Size &expand) const
+	constexpr Size expandedTo(const Size &expand) const
 	{
 		return {
 			std::max(width, expand.width),
@@ -131,17 +133,17 @@  static inline bool operator!=(const SizeRange &lhs, const SizeRange &rhs)
 class Rectangle
 {
 public:
-	Rectangle()
+	constexpr Rectangle()
 		: Rectangle(0, 0, 0, 0)
 	{
 	}
 
-	Rectangle(int xpos, int ypos, const Size &size)
+	constexpr Rectangle(int xpos, int ypos, const Size &size)
 		: x(xpos), y(ypos), width(size.width), height(size.height)
 	{
 	}
 
-	Rectangle(int xpos, int ypos, unsigned int w, unsigned int h)
+	constexpr Rectangle(int xpos, int ypos, unsigned int w, unsigned int h)
 		: x(xpos), y(ypos), width(w), height(h)
 	{
 	}