/* ----------------------------------------------------------------------- *//** * * @file ByteStream_impl.hpp * *//* ----------------------------------------------------------------------- */ #ifndef MADLIB_DBAL_BYTESTREAM_IMPL_HPP #define MADLIB_DBAL_BYTESTREAM_IMPL_HPP namespace madlib { namespace dbal { // ByteStream::DryRun template class TypeTraits, bool IsMutable> ByteStream::DryRun::DryRun( ByteStream& inStream) : mStream(inStream), mIsIn(true) { mStream.enterDryRun(); } template class TypeTraits, bool IsMutable> ByteStream::DryRun::~DryRun() { leave(); } template class TypeTraits, bool IsMutable> void ByteStream::DryRun::leave() { if (mIsIn) { mStream.leaveDryRun(); mIsIn = false; } } // ByteStream template class TypeTraits, bool IsMutable> ByteStream::ByteStream( StreamBuf_type* inStreamBuf) : mStreamBuf(inStreamBuf), mDryRun(false) { } template class TypeTraits, bool IsMutable> template inline const T* ByteStream::read(size_t inCount) { this->seek::type>::alignment>( 0, std::ios_base::cur); const T* pointer = this->available() >= inCount * sizeof(T) ? reinterpret_cast(this->ptr() + this->tell()) : NULL; this->seek(inCount * sizeof(T), std::ios_base::cur); return pointer; } template class TypeTraits, bool IsMutable> template inline size_t ByteStream::seek(std::ptrdiff_t inOffset, std::ios_base::seekdir inDir) { BOOST_STATIC_ASSERT_MSG( (Alignment & (Alignment - 1)) == 0 && Alignment > 0, "Alignment must be a power of 2."); madlib_assert( reinterpret_cast(this->ptr()) % Alignment == 0, std::logic_error("ByteString improperly aligned for " "alignment request in seek().")); size_t newPos = inDir == std::ios_base::beg ? 0 : (inDir == std::ios_base::cur ? this->tell() : this->size()); if (inOffset >= 0) newPos += static_cast(inOffset); else if (static_cast(-inOffset) > newPos) newPos = 0; else newPos -= static_cast(-inOffset); newPos = ((newPos - 1) & ~(Alignment - 1)) + Alignment; return this->seek(newPos); } template class TypeTraits, bool IsMutable> inline size_t ByteStream::seek(std::ptrdiff_t inOffset, std::ios_base::seekdir inDir) { return seek<1>(inOffset, inDir); } template class TypeTraits, bool IsMutable> inline size_t ByteStream::seek(size_t inPos) { return mStreamBuf->seek(inPos); } template class TypeTraits, bool IsMutable> inline size_t ByteStream::size() const { return mStreamBuf->size(); } template class TypeTraits, bool IsMutable> inline std::ios_base::iostate ByteStream::rdstate() const { return (this->tell() <= this->size() ? std::ios_base::goodbit : std::ios_base::eofbit); } template class TypeTraits, bool IsMutable> inline bool ByteStream::eof() const { return rdstate() & std::ios_base::eofbit; } template class TypeTraits, bool IsMutable> inline size_t ByteStream::available() const { if (this->size() < this->tell()) return 0; else return this->size() - this->tell(); } template class TypeTraits, bool IsMutable> inline size_t ByteStream::tell() const { return mStreamBuf->tell(); } template class TypeTraits, bool IsMutable> inline const typename ByteStream::char_type* ByteStream::ptr() const { return mStreamBuf->ptr(); } template class TypeTraits, bool IsMutable> inline bool ByteStream::isInDryRun() const { return mDryRun > 0; } template class TypeTraits, bool IsMutable> inline void ByteStream::enterDryRun() { ++mDryRun; } template class TypeTraits, bool IsMutable> inline void ByteStream::leaveDryRun() { madlib_assert(mDryRun > 0, std::logic_error("Non-positive dry-mode " "counter detected.")); --mDryRun; } // ByteStream template class TypeTraits> ByteStream::ByteStream( StreamBuf_type* inStreamBuf) : Base(inStreamBuf) { } template class TypeTraits> template inline T* ByteStream::read(size_t inCount) { return const_cast(static_cast(this)->template read(inCount)); } // operator>>(ByteStream&, ...) template < class StreamBuf, template class TypeTraits, bool StreamBufIsMutable, bool ReferenceIsMutable, typename T> ByteStream& operator>>( ByteStream& inStream, Ref& inReference) { typedef typename Ref::val_type val_type; val_type* data = inStream.template read(); if (!inStream.isInDryRun()) inReference.rebind(data); return inStream; } template < class StreamBuf, template class TypeTraits, bool StreamBufIsMutable, bool TransparentHandleIsMutable, typename EigenType> ByteStream& operator>>( ByteStream& inStream, dbal::eigen_integration::HandleMap< EigenType, TransparentHandle >& inReference) { typedef typename TransparentHandle::val_type val_type; val_type* data = inStream.template read(inReference.size()); if (!inStream.isInDryRun()) inReference.rebind(data); return inStream; } } // namespace dbal } // namespace madlib #endif // defined(MADLIB_DBAL_BYTESTREAM_IMPL_HPP)