folly: replace old-style header guards with "pragma once"
[folly.git] / folly / gen / File.h
1 /*
2  * Copyright 2016 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 #pragma once
18 #define FOLLY_GEN_FILE_H_
19
20 #include <folly/File.h>
21 #include <folly/gen/Base.h>
22 #include <folly/io/IOBuf.h>
23
24 namespace folly {
25 namespace gen {
26
27 namespace detail {
28 class FileReader;
29 class FileWriter;
30 }  // namespace detail
31
32 /**
33  * Generator that reads from a file with a buffer of the given size.
34  * Reads must be buffered (the generator interface expects the generator
35  * to hold each value).
36  */
37 template <class S = detail::FileReader>
38 S fromFile(File file, size_t bufferSize=4096) {
39   return S(std::move(file), IOBuf::create(bufferSize));
40 }
41
42 /**
43  * Generator that reads from a file using a given buffer.
44  */
45 template <class S = detail::FileReader>
46 S fromFile(File file, std::unique_ptr<IOBuf> buffer) {
47   return S(std::move(file), std::move(buffer));
48 }
49
50 /**
51  * Sink that writes to a file with a buffer of the given size.
52  * If bufferSize is 0, writes will be unbuffered.
53  */
54 template <class S = detail::FileWriter>
55 S toFile(File file, size_t bufferSize=4096) {
56   return S(std::move(file), bufferSize ? nullptr : IOBuf::create(bufferSize));
57 }
58
59 /**
60  * Sink that writes to a file using a given buffer.
61  * If the buffer is nullptr, writes will be unbuffered.
62  */
63 template <class S = detail::FileWriter>
64 S toFile(File file, std::unique_ptr<IOBuf> buffer) {
65   return S(std::move(file), std::move(buffer));
66 }
67
68 }}  // !folly::gen
69
70 #include <folly/gen/File-inl.h>