File.h 2.0 KB

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