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-21 01:32:04 +00:00
|
|
|
#include "stddef.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 {
|
|
|
|
public:
|
2022-10-05 01:31:43 +00:00
|
|
|
COutputStream(int len);
|
|
|
|
virtual ~COutputStream();
|
2022-10-13 06:09:15 +00:00
|
|
|
virtual void Write(const void* ptr, size_t len) = 0;
|
2022-10-12 03:18:01 +00:00
|
|
|
void WriteBits(uint val, uint bitCount);
|
2022-10-05 01:31:43 +00:00
|
|
|
|
2022-12-06 05:30:20 +00:00
|
|
|
void DoPut(const void* ptr, size_t len);
|
|
|
|
void DoFlush();
|
2022-10-13 06:09:15 +00:00
|
|
|
void Flush();
|
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();
|
2022-10-12 16:58:24 +00:00
|
|
|
if (mUnwrittenLen >= mBufLen) {
|
2022-10-11 13:41:20 +00:00
|
|
|
DoFlush();
|
|
|
|
}
|
|
|
|
++mNumWrites;
|
2022-10-12 16:58:24 +00:00
|
|
|
*(reinterpret_cast< u8* >(mBufPtr) + mUnwrittenLen++) = c;
|
2022-10-11 13:41:20 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 01:31:43 +00:00
|
|
|
private:
|
2022-10-12 03:18:01 +00:00
|
|
|
uint mUnwrittenLen;
|
2022-10-05 01:31:43 +00:00
|
|
|
uint mBufLen;
|
|
|
|
void* mBufPtr;
|
|
|
|
uint mNumWrites;
|
|
|
|
uint mShiftRegister;
|
2022-10-12 03:18:01 +00:00
|
|
|
volatile uint mShiftRegisterOffset;
|
2022-10-05 01:31:43 +00:00
|
|
|
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) {
|
2022-10-12 16:58:24 +00:00
|
|
|
out.WriteChar(static_cast< u8 >(t));
|
2022-10-11 13:41:20 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 05:13:17 +00:00
|
|
|
#endif // _COUTPUTSTREAM
|