prime/include/Kyoto/Streams/CInputStream.hpp

133 lines
3.4 KiB
C++
Raw Normal View History

#ifndef _CINPUTSTREAM
#define _CINPUTSTREAM
#include "types.h"
2022-10-21 01:32:04 +00: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-08 02:11:54 +00:00
template < typename T >
TType< T > TGetType() {
return TType< T >();
}
class CInputStream {
public:
CInputStream(int len);
CInputStream(const void* ptr, int len, bool owned);
virtual ~CInputStream();
virtual size_t Read(void* dest, size_t len) = 0;
float ReadFloat();
2022-07-18 17:53:58 +00:00
u64 ReadLongLong();
uint ReadLong();
ushort ReadShort();
2022-07-15 00:48:18 +00:00
bool ReadBool();
uchar ReadChar();
uint ReadBits(uint len);
size_t ReadBytes(void* dest, size_t len);
2022-07-15 00:48:18 +00:00
void Get(void* dest, unsigned long len);
2022-07-18 17:53:58 +00:00
template < typename T >
inline T Get(const TType< T >& type = TType< T >()) {
2022-07-31 00:38:13 +00:00
return cinput_stream_helper(type, *this);
}
bool ReadPackedBool() { return ReadBits(1) != 0; }
2022-10-05 16:28:37 +00:00
2022-10-09 16:34:58 +00:00
// TODO: this cast to uint fixes regalloc in
// CIEKeyframeEmitter / rstl::vector(CInputStream&)
// why?
int ReadInt32() { return static_cast< uint >(Get(TType< int >())); }
uint GetBlockOffset() const { return x4_blockOffset; }
private:
2022-07-15 00:48:18 +00:00
bool GrabAnotherBlock();
bool InternalReadNext();
uint x4_blockOffset;
uint x8_blockLen;
uint xc_len;
uchar* x10_ptr;
bool x14_owned;
uint x18_readPosition;
uint x1c_bitWord;
uint x20_bitOffset;
};
2023-01-08 02:11:54 +00:00
template < typename T >
inline T cinput_stream_helper(const TType< T >& type, CInputStream& in) {
return T(in);
}
2022-10-15 16:09:59 +00: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();
}
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-31 00:38:13 +00:00
template <>
inline float cinput_stream_helper(const TType< float >& type, CInputStream& in) {
return in.ReadFloat();
}
2022-10-09 16:34:58 +00: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 >
2022-09-18 06:05:46 +00:00
inline rstl::pair< L, R > cinput_stream_helper(const TType< rstl::pair< L, R > >& type,
CInputStream& in) {
rstl::pair< L, R > result;
2022-07-31 00:38:13 +00:00
result.first = in.Get(TType< L >());
result.second = in.Get(TType< R >());
return result;
}
2022-10-09 16:34:58 +00:00
#include "rstl/vector.hpp"
template < typename T, typename Alloc >
inline rstl::vector< T, Alloc >::vector(CInputStream& in, const Alloc& allocator)
2022-10-09 16:34:58 +00:00
: x4_count(0), x8_capacity(0), xc_items(nullptr) {
int count = in.ReadInt32();
reserve(count);
for (int i = 0; i < count; i++) {
push_back(in.Get(TType< T >()));
}
}
#include "rstl/reserved_vector.hpp"
template < typename T, int N >
2023-01-08 02:11:54 +00: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