Audacity 3.2.0
SafeConnection.h
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 * SPDX-FileName: SafeConnection.h
4 * SPDX-FileContributor: Dmitry Vedenko
5 */
6
7#pragma once
8
9#include <memory>
10#include <mutex>
11
12#include "Connection.h"
13
14namespace audacity::sqlite
15{
17
21class SQLITE_HELPERS_API SafeConnection final :
22 public std::enable_shared_from_this<SafeConnection>
23{
24 struct Tag final
25 {
26 };
27
28 using MutexType = std::recursive_mutex;
29
30public:
31 SafeConnection(Tag, Connection connection);
32
33 static std::shared_ptr<SafeConnection> Open(
34 std::string_view path, OpenMode mode = OpenMode::ReadWriteCreate,
36 Error* openError = nullptr);
37
38 static std::shared_ptr<SafeConnection> Reopen(
39 const Connection& connection, OpenMode mode = OpenMode::ReadWriteCreate,
41 Error* openError = nullptr);
42
43 static std::shared_ptr<SafeConnection> Reopen(
44 sqlite3* connection, OpenMode mode = OpenMode::ReadWriteCreate,
46 Error* openError = nullptr);
47
48 static std::shared_ptr<SafeConnection> Reopen(
51 Error* openError = nullptr);
52
53 struct SQLITE_HELPERS_API Lock final
54 {
55 explicit Lock(std::shared_ptr<SafeConnection> connection);
56 ~Lock() = default;
57
58 Lock(const Lock&) = delete;
59 Lock& operator=(const Lock&) = delete;
60 Lock(Lock&&) = default;
61 Lock& operator=(Lock&&) = default;
62
63 Connection* operator->() noexcept;
64 const Connection* operator->() const noexcept;
65
66 Connection& operator*() noexcept;
67 const Connection& operator*() const noexcept;
68
69 explicit operator bool() const noexcept;
70 bool IsValid() const noexcept;
71
72 private:
73 std::shared_ptr<SafeConnection> mSafeConnection;
74 std::unique_lock<MutexType> mLock;
75 };
76
77 Lock Acquire() noexcept;
78
79private:
80 Connection mConnection;
81 MutexType mConnectionMutex;
82};
83} // namespace audacity::sqlite
A class representing a connection to a SQLite database.
Definition: Connection.h:48
A class representing an error in SQLite.
Definition: Error.h:17
A class representing a safe connection to SQLite.
std::recursive_mutex MutexType
OpenMode
The mode in which the database should be opened.
Definition: Connection.h:26
ThreadMode
The mode in which the database should be accessed.
Definition: Connection.h:39
STL namespace.
Lock & operator=(Lock &&)=default
Lock & operator=(const Lock &)=delete