xref: /aosp_15_r20/external/stg/file_descriptor.h (revision 9e3b08ae94a55201065475453d799e8b1378bea6)
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- mode: C++ -*-
3 //
4 // Copyright 2022 Google LLC
5 //
6 // Licensed under the Apache License v2.0 with LLVM Exceptions (the
7 // "License"); you may not use this file except in compliance with the
8 // License.  You may obtain a copy of the License at
9 //
10 //     https://llvm.org/LICENSE.txt
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
18 // Author: Aleksei Vetrov
19 // Author: Matthias Maennich
20 
21 #ifndef STG_FILE_DESCRIPTOR_H_
22 #define STG_FILE_DESCRIPTOR_H_
23 
24 #include <sys/stat.h>  // for mode_t
25 
26 #include <utility>
27 
28 namespace stg {
29 
30 // RAII wrapper over file descriptor
31 class FileDescriptor {
32  public:
33   FileDescriptor() = default;
34   FileDescriptor(const char* filename, int flags, mode_t mode = 0);
35   FileDescriptor(const FileDescriptor&) = delete;
36   FileDescriptor& operator=(const FileDescriptor&) = delete;
FileDescriptor(FileDescriptor && other)37   FileDescriptor(FileDescriptor&& other) noexcept {
38     std::swap(fd_, other.fd_);
39   }
40   FileDescriptor& operator=(FileDescriptor&& other) = delete;
41   ~FileDescriptor() noexcept(false);
42 
43   int Value() const;
44 
45  private:
46   int fd_ = -1;
47 };
48 
49 }  // namespace stg
50 
51 #endif  // STG_FILE_DESCRIPTOR_H_
52