OpenRCT2/src/core/IStream.hpp

83 lines
2.3 KiB
C++
Raw Normal View History

#pragma once
#include "../common.h"
2016-01-23 19:58:31 +01:00
#include "Exception.hpp"
#include "IDisposable.hpp"
enum {
2016-01-23 19:58:31 +01:00
STREAM_SEEK_BEGIN,
STREAM_SEEK_CURRENT,
STREAM_SEEK_END
};
/**
* Represents a stream that can be read or written to. Implemented by types such as FileStream, NetworkStream or MemoryStream.
*/
2016-01-23 19:58:31 +01:00
interface IStream : public IDisposable
{
///////////////////////////////////////////////////////////////////////////
// Interface methods
///////////////////////////////////////////////////////////////////////////
// virtual ~IStream() abstract;
2016-01-23 19:58:31 +01:00
virtual bool CanRead() const abstract;
virtual bool CanWrite() const abstract;
2016-01-23 19:58:31 +01:00
virtual uint64 GetLength() const abstract;
virtual uint64 GetPosition() const abstract;
virtual void SetPosition(uint64 position) abstract;
virtual void Seek(sint64 offset, int origin) abstract;
2016-01-23 19:58:31 +01:00
virtual void Read(void * buffer, uint64 length) abstract;
virtual void Write(const void * buffer, uint64 length) abstract;
2016-01-23 19:58:31 +01:00
///////////////////////////////////////////////////////////////////////////
// Helper methods
///////////////////////////////////////////////////////////////////////////
2016-01-23 19:58:31 +01:00
/**
* Reads the size of the given type from the stream directly into the given address.
*/
template<typename T>
void Read(T * value)
{
Read(value, sizeof(T));
}
2016-01-23 19:58:31 +01:00
/**
* Writes the size of the given type to the stream directly from the given address.
*/
template<typename T>
void Write(const T * value)
{
Write(value, sizeof(T));
}
2016-01-23 19:58:31 +01:00
/**
* Reads the given type from the stream. Use this only for small types (e.g. sint8, sint64, double)
*/
template<typename T>
T ReadValue()
{
T buffer;
Read(&buffer);
return buffer;
}
2016-01-23 19:58:31 +01:00
/**
* Writes the given type to the stream. Use this only for small types (e.g. sint8, sint64, double)
*/
template<typename T>
void WriteValue(const T value)
{
Write(&value);
}
};
2016-01-23 19:58:31 +01:00
class IOException : public Exception
{
public:
2016-01-23 19:58:31 +01:00
IOException(const char * message) : Exception(message) { }
};