[libcamera-devel,06/19] libcamera: bound_method: Use std::index_sequence

Message ID 20200120002437.6633-7-laurent.pinchart@ideasonboard.com
State Superseded
Headers show
Series
  • Initial libcamera threading model
Related show

Commit Message

Laurent Pinchart Jan. 20, 2020, 12:24 a.m. UTC
Now that we're using C++-14, replace the manual implementation of
std::integer_sequence with std::index_sequence, a specialization of
std::integer_sequence with the integer type equal to std::size_t.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 include/libcamera/bound_method.h | 38 +++++++++-----------------------
 1 file changed, 10 insertions(+), 28 deletions(-)

Comments

Jacopo Mondi Jan. 20, 2020, 11:48 a.m. UTC | #1
Hi Laurent

On Mon, Jan 20, 2020 at 02:24:24AM +0200, Laurent Pinchart wrote:
> Now that we're using C++-14, replace the manual implementation of
> std::integer_sequence with std::index_sequence, a specialization of
> std::integer_sequence with the integer type equal to std::size_t.
>
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Why the template parameter name change ? :D
Anyway, that's good!
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>

Thanks
   j

> ---
>  include/libcamera/bound_method.h | 38 +++++++++-----------------------
>  1 file changed, 10 insertions(+), 28 deletions(-)
>
> diff --git a/include/libcamera/bound_method.h b/include/libcamera/bound_method.h
> index ca501493bce2..7ffd2e426e18 100644
> --- a/include/libcamera/bound_method.h
> +++ b/include/libcamera/bound_method.h
> @@ -10,6 +10,7 @@
>  #include <memory>
>  #include <tuple>
>  #include <type_traits>
> +#include <utility>
>
>  namespace libcamera {
>
> @@ -71,25 +72,6 @@ public:
>  	virtual void invokePack(BoundMethodPackBase *pack) = 0;
>
>  protected:
> -#ifndef __DOXYGEN__
> -	/*
> -	 * This is a cheap partial implementation of std::integer_sequence<>
> -	 * from C++14.
> -	 */
> -	template<int...>
> -	struct sequence {
> -	};
> -
> -	template<int N, int... S>
> -	struct generator : generator<N-1, N-1, S...> {
> -	};
> -
> -	template<int... S>
> -	struct generator<0, S...> {
> -		typedef sequence<S...> type;
> -	};
> -#endif
> -
>  	bool activatePack(std::shared_ptr<BoundMethodPackBase> pack,
>  			  bool deleteMethod);
>
> @@ -107,11 +89,11 @@ public:
>  	using PackType = BoundMethodPack<R, Args...>;
>
>  private:
> -	template<int... S>
> -	void invokePack(BoundMethodPackBase *pack, BoundMethodBase::sequence<S...>)
> +	template<std::size_t... I>
> +	void invokePack(BoundMethodPackBase *pack, std::index_sequence<I...>)
>  	{
>  		PackType *args = static_cast<PackType *>(pack);
> -		args->ret_ = invoke(std::get<S>(args->args_)...);
> +		args->ret_ = invoke(std::get<I>(args->args_)...);
>  	}
>
>  public:
> @@ -120,7 +102,7 @@ public:
>
>  	void invokePack(BoundMethodPackBase *pack) override
>  	{
> -		invokePack(pack, typename BoundMethodBase::generator<sizeof...(Args)>::type());
> +		invokePack(pack, std::make_index_sequence<sizeof...(Args)>{});
>  	}
>
>  	virtual R activate(Args... args, bool deleteMethod = false) = 0;
> @@ -134,12 +116,12 @@ public:
>  	using PackType = BoundMethodPack<void, Args...>;
>
>  private:
> -	template<int... S>
> -	void invokePack(BoundMethodPackBase *pack, BoundMethodBase::sequence<S...>)
> +	template<std::size_t... I>
> +	void invokePack(BoundMethodPackBase *pack, std::index_sequence<I...>)
>  	{
> -		/* args is effectively unused when the sequence S is empty. */
> +		/* args is effectively unused when the sequence I is empty. */
>  		PackType *args [[gnu::unused]] = static_cast<PackType *>(pack);
> -		invoke(std::get<S>(args->args_)...);
> +		invoke(std::get<I>(args->args_)...);
>  	}
>
>  public:
> @@ -148,7 +130,7 @@ public:
>
>  	void invokePack(BoundMethodPackBase *pack) override
>  	{
> -		invokePack(pack, typename BoundMethodBase::generator<sizeof...(Args)>::type());
> +		invokePack(pack, std::make_index_sequence<sizeof...(Args)>{});
>  	}
>
>  	virtual void activate(Args... args, bool deleteMethod = false) = 0;
> --
> Regards,
>
> Laurent Pinchart
>
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel
Laurent Pinchart Jan. 20, 2020, 11:50 a.m. UTC | #2
On Mon, Jan 20, 2020 at 12:48:29PM +0100, Jacopo Mondi wrote:
> Hi Laurent
> 
> On Mon, Jan 20, 2020 at 02:24:24AM +0200, Laurent Pinchart wrote:
> > Now that we're using C++-14, replace the manual implementation of
> > std::integer_sequence with std::index_sequence, a specialization of
> > std::integer_sequence with the integer type equal to std::size_t.
> >
> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> 
> Why the template parameter name change ? :D

To align to the usage in the cppreference.com examples.

> Anyway, that's good!
> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>

Thank you.

> > ---
> >  include/libcamera/bound_method.h | 38 +++++++++-----------------------
> >  1 file changed, 10 insertions(+), 28 deletions(-)
> >
> > diff --git a/include/libcamera/bound_method.h b/include/libcamera/bound_method.h
> > index ca501493bce2..7ffd2e426e18 100644
> > --- a/include/libcamera/bound_method.h
> > +++ b/include/libcamera/bound_method.h
> > @@ -10,6 +10,7 @@
> >  #include <memory>
> >  #include <tuple>
> >  #include <type_traits>
> > +#include <utility>
> >
> >  namespace libcamera {
> >
> > @@ -71,25 +72,6 @@ public:
> >  	virtual void invokePack(BoundMethodPackBase *pack) = 0;
> >
> >  protected:
> > -#ifndef __DOXYGEN__
> > -	/*
> > -	 * This is a cheap partial implementation of std::integer_sequence<>
> > -	 * from C++14.
> > -	 */
> > -	template<int...>
> > -	struct sequence {
> > -	};
> > -
> > -	template<int N, int... S>
> > -	struct generator : generator<N-1, N-1, S...> {
> > -	};
> > -
> > -	template<int... S>
> > -	struct generator<0, S...> {
> > -		typedef sequence<S...> type;
> > -	};
> > -#endif
> > -
> >  	bool activatePack(std::shared_ptr<BoundMethodPackBase> pack,
> >  			  bool deleteMethod);
> >
> > @@ -107,11 +89,11 @@ public:
> >  	using PackType = BoundMethodPack<R, Args...>;
> >
> >  private:
> > -	template<int... S>
> > -	void invokePack(BoundMethodPackBase *pack, BoundMethodBase::sequence<S...>)
> > +	template<std::size_t... I>
> > +	void invokePack(BoundMethodPackBase *pack, std::index_sequence<I...>)
> >  	{
> >  		PackType *args = static_cast<PackType *>(pack);
> > -		args->ret_ = invoke(std::get<S>(args->args_)...);
> > +		args->ret_ = invoke(std::get<I>(args->args_)...);
> >  	}
> >
> >  public:
> > @@ -120,7 +102,7 @@ public:
> >
> >  	void invokePack(BoundMethodPackBase *pack) override
> >  	{
> > -		invokePack(pack, typename BoundMethodBase::generator<sizeof...(Args)>::type());
> > +		invokePack(pack, std::make_index_sequence<sizeof...(Args)>{});
> >  	}
> >
> >  	virtual R activate(Args... args, bool deleteMethod = false) = 0;
> > @@ -134,12 +116,12 @@ public:
> >  	using PackType = BoundMethodPack<void, Args...>;
> >
> >  private:
> > -	template<int... S>
> > -	void invokePack(BoundMethodPackBase *pack, BoundMethodBase::sequence<S...>)
> > +	template<std::size_t... I>
> > +	void invokePack(BoundMethodPackBase *pack, std::index_sequence<I...>)
> >  	{
> > -		/* args is effectively unused when the sequence S is empty. */
> > +		/* args is effectively unused when the sequence I is empty. */
> >  		PackType *args [[gnu::unused]] = static_cast<PackType *>(pack);
> > -		invoke(std::get<S>(args->args_)...);
> > +		invoke(std::get<I>(args->args_)...);
> >  	}
> >
> >  public:
> > @@ -148,7 +130,7 @@ public:
> >
> >  	void invokePack(BoundMethodPackBase *pack) override
> >  	{
> > -		invokePack(pack, typename BoundMethodBase::generator<sizeof...(Args)>::type());
> > +		invokePack(pack, std::make_index_sequence<sizeof...(Args)>{});
> >  	}
> >
> >  	virtual void activate(Args... args, bool deleteMethod = false) = 0;
Niklas Söderlund Jan. 22, 2020, 3:18 p.m. UTC | #3
Hi Laurent,

Thanks for your work.

On 2020-01-20 02:24:24 +0200, Laurent Pinchart wrote:
> Now that we're using C++-14, replace the manual implementation of
> std::integer_sequence with std::index_sequence, a specialization of
> std::integer_sequence with the integer type equal to std::size_t.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

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

> ---
>  include/libcamera/bound_method.h | 38 +++++++++-----------------------
>  1 file changed, 10 insertions(+), 28 deletions(-)
> 
> diff --git a/include/libcamera/bound_method.h b/include/libcamera/bound_method.h
> index ca501493bce2..7ffd2e426e18 100644
> --- a/include/libcamera/bound_method.h
> +++ b/include/libcamera/bound_method.h
> @@ -10,6 +10,7 @@
>  #include <memory>
>  #include <tuple>
>  #include <type_traits>
> +#include <utility>
>  
>  namespace libcamera {
>  
> @@ -71,25 +72,6 @@ public:
>  	virtual void invokePack(BoundMethodPackBase *pack) = 0;
>  
>  protected:
> -#ifndef __DOXYGEN__
> -	/*
> -	 * This is a cheap partial implementation of std::integer_sequence<>
> -	 * from C++14.
> -	 */
> -	template<int...>
> -	struct sequence {
> -	};
> -
> -	template<int N, int... S>
> -	struct generator : generator<N-1, N-1, S...> {
> -	};
> -
> -	template<int... S>
> -	struct generator<0, S...> {
> -		typedef sequence<S...> type;
> -	};
> -#endif
> -
>  	bool activatePack(std::shared_ptr<BoundMethodPackBase> pack,
>  			  bool deleteMethod);
>  
> @@ -107,11 +89,11 @@ public:
>  	using PackType = BoundMethodPack<R, Args...>;
>  
>  private:
> -	template<int... S>
> -	void invokePack(BoundMethodPackBase *pack, BoundMethodBase::sequence<S...>)
> +	template<std::size_t... I>
> +	void invokePack(BoundMethodPackBase *pack, std::index_sequence<I...>)
>  	{
>  		PackType *args = static_cast<PackType *>(pack);
> -		args->ret_ = invoke(std::get<S>(args->args_)...);
> +		args->ret_ = invoke(std::get<I>(args->args_)...);
>  	}
>  
>  public:
> @@ -120,7 +102,7 @@ public:
>  
>  	void invokePack(BoundMethodPackBase *pack) override
>  	{
> -		invokePack(pack, typename BoundMethodBase::generator<sizeof...(Args)>::type());
> +		invokePack(pack, std::make_index_sequence<sizeof...(Args)>{});
>  	}
>  
>  	virtual R activate(Args... args, bool deleteMethod = false) = 0;
> @@ -134,12 +116,12 @@ public:
>  	using PackType = BoundMethodPack<void, Args...>;
>  
>  private:
> -	template<int... S>
> -	void invokePack(BoundMethodPackBase *pack, BoundMethodBase::sequence<S...>)
> +	template<std::size_t... I>
> +	void invokePack(BoundMethodPackBase *pack, std::index_sequence<I...>)
>  	{
> -		/* args is effectively unused when the sequence S is empty. */
> +		/* args is effectively unused when the sequence I is empty. */
>  		PackType *args [[gnu::unused]] = static_cast<PackType *>(pack);
> -		invoke(std::get<S>(args->args_)...);
> +		invoke(std::get<I>(args->args_)...);
>  	}
>  
>  public:
> @@ -148,7 +130,7 @@ public:
>  
>  	void invokePack(BoundMethodPackBase *pack) override
>  	{
> -		invokePack(pack, typename BoundMethodBase::generator<sizeof...(Args)>::type());
> +		invokePack(pack, std::make_index_sequence<sizeof...(Args)>{});
>  	}
>  
>  	virtual void activate(Args... args, bool deleteMethod = false) = 0;
> -- 
> 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/bound_method.h b/include/libcamera/bound_method.h
index ca501493bce2..7ffd2e426e18 100644
--- a/include/libcamera/bound_method.h
+++ b/include/libcamera/bound_method.h
@@ -10,6 +10,7 @@ 
 #include <memory>
 #include <tuple>
 #include <type_traits>
+#include <utility>
 
 namespace libcamera {
 
@@ -71,25 +72,6 @@  public:
 	virtual void invokePack(BoundMethodPackBase *pack) = 0;
 
 protected:
-#ifndef __DOXYGEN__
-	/*
-	 * This is a cheap partial implementation of std::integer_sequence<>
-	 * from C++14.
-	 */
-	template<int...>
-	struct sequence {
-	};
-
-	template<int N, int... S>
-	struct generator : generator<N-1, N-1, S...> {
-	};
-
-	template<int... S>
-	struct generator<0, S...> {
-		typedef sequence<S...> type;
-	};
-#endif
-
 	bool activatePack(std::shared_ptr<BoundMethodPackBase> pack,
 			  bool deleteMethod);
 
@@ -107,11 +89,11 @@  public:
 	using PackType = BoundMethodPack<R, Args...>;
 
 private:
-	template<int... S>
-	void invokePack(BoundMethodPackBase *pack, BoundMethodBase::sequence<S...>)
+	template<std::size_t... I>
+	void invokePack(BoundMethodPackBase *pack, std::index_sequence<I...>)
 	{
 		PackType *args = static_cast<PackType *>(pack);
-		args->ret_ = invoke(std::get<S>(args->args_)...);
+		args->ret_ = invoke(std::get<I>(args->args_)...);
 	}
 
 public:
@@ -120,7 +102,7 @@  public:
 
 	void invokePack(BoundMethodPackBase *pack) override
 	{
-		invokePack(pack, typename BoundMethodBase::generator<sizeof...(Args)>::type());
+		invokePack(pack, std::make_index_sequence<sizeof...(Args)>{});
 	}
 
 	virtual R activate(Args... args, bool deleteMethod = false) = 0;
@@ -134,12 +116,12 @@  public:
 	using PackType = BoundMethodPack<void, Args...>;
 
 private:
-	template<int... S>
-	void invokePack(BoundMethodPackBase *pack, BoundMethodBase::sequence<S...>)
+	template<std::size_t... I>
+	void invokePack(BoundMethodPackBase *pack, std::index_sequence<I...>)
 	{
-		/* args is effectively unused when the sequence S is empty. */
+		/* args is effectively unused when the sequence I is empty. */
 		PackType *args [[gnu::unused]] = static_cast<PackType *>(pack);
-		invoke(std::get<S>(args->args_)...);
+		invoke(std::get<I>(args->args_)...);
 	}
 
 public:
@@ -148,7 +130,7 @@  public:
 
 	void invokePack(BoundMethodPackBase *pack) override
 	{
-		invokePack(pack, typename BoundMethodBase::generator<sizeof...(Args)>::type());
+		invokePack(pack, std::make_index_sequence<sizeof...(Args)>{});
 	}
 
 	virtual void activate(Args... args, bool deleteMethod = false) = 0;