2 * Copyright 2012 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef FOLLY_FILEGEN_H_
18 #define FOLLY_FILEGEN_H_
20 #include "folly/experimental/File.h"
21 #include "folly/experimental/Gen.h"
22 #include "folly/experimental/io/IOBuf.h"
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).
37 template <class S = detail::FileReader>
38 S fromFile(File file, size_t bufferSize=4096) {
39 return S(std::move(file), IOBuf::create(bufferSize));
43 * Generator that reads from a file using a given buffer.
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));
51 * Sink that writes to a file with a buffer of the given size.
52 * If bufferSize is 0, writes will be unbuffered.
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));
60 * Sink that writes to a file using a given buffer.
61 * If the buffer is nullptr, writes will be unbuffered.
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));
71 #include "folly/experimental/FileGen-inl.h"
73 #endif /* FOLLY_FILEGEN_H_ */