// Copyright 2019 Google LLC1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// https://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314#ifndef dap_io_h15#define dap_io_h1617#include <stddef.h> // size_t18#include <stdio.h> // FILE19#include <memory> // std::unique_ptr20#include <utility> // std::pair2122namespace dap {2324class Closable {25public:26virtual ~Closable() = default;2728// isOpen() returns true if the stream has not been closed.29virtual bool isOpen() = 0;3031// close() closes the stream.32virtual void close() = 0;33};3435// Reader is an interface for reading from a byte stream.36class Reader : virtual public Closable {37public:38// read() attempts to read at most n bytes into buffer, returning the number39// of bytes read.40// read() will block until the stream is closed or at least one byte is read.41virtual size_t read(void* buffer, size_t n) = 0;42};4344// Writer is an interface for writing to a byte stream.45class Writer : virtual public Closable {46public: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 was49// closed.50virtual bool write(const void* buffer, size_t n) = 0;51};5253// ReaderWriter is an interface that combines the Reader and Writer interfaces.54class ReaderWriter : public Reader, public Writer {55public:56// create() returns a ReaderWriter that delegates the interface methods on to57// the provided Reader and Writer.58// isOpen() returns true if the Reader and Writer both return true for59// isOpen().60// close() closes both the Reader and Writer.61static std::shared_ptr<ReaderWriter> create(const std::shared_ptr<Reader>&,62const std::shared_ptr<Writer>&);63};6465// 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 the68// stream.69std::shared_ptr<ReaderWriter> pipe();7071// file() wraps file with a ReaderWriter.72// If closable is false, then a call to ReaderWriter::close() will not close the73// underlying file.74std::shared_ptr<ReaderWriter> file(FILE* file, bool closable = true);7576// file() opens (or creates) the file with the given path.77std::shared_ptr<ReaderWriter> file(const char* path);7879// spy() returns a Reader that copies all reads from the Reader r to the Writer80// s, using the given optional prefix.81std::shared_ptr<Reader> spy(const std::shared_ptr<Reader>& r,82const std::shared_ptr<Writer>& s,83const char* prefix = "\n->");8485// spy() returns a Writer that copies all writes to the Writer w to the Writer86// s, using the given optional prefix.87std::shared_ptr<Writer> spy(const std::shared_ptr<Writer>& w,88const std::shared_ptr<Writer>& s,89const char* prefix = "\n<-");9091// writef writes the printf style string to the writer w.92bool writef(const std::shared_ptr<Writer>& w, const char* msg, ...);9394} // namespace dap9596#endif // dap_io_h979899