DBus-1-TQt  1.0
tqdbusunixfd.cpp
Go to the documentation of this file.
00001 /* tqdbusunixfd.cpp DBUS unix file handle data type
00002  *
00003  * Copyright (C) 2013 Slávek Banko <slavek.banko@axis.cz>
00004  *
00005  * Licensed under the Academic Free License version 2.1
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020  * USA.
00021  *
00022  */
00023 
00024 #include <unistd.h> 
00025 #include "tqdbusunixfd.h"
00026 
00027 TQT_DBusUnixFd::TQT_DBusUnixFd() : d(new TQT_DBusUnixFdPrivate())
00028 {
00029     d->ref();
00030     d->fd = -1;
00031 };
00032 
00033 TQT_DBusUnixFd::TQT_DBusUnixFd(const TQT_DBusUnixFd& other) : d(other.d)
00034 {
00035     d->ref();
00036 }
00037 
00038 TQT_DBusUnixFd::TQT_DBusUnixFd(int other) : d(0)
00039 {
00040     setFileDescriptor(other);
00041 }
00042 
00043 TQT_DBusUnixFd::~TQT_DBusUnixFd()
00044 {
00045     if (d && d->deref() ) {
00046         if ( isValid() ) {
00047             close(d->fd);
00048         }
00049         delete d;
00050     }
00051 }
00052 
00053 bool TQT_DBusUnixFd::isValid() const
00054 {
00055     return d ? d->fd != -1 : false;
00056 }
00057 
00058 int TQT_DBusUnixFd::fileDescriptor() const
00059 {
00060     return d ? d->fd : -1;
00061 }
00062 
00063 void TQT_DBusUnixFd::setFileDescriptor(int fileDescriptor)
00064 {
00065     giveFileDescriptor(fileDescriptor != -1 ? dup(fileDescriptor) : -1);
00066 }
00067 
00068 void TQT_DBusUnixFd::giveFileDescriptor(int fileDescriptor) 
00069 {
00070     if ( d && d->deref() ) {
00071         if ( isValid() ) {
00072             close(d->fd);
00073         }
00074     }
00075     else {
00076         d = new TQT_DBusUnixFdPrivate;
00077     }
00078     d->ref();
00079     d->fd = fileDescriptor;
00080 }
00081 
00082 TQT_DBusUnixFd &TQT_DBusUnixFd::operator=( const TQT_DBusUnixFd &other )
00083 {
00084     if (other.d) {
00085         other.d->ref();
00086     }
00087     if ( d && d->deref() ) {
00088         if ( isValid() ) {
00089             close(d->fd);
00090         }
00091         delete d;
00092     }
00093     d = other.d;
00094     return *this;
00095 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines