[libcamera-devel] libcamera: request: Add operator<<()
diff mbox series

Message ID 20220603093224.28579-1-jacopo@jmondi.org
State Accepted
Headers show
Series
  • [libcamera-devel] libcamera: request: Add operator<<()
Related show

Commit Message

Jacopo Mondi June 3, 2022, 9:32 a.m. UTC
With the recent addition of operator<<() to most libcamera core classes
to replace usage of the toString() function, the Request class was left
behind.

Add operator<<() for the Request class.

Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
---
 include/libcamera/request.h |  3 +++
 src/libcamera/request.cpp   | 12 ++++++++++++
 2 files changed, 15 insertions(+)

Comments

Laurent Pinchart June 3, 2022, 12:46 p.m. UTC | #1
Hi Jacopo,

Thank you for the patch.

On Fri, Jun 03, 2022 at 11:32:24AM +0200, Jacopo Mondi via libcamera-devel wrote:
> With the recent addition of operator<<() to most libcamera core classes
> to replace usage of the toString() function, the Request class was left
> behind.
> 
> Add operator<<() for the Request class.
> 
> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
> ---
>  include/libcamera/request.h |  3 +++
>  src/libcamera/request.cpp   | 12 ++++++++++++
>  2 files changed, 15 insertions(+)
> 
> diff --git a/include/libcamera/request.h b/include/libcamera/request.h
> index 1eb537e9b09b..dffde1536cad 100644
> --- a/include/libcamera/request.h
> +++ b/include/libcamera/request.h
> @@ -9,6 +9,7 @@
>  
>  #include <map>
>  #include <memory>
> +#include <ostream>
>  #include <stdint.h>
>  #include <string>
>  #include <unordered_set>
> @@ -75,4 +76,6 @@ private:
>  	Status status_;
>  };
>  
> +std::ostream &operator<<(std::ostream &out, const Request &r);
> +
>  } /* namespace libcamera */
> diff --git a/src/libcamera/request.cpp b/src/libcamera/request.cpp
> index 5704972d86af..f630f0818cbd 100644
> --- a/src/libcamera/request.cpp
> +++ b/src/libcamera/request.cpp
> @@ -594,4 +594,16 @@ std::string Request::toString() const
>  	return ss.str();
>  }
>  
> +/**
> + * \brief Insert a text representation of a Request into an output stream
> + * \param[in] out The output stream
> + * \param[in] r The Request
> + * \return The output stream \a out
> + */
> +std::ostream &operator<<(std::ostream &out, const Request &r)
> +{
> +	out << r.toString();
> +	return out;

Request::toString() already uses an std::stringstream, it's thus better
do to this the other way around, move the implementation to operator<<()
and implement toString() as a wrapper. See V4L2SubdeviceFormat for
instance.

> +}
> +
>  } /* namespace libcamera */
Jacopo Mondi June 3, 2022, 4:12 p.m. UTC | #2
Hi Laurent

On Fri, Jun 03, 2022 at 03:46:45PM +0300, Laurent Pinchart wrote:
> Hi Jacopo,
>
> Thank you for the patch.
>
> On Fri, Jun 03, 2022 at 11:32:24AM +0200, Jacopo Mondi via libcamera-devel wrote:
> > With the recent addition of operator<<() to most libcamera core classes
> > to replace usage of the toString() function, the Request class was left
> > behind.
> >
> > Add operator<<() for the Request class.
> >
> > Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
> > ---
> >  include/libcamera/request.h |  3 +++
> >  src/libcamera/request.cpp   | 12 ++++++++++++
> >  2 files changed, 15 insertions(+)
> >
> > diff --git a/include/libcamera/request.h b/include/libcamera/request.h
> > index 1eb537e9b09b..dffde1536cad 100644
> > --- a/include/libcamera/request.h
> > +++ b/include/libcamera/request.h
> > @@ -9,6 +9,7 @@
> >
> >  #include <map>
> >  #include <memory>
> > +#include <ostream>
> >  #include <stdint.h>
> >  #include <string>
> >  #include <unordered_set>
> > @@ -75,4 +76,6 @@ private:
> >  	Status status_;
> >  };
> >
> > +std::ostream &operator<<(std::ostream &out, const Request &r);
> > +
> >  } /* namespace libcamera */
> > diff --git a/src/libcamera/request.cpp b/src/libcamera/request.cpp
> > index 5704972d86af..f630f0818cbd 100644
> > --- a/src/libcamera/request.cpp
> > +++ b/src/libcamera/request.cpp
> > @@ -594,4 +594,16 @@ std::string Request::toString() const
> >  	return ss.str();
> >  }
> >
> > +/**
> > + * \brief Insert a text representation of a Request into an output stream
> > + * \param[in] out The output stream
> > + * \param[in] r The Request
> > + * \return The output stream \a out
> > + */
> > +std::ostream &operator<<(std::ostream &out, const Request &r)
> > +{
> > +	out << r.toString();
> > +	return out;
>
> Request::toString() already uses an std::stringstream, it's thus better
> do to this the other way around, move the implementation to operator<<()
> and implement toString() as a wrapper. See V4L2SubdeviceFormat for
> instance.
>

If not that we need
         r._d()->pending_.size()
which is private.

Either we expose the pendin buffers array (so far we only have
Request::Private::hasPendingBuffers()) or we keep it the way it is.

> > +}
> > +
> >  } /* namespace libcamera */
>
> --
> Regards,
>
> Laurent Pinchart
Laurent Pinchart June 3, 2022, 8:27 p.m. UTC | #3
Hi Jacopo,

