Debian Codegraph @

Utilities/cmcppdap/include/dap/io.h

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