TPIE

v1.1rc1-6-g0c97303
file_stream.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2009, 2011, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 #ifndef __TPIE_FILE_STREAM_H__
20 #define __TPIE_FILE_STREAM_H__
21 #include <tpie/tempname.h>
22 #include <tpie/file.h>
23 #include <tpie/memory.h>
24 #include <tpie/file_stream_base.h>
30 
31 namespace tpie {
32 
33 
43 template <typename T>
45 public:
47  typedef T item_type;
48 
54  inline file_stream(double blockFactor=1.0,
55  file_accessor::file_accessor * fileAccessor=NULL):
56  file_stream_base(sizeof(item_type), blockFactor, fileAccessor) {};
57 
58 
64  inline void write(const item_type & item) throw(stream_exception) {
65  assert(m_open);
66 #ifndef NDEBUG
67  if (!is_writable())
68  throw io_exception("Cannot write to read only stream");
69 #endif
70  if (m_index >= m_blockItems) update_block();
71  reinterpret_cast<item_type*>(m_block.data)[m_index++] = item;
72  write_update();
73  }
74 
80  template <typename IT>
81  inline void write(const IT & start, const IT & end) throw(stream_exception) {
82  assert(m_open);
83  write_array(*this, start, end);
84  }
85 
91  inline const item_type & read() throw(stream_exception) {
92  assert(m_open);
93  if (m_index >= m_block.size) {
94  update_block();
95  if (offset() >= size()) {
97  }
98  }
99  return reinterpret_cast<item_type*>(m_block.data)[m_index++];
100  }
101 
107  template <typename IT>
108  inline void read(const IT & start, const IT & end) throw(stream_exception) {
109  assert(m_open);
110  read_array(*this, start, end);
111  }
112 
118  inline const item_type & read_back() throw(stream_exception) {
119  assert(m_open);
120  seek(-1, current);
121  const item_type & i = read();
122  seek(-1, current);
123  return i;
124  }
125 
134  inline static memory_size_type memory_usage(
135  float blockFactor=1.0,
136  bool includeDefaultFileAccessor=true) throw() {
137  // TODO
138  memory_size_type x = sizeof(file_stream);
139  x += block_memory_usage(blockFactor); // allocated in constructor
140  if (includeDefaultFileAccessor)
142  return x;
143  }
144 
145  void swap(file_stream<T> & other) {
146  file_stream_base::swap(other);
147  }
148 
149  friend struct stream_item_array_operations;
150 };
151 
152 } // namespace tpie
153 
154 namespace std {
155 
159 template <typename T>
160 void swap(tpie::file_stream<T> & a, tpie::file_stream<T> & b) {
161  a.swap(b);
162 }
163 
164 } // namespace std
165 
166 #endif //__TPIE_FILE_STREAM_H__
static void write_array(Stream &stream, const IT &start, const IT &end)
Write several items to the stream.
Definition: stream_crtp.h:211
static memory_size_type block_memory_usage(double blockFactor)
Amount of memory used by a single block given the block factor.
Memory management subsystem.
T item_type
The type of the items stored in the stream.
Definition: file_stream.h:47
static void read_array(Stream &stream, const IT &start, const IT &end)
Reads several items from the stream.
Definition: stream_crtp.h:165
void write(const IT &start, const IT &end)
Write several items to the stream.
Definition: file_stream.h:81
stream_size_type offset() const
Calculate the current offset in the stream.
Definition: stream_crtp.h:90
const item_type & read()
Read an item from the stream.
Definition: file_stream.h:91
const item_type & read_back()
Read an item from the stream.
Definition: file_stream.h:118
memory_size_type m_index
Item index into the current block, or maxint if we don't have a block.
Definition: stream_crtp.h:244
Streams that support substreams.
void write(const item_type &item)
Write an item to the stream.
Definition: file_stream.h:64
bool is_writable() const
Check if we can write to the file.
void seek(stream_offset_type offset, offset_type whence=beginning)
Moves the logical offset in the stream.
Definition: stream_crtp.h:50
static memory_size_type memory_usage()
Return memory usage of this file accessor.
static memory_size_type memory_usage(float blockFactor=1.0, bool includeDefaultFileAccessor=true)
Calculate the amount of memory used by a single file_stream.
Definition: file_stream.h:134
void read(const IT &start, const IT &end)
Reads several items from the stream.
Definition: file_stream.h:108
Simple class acting both as file and a file::stream.
Definition: file_stream.h:44
file_stream(double blockFactor=1.0, file_accessor::file_accessor *fileAccessor=NULL)
Construct a new file_stream.
Definition: file_stream.h:54
void update_block()
Fetch block from disk as indicated by m_nextBlock, writing old block to disk if needed.
Item type-agnostic file_stream operations.
Temporary file names.
stream_size_type size() const
Get the size of the file measured in items.
Definition: stream_crtp.h:132