TPIE

11a2c2d
file.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; c-file-style: "stroustrup"; -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2009, 2010, 2012, 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_H
20 #define _TPIE_FILE_H
21 
25 
26 #include <limits>
27 #include <tpie/file_base.h>
28 namespace tpie {
29 
30 
31 #ifdef _MSC_VER
32 #pragma warning( disable: 4200 )
33 #endif //_MSC_VER
34 
38 template <typename T>
39 class file: public file_base {
40 public:
42  typedef T item_type;
43 
47  static inline memory_size_type memory_usage(bool includeDefaultFileAccessor=true) {
48  memory_size_type x = sizeof(file);
49  if (includeDefaultFileAccessor)
51  return x;
52  }
53 
63  file(double blockFactor=1.0,
64  file_accessor::file_accessor * fileAccessor=NULL):
65  file_base(sizeof(T), blockFactor, fileAccessor) {};
66 
71  class stream: public file_base::stream {
72  public:
74  typedef T item_type;
76  typedef file file_type;
77  private:
79  typedef typename file::block_t block_t;
80  public:
84  inline static memory_size_type memory_usage(double blockFactor=1.0) {
85  return sizeof(stream) + block_size(blockFactor) + sizeof(block_t);
86  }
87 
88  stream() {}
89 
90  stream(file_type & file, stream_size_type offset=0):
91  file_base::stream(file, offset) {}
92 
93 
101  inline item_type & read_mutable() {
102  assert(get_file().is_open());
103  if (m_index >= m_block->size) {
104  update_block();
105  if (offset() >= get_file().size()) {
106  throw end_of_stream_exception();
107  }
108  }
109  return reinterpret_cast<T*>(m_block->data)[m_index++];
110  }
111 
126  inline const item_type & read() {
127  return read_mutable();
128  }
129 
143  inline const item_type & read_back() {
144  assert(get_file().is_open());
145  seek(-1, current);
146  const item_type & i = read();
147  seek(-1, current);
148  return i;
149  }
150 
156  inline void write(const item_type& item) {
157  assert(get_file().is_open());
158 #ifndef NDEBUG
159  if (!get_file().is_writable())
160  throw io_exception("Cannot write to read only stream");
161 #endif
162  if (m_index >= block_items()) update_block();
163  reinterpret_cast<T*>(m_block->data)[m_index++] = item;
164  write_update();
165  }
166 
171  template <typename IT>
172  inline void write(const IT & start, const IT & end) {
173  assert(get_file().is_open());
174  write_array(*this, start, end);
175  }
176 
181  template <typename IT>
182  inline void read(const IT & start, const IT & end) {
183  assert(get_file().is_open());
184  read_array(*this, start, end);
185  }
186 
190  inline void attach(file & f) {
191  attach_inner(f);
192  }
193 
197  inline void detach() {
198  detach_inner();
199  }
200  };
201 };
202 }
203 #endif //_TPIE_FILE_H
tpie::file_base::stream::write_update
void write_update()
Call whenever the current block buffer is modified.
Definition: file_base.h:140
tpie::file::stream::read_back
const item_type & read_back()
Read an item from the stream.
Definition: file.h:143
tpie::file_base::block_t
This is the type of our block buffers.
Definition: file_base.h:59
tpie::file
Central file abstraction.
Definition: file.h:39
tpie::stream_crtp< stream >::read_array
static void read_array(Stream &stream, const IT &start, const IT &end)
Reads several items from the stream.
Definition: stream_crtp.h:166
tpie::stream_crtp< stream >::seek
void seek(stream_offset_type offset, offset_type whence=beginning)
Moves the logical offset in the stream.
Definition: stream_crtp.h:51
tpie::file::stream::write
void write(const item_type &item)
Write an item to the stream.
Definition: file.h:156
tpie::file_base::stream::block_items
memory_size_type block_items() const
Fetch number of items per block.
Definition: file_base.h:130
tpie::file::stream::read
const item_type & read()
Read an item from the stream.
Definition: file.h:126
tpie::stream_crtp< stream >::m_index
memory_size_type m_index
Item index into the current block, or maxint if we don't have a block.
Definition: stream_crtp.h:245
tpie::file_base::stream::m_block
block_t * m_block
Current block.
Definition: file_base.h:107
tpie::file_base_crtp< file_base >::is_writable
bool is_writable() const
Check if we can write to the file.
Definition: file_base_crtp.h:66
tpie::file::stream::detach
void detach()
Detach from a tpie::file.
Definition: file.h:197
tpie::end_of_stream_exception
Definition: exception.h:62
tpie::file::stream::memory_usage
static memory_size_type memory_usage(double blockFactor=1.0)
Calculate the memory usage of a stream.
Definition: file.h:84
tpie::file::stream::attach
void attach(file &f)
Attach to the given tpie::file. If necessary, detach first.
Definition: file.h:190
tpie::file::stream::item_type
T item_type
Type of items stored in the stream.
Definition: file.h:74
tpie::stream_crtp< stream >::size
stream_size_type size() const
Get the size of the file measured in items.
Definition: stream_crtp.h:133
tpie::file::file
file(double blockFactor=1.0, file_accessor::file_accessor *fileAccessor=NULL)
Construct a file object with the given block factor and file accessor.
Definition: file.h:63
tpie::file_base_crtp< file_base >::block_size
memory_size_type block_size() const
Get the size of a block in bytes.
Definition: file_base_crtp.h:113
tpie::stream_crtp< stream >::update_block
void update_block()
Fetch block from disk as indicated by m_nextBlock, writing old block to disk if needed.
tpie::stream_crtp< stream >::write_array
static void write_array(Stream &stream, const IT &start, const IT &end)
Write several items to the stream.
Definition: stream_crtp.h:212
tpie::file_base_crtp< file_base >::is_open
bool is_open() const
Check if file is open.
Definition: file_base_crtp.h:257
tpie::file::stream::read_mutable
item_type & read_mutable()
Read a mutable item from the stream.
Definition: file.h:101
tpie::file::stream
Central stream abstraction.
Definition: file.h:71
file_base.h
tpie::file_accessor::stream_accessor_base::memory_usage
static memory_size_type memory_usage()
Return memory usage of this file accessor.
Definition: stream_accessor_base.h:177
tpie::file_accessor::stream_accessor_base
Definition: stream_accessor_base.h:34
tpie::file::stream::file_type
file file_type
Type of underlying file object.
Definition: file.h:76
tpie::file::item_type
T item_type
Type of items stored in the file.
Definition: file.h:42
tpie::file::stream::write
void write(const IT &start, const IT &end)
Write several items to the stream.
Definition: file.h:172
tpie::file_base::stream::attach_inner
void attach_inner(file_base &f)
Attach to the given tpie::file. If necessary, detach first.
tpie::file::stream::read
void read(const IT &start, const IT &end)
Reads several items from the stream.
Definition: file.h:182
tpie::file_base::stream
Stream in file. We support multiple streams per file.
Definition: file_base.h:86
tpie::file_base::stream::detach_inner
void detach_inner()
Detach from a tpie::file.
tpie::io_exception
Definition: exception.h:50
tpie::file_base
Definition: file_base.h:48
tpie
Definition: access_type.h:26
tpie::file::memory_usage
static memory_size_type memory_usage(bool includeDefaultFileAccessor=true)
Calculate the memory usage of a file.
Definition: file.h:47
tpie::stream_crtp< stream >::offset
stream_size_type offset() const
Calculate the current offset in the stream.
Definition: stream_crtp.h:91