Matter SDK Coverage Report
Current view: top level - lib/support - FileDescriptor.h (source / functions) Coverage Total Hit
Test: SHA:f1767a8b0a3778fdf31b1d979afbdf544892fd94 Lines: 100.0 % 13 13
Test Date: 2026-06-03 07:35:21 Functions: 100.0 % 7 7

            Line data    Source code
       1              : /*
       2              :  *
       3              :  *    Copyright (c) 2024 Project CHIP Authors
       4              :  *
       5              :  *    Licensed under the Apache License, Version 2.0 (the "License");
       6              :  *    you may not use this file except in compliance with the License.
       7              :  *    You may obtain a copy of the License at
       8              :  *
       9              :  *        http://www.apache.org/licenses/LICENSE-2.0
      10              :  *
      11              :  *    Unless required by applicable law or agreed to in writing, software
      12              :  *    distributed under the License is distributed on an "AS IS" BASIS,
      13              :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      14              :  *    See the License for the specific language governing permissions and
      15              :  *    limitations under the License.
      16              :  */
      17              : 
      18              : #pragma once
      19              : 
      20              : #include <unistd.h>
      21              : #include <utility>
      22              : 
      23              : namespace chip {
      24              : 
      25              : /// Unix file descriptor wrapper with RAII semantics.
      26              : class FileDescriptor
      27              : {
      28              : public:
      29          165 :     FileDescriptor() = default;
      30          165 :     explicit FileDescriptor(int fd) : mFd(fd) {}
      31          330 :     ~FileDescriptor() { Close(); }
      32              : 
      33              :     /// Disallow copy and assignment.
      34              :     FileDescriptor(const FileDescriptor &)             = delete;
      35              :     FileDescriptor & operator=(const FileDescriptor &) = delete;
      36              : 
      37              :     FileDescriptor(FileDescriptor && other) noexcept : mFd(other.Release()) {}
      38          165 :     FileDescriptor & operator=(FileDescriptor && other) noexcept
      39              :     {
      40          165 :         Close();
      41          165 :         mFd = other.Release();
      42          165 :         return *this;
      43              :     }
      44              : 
      45          495 :     int Get() const { return mFd; }
      46              : 
      47          165 :     int Release() { return std::exchange(mFd, -1); }
      48              : 
      49          495 :     int Close()
      50              :     {
      51          495 :         if (mFd != -1)
      52              :         {
      53          165 :             return close(std::exchange(mFd, -1));
      54              :         }
      55          330 :         return 0;
      56              :     }
      57              : 
      58              : private:
      59              :     int mFd = -1;
      60              : };
      61              : 
      62              : } // namespace chip
        

Generated by: LCOV version 2.0-1