OpenRCT2/src/core/IStream.hpp

117 lines
3.5 KiB
C++
Raw Normal View History

#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
/*****************************************************************************
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
*
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* A full copy of the GNU General Public License can be found in licence.txt
*****************************************************************************/
#pragma endregion
#pragma once
#include "../common.h"
#include "Exception.hpp"
2016-06-25 23:07:01 +02:00
#include "Memory.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-06-25 13:04:12 +02:00
interface IStream
2016-01-23 19:58:31 +01:00
{
///////////////////////////////////////////////////////////////////////////
// Interface methods
///////////////////////////////////////////////////////////////////////////
2016-06-25 13:04:12 +02:00
virtual ~IStream() { }
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;
virtual uint64 TryRead(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-06-24 22:16:51 +02:00
2016-06-25 23:07:01 +02:00
template<typename T>
T * ReadArray(size_t count)
{
T * buffer = Memory::AllocateArray<T>(count);
Read(buffer, sizeof(T) * count);
return buffer;
}
template<typename T>
void WriteArray(T * buffer, size_t count)
{
Write(buffer, sizeof(T) * count);
}
2016-06-24 22:16:51 +02:00
utf8 * ReadString();
void WriteString(utf8 * str);
};
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) { }
};