File: SafeConnection.cpp

package info (click to toggle)
audacity 3.7.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 134,800 kB
  • sloc: cpp: 366,277; ansic: 198,323; lisp: 7,761; sh: 3,414; python: 1,501; xml: 1,385; perl: 854; makefile: 125
file content (127 lines) | stat: -rw-r--r-- 2,920 bytes parent folder | download | duplicates (2)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * SPDX-License-Identifier: GPL-2.0-or-later
 * SPDX-FileName: SafeConnection.cpp
 * SPDX-FileContributor: Dmitry Vedenko
 */

#include "SafeConnection.h"

namespace audacity::sqlite
{
SafeConnection::SafeConnection(Tag, Connection connection)
    : mConnection(std::move(connection))
{
}

std::shared_ptr<SafeConnection> SafeConnection::Open(
   std::string_view path, OpenMode mode, ThreadMode threadMode,
   Error* openError)
{
   auto connection = Connection::Open(path, mode, threadMode);

   if (!connection)
   {
      if (openError)
         *openError = connection.GetError();

      return {};
   }

   return std::make_shared<SafeConnection>(Tag {}, std::move(*connection));
}

std::shared_ptr<SafeConnection> SafeConnection::Reopen(
   const Connection& prevConnection, OpenMode mode, ThreadMode threadMode,
   Error* openError)
{
   auto connection = Connection::Reopen(prevConnection, mode, threadMode);

   if (!connection)
   {
      if (openError)
         *openError = connection.GetError();

      return {};
   }

   return std::make_shared<SafeConnection>(Tag {}, std::move(*connection));
}

std::shared_ptr<SafeConnection> SafeConnection::Reopen(
   sqlite3* prevConnection, OpenMode mode, ThreadMode threadMode,
   Error* openError)
{
   auto connection = Connection::Reopen(prevConnection, mode, threadMode);

   if (!connection)
   {
      if (openError)
         *openError = connection.GetError();

      return {};
   }

   return std::make_shared<SafeConnection>(Tag {}, std::move(*connection));
}

std::shared_ptr<SafeConnection> SafeConnection::Reopen(
   SafeConnection& prevConnection, OpenMode mode, ThreadMode threadMode,
   Error* openError)
{
   auto connection =
      Connection::Reopen(prevConnection.mConnection, mode, threadMode);

   if (!connection)
   {
      if (openError)
         *openError = connection.GetError();

      return {};
   }

   return std::make_shared<SafeConnection>(Tag {}, std::move(*connection));
}

SafeConnection::Lock SafeConnection::Acquire() noexcept
{
   return Lock { shared_from_this() };
}

SafeConnection::Lock::Lock(std::shared_ptr<SafeConnection> connection)
    : mSafeConnection(std::move(connection))
{
   if (mSafeConnection)
      mLock = std::unique_lock { mSafeConnection->mConnectionMutex };
}

Connection* SafeConnection::Lock::operator->() noexcept
{
   return &mSafeConnection->mConnection;
}

const Connection* SafeConnection::Lock::operator->() const noexcept
{
   return &mSafeConnection->mConnection;
}

Connection& SafeConnection::Lock::operator*() noexcept
{
   return mSafeConnection->mConnection;
}

const Connection& SafeConnection::Lock::operator*() const noexcept
{
   return mSafeConnection->mConnection;
}

SafeConnection::Lock::operator bool() const noexcept
{
   return IsValid();
}

bool SafeConnection::Lock::IsValid() const noexcept
{
   return mSafeConnection != nullptr;
}

} // namespace audacity::sqlite