00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
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 
00023 
00024 
00029 template <typename BlkTp_, typename BIDIteratorTp_>
00030 class buf_ostream
00031 {
00032     typedef BlkTp_ block_type;
00033     typedef BIDIteratorTp_ bid_iterator_type;
00034 
00035 protected:
00036     buffered_writer<block_type> writer;
00037     bid_iterator_type current_bid;
00038     int_type current_elem;
00039     block_type * current_blk;
00040 
00041 public:
00042     typedef typename block_type::const_reference const_reference;
00043     typedef typename block_type::reference reference;
00044     typedef buf_ostream<block_type, bid_iterator_type> _Self;
00045 
00049     buf_ostream(bid_iterator_type first_bid, int_type nbuffers) :
00050         writer(nbuffers, nbuffers / 2), current_bid(first_bid),
00051         current_elem(0)
00052     {
00053         current_blk = writer.get_free_block();
00054     }
00055 
00059     _Self & operator << (const_reference record)
00060     {
00061         current_blk->elem[current_elem++] = record;
00062         if (current_elem >= block_type::size)
00063         {
00064             current_elem = 0;
00065             current_blk = writer.write(current_blk, *(current_bid++));
00066         }
00067         return (*this);
00068     }
00069 
00072     reference current()
00073     {
00074         return current_blk->elem[current_elem];
00075     }
00076 
00079     reference operator * ()
00080     {
00081         return current_blk->elem[current_elem];
00082     }
00083 
00086     _Self & operator ++ ()
00087     {
00088         ++current_elem;
00089         if (current_elem >= block_type::size)
00090         {
00091             current_elem = 0;
00092             current_blk = writer.write(current_blk, *(current_bid++));
00093         }
00094         return (*this);
00095     }
00096 
00097 
00099     virtual ~buf_ostream()
00100     {
00101         assert(current_elem == 0);
00102     }
00103 };
00104 
00106 
00107 __STXXL_END_NAMESPACE
00108 
00109 #endif // !STXXL_BUF_OSTREAM_HEADER