Stxxl  1.3.2
buf_ostream.h
1 /***************************************************************************
2  * include/stxxl/bits/mng/buf_ostream.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002-2004 Roman Dementiev <[email protected]>
7  *
8  * Distributed under the Boost Software License, Version 1.0.
9  * (See accompanying file LICENSE_1_0.txt or copy at
10  * http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
13 #ifndef STXXL_BUF_OSTREAM_HEADER
14 #define STXXL_BUF_OSTREAM_HEADER
15 
16 #include <stxxl/bits/mng/buf_writer.h>
17 
18 
19 __STXXL_BEGIN_NAMESPACE
20 
23 
24 
29 template <typename BlkTp_, typename BIDIteratorTp_>
31 {
32  typedef BlkTp_ block_type;
33  typedef BIDIteratorTp_ bid_iterator_type;
34 
35 protected:
37  bid_iterator_type current_bid;
38  int_type current_elem;
39  block_type * current_blk;
40 
41 public:
42  typedef typename block_type::const_reference const_reference;
43  typedef typename block_type::reference reference;
45 
49  buf_ostream(bid_iterator_type first_bid, int_type nbuffers) :
50  writer(nbuffers, nbuffers / 2), current_bid(first_bid),
51  current_elem(0)
52  {
53  current_blk = writer.get_free_block();
54  }
55 
59  _Self & operator << (const_reference record)
60  {
61  current_blk->elem[current_elem++] = record;
62  if (current_elem >= block_type::size)
63  {
64  current_elem = 0;
65  current_blk = writer.write(current_blk, *(current_bid++));
66  }
67  return (*this);
68  }
69 
72  reference current()
73  {
74  return current_blk->elem[current_elem];
75  }
76 
79  reference operator * ()
80  {
81  return current_blk->elem[current_elem];
82  }
83 
87  {
88  ++current_elem;
89  if (current_elem >= block_type::size)
90  {
91  current_elem = 0;
92  current_blk = writer.write(current_blk, *(current_bid++));
93  }
94  return (*this);
95  }
96 
97 
99  virtual ~buf_ostream()
100  {
101  assert(current_elem == 0);
102  }
103 };
104 
106 
107 __STXXL_END_NAMESPACE
108 
109 #endif // !STXXL_BUF_OSTREAM_HEADER
Encapsulates asynchronous buffered block writing engine.
Definition: buf_writer.h:37
_Self & operator++()
Moves to the next record in the stream.
Definition: buf_ostream.h:86
virtual ~buf_ostream()
Deallocates internal objects.
Definition: buf_ostream.h:99
buf_ostream(bid_iterator_type first_bid, int_type nbuffers)
Constructs output stream object.
Definition: buf_ostream.h:49
Buffered output stream.
Definition: buf_ostream.h:30
_Self & operator<<(const_reference record)
Output stream operator, writes out record.
Definition: buf_ostream.h:59
reference current()
Returns reference to the current record.
Definition: buf_ostream.h:72
reference operator*()
Returns reference to the current record.
Definition: buf_ostream.h:79