TPIE

2362a60
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 #ifdef _MSC_VER
27 //Yes we know you do not support throw(stream_exception)
28 #pragma warning( disable: 4290 )
29 #endif //_MSC_VER
30 
31 #include <limits>
32 #include <tpie/file_base.h>
33 namespace tpie {
34 
35 
36 #ifdef _MSC_VER
37 #pragma warning( disable: 4200 )
38 #endif //_MSC_VER
39 
43 template <typename T>
44 class file: public file_base {
45 public:
47  typedef T item_type;
48 
52  static inline memory_size_type memory_usage(bool includeDefaultFileAccessor=true) {
53  memory_size_type x = sizeof(file);
54  if (includeDefaultFileAccessor)
56  return x;
57  }
58 
68  file(double blockFactor=1.0,
69  file_accessor::file_accessor * fileAccessor=NULL):
70  file_base(sizeof(T), blockFactor, fileAccessor) {};
71 
76  class stream: public file_base::stream {
77  public:
79  typedef T item_type;
81  typedef file file_type;
82  private:
84  typedef typename file::block_t block_t;
85  public:
89  inline static memory_size_type memory_usage(double blockFactor=1.0) {
90  return sizeof(stream) + block_size(blockFactor) + sizeof(block_t);
91  }
92 
93  stream() {}
94 
95  stream(file_type & file, stream_size_type offset=0):
96  file_base::stream(file, offset) {}
97 
98 
106  inline item_type & read_mutable() {
107  assert(get_file().is_open());
108  if (m_index >= m_block->size) {
109  update_block();
110  if (offset() >= get_file().size()) {
111  throw end_of_stream_exception();
112  }
113  }
114  return reinterpret_cast<T*>(m_block->data)[m_index++];
115  }
116 
131  inline const item_type & read() {
132  return read_mutable();
133  }
134 
148  inline const item_type & read_back() {
149  assert(get_file().is_open());
150  seek(-1, current);
151  const item_type & i = read();
152  seek(-1, current);
153  return i;
154  }
155 
161  inline void write(const item_type& item) throw(stream_exception) {
162  assert(get_file().is_open());
163 #ifndef NDEBUG
164  if (!get_file().is_writable())
165  throw io_exception("Cannot write to read only stream");
166 #endif
167  if (m_index >= block_items()) update_block();
168  reinterpret_cast<T*>(m_block->data)[m_index++] = item;
169  write_update();
170  }
171 
176  template <typename IT>
177  inline void write(const IT & start, const IT & end) {
178  assert(get_file().is_open());
179  write_array(*this, start, end);
180  }
181 
186  template <typename IT>
187  inline void read(const IT & start, const IT & end) {
188  assert(get_file().is_open());
189  read_array(*this, start, end);
190  }
191 
195  inline void attach(file & f) {
196  attach_inner(f);
197  }
198 
202  inline void detach() {
203  detach_inner();
204  }
205  };
206 };
207 }
208 #endif //_TPIE_FILE_H
static void write_array(Stream &stream, const IT &start, const IT &end)
Write several items to the stream.
Definition: stream_crtp.h:211
const item_type & read_back()
Read an item from the stream.
Definition: file.h:148
Central stream abstraction.
Definition: file.h:76
file file_type
Type of underlying file object.
Definition: file.h:81
static void read_array(Stream &stream, const IT &start, const IT &end)
Reads several items from the stream.
Definition: stream_crtp.h:165
memory_size_type block_items() const
Fetch number of items per block.
Definition: file_base.h:128
void write(const item_type &item)
Write an item to the stream.
Definition: file.h:161
Basic file and stream operations.
Central file abstraction.
Definition: file.h:44
stream_size_type offset() const
Calculate the current offset in the stream.
Definition: stream_crtp.h:90
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
T item_type
Type of items stored in the file.
Definition: file.h:47
void attach_inner(file_base &f)
Attach to the given tpie::file. If necessary, detach first.
T item_type
Type of items stored in the stream.
Definition: file.h:79
void detach_inner()
Detach from a tpie::file.
bool is_writable() const
Check if we can write to the file.
void write(const IT &start, const IT &end)
Write several items to the stream.
Definition: file.h:177
Stream in file. We support multiple streams per file.
Definition: file_base.h:84
This is the type of our block buffers.
Definition: file_base.h:57
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(bool includeDefaultFileAccessor=true)
Calculate the memory usage of a file.
Definition: file.h:52
void write_update()
Call whenever the current block buffer is modified.
Definition: file_base.h:138
const item_type & read()
Read an item from the stream.
Definition: file.h:131
static memory_size_type memory_usage(double blockFactor=1.0)
Calculate the memory usage of a stream.
Definition: file.h:89
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:68
memory_size_type block_size() const
Get the size of a block in bytes.
bool is_open() const
Check if file is open.
void update_block()
Fetch block from disk as indicated by m_nextBlock, writing old block to disk if needed.
void attach(file &f)
Attach to the given tpie::file. If necessary, detach first.
Definition: file.h:195
void detach()
Detach from a tpie::file.
Definition: file.h:202
block_t * m_block
Current block.
Definition: file_base.h:105
item_type & read_mutable()
Read a mutable item from the stream.
Definition: file.h:106
stream_size_type size() const
Get the size of the file measured in items.
Definition: stream_crtp.h:132
static memory_size_type memory_usage()
Return memory usage of this file accessor.
void read(const IT &start, const IT &end)
Reads several items from the stream.
Definition: file.h:187