Files

133 lines
3.4 KiB
C++
Raw Permalink Normal View History

#ifndef _CINPUTSTREAM
#define _CINPUTSTREAM
#include "types.h"
2022-10-20 21:32:04 -04:00
#include "stddef.h"
class CInputStream;
template < typename T >
struct TType {};
template < typename T >
T cinput_stream_helper(const TType< T >& type, CInputStream& in);
2023-01-07 18:11:54 -08:00
template < typename T >
2025-05-23 03:05:25 -07:00
inline TType< T > TGetType(const T&) {
2023-01-07 18:11:54 -08:00
return TType< T >();
}
class CInputStream {
public:
CInputStream(int len);
CInputStream(const void* ptr, int len, bool owned);
virtual ~CInputStream();
2022-10-12 23:09:15 -07:00
virtual size_t Read(void* dest, size_t len) = 0;
2022-10-09 01:37:23 -04:00
float ReadFloat();
2022-07-18 13:53:58 -04:00
u64 ReadLongLong();
uint ReadLong();
2022-10-09 01:37:23 -04:00
ushort ReadShort();
2022-07-14 20:48:18 -04:00
bool ReadBool();
2026-01-07 11:01:50 -08:00
char ReadChar();
uint ReadBits(uint len);
2022-10-12 23:09:15 -07:00
size_t ReadBytes(void* dest, size_t len);
2022-07-14 20:48:18 -04:00
void Get(void* dest, unsigned long len);
2022-07-18 13:53:58 -04:00
template < typename T >
2026-01-20 13:41:07 -08:00
T Get(const TType< T >& type = TType< T >()) {
2025-05-23 03:05:25 -07:00
return cinput_stream_helper(TType< T >(), *this);
}
bool ReadPackedBool() { return ReadBits(1) != 0; }
2022-10-05 09:28:37 -07:00
2023-10-17 17:36:08 -04:00
int ReadInt32() { return Get< int >(); }
ushort ReadUint16() { return Get< ushort >(); }
short ReadInt16() { return Get< short >(); }
2022-10-09 12:34:58 -04:00
uint GetBlockOffset() const { return x4_blockOffset; }
2023-10-19 21:15:38 -07:00
const uint GetReadPosition() const { return x18_readPosition; }
private:
2022-07-14 20:48:18 -04:00
bool GrabAnotherBlock();
bool InternalReadNext();
uint x4_blockOffset;
uint x8_blockLen;
uint xc_len;
2022-10-09 01:37:23 -04:00
uchar* x10_ptr;
bool x14_owned;
uint x18_readPosition;
uint x1c_bitWord;
uint x20_bitOffset;
};
2023-01-07 18:11:54 -08:00
template < typename T >
inline T cinput_stream_helper(const TType< T >& type, CInputStream& in) {
return T(in);
}
2022-10-15 19:09:59 +03:00
template <>
inline bool cinput_stream_helper(const TType< bool >& type, CInputStream& in) {
return in.ReadBool();
}
template <>
inline char cinput_stream_helper(const TType< char >& type, CInputStream& in) {
return in.ReadChar();
}
2023-06-23 17:15:06 -07:00
template <>
inline unsigned char cinput_stream_helper(const TType< unsigned char >& type, CInputStream& in) {
return in.ReadChar();
}
template <>
inline int cinput_stream_helper(const TType< int >& type, CInputStream& in) {
return in.ReadLong();
}
template <>
inline uint cinput_stream_helper(const TType< uint >& type, CInputStream& in) {
return in.ReadLong();
}
template <>
inline unsigned long cinput_stream_helper(const TType< unsigned long >& type, CInputStream& in) {
return in.ReadLong();
}
2022-07-30 17:38:13 -07:00
template <>
inline float cinput_stream_helper(const TType< float >& type, CInputStream& in) {
return in.ReadFloat();
}
2022-10-09 12:34:58 -04:00
template <>
inline short cinput_stream_helper(const TType< short >& type, CInputStream& in) {
return in.ReadShort();
}
template <>
inline ushort cinput_stream_helper(const TType< ushort >& type, CInputStream& in) {
return in.ReadShort();
}
// rstl
#include "rstl/pair.hpp"
template < typename L, typename R >
2026-01-30 10:02:47 -08:00
inline rstl::pair< L, R >::pair(CInputStream& in)
: first(in.Get< L >()), second(in.Get< R >()) {}
2022-10-09 12:34:58 -04:00
#include "rstl/vector.hpp"
template < typename T, typename Alloc >
inline rstl::vector< T, Alloc >::vector(CInputStream& in, const Alloc& allocator)
2022-10-09 12:34:58 -04:00
: x4_count(0), x8_capacity(0), xc_items(nullptr) {
2025-05-23 03:05:25 -07:00
int count = in.Get(TGetType(0));
2022-10-09 12:34:58 -04:00
reserve(count);
for (int i = 0; i < count; i++) {
2026-01-30 10:02:47 -08:00
push_back(in.Get< T >());
2022-10-09 12:34:58 -04:00
}
}
#include "rstl/reserved_vector.hpp"
template < typename T, int N >
2023-01-07 18:11:54 -08:00
inline rstl::reserved_vector< T, N >::reserved_vector(CInputStream& in) : x0_count(in.ReadInt32()) {
for (int i = 0; i < x0_count; i++) {
construct(&data()[i], in.Get(TType< T >()));
}
}
#endif // _CINPUTSTREAM