prime/include/Kyoto/Streams/COutputStream.hpp

88 lines
1.8 KiB
C++
Raw Normal View History

#ifndef _COUTPUTSTREAM
#define _COUTPUTSTREAM
2022-10-04 18:50:29 +00:00
#include "types.h"
2022-10-11 18:38:08 +00:00
class COutputStream;
2022-10-11 18:35:41 +00:00
template < typename T >
2022-10-11 18:35:41 +00:00
void coutput_stream_helper(const T& t, COutputStream& out);
2022-10-04 18:50:29 +00:00
class COutputStream {
void DoPut(const void* ptr, size_t len);
void DoFlush();
2022-10-04 18:50:29 +00:00
public:
COutputStream(int len);
virtual ~COutputStream();
virtual void Write(const void* ptr, size_t len) = 0;
void WriteBits(uint val, uint bitCount);
void Flush();
void FlushShiftRegister();
void Put(const void* ptr, size_t len) {
FlushShiftRegister();
DoPut(ptr, len);
}
template < typename T >
void Put(const T& t) {
coutput_stream_helper(t, *this);
}
void WriteReal32(float t) { Put(t); }
void WriteUint32(uint t) { Put(t); }
void WriteInt32(int t) { Put(t); }
void WriteLong(int t) { Put(&t, sizeof(int)); }
void WriteChar(u8 c) {
FlushShiftRegister();
2022-10-12 16:58:24 +00:00
if (mUnwrittenLen >= mBufLen) {
DoFlush();
}
++mNumWrites;
2022-10-12 16:58:24 +00:00
*(reinterpret_cast< u8* >(mBufPtr) + mUnwrittenLen++) = c;
}
2022-10-11 18:38:08 +00:00
private:
uint mUnwrittenLen;
uint mBufLen;
void* mBufPtr;
uint mNumWrites;
uint mShiftRegister;
volatile 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);
}
template <>
inline void coutput_stream_helper(const float& t, COutputStream& out) {
int tmp = *(int*)&t;
out.Put(&tmp, sizeof(float));
}
template <>
inline void coutput_stream_helper(const int& t, COutputStream& out) {
out.WriteLong(t);
}
template <>
inline void coutput_stream_helper(const uint& t, COutputStream& out) {
out.WriteLong(t);
}
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));
}
#endif // _COUTPUTSTREAM