Stxxl  1.4.0
include/stxxl/bits/mng/buf_ostream.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  include/stxxl/bits/mng/buf_ostream.h
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2002-2004 Roman Dementiev <dementiev@mpi-sb.mpg.de>
00007  *
00008  *  Distributed under the Boost Software License, Version 1.0.
00009  *  (See accompanying file LICENSE_1_0.txt or copy at
00010  *  http://www.boost.org/LICENSE_1_0.txt)
00011  **************************************************************************/
00012 
00013 #ifndef STXXL_BUF_OSTREAM_HEADER
00014 #define STXXL_BUF_OSTREAM_HEADER
00015 
00016 #include <stxxl/bits/mng/buf_writer.h>
00017 
00018 
00019 __STXXL_BEGIN_NAMESPACE
00020 
00021 //! \addtogroup schedlayer
00022 //! \{
00023 
00024 
00025 //! \brief Buffered output stream
00026 //!
00027 //! Writes data records to the stream of blocks.
00028 //! \remark Writing performed in the background, i.e. with overlapping of I/O and computation
00029 template <typename BlkTp_, typename BIDIteratorTp_>
00030 class buf_ostream
00031 {
00032 public:
00033     typedef BlkTp_ block_type;
00034     typedef BIDIteratorTp_ bid_iterator_type;
00035 
00036 protected:
00037     buffered_writer<block_type> writer;
00038     bid_iterator_type current_bid;
00039     int_type current_elem;
00040     block_type * current_blk;
00041 
00042 public:
00043     typedef typename block_type::const_reference const_reference;
00044     typedef typename block_type::reference reference;
00045     typedef buf_ostream<block_type, bid_iterator_type> _Self;
00046 
00047     //! \brief Constructs output stream object
00048     //! \param first_bid \c bid_iterator pointing to the first block of the stream
00049     //! \param nbuffers number of buffers for internal use
00050     buf_ostream(bid_iterator_type first_bid, int_type nbuffers) :
00051         writer(nbuffers, nbuffers / 2), current_bid(first_bid),
00052         current_elem(0)
00053     {
00054         current_blk = writer.get_free_block();
00055     }
00056 
00057     //! \brief Output stream operator, writes out \c record
00058     //! \param record const reference to block record type, containing a value of record to write to the stream
00059     //! \return reference to itself (stream object)
00060     _Self & operator << (const_reference record)
00061     {
00062         current_blk->elem[current_elem++] = record;
00063         if (UNLIKELY(current_elem >= block_type::size))
00064         {
00065             current_elem = 0;
00066             current_blk = writer.write(current_blk, *(current_bid++));
00067         }
00068         return (*this);
00069     }
00070 
00071     //! \brief Returns reference to the current record
00072     //! \return reference to the current record
00073     reference current()
00074     {
00075         return current_blk->elem[current_elem];
00076     }
00077 
00078     //! \brief Returns reference to the current record
00079     //! \return reference to the current record
00080     reference operator * ()
00081     {
00082         return current_blk->elem[current_elem];
00083     }
00084 
00085     //! \brief Moves to the next record in the stream
00086     //! \return reference to itself after the advance
00087     _Self& operator ++ ()
00088     {
00089         ++current_elem;
00090         if (UNLIKELY(current_elem >= block_type::size))
00091         {
00092             current_elem = 0;
00093             current_blk = writer.write(current_blk, *(current_bid++));
00094         }
00095         return (*this);
00096     }
00097 
00098     //! \brief fill current block with padding and flush
00099     _Self& fill(const_reference record)
00100     {
00101         while (current_elem != 0)
00102         {
00103             operator<< (record);
00104         }
00105         return *this;
00106     }
00107 
00108     //! \brief Deallocates internal objects
00109     virtual ~buf_ostream()
00110     {
00111         assert(current_elem == 0);
00112     }
00113 };
00114 
00115 //! \}
00116 
00117 __STXXL_END_NAMESPACE
00118 
00119 #endif // !STXXL_BUF_OSTREAM_HEADER
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines