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 | |
15 | #ifndef dap_io_h |
16 | #define dap_io_h |
17 | |
18 | #include <stddef.h> // size_t |
19 | #include <memory> // std::unique_ptr |
20 | #include <utility> // std::pair |
21 | |
22 | namespace dap { |
23 | |
24 | class Closable { |
25 | public: |
26 | virtual ~Closable() = default; |
27 | |
28 | // isOpen() returns true if the stream has not been closed. |
29 | virtual bool isOpen() = 0; |
30 | |
31 | // close() closes the stream. |
32 | virtual void close() = 0; |
33 | }; |
34 | |
35 | // Reader is an interface for reading from a byte stream. |
36 | class Reader : virtual public Closable { |
37 | public: |
38 | // read() attempts to read at most n bytes into buffer, returning the number |
39 | // of bytes read. |
40 | // read() will block until the stream is closed or at least one byte is read. |
41 | virtual size_t read(void* buffer, size_t n) = 0; |
42 | }; |
43 | |
44 | // Writer is an interface for writing to a byte stream. |
45 | class Writer : virtual public Closable { |
46 | public: |
47 | // write() writes n bytes from buffer into the stream. |
48 | // Returns true on success, or false if there was an error or the stream was |
49 | // closed. |
50 | virtual bool write(const void* buffer, size_t n) = 0; |
51 | }; |
52 | |
53 | // ReaderWriter is an interface that combines the Reader and Writer interfaces. |
54 | class ReaderWriter : public Reader, public Writer { |
55 | public: |
56 | // create() returns a ReaderWriter that delegates the interface methods on to |
57 | // the provided Reader and Writer. |
58 | // isOpen() returns true if the Reader and Writer both return true for |
59 | // isOpen(). |
60 | // close() closes both the Reader and Writer. |
61 | static std::shared_ptr<ReaderWriter> create(const std::shared_ptr<Reader>&, |
62 | const std::shared_ptr<Writer>&); |
63 | }; |
64 | |
65 | // pipe() returns a ReaderWriter where the Writer streams to the Reader. |
66 | // Writes are internally buffered. |
67 | // Calling close() on either the Reader or Writer will close both ends of the |
68 | // stream. |
69 | std::shared_ptr<ReaderWriter> pipe(); |
70 | |
71 | // file() wraps file with a ReaderWriter. |
72 | // If closable is false, then a call to ReaderWriter::close() will not close the |
73 | // underlying file. |
74 | std::shared_ptr<ReaderWriter> file(FILE* file, bool closable = true); |
75 | |
76 | // file() opens (or creates) the file with the given path. |
77 | std::shared_ptr<ReaderWriter> file(const char* path); |
78 | |
79 | // spy() returns a Reader that copies all reads from the Reader r to the Writer |
80 | // s, using the given optional prefix. |
81 | std::shared_ptr<Reader> spy(const std::shared_ptr<Reader>& r, |
82 | const std::shared_ptr<Writer>& s, |
83 | const char* prefix = "\n->" ); |
84 | |
85 | // spy() returns a Writer that copies all writes to the Writer w to the Writer |
86 | // s, using the given optional prefix. |
87 | std::shared_ptr<Writer> spy(const std::shared_ptr<Writer>& w, |
88 | const std::shared_ptr<Writer>& s, |
89 | const char* prefix = "\n<-" ); |
90 | |
91 | // writef writes the printf style string to the writer w. |
92 | bool writef(const std::shared_ptr<Writer>& w, const char* msg, ...); |
93 | |
94 | } // namespace dap |
95 | |
96 | #endif // dap_io_h |
97 | |