Speed-up StringPiece::find_first_of()
[folly.git] / folly / experimental / File.h
1 /*
2  * Copyright 2012 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef FOLLY_FILE_H_
18 #define FOLLY_FILE_H_
19
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <fcntl.h>
23
24 namespace folly {
25
26 /**
27  * A File represents an open file.
28  */
29 class File {
30  public:
31   /**
32    * Create a File object from an existing file descriptor.
33    * Takes ownership of the file descriptor if ownsFd is true.
34    */
35   /* implicit */ File(int fd, bool ownsFd=false);
36
37   /**
38    * Open and create a file object.  Throws on error.
39    */
40   /* implicit */ File(const char* name, int flags=O_RDONLY, mode_t mode=0644);
41
42   ~File();
43
44   /**
45    * Return the file descriptor, or -1 if the file was closed.
46    */
47   int fd() const { return fd_; }
48
49   /**
50    * If we own the file descriptor, close the file and throw on error.
51    * Otherwise, do nothing.
52    */
53   void close();
54
55   /**
56    * Closes the file (if owned).  Returns true on success, false (and sets
57    * errno) on error.
58    */
59   bool closeNoThrow();
60
61   /**
62    * Releases the file descriptor; no longer owned by this File.
63    */
64   void release();
65
66   /**
67    * Swap this File with another.
68    */
69   void swap(File& other);
70
71   // movable
72   File(File&&);
73   File& operator=(File&&);
74  private:
75   // not copyable
76   File(const File&) = delete;
77   File& operator=(const File&) = delete;
78
79   int fd_;
80   bool ownsFd_;
81 };
82
83 void swap(File& a, File& b);
84
85 }  // namespace folly
86
87 #include "folly/experimental/File-inl.h"
88
89 #endif /* FOLLY_FILE_H_ */
90