[libcamera-devel,v2,1/3] libcamera: base: class: Expose Extensible private data to other classes
diff mbox series

Message ID 20210711170359.300-2-laurent.pinchart@ideasonboard.com
State Accepted
Headers show
Series
  • libcamera: Make Framebuffer class Extensible
Related show

Commit Message

Laurent Pinchart July 11, 2021, 5:03 p.m. UTC
Despite sharing the same name, the private data class created by the
Extensible design pattern and the C++ private access specifier have
different goals. The latter specifies class members private to the
class, while the former stores data not visible to the application.

There are use cases for accessing the private data class from other
classes inside libcamera. Make this possible by exposing public _d()
functions in the class deriving from Extensible. This won't allow access
to the private data by applications as the definition of the Private
class isn't visible outside of libcamera.

The _d() functions need to be defined as template functions to delay
their evaluation, as the static_cast() operator in the Extensible::_d()
functions needs the Private class to be fully defined.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
Reviewed-by: Umang Jain <umang.jain@ideasonboard.com>
Reviewed-by: Hirokazu Honda <hiroh@chromium.org>
---
 include/libcamera/base/class.h | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

Comments

Laurent Pinchart July 11, 2021, 9:06 p.m. UTC | #1
On Sun, Jul 11, 2021 at 08:03:57PM +0300, Laurent Pinchart wrote:
> Despite sharing the same name, the private data class created by the
> Extensible design pattern and the C++ private access specifier have
> different goals. The latter specifies class members private to the
> class, while the former stores data not visible to the application.
> 
> There are use cases for accessing the private data class from other
> classes inside libcamera. Make this possible by exposing public _d()
> functions in the class deriving from Extensible. This won't allow access
> to the private data by applications as the definition of the Private
> class isn't visible outside of libcamera.
> 
> The _d() functions need to be defined as template functions to delay
> their evaluation, as the static_cast() operator in the Extensible::_d()
> functions needs the Private class to be fully defined.

This should read

The _d() functions need to be defined as template functions to delay
their evaluation, as the static_cast() operator in the Extensible::_d()
functions needs the Private class to be fully defined. The template
argument is defaulted an ignored, as only its presence is required to
delay evaluation.

> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
> Reviewed-by: Umang Jain <umang.jain@ideasonboard.com>
> Reviewed-by: Hirokazu Honda <hiroh@chromium.org>
> ---
>  include/libcamera/base/class.h | 14 ++++++++++++--
>  1 file changed, 12 insertions(+), 2 deletions(-)
> 
> diff --git a/include/libcamera/base/class.h b/include/libcamera/base/class.h
> index a07dac057331..8212c3d4a5ae 100644
> --- a/include/libcamera/base/class.h
> +++ b/include/libcamera/base/class.h
> @@ -33,14 +33,24 @@ namespace libcamera {
>  #define LIBCAMERA_DECLARE_PRIVATE()					\
>  public:									\
>  	class Private;							\
> -	friend class Private;
> +	friend class Private;						\
> +	template <bool B = true>					\
> +	const Private *_d() const					\
> +	{								\
> +		return Extensible::_d<Private>();			\
> +	}								\
> +	template <bool B = true>					\
> +	Private *_d()							\
> +	{								\
> +		return Extensible::_d<Private>();			\
> +	}
>  
>  #define LIBCAMERA_DECLARE_PUBLIC(klass)					\
>  	friend class klass;						\
>  	using Public = klass;
>  
>  #define LIBCAMERA_D_PTR()						\
> -	_d<Private>();
> +	_d();
>  
>  #define LIBCAMERA_O_PTR()						\
>  	_o<Public>();
Kieran Bingham July 12, 2021, 8:28 a.m. UTC | #2
Hi Laurent,

On 11/07/2021 22:06, Laurent Pinchart wrote:
> On Sun, Jul 11, 2021 at 08:03:57PM +0300, Laurent Pinchart wrote:
>> Despite sharing the same name, the private data class created by the
>> Extensible design pattern and the C++ private access specifier have
>> different goals. The latter specifies class members private to the
>> class, while the former stores data not visible to the application.
>>
>> There are use cases for accessing the private data class from other
>> classes inside libcamera. Make this possible by exposing public _d()
>> functions in the class deriving from Extensible. This won't allow access
>> to the private data by applications as the definition of the Private
>> class isn't visible outside of libcamera.
>>
>> The _d() functions need to be defined as template functions to delay
>> their evaluation, as the static_cast() operator in the Extensible::_d()
>> functions needs the Private class to be fully defined.
> 
> This should read
> 
> The _d() functions need to be defined as template functions to delay
> their evaluation, as the static_cast() operator in the Extensible::_d()
> functions needs the Private class to be fully defined. The template
> argument is defaulted an ignored, as only its presence is required to

s/an/and/

> delay evaluation.
> 
>> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
>> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
>> Reviewed-by: Umang Jain <umang.jain@ideasonboard.com>
>> Reviewed-by: Hirokazu Honda <hiroh@chromium.org>

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

>> ---
>>  include/libcamera/base/class.h | 14 ++++++++++++--
>>  1 file changed, 12 insertions(+), 2 deletions(-)
>>
>> diff --git a/include/libcamera/base/class.h b/include/libcamera/base/class.h
>> index a07dac057331..8212c3d4a5ae 100644
>> --- a/include/libcamera/base/class.h
>> +++ b/include/libcamera/base/class.h
>> @@ -33,14 +33,24 @@ namespace libcamera {
>>  #define LIBCAMERA_DECLARE_PRIVATE()					\
>>  public:									\
>>  	class Private;							\
>> -	friend class Private;
>> +	friend class Private;						\
>> +	template <bool B = true>					\
>> +	const Private *_d() const					\
>> +	{								\
>> +		return Extensible::_d<Private>();			\
>> +	}								\
>> +	template <bool B = true>					\>> +	Private *_d()							\
>> +	{								\
>> +		return Extensible::_d<Private>();			\
>> +	}
>>  
>>  #define LIBCAMERA_DECLARE_PUBLIC(klass)					\
>>  	friend class klass;						\
>>  	using Public = klass;
>>  
>>  #define LIBCAMERA_D_PTR()						\
>> -	_d<Private>();
>> +	_d();
>>  
>>  #define LIBCAMERA_O_PTR()						\
>>  	_o<Public>();
>

Patch
diff mbox series

diff --git a/include/libcamera/base/class.h b/include/libcamera/base/class.h
index a07dac057331..8212c3d4a5ae 100644
--- a/include/libcamera/base/class.h
+++ b/include/libcamera/base/class.h
@@ -33,14 +33,24 @@  namespace libcamera {
 #define LIBCAMERA_DECLARE_PRIVATE()					\
 public:									\
 	class Private;							\
-	friend class Private;
+	friend class Private;						\
+	template <bool B = true>					\
+	const Private *_d() const					\
+	{								\
+		return Extensible::_d<Private>();			\
+	}								\
+	template <bool B = true>					\
+	Private *_d()							\
+	{								\
+		return Extensible::_d<Private>();			\
+	}
 
 #define LIBCAMERA_DECLARE_PUBLIC(klass)					\
 	friend class klass;						\
 	using Public = klass;
 
 #define LIBCAMERA_D_PTR()						\
-	_d<Private>();
+	_d();
 
 #define LIBCAMERA_O_PTR()						\
 	_o<Public>();