2022-10-09 05:13:17 +00:00
|
|
|
#ifndef _COUTPUTSTREAM
|
|
|
|
#define _COUTPUTSTREAM
|
2022-10-04 18:50:29 +00:00
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
|
2022-10-11 18:38:08 +00:00
|
|
|
|
2022-10-05 01:31:43 +00:00
|
|
|
class COutputStream;
|
2022-10-11 18:35:41 +00:00
|
|
|
|
2022-10-09 05:13:17 +00:00
|
|
|
template < typename T >
|
2022-10-11 18:35:41 +00:00
|
|
|
void coutput_stream_helper(const T& t, COutputStream& out);
|
2022-10-05 01:31:43 +00:00
|
|
|
|
2022-10-04 18:50:29 +00:00
|
|
|
class COutputStream {
|
2022-10-05 01:31:43 +00:00
|
|
|
void DoPut(const void* ptr, size_t len);
|
|
|
|
void Flush();
|
|
|
|
void DoFlush();
|
2022-10-09 05:13:17 +00:00
|
|
|
|
2022-10-04 18:50:29 +00:00
|
|
|
public:
|
2022-10-05 01:31:43 +00:00
|
|
|
COutputStream(int len);
|
|
|
|
virtual ~COutputStream();
|
2022-10-11 18:38:08 +00:00
|
|
|
virtual void Write(const void* ptr, u32 len);
|
2022-10-04 18:50:29 +00:00
|
|
|
void WriteBits(int val, int bitCount);
|
2022-10-05 01:31:43 +00:00
|
|
|
|
|
|
|
void FlushShiftRegister();
|
|
|
|
void Put(const void* ptr, size_t len) {
|
|
|
|
FlushShiftRegister();
|
|
|
|
DoPut(ptr, len);
|
|
|
|
}
|
|
|
|
|
2022-10-09 05:13:17 +00:00
|
|
|
template < typename T >
|
2022-10-05 01:31:43 +00:00
|
|
|
void Put(const T& t) {
|
|
|
|
coutput_stream_helper(t, *this);
|
|
|
|
}
|
|
|
|
|
2022-10-09 05:13:17 +00:00
|
|
|
void WriteReal32(float t) { Put(t); }
|
2022-10-05 01:31:43 +00:00
|
|
|
|
2022-10-09 05:13:17 +00:00
|
|
|
void WriteUint32(uint t) { Put(t); }
|
|
|
|
void WriteInt32(int t) { Put(t); }
|
2022-10-05 01:31:43 +00:00
|
|
|
|
2022-10-09 05:13:17 +00:00
|
|
|
void WriteLong(int t) { Put(&t, sizeof(int)); }
|
2022-10-05 01:31:43 +00:00
|
|
|
|
2022-10-11 13:41:20 +00:00
|
|
|
void WriteChar(u8 c) {
|
|
|
|
FlushShiftRegister();
|
|
|
|
if (mBufLen <= mPosition) {
|
|
|
|
DoFlush();
|
|
|
|
}
|
|
|
|
++mNumWrites;
|
|
|
|
*(reinterpret_cast< u8* >(mBufPtr) + mPosition) = c;
|
|
|
|
++mPosition;
|
|
|
|
}
|
|
|
|
|
2022-10-11 18:38:08 +00:00
|
|
|
|
2022-10-05 01:31:43 +00:00
|
|
|
private:
|
|
|
|
uint mPosition;
|
|
|
|
uint mBufLen;
|
|
|
|
void* mBufPtr;
|
|
|
|
uint mNumWrites;
|
|
|
|
uint mShiftRegister;
|
|
|
|
uint mShiftRegisterOffset;
|
|
|
|
uchar mScratch[96];
|
2022-10-04 18:50:29 +00:00
|
|
|
};
|
|
|
|
|
2022-10-11 18:35:41 +00:00
|
|
|
template < typename T >
|
|
|
|
inline void coutput_stream_helper(const T& t, COutputStream& out) {
|
|
|
|
t.PutTo(out);
|
|
|
|
}
|
|
|
|
|
2022-10-05 01:31:43 +00:00
|
|
|
template <>
|
|
|
|
inline void coutput_stream_helper(const float& t, COutputStream& out) {
|
|
|
|
int tmp = *(int*)&t;
|
|
|
|
out.Put(&tmp, sizeof(float));
|
|
|
|
}
|
|
|
|
|
2022-10-09 05:13:17 +00:00
|
|
|
template <>
|
2022-10-05 01:31:43 +00:00
|
|
|
inline void coutput_stream_helper(const int& t, COutputStream& out) {
|
|
|
|
out.WriteLong(t);
|
|
|
|
}
|
|
|
|
|
2022-10-09 05:13:17 +00:00
|
|
|
template <>
|
2022-10-05 01:31:43 +00:00
|
|
|
inline void coutput_stream_helper(const uint& t, COutputStream& out) {
|
|
|
|
out.WriteLong(t);
|
|
|
|
}
|
|
|
|
|
2022-10-11 13:41:20 +00:00
|
|
|
template <>
|
|
|
|
inline void coutput_stream_helper(const bool& t, COutputStream& out) {
|
|
|
|
out.WriteChar(static_cast< char >(t));
|
|
|
|
}
|
|
|
|
|
2022-10-09 05:13:17 +00:00
|
|
|
#endif // _COUTPUTSTREAM
|