[libcamera-devel] [PATCH v4 3/5] lc-compliance: Add Environment singleton

Niklas Söderlund niklas.soderlund at ragnatech.se
Sat May 22 12:12:10 CEST 2021


Hi Nícolas,

Thanks for your patch.

On 2021-05-21 10:30:52 -0300, Nícolas F. R. A. Prado wrote:
> Add a singleton Environment class in order to make the camera available
> inside all tests. This is needed for the Googletest refactor, otherwise
> the tests, which are statically declared, won't be able to access the
> camera.
> 
> Signed-off-by: Nícolas F. R. A. Prado <nfraprado at collabora.com>
> ---
> Added in v4
> 
>  src/lc-compliance/environment.cpp | 35 +++++++++++++++++++++++++++++++
>  src/lc-compliance/environment.h   | 31 +++++++++++++++++++++++++++
>  src/lc-compliance/meson.build     |  1 +
>  3 files changed, 67 insertions(+)
>  create mode 100644 src/lc-compliance/environment.cpp
>  create mode 100644 src/lc-compliance/environment.h
> 
> ronment::instance()diff --git a/src/lc-compliance/environment.cpp 
> b/src/lc-compliance/environment.cpp
> new file mode 100644
> index 000000000000..de7fa5bbadd1
> --- /dev/null
> +++ b/src/lc-compliance/environment.cpp
> @@ -0,0 +1,35 @@
> +/* SPDX-License-Identifier: GPL-2.0-or-later */
> +/*
> + * Copyright (C) 2021, Collabora Ltd.
> + *
> + * environment.cpp - Common environment for tests
> + */
> +
> +#include "environment.h"
> +
> +Environment *Environment::instance_ = nullptr;
> +std::shared_ptr<Camera> Environment::camera_ = nullptr;
> +
> +bool Environment::create(std::shared_ptr<Camera> camera)
> +{
> +	if (instance_)
> +		return false;
> +
> +	camera_ = camera;
> +	instance_ = new Environment;
> +	return true;
> +}
> +
> +void Environment::destroy()
> +{
> +	if (!camera_)
> +		return;
> +
> +	camera_->release();
> +	camera_.reset();
> +}
> +
> +Environment *Environment::instance()
> +{
> +	return instance_;
> +}

Would it make sens to refactor this as Environment::get() while making 
instance_ a shared pointer and keeping camera_ as a non-static member?

std::shared_ptr<Environment> Environment::instance_ = nullptr;

Environment *Environment::get()
{
    if (!instance_)
        instance_ = new Environment;

    return instance_;
}

void Environment::setup(std::shared_ptr<Camera> camera)
{
        camera_ = camera;
}

void Environment::destroy()
{
	if (!camera_)
		return;

	camera_->release();
	camera_.reset();
}

> diff --git a/src/lc-compliance/environment.h b/src/lc-compliance/environment.h
> new file mode 100644
> index 000000000000..f4832d371b6c
> --- /dev/null
> +++ b/src/lc-compliance/environment.h
> @@ -0,0 +1,31 @@
> +/* SPDX-License-Identifier: GPL-2.0-or-later */
> +/*
> + * Copyright (C) 2021, Collabora Ltd.
> + *
> + * environment.h - Common environment for tests
> + */
> +#ifndef __LC_COMPLIANCE_ENVIRONMENT_H__
> +#define __LC_COMPLIANCE_ENVIRONMENT_H__
> +
> +#include <libcamera/libcamera.h>
> +
> +using namespace libcamera;
> +
> +class Environment
> +{
> +public:
> +	static bool create(std::shared_ptr<Camera> camera);
> +	void destroy();
> +
> +	static Environment *instance();
> +
> +	std::shared_ptr<Camera> camera() const { return camera_; }
> +
> +private:
> +	Environment() {};
> +
> +	static Environment *instance_;
> +	static std::shared_ptr<Camera> camera_;
> +};
> +
> +#endif /* __LC_COMPLIANCE_ENVIRONMENT_H__ */
> diff --git a/src/lc-compliance/meson.build b/src/lc-compliance/meson.build
> index a2bfcceb1259..c287017575bd 100644
> --- a/src/lc-compliance/meson.build
> +++ b/src/lc-compliance/meson.build
> @@ -14,6 +14,7 @@ lc_compliance_sources = files([
>      '../cam/options.cpp',
>      'main.cpp',
>      'results.cpp',
> +    'environment.cpp',
>      'simple_capture.cpp',
>      'single_stream.cpp',
>  ])
> -- 
> 2.31.1
> 

-- 
Regards,
Niklas Söderlund


More information about the libcamera-devel mailing list