OpenRCT2/src/openrct2/core/IStream.hpp

245 lines
6.2 KiB
C++
Raw Normal View History

/*****************************************************************************
* Copyright (c) 2014-2019 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#pragma once
2018-06-22 22:58:39 +02:00
#include "../common.h"
#include "Memory.hpp"
2018-06-01 22:08:40 +02:00
#include <istream>
2018-01-02 20:23:22 +01:00
#include <stdexcept>
#include <string>
#include <vector>
2018-06-22 22:58:39 +02:00
enum
{
2016-01-23 19:58:31 +01:00
STREAM_SEEK_BEGIN,
STREAM_SEEK_CURRENT,
STREAM_SEEK_END
};
#ifdef __WARN_SUGGEST_FINAL_METHODS__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wsuggest-final-methods"
# pragma GCC diagnostic ignored "-Wsuggest-final-types"
#endif
/**
* 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
///////////////////////////////////////////////////////////////////////////
2018-06-22 22:58:39 +02:00
virtual ~IStream()
{
}
2018-06-22 22:58:39 +02:00
virtual bool CanRead() const abstract;
virtual bool CanWrite() const abstract;
2018-06-22 22:58:39 +02:00
virtual uint64_t GetLength() const abstract;
virtual uint64_t GetPosition() const abstract;
virtual void SetPosition(uint64_t position) abstract;
virtual void Seek(int64_t offset, int32_t origin) abstract;
2018-06-22 22:58:39 +02:00
virtual void Read(void* buffer, uint64_t length) abstract;
virtual void Write(const void* buffer, uint64_t length) abstract;
2018-06-22 22:58:39 +02:00
virtual uint64_t TryRead(void* buffer, uint64_t length) abstract;
virtual const void* GetData() const abstract;
2016-01-23 19:58:31 +01:00
///////////////////////////////////////////////////////////////////////////
// Fast path methods, class can override them to use specialised copies.
2016-01-23 19:58:31 +01:00
///////////////////////////////////////////////////////////////////////////
virtual void Read1(void* buffer)
{
Read(buffer, 1);
}
virtual void Read2(void* buffer)
{
Read(buffer, 2);
}
virtual void Read4(void* buffer)
{
Read(buffer, 4);
}
virtual void Read8(void* buffer)
{
Read(buffer, 8);
}
virtual void Read16(void* buffer)
{
Read(buffer, 16);
}
virtual void Write1(const void* buffer)
{
Write(buffer, 1);
}
virtual void Write2(const void* buffer)
{
Write(buffer, 2);
}
virtual void Write4(const void* buffer)
{
Write(buffer, 4);
}
virtual void Write8(const void* buffer)
{
Write(buffer, 8);
}
virtual void Write16(const void* buffer)
{
Write(buffer, 16);
}
///////////////////////////////////////////////////////////////////////////
// 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.
*/
2018-06-22 22:58:39 +02:00
template<typename T> void Read(T * value)
2016-01-23 19:58:31 +01:00
{
// Selects the best path at compile time
if constexpr (sizeof(T) == 1)
{
Read1(value);
}
else if constexpr (sizeof(T) == 2)
{
Read2(value);
}
else if constexpr (sizeof(T) == 4)
{
Read4(value);
}
else if constexpr (sizeof(T) == 8)
{
Read8(value);
}
else if constexpr (sizeof(T) == 16)
{
Read16(value);
}
else
{
Read(value, sizeof(T));
}
2016-01-23 19:58:31 +01:00
}
2016-01-23 19:58:31 +01:00
/**
* Writes the size of the given type to the stream directly from the given address.
*/
2018-06-22 22:58:39 +02:00
template<typename T> void Write(const T* value)
2016-01-23 19:58:31 +01:00
{
// Selects the best path at compile time
if constexpr (sizeof(T) == 1)
{
Write1(value);
}
else if constexpr (sizeof(T) == 2)
{
Write2(value);
}
else if constexpr (sizeof(T) == 4)
{
Write4(value);
}
else if constexpr (sizeof(T) == 8)
{
Write8(value);
}
else if constexpr (sizeof(T) == 16)
{
Write16(value);
}
else
{
Write(value, sizeof(T));
}
2016-01-23 19:58:31 +01:00
}
2016-01-23 19:58:31 +01:00
/**
* Reads the given type from the stream. Use this only for small types (e.g. int8_t, int64_t, double)
2016-01-23 19:58:31 +01:00
*/
2018-06-22 22:58:39 +02:00
template<typename T> T ReadValue()
2016-01-23 19:58:31 +01:00
{
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. int8_t, int64_t, double)
2016-01-23 19:58:31 +01:00
*/
2018-06-22 22:58:39 +02:00
template<typename T> void WriteValue(const T value)
2016-01-23 19:58:31 +01:00
{
Write(&value);
}
2016-06-24 22:16:51 +02:00
2018-06-22 22:58:39 +02:00
template<typename T> T* ReadArray(size_t count)
2016-06-25 23:07:01 +02:00
{
2018-06-22 22:58:39 +02:00
T* buffer = Memory::AllocateArray<T>(count);
2016-06-25 23:07:01 +02:00
Read(buffer, sizeof(T) * count);
return buffer;
}
2018-06-22 22:58:39 +02:00
template<typename T> void WriteArray(T * buffer, size_t count)
2016-06-25 23:07:01 +02:00
{
Write(buffer, sizeof(T) * count);
}
2018-06-22 22:58:39 +02:00
utf8* ReadString();
std::string ReadStdString();
2018-06-22 22:58:39 +02:00
void WriteString(const utf8* str);
void WriteString(const std::string& string);
};
#ifdef __WARN_SUGGEST_FINAL_METHODS__
# pragma GCC diagnostic pop
#endif
2018-01-02 20:23:22 +01:00
class IOException : public std::runtime_error
2016-01-23 19:58:31 +01:00
{
public:
2018-06-22 22:58:39 +02:00
explicit IOException(const std::string& message)
: std::runtime_error(message)
{
}
};
2018-06-22 22:58:39 +02:00
template<typename T> class ivstream : public std::istream
{
private:
class vector_streambuf : public std::basic_streambuf<char, std::char_traits<char>>
{
public:
explicit vector_streambuf(const std::vector<T>& vec)
{
this->setg(
reinterpret_cast<char*>(const_cast<unsigned char*>(vec.data())),
reinterpret_cast<char*>(const_cast<unsigned char*>(vec.data())),
reinterpret_cast<char*>(const_cast<unsigned char*>(vec.data() + vec.size())));
}
};
vector_streambuf _streambuf;
public:
ivstream(const std::vector<T>& vec)
2018-06-22 22:58:39 +02:00
: std::istream(&_streambuf)
, _streambuf(vec)
{
}
};