Thrill  0.1
buf_ostream.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * foxxll/mng/buf_ostream.hpp
3  *
4  * Part of FOXXLL. See http://foxxll.org
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 FOXXLL_MNG_BUF_OSTREAM_HEADER
14 #define FOXXLL_MNG_BUF_OSTREAM_HEADER
15 
17 
18 #include <tlx/define/likely.hpp>
19 
20 namespace foxxll {
21 
22 //! \addtogroup foxxll_schedlayer
23 //! \{
24 
25 //! Buffered output stream.
26 //!
27 //! Writes data records to the stream of blocks.
28 //! \remark Writing performed in the background, i.e. with overlapping of I/O and computation
29 template <typename BlockType, typename BidIteratorType>
31 {
32 public:
33  using block_type = BlockType;
34  using bid_iterator_type = BidIteratorType;
35 
36 protected:
39  size_t current_elem;
41 
42 public:
43  using const_reference = typename block_type::const_reference;
44  using reference = typename block_type::reference;
46 
47  //! Constructs output stream object.
48  //! \param first_bid \c bid_iterator pointing to the first block of the stream
49  //! \param nbuffers number of buffers for internal use
50  buf_ostream(bid_iterator_type first_bid, size_t nbuffers)
51  : writer(nbuffers, nbuffers / 2), current_bid(first_bid),
52  current_elem(0)
53  {
54  current_blk = writer.get_free_block();
55  }
56 
57  //! non-copyable: delete copy-constructor
58  buf_ostream(const buf_ostream&) = delete;
59  //! non-copyable: delete assignment operator
60  buf_ostream& operator = (const buf_ostream&) = delete;
61 
62  //! Output stream operator, writes out \c record.
63  //! \param record const reference to block record type, containing a value of record to write to the stream
64  //! \return reference to itself (stream object)
66  {
67  current_blk->elem[current_elem++] = record;
68  if (TLX_UNLIKELY(current_elem >= block_type::size))
69  {
70  current_elem = 0;
71  current_blk = writer.write(current_blk, *(current_bid++));
72  }
73  return *this;
74  }
75 
76  //! Returns reference to the current record.
77  //! \return reference to the current record
79  {
80  return current_blk->elem[current_elem];
81  }
82 
83  //! Returns reference to the current record.
84  //! \return reference to the current record
86  {
87  return current_blk->elem[current_elem];
88  }
89 
90  //! Moves to the next record in the stream.
91  //! \return reference to itself after the advance
93  {
94  ++current_elem;
95  if (TLX_UNLIKELY(current_elem >= block_type::size))
96  {
97  current_elem = 0;
98  current_blk = writer.write(current_blk, *(current_bid++));
99  }
100  return *this;
101  }
102 
103  //! Fill current block with padding and flush
105  {
106  while (current_elem != 0)
107  {
108  operator << (record);
109  }
110  return *this;
111  }
112 
113  //! Force flush of current block, for finishing writing within a block.
114  //! \warning Use with caution as the block may contain uninitialized data
116  {
117  current_elem = 0;
118  current_blk = writer.write(current_blk, *(current_bid++));
119  return *this;
120  }
121 
122  //! Deallocates internal objects.
124  {
125  assert(current_elem == 0);
126  }
127 };
128 
129 //! \}
130 
131 } // namespace foxxll
132 
133 #endif // !FOXXLL_MNG_BUF_OSTREAM_HEADER
134 
135 /**************************************************************************/
self_type & operator++()
Definition: buf_ostream.hpp:92
buffered_writer< block_type > writer
Definition: buf_ostream.hpp:37
typename block_type::reference reference
Definition: buf_ostream.hpp:44
block_type * get_free_block()
Definition: buf_writer.hpp:98
self_type & fill(const_reference record)
Fill current block with padding and flush.
#define TLX_UNLIKELY(c)
Definition: likely.hpp:24
reference current()
Definition: buf_ostream.hpp:78
typename block_type::const_reference const_reference
Definition: buf_ostream.hpp:43
buf_ostream & operator=(const buf_ostream &)=delete
non-copyable: delete assignment operator
~buf_ostream()
Deallocates internal objects.
FOXXLL library namespace
reference operator*()
Definition: buf_ostream.hpp:85
buf_ostream(bid_iterator_type first_bid, size_t nbuffers)
Definition: buf_ostream.hpp:50
self_type & operator<<(const_reference record)
Definition: buf_ostream.hpp:65
bid_iterator_type current_bid
Definition: buf_ostream.hpp:38
block_type * write(block_type *filled_block, const bid_type &bid)
Definition: buf_writer.hpp:137
block_type * current_blk
Definition: buf_ostream.hpp:40
self_type & flush()
BidIteratorType bid_iterator_type
Definition: buf_ostream.hpp:34