STXXL  1.4.0
 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 
19 
21 
22 //! \addtogroup schedlayer
23 //! \{
24 
25 
26 //! Buffered output stream.
27 //!
28 //! Writes data records to the stream of blocks.
29 //! \remark Writing performed in the background, i.e. with overlapping of I/O and computation
30 template <typename BlockType, typename BIDIteratorType>
31 class buf_ostream : private noncopyable
32 {
33 public:
34  typedef BlockType block_type;
35  typedef BIDIteratorType bid_iterator_type;
36 
37 protected:
42 
43 public:
44  typedef typename block_type::const_reference const_reference;
45  typedef typename block_type::reference reference;
47 
48  //! Constructs output stream object.
49  //! \param first_bid \c bid_iterator pointing to the first block of the stream
50  //! \param nbuffers number of buffers for internal use
51  buf_ostream(bid_iterator_type first_bid, int_type nbuffers) :
52  writer(nbuffers, nbuffers / 2), current_bid(first_bid),
53  current_elem(0)
54  {
55  current_blk = writer.get_free_block();
56  }
57 
58  //! Output stream operator, writes out \c record.
59  //! \param record const reference to block record type, containing a value of record to write to the stream
60  //! \return reference to itself (stream object)
62  {
63  current_blk->elem[current_elem++] = record;
64  if (UNLIKELY(current_elem >= block_type::size))
65  {
66  current_elem = 0;
67  current_blk = writer.write(current_blk, *(current_bid++));
68  }
69  return *this;
70  }
71 
72  //! Returns reference to the current record.
73  //! \return reference to the current record
75  {
76  return current_blk->elem[current_elem];
77  }
78 
79  //! Returns reference to the current record.
80  //! \return reference to the current record
81  reference operator * ()
82  {
83  return current_blk->elem[current_elem];
84  }
85 
86  //! Moves to the next record in the stream.
87  //! \return reference to itself after the advance
89  {
90  ++current_elem;
91  if (UNLIKELY(current_elem >= block_type::size))
92  {
93  current_elem = 0;
94  current_blk = writer.write(current_blk, *(current_bid++));
95  }
96  return *this;
97  }
98 
99  //! Fill current block with padding and flush
101  {
102  while (current_elem != 0)
103  {
104  operator << (record);
105  }
106  return *this;
107  }
108 
109  //! Force flush of current block, for finishing writing within a block.
110  //! \warning Use with caution as the block may contain uninitialized data
112  {
113  current_elem = 0;
114  current_blk = writer.write(current_blk, *(current_bid++));
115  return *this;
116  }
117 
118  //! Deallocates internal objects.
120  {
121  assert(current_elem == 0);
122  }
123 };
124 
125 //! \}
126 
128 
129 #endif // !STXXL_MNG_BUF_OSTREAM_HEADER
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:231
buf_ostream< block_type, bid_iterator_type > _Self
Definition: buf_ostream.h:46
int_type current_elem
Definition: buf_ostream.h:40
_Self & flush()
Force flush of current block, for finishing writing within a block.
Definition: buf_ostream.h:111
Buffered output stream.
Definition: buf_ostream.h:31
buf_ostream(bid_iterator_type first_bid, int_type nbuffers)
Constructs output stream object.
Definition: buf_ostream.h:51
uint_pair & operator++()
prefix increment operator (directly manipulates the integer parts)
Definition: uint_types.h:166
BIDIteratorType bid_iterator_type
Definition: buf_ostream.h:35
block_type::reference reference
Definition: buf_ostream.h:45
choose_int_types< my_pointer_size >::int_type int_type
Definition: types.h:66
~buf_ostream()
Deallocates internal objects.
Definition: buf_ostream.h:119
BlockType block_type
Definition: buf_ostream.h:34
#define UNLIKELY(c)
Definition: utils.h:226
buffered_writer< block_type > writer
Definition: buf_ostream.h:38
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
block_type::const_reference const_reference
Definition: buf_ostream.h:44
reference current()
Returns reference to the current record.
Definition: buf_ostream.h:74
bid_iterator_type current_bid
Definition: buf_ostream.h:39
_Self & fill(const_reference record)
Fill current block with padding and flush.
Definition: buf_ostream.h:100
Encapsulates asynchronous buffered block writing engine.
Definition: buf_writer.h:38
block_type * current_blk
Definition: buf_ostream.h:41
#define STXXL_END_NAMESPACE
Definition: namespace.h:17