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