/* ----------------------------------------------------------------------- *//** * * @file OutputStreamBufferBase_impl.hpp * *//* ----------------------------------------------------------------------- */ // Workaround for Doxygen: Ignore if not included by EigenLinAlgTypes.hpp #ifdef MADLIB_DBAL_HPP /** * @internal One extra byte is allocated for the terminating null character. */ template OutputStreamBufferBase::OutputStreamBufferBase() : mStorageSize(kInitialBufferSize), mStorage(new C[kInitialBufferSize + 1]) { setp(mStorage, mStorage + mStorageSize); } template OutputStreamBufferBase::~OutputStreamBufferBase() { delete mStorage; } /** * @brief Output a string * * Subclasses are required to implement this method and to feed the message * the the the DBMS-specific logging routine. * @param inMsg Null-terminated string to be output. * @param inLength Length of inMsg (for convenience). */ template void OutputStreamBufferBase::output(C* inMsg, uint32_t inLength) const { static_cast(this)->output(inMsg, inLength); } /** * @brief Handle case when stream receives a character that does not fit * into the current buffer any more * * This function will allocate a new buffer of twice the old buffer size. If * the buffer has already the maximum size kMaxBufferSize, eof is returned * to indicate that the buffer cannot take any more input before a flush. */ template typename OutputStreamBufferBase::int_type OutputStreamBufferBase::overflow(int_type c) { if (this->pptr() >= this->epptr()) { if (mStorageSize >= kMaxBufferSize) return traits_type::eof(); uint32_t newStorageSize = mStorageSize * 2; C* newStorage = new C[newStorageSize + 1]; std::memcpy(newStorage, mStorage, mStorageSize); delete mStorage; mStorage = newStorage; madlib_assert( this->pptr() == this->epptr() && this->pptr() - this->pbase() == static_cast(mStorageSize), std::logic_error("Internal error: Logging buffer has become " "inconsistent")); setp(mStorage, mStorage + newStorageSize); this->pbump(mStorageSize); mStorageSize = newStorageSize; } else if (c == traits_type::eof()) return traits_type::eof(); *this->pptr() = c; this->pbump(1); return traits_type::not_eof(c); } /** * @brief Flush and reset buffer. */ template int OutputStreamBufferBase::sync() { int length = this->pptr() - this->pbase(); mStorage[length] = '\0'; output(mStorage, length); setp(mStorage, mStorage + mStorageSize); return 0; } #endif // MADLIB_DBAL_HPP (workaround for Doxygen)