/* ----------------------------------------------------------------------- *//** * * @file ByteStreamHandleBuf_impl.hpp * *//* ----------------------------------------------------------------------- */ #ifndef MADLIB_DBAL_BYTESTREAMHANDLEBUF_IMPL_HPP #define MADLIB_DBAL_BYTESTREAMHANDLEBUF_IMPL_HPP namespace madlib { namespace dbal { template ByteStreamHandleBuf::ByteStreamHandleBuf( size_t inSize) : mStorage(defaultAllocator().allocateByteString< dbal::FunctionContext, dbal::DoZero, dbal::ThrowBadAlloc>(inSize)), mPos(0) { } template ByteStreamHandleBuf::ByteStreamHandleBuf( const Storage_type& inStorage) : mStorage(inStorage), mPos(0) { } template size_t ByteStreamHandleBuf::seek( size_t inPos) { mPos = inPos; if (inPos > size()) return size_t(-1); else return mPos; } template const typename ByteStreamHandleBuf::char_type* ByteStreamHandleBuf::ptr() const { return storage().ptr(); } template size_t ByteStreamHandleBuf::size() const { return storage().size(); } template size_t ByteStreamHandleBuf::tell() const { return mPos; } template void ByteStreamHandleBuf::setStorage( Storage_type& inStorage) { mStorage = inStorage; } template typename ByteStreamHandleBuf::Storage_type& ByteStreamHandleBuf::storage() { return mStorage; } template const typename ByteStreamHandleBuf::Storage_type& ByteStreamHandleBuf::storage() const { return mStorage; } // ByteStreamHandleBuf template ByteStreamHandleBuf::ByteStreamHandleBuf( size_t inSize) : Base(inSize) { } template ByteStreamHandleBuf::ByteStreamHandleBuf( const Storage_type& inStorage) : Base(inStorage) { } template typename ByteStreamHandleBuf::char_type* ByteStreamHandleBuf::ptr() { return const_cast(static_cast(this)->ptr()); } template void ByteStreamHandleBuf::resize( size_t inSize, size_t inPivot) { if (inSize == this->size()) return; char_type* oldPtr = this->ptr(); size_t secondChunkStart = inPivot > this->size() ? this->size() : inPivot; size_t oldSize = this->size(); ptrdiff_t delta = inSize - oldSize; // FIXME: Deallocate old memory! this->mStorage = defaultAllocator().allocateByteString< dbal::FunctionContext, dbal::DoZero, dbal::ThrowBadAlloc>(inSize); std::copy(oldPtr, oldPtr + secondChunkStart, this->ptr()); std::copy(oldPtr + secondChunkStart, oldPtr + oldSize, this->ptr() + secondChunkStart + delta); std::fill(this->ptr() + secondChunkStart, this->ptr() + secondChunkStart + delta, 0); } } // namespace dbal } // namespace madlib #endif // defined(MADLIB_DBAL_BYTESTREAMHANDLEBUF_IMPL_HPP)