STXXL  1.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
buf_ostream.h
Go to the documentation of this file.
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_MNG_BUF_OSTREAM_HEADER
14 #define STXXL_MNG_BUF_OSTREAM_HEADER
15 
16 #include <stxxl/bits/noncopyable.h>
18 
20 
21 //! \addtogroup schedlayer
22 //! \{
23 
24 //! Buffered output stream.
25 //!
26 //! Writes data records to the stream of blocks.
27 //! \remark Writing performed in the background, i.e. with overlapping of I/O and computation
28 template <typename BlockType, typename BidIteratorType>
29 class buf_ostream : private noncopyable
30 {
31 public:
32  typedef BlockType block_type;
33  typedef BidIteratorType bid_iterator_type;
34 
35 protected:
40 
41 public:
42  typedef typename block_type::const_reference const_reference;
43  typedef typename block_type::reference reference;
45 
46  //! Constructs output stream object.
47  //! \param first_bid \c bid_iterator pointing to the first block of the stream
48  //! \param nbuffers number of buffers for internal use
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 
56  //! Output stream operator, writes out \c record.
57  //! \param record const reference to block record type, containing a value of record to write to the stream
58  //! \return reference to itself (stream object)
60  {
61  current_blk->elem[current_elem++] = record;
62  if (UNLIKELY(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 
70  //! Returns reference to the current record.
71  //! \return reference to the current record
73  {
74  return current_blk->elem[current_elem];
75  }
76 
77  //! Returns reference to the current record.
78  //! \return reference to the current record
79  reference operator * ()
80  {
81  return current_blk->elem[current_elem];
82  }
83 
84  //! Moves to the next record in the stream.
85  //! \return reference to itself after the advance
87  {
88  ++current_elem;
89  if (UNLIKELY(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  //! Fill current block with padding and flush
99  {
100  while (current_elem != 0)
101  {
102  operator << (record);
103  }
104  return *this;
105  }
106 
107  //! Force flush of current block, for finishing writing within a block.
108  //! \warning Use with caution as the block may contain uninitialized data
110  {
111  current_elem = 0;
112  current_blk = writer.write(current_blk, *(current_bid++));
113  return *this;
114  }
115 
116  //! Deallocates internal objects.
118  {
119  assert(current_elem == 0);
120  }
121 };
122 
123 //! \}
124 
126 
127 #endif // !STXXL_MNG_BUF_OSTREAM_HEADER
BidIteratorType bid_iterator_type
Definition: buf_ostream.h:33
friend std::ostream & operator<<(std::ostream &os, const uint_pair &a)
make a uint_pair outputtable via iostreams, using unsigned long long.
Definition: uint_types.h:228
~buf_ostream()
Deallocates internal objects.
Definition: buf_ostream.h:117
Buffered output stream.
Definition: buf_ostream.h:29
self_type & flush()
Force flush of current block, for finishing writing within a block.
Definition: buf_ostream.h:109
uint_pair & operator++()
prefix increment operator (directly manipulates the integer parts)
Definition: uint_types.h:163
self_type & fill(const_reference record)
Fill current block with padding and flush.
Definition: buf_ostream.h:98
bid_iterator_type current_bid
Definition: buf_ostream.h:37
choose_int_types< my_pointer_size >::int_type int_type
Definition: types.h:63
int_type current_elem
Definition: buf_ostream.h:38
#define UNLIKELY(c)
Definition: utils.h:225
reference current()
Returns reference to the current record.
Definition: buf_ostream.h:72
buf_ostream< block_type, bid_iterator_type > self_type
Definition: buf_ostream.h:44
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
block_type * current_blk
Definition: buf_ostream.h:39
BlockType block_type
Definition: buf_ostream.h:32
block_type::reference reference
Definition: buf_ostream.h:43
buffered_writer< block_type > writer
Definition: buf_ostream.h:36
buf_ostream(bid_iterator_type first_bid, int_type nbuffers)
Constructs output stream object.
Definition: buf_ostream.h:49
block_type::const_reference const_reference
Definition: buf_ostream.h:42
#define STXXL_END_NAMESPACE
Definition: namespace.h:17