File indexing completed on 2024-05-19 16:34:57

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText:: 2022 Xaver Hugl <xaver.hugl@gmail.com>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #include "filedescriptor.h"
0010 
0011 #include <unistd.h>
0012 #include <utility>
0013 
0014 namespace KWin
0015 {
0016 
0017 FileDescriptor::FileDescriptor(int fd)
0018     : m_fd(fd)
0019 {
0020 }
0021 
0022 FileDescriptor::FileDescriptor(FileDescriptor &&other)
0023     : m_fd(std::exchange(other.m_fd, -1))
0024 {
0025 }
0026 
0027 FileDescriptor &FileDescriptor::operator=(FileDescriptor &&other)
0028 {
0029     if (m_fd != -1) {
0030         ::close(m_fd);
0031     }
0032     m_fd = std::exchange(other.m_fd, -1);
0033     return *this;
0034 }
0035 
0036 FileDescriptor::~FileDescriptor()
0037 {
0038     if (m_fd != -1) {
0039         ::close(m_fd);
0040     }
0041 }
0042 
0043 bool FileDescriptor::isValid() const
0044 {
0045     return m_fd != -1;
0046 }
0047 
0048 int FileDescriptor::get() const
0049 {
0050     return m_fd;
0051 }
0052 
0053 FileDescriptor FileDescriptor::duplicate() const
0054 {
0055     if (m_fd != -1) {
0056         return FileDescriptor{dup(m_fd)};
0057     } else {
0058         return {};
0059     }
0060 }
0061 }