corda/src/stream.h

79 lines
1.3 KiB
C
Raw Normal View History

2007-06-16 01:02:24 +00:00
#ifndef STREAM_H
#define STREAM_H
#include "common.h"
namespace vm {
2007-06-16 01:02:24 +00:00
class Stream {
public:
class Client {
public:
virtual ~Client() { }
virtual void NO_RETURN handleEOS() = 0;
};
Stream(Client* client, const uint8_t* data, unsigned size):
client(client), data(data), size(size), position(0)
{ }
void skip(unsigned size) {
if (size > this->size - position) {
client->handleEOS();
} else {
position += size;
}
}
void read(uint8_t* data, unsigned size) {
if (size > this->size - position) {
client->handleEOS();
} else {
memcpy(data, this->data + position, size);
position += size;
}
}
uint8_t read1() {
uint8_t v;
read(&v, 1);
return v;
}
uint16_t read2() {
uint16_t a = read1();
uint16_t b = read1();
return (a << 8) | b;
}
uint32_t read4() {
uint32_t a = read2();
uint32_t b = read2();
return (a << 16) | b;
}
uint64_t read8() {
uint64_t a = read4();
uint64_t b = read4();
return (a << 32) | b;
}
uint32_t readFloat() {
2007-08-13 14:06:31 +00:00
return read4();
2007-06-16 01:02:24 +00:00
}
uint64_t readDouble() {
2007-08-13 14:06:31 +00:00
return read8();
2007-06-16 01:02:24 +00:00
}
private:
Client* client;
const uint8_t* data;
unsigned size;
unsigned position;
};
} // namespace vm
2007-06-16 01:02:24 +00:00
#endif//STREAM_H