Retro68/LaunchAPPL/Common/Stream.h
2018-04-27 09:14:48 +02:00

53 lines
1.3 KiB
C++

#ifndef STREAM_H_
#define STREAM_H_
#include <stdint.h>
#include <stddef.h>
#include <vector>
class StreamListener
{
public:
virtual size_t onReceive(const uint8_t* p, size_t n) = 0;
virtual void onReset() {}
};
class Stream
{
StreamListener *listener_ = nullptr;
std::vector<uint8_t> buffer_;
public:
Stream();
virtual ~Stream();
void setListener(StreamListener *l) { listener_ = l; }
void clearListener(StreamListener *l = nullptr) { if(!l || listener_ == l) listener_ = nullptr; }
virtual void write(const void* p, size_t n) = 0;
virtual void flushWrite() {}
long read(void *p, size_t n);
virtual bool readyToWrite() { return true; }
bool readyToRead() { return !buffer_.empty(); }
protected:
void notifyReceive(const uint8_t* p, size_t n);
void notifyReset();
};
class StreamWrapper : public Stream, private StreamListener
{
Stream* underlying_;
public:
StreamWrapper(Stream* underlying_);
virtual ~StreamWrapper();
StreamWrapper(const StreamWrapper& other) = delete;
StreamWrapper& operator=(const StreamWrapper& other) = delete;
StreamWrapper(StreamWrapper&& other);
StreamWrapper& operator=(StreamWrapper&& other);
Stream& underlying() const { return *underlying_; }
};
#endif