Retro68/LaunchAPPL/Common/Stream.h

53 lines
1.3 KiB
C
Raw Normal View History

#ifndef STREAM_H_
#define STREAM_H_
2018-04-20 22:03:29 +00:00
#include <stdint.h>
#include <stddef.h>
#include <vector>
class StreamListener
{
public:
virtual size_t onReceive(const uint8_t* p, size_t n) = 0;
2018-04-23 23:42:36 +00:00
virtual void onReset() {}
2018-04-20 22:03:29 +00:00
};
class Stream
{
StreamListener *listener_ = nullptr;
2018-04-20 22:03:29 +00:00
std::vector<uint8_t> buffer_;
public:
Stream();
virtual ~Stream();
void setListener(StreamListener *l) { listener_ = l; }
2018-04-23 23:42:36 +00:00
void clearListener(StreamListener *l = nullptr) { if(!l || listener_ == l) listener_ = nullptr; }
2018-04-20 22:03:29 +00:00
virtual void write(const void* p, size_t n) = 0;
virtual void flushWrite() {}
2018-04-20 22:03:29 +00:00
long read(void *p, size_t n);
2018-04-23 23:42:36 +00:00
virtual bool readyToWrite() { return true; }
bool readyToRead() { return !buffer_.empty(); }
2018-04-20 22:03:29 +00:00
protected:
void notifyReceive(const uint8_t* p, size_t n);
2018-04-23 23:42:36 +00:00
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_; }
2018-04-20 22:03:29 +00:00
};
#endif