On Fri, Jun 03, 2022 at 06:12:11PM +0200, Jacopo Mondi wrote:
> On Fri, Jun 03, 2022 at 03:46:45PM +0300, Laurent Pinchart wrote:
> > On Fri, Jun 03, 2022 at 11:32:24AM +0200, Jacopo Mondi via libcamera-devel wrote:
> > > With the recent addition of operator<<() to most libcamera core classes
> > > to replace usage of the toString() function, the Request class was left
> > > behind.
> > >
> > > Add operator<<() for the Request class.
> > >
> > > Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
> > > ---
> > >  include/libcamera/request.h |  3 +++
> > >  src/libcamera/request.cpp   | 12 ++++++++++++
> > >  2 files changed, 15 insertions(+)
> > >
> > > diff --git a/include/libcamera/request.h b/include/libcamera/request.h
> > > index 1eb537e9b09b..dffde1536cad 100644
> > > --- a/include/libcamera/request.h
> > > +++ b/include/libcamera/request.h
> > > @@ -9,6 +9,7 @@
> > >
> > >  #include <map>
> > >  #include <memory>
> > > +#include <ostream>
> > >  #include <stdint.h>
> > >  #include <string>
> > >  #include <unordered_set>
> > > @@ -75,4 +76,6 @@ private:
> > >  	Status status_;
> > >  };
> > >
> > > +std::ostream &operator<<(std::ostream &out, const Request &r);
> > > +
> > >  } /* namespace libcamera */
> > > diff --git a/src/libcamera/request.cpp b/src/libcamera/request.cpp
> > > index 5704972d86af..f630f0818cbd 100644
> > > --- a/src/libcamera/request.cpp
> > > +++ b/src/libcamera/request.cpp
> > > @@ -594,4 +594,16 @@ std::string Request::toString() const
> > >  	return ss.str();
> > >  }
> > >
> > > +/**
> > > + * \brief Insert a text representation of a Request into an output stream
> > > + * \param[in] out The output stream
> > > + * \param[in] r The Request
> > > + * \return The output stream \a out
> > > + */
> > > +std::ostream &operator<<(std::ostream &out, const Request &r)
> > > +{
> > > +	out << r.toString();
> > > +	return out;
> >
> > Request::toString() already uses an std::stringstream, it's thus better
> > do to this the other way around, move the implementation to operator<<()
> > and implement toString() as a wrapper. See V4L2SubdeviceFormat for
> > instance.
> 
> If not that we need
>          r._d()->pending_.size()
> which is private.
> 
> Either we expose the pendin buffers array (so far we only have
> Request::Private::hasPendingBuffers()) or we keep it the way it is.

You can fix this by making the operator<<() function a friend of the
Request class. That's a very valid use case for a friend statement, as
operator<<() is meant to operate on an instance of the Request class.

> > > +}
> > > +
> > >  } /* namespace libcamera */

Patch
diff mbox series

diff --git a/include/libcamera/request.h b/include/libcamera/request.h
index 1eb537e9b09b..dffde1536cad 100644
--- a/include/libcamera/request.h
+++ b/include/libcamera/request.h
@@ -9,6 +9,7 @@ 
 
 #include <map>
 #include <memory>
+#include <ostream>
 #include <stdint.h>
 #include <string>
 #include <unordered_set>
@@ -75,4 +76,6 @@  private:
 	Status status_;
 };
 
+std::ostream &operator<<(std::ostream &out, const Request &r);
+
 } /* namespace libcamera */
diff --git a/src/libcamera/request.cpp b/src/libcamera/request.cpp
index 5704972d86af..f630f0818cbd 100644
--- a/src/libcamera/request.cpp
+++ b/src/libcamera/request.cpp
@@ -594,4 +594,16 @@  std::string Request::toString() const
 	return ss.str();
 }
 
+/**
+ * \brief Insert a text representation of a Request into an output stream
+ * \param[in] out The output stream
+ * \param[in] r The Request
+ * \return The output stream \a out
+ */
+std::ostream &operator<<(std::ostream &out, const Request &r)
+{
+	out << r.toString();
+	return out;
+}
+
 } /* namespace libcamera */