summaryrefslogtreecommitdiff
path: root/src/plugins/multimedia/qnx/camera/qqnxcamerahandle_p.h
blob: 3d7863dc2be6bd6ac095bff99a76cd1cbdfd798a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QQNXCAMERAHANDLE_P_H
#define QQNXCAMERAHANDLE_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <camera/camera_api.h>

#include <utility>

class QQnxCameraHandle
{
public:
    QQnxCameraHandle() = default;

    explicit QQnxCameraHandle(camera_handle_t h)
        : m_handle (h) {}

    explicit QQnxCameraHandle(QQnxCameraHandle &&other)
        : m_handle(other.m_handle)
        , m_lastError(other.m_lastError)
    {
        other = QQnxCameraHandle();
    }

    QQnxCameraHandle(const QQnxCameraHandle&) = delete;

    QQnxCameraHandle& operator=(QQnxCameraHandle&& other)
    {
        m_handle = other.m_handle;
        m_lastError = other.m_lastError;

        other = QQnxCameraHandle();

        return *this;
    }

    ~QQnxCameraHandle()
    {
        close();
    }

    bool open(camera_unit_t unit, uint32_t mode)
    {
        if (isOpen()) {
            m_lastError = CAMERA_EALREADY;
            return false;
        }

        return cacheError(camera_open, unit, mode, &m_handle);
    }

    bool close()
    {
        if (!isOpen())
            return true;

        const bool success = cacheError(camera_close, m_handle);
        m_handle = CAMERA_HANDLE_INVALID;

        return success;
    }

    camera_handle_t get() const
    {
        return m_handle;
    }

    bool isOpen() const
    {
        return m_handle != CAMERA_HANDLE_INVALID;
    }

    camera_error_t lastError() const
    {
        return m_lastError;
    }

private:
    template <typename Func, typename ...Args>
    bool cacheError(Func f, Args &&...args)
    {
        m_lastError = f(std::forward<Args>(args)...);

        return m_lastError == CAMERA_EOK;
    }

    camera_handle_t m_handle = CAMERA_HANDLE_INVALID;
    camera_error_t m_lastError = CAMERA_EOK;
};

#endif