Qt 4.8
qdbusunixfiledescriptor.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the FOO module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 
44 #include <QSharedData>
45 
46 #ifdef Q_OS_UNIX
47 # include <private/qcore_unix_p.h>
48 #endif
49 
51 
119 public:
122  : QSharedData(other), fd(-1)
123  { }
125 
127 };
128 
129 template<> inline
131 { if (d && !d->ref.deref()) delete d; }
132 
141  : d(0)
142 {
143 }
144 
159  : d(0)
160 {
161  if (fileDescriptor != -1)
162  setFileDescriptor(fileDescriptor);
163 }
164 
169  : d(other.d)
170 {
171 }
172 
179 {
180  if (this != &other)
181  d.operator=(other.d);
182  return *this;
183 }
184 
189 {
190 }
191 
199 {
200  return d ? d->fd != -1 : false;
201 }
202 
217 {
218  return d ? d->fd.operator int() : -1;
219 }
220 
221 // actual implementation
222 #ifdef Q_OS_UNIX
223 
224 // qdoc documentation is generated on Unix
225 
237 {
238  return true;
239 }
240 
255 {
256  if (fileDescriptor != -1)
257  giveFileDescriptor(qt_safe_dup(fileDescriptor));
258 }
259 
270 {
271  // if we are the sole ref, d remains unchanged
272  // if detaching happens, d->fd will be -1
273  if (d)
274  d.detach();
275  else
277 
278  if (d->fd != -1)
279  qt_safe_close(d->fd);
280 
281  if (fileDescriptor != -1)
282  d->fd = fileDescriptor;
283 }
284 
297 {
298  if (!d)
299  return -1;
300 
301  return d->fd.fetchAndStoreRelaxed(-1);
302 }
303 
305 {
306  if (fd != -1)
307  qt_safe_close(fd);
308 }
309 
310 #else
312 {
313  return false;
314 }
315 
317 {
318 }
319 
321 {
322 }
323 
325 {
326  return -1;
327 }
328 
330 {
331 }
332 
333 #endif
334 
double d
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QDBusUnixFileDescriptor()
Constructs a QDBusUnixFileDescriptor without a wrapped file descriptor.
The QAtomicInt class provides platform-independent atomic operations on integers. ...
Definition: qatomic.h:55
static bool isSupported()
Returns true if Unix file descriptors are supported on this platform.
static int qt_safe_close(int fd)
Definition: qcore_unix_p.h:297
void setFileDescriptor(int fileDescriptor)
Sets the file descriptor that this QDBusUnixFileDescriptor object holds to a copy of fileDescriptor...
The QDBusUnixFileDescriptor class holds one Unix file descriptor.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
~QDBusUnixFileDescriptor()
Destroys this QDBusUnixFileDescriptor object and disposes of the Unix file descriptor that it contain...
QDBusUnixFileDescriptor & operator=(const QDBusUnixFileDescriptor &other)
Copies the Unix file descriptor from the other QDBusUnixFileDescriptor object.
QDBusUnixFileDescriptorPrivate(const QDBusUnixFileDescriptorPrivate &other)
bool isValid() const
Returns true if this Unix file descriptor is valid.
The QSharedData class is a base class for shared data objects.
Definition: qshareddata.h:56
void detach()
If the shared data object&#39;s reference count is greater than 1, this function creates a deep copy of t...
Definition: qshareddata.h:148
int takeFileDescriptor()
Extracts the Unix file descriptor from the QDBusUnixFileDescriptor object and transfers ownership...
int fileDescriptor() const
Returns the Unix file descriptor contained by this QDBusUnixFileDescriptor object.
void giveFileDescriptor(int fileDescriptor)
Sets the Unix file descriptor to fileDescriptor without copying.
~QExplicitlySharedDataPointer()
Decrements the reference count of the shared data object.
Definition: qshareddata.h:166
int fetchAndStoreRelaxed(int newValue)
Atomic fetch-and-store.
static int qt_safe_dup(int oldfd, int atleast=0, int flags=FD_CLOEXEC)
Definition: qcore_unix_p.h:227