CharOutputStreamBuf.h array streambuf OutputArchive.h terse::CharOutputStreamBuf terse //CopyrightEpicGames,Inc.AllRightsReserved. #pragmaonce #ifdef_MSC_VER #pragmawarning(push) #pragmawarning(disable:43654987) #endif #include<array> #include<streambuf> #ifdef_MSC_VER #pragmawarning(pop) #endif namespaceterse{ template<classTStream,std::size_tBufferSize=4096ul> classCharOutputStreamBuf:publicstd::streambuf{ public: usingPosType=std::streambuf::pos_type; usingOffType=std::streambuf::off_type; usingCharType=std::streambuf::char_type; usingIntType=std::streambuf::int_type; usingTraitsType=std::streambuf::traits_type; public: explicitCharOutputStreamBuf(TStream*stream_):stream{stream_},buffer{}{ setp(buffer.data(),buffer.data()+buffer.size()-1ul); } ~CharOutputStreamBuf(){ sync(); } std::streamsizexsputn(constCharType*source,std::streamsizesize)override{ //Writeasequenceofcharacters if(size<=0){ return0; } //Ifthere'sspace,writedataintobuffer conststd::ptrdiff_tspaceLeft=epptr()-pptr(); if(size<spaceLeft){ std::memcpy(pptr(),source,static_cast<std::size_t>(size)); pbump(static_cast<IntType>(size)); returnsize; } //Nospaceinbuffer,flushitfirst,andwritedirectlyintostream if(sync()){ return0; } returnstatic_cast<std::streamsize>(stream->write(source,static_cast<std::size_t>(size))); } IntTypeoverflow(IntTypevalue)override{ //Writeasinglecharacter if(value==TraitsType::eof()){ returnvalue; } constCharTypedata=static_cast<CharType>(value); *pptr()=data; pbump(1); return(sync()?TraitsType::eof():value); } IntTypesync()override{ conststd::ptrdiff_tdiff=pptr()-pbase(); if(diff>0){ conststd::size_tbytesToWrite=static_cast<std::size_t>(diff); conststd::size_tbytesWritten=stream->write(buffer.data(),bytesToWrite); pbump(static_cast<IntType>(-diff)); return(bytesToWrite==bytesWritten?0:-1); } return0; } private: TStream*stream; std::array<char,BufferSize>buffer; }; }//namespaceterse