Files
external_libcamera/include/libcamera/base/class.h
Laurent Pinchart a81ccba6a2 libcamera: base: class: Expose Extensible private data to other classes
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. The template
argument is defaulted and 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>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
2021-07-12 19:59:19 +03:00

116 lines
2.1 KiB
C++

/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2020, Google Inc.
*
* class.h - Utilities and helpers for classes
*/
#ifndef __LIBCAMERA_BASE_CLASS_H__
#define __LIBCAMERA_BASE_CLASS_H__
#include <memory>
namespace libcamera {
#ifndef __DOXYGEN__
#define LIBCAMERA_DISABLE_COPY(klass) \
klass(const klass &) = delete; \
klass &operator=(const klass &) = delete;
#define LIBCAMERA_DISABLE_MOVE(klass) \
klass(klass &&) = delete; \
klass &operator=(klass &&) = delete;
#define LIBCAMERA_DISABLE_COPY_AND_MOVE(klass) \
LIBCAMERA_DISABLE_COPY(klass) \
LIBCAMERA_DISABLE_MOVE(klass)
#else
#define LIBCAMERA_DISABLE_COPY(klass)
#define LIBCAMERA_DISABLE_MOVE(klass)
#define LIBCAMERA_DISABLE_COPY_AND_MOVE(klass)
#endif
#ifndef __DOXYGEN__
#define LIBCAMERA_DECLARE_PRIVATE() \
public: \
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();
#define LIBCAMERA_O_PTR() \
_o<Public>();
#else
#define LIBCAMERA_DECLARE_PRIVATE()
#define LIBCAMERA_DECLARE_PUBLIC(klass)
#define LIBCAMERA_D_PTR()
#define LIBCAMERA_O_PTR()
#endif
class Extensible
{
public:
class Private
{
public:
Private(Extensible *o);
virtual ~Private();
#ifndef __DOXYGEN__
template<typename T>
const T *_o() const
{
return static_cast<const T *>(o_);
}
template<typename T>
T *_o()
{
return static_cast<T *>(o_);
}
#endif
private:
Extensible *const o_;
};
Extensible(Private *d);
protected:
#ifndef __DOXYGEN__
template<typename T>
const T *_d() const
{
return static_cast<const T *>(d_.get());
}
template<typename T>
T *_d()
{
return static_cast<T *>(d_.get());
}
#endif
private:
const std::unique_ptr<Private> d_;
};
} /* namespace libcamera */
#endif /* __LIBCAMERA_BASE_CLASS_H__ */