io.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2019 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef dap_io_h
  15. #define dap_io_h
  16. #include <stddef.h> // size_t
  17. #include <stdio.h> // FILE
  18. #include <memory> // std::unique_ptr
  19. #include <utility> // std::pair
  20. namespace dap {
  21. class Closable {
  22. public:
  23. virtual ~Closable() = default;
  24. // isOpen() returns true if the stream has not been closed.
  25. virtual bool isOpen() = 0;
  26. // close() closes the stream.
  27. virtual void close() = 0;
  28. };
  29. // Reader is an interface for reading from a byte stream.
  30. class Reader : virtual public Closable {
  31. public:
  32. // read() attempts to read at most n bytes into buffer, returning the number
  33. // of bytes read.
  34. // read() will block until the stream is closed or at least one byte is read.
  35. virtual size_t read(void* buffer, size_t n) = 0;
  36. };
  37. // Writer is an interface for writing to a byte stream.
  38. class Writer : virtual public Closable {
  39. public:
  40. // write() writes n bytes from buffer into the stream.
  41. // Returns true on success, or false if there was an error or the stream was
  42. // closed.
  43. virtual bool write(const void* buffer, size_t n) = 0;
  44. };
  45. // ReaderWriter is an interface that combines the Reader and Writer interfaces.
  46. class ReaderWriter : public Reader, public Writer {
  47. public:
  48. // create() returns a ReaderWriter that delegates the interface methods on to
  49. // the provided Reader and Writer.
  50. // isOpen() returns true if the Reader and Writer both return true for
  51. // isOpen().
  52. // close() closes both the Reader and Writer.
  53. static std::shared_ptr<ReaderWriter> create(const std::shared_ptr<Reader>&,
  54. const std::shared_ptr<Writer>&);
  55. };
  56. // pipe() returns a ReaderWriter where the Writer streams to the Reader.
  57. // Writes are internally buffered.
  58. // Calling close() on either the Reader or Writer will close both ends of the
  59. // stream.
  60. std::shared_ptr<ReaderWriter> pipe();
  61. // file() wraps file with a ReaderWriter.
  62. // If closable is false, then a call to ReaderWriter::close() will not close the
  63. // underlying file.
  64. std::shared_ptr<ReaderWriter> file(FILE* file, bool closable = true);
  65. // file() opens (or creates) the file with the given path.
  66. std::shared_ptr<ReaderWriter> file(const char* path);
  67. // spy() returns a Reader that copies all reads from the Reader r to the Writer
  68. // s, using the given optional prefix.
  69. std::shared_ptr<Reader> spy(const std::shared_ptr<Reader>& r,
  70. const std::shared_ptr<Writer>& s,
  71. const char* prefix = "\n->");
  72. // spy() returns a Writer that copies all writes to the Writer w to the Writer
  73. // s, using the given optional prefix.
  74. std::shared_ptr<Writer> spy(const std::shared_ptr<Writer>& w,
  75. const std::shared_ptr<Writer>& s,
  76. const char* prefix = "\n<-");
  77. // writef writes the printf style string to the writer w.
  78. bool writef(const std::shared_ptr<Writer>& w, const char* msg, ...);
  79. } // namespace dap
  80. #endif // dap_io_h