TPIE

2362a60
file_base_crtp.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 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 
23 
24 #ifndef __TPIE_FILE_BASE_CRTP_H__
25 #define __TPIE_FILE_BASE_CRTP_H__
26 
27 #include <tpie/types.h>
28 #include <tpie/tpie.h>
29 #include <tpie/exception.h>
30 #include <tpie/memory.h>
31 #include <tpie/access_type.h>
32 #include <tpie/cache_hint.h>
33 #include <tpie/stream_header.h>
35 #include <tpie/tempname.h>
36 
37 namespace tpie {
38 
48 template <typename child_t>
50 public:
56  bool is_readable() const throw() {
57  return m_canRead;
58  }
59 
65  bool is_writable() const throw() {
66  return m_canWrite;
67  }
68 
69 
78  static inline memory_size_type block_size(double blockFactor) throw () {
79  return static_cast<memory_size_type>(get_block_size() * blockFactor);
80  }
81 
91  static inline double calculate_block_factor(memory_size_type blockSize) throw () {
92  return (double)blockSize / (double)block_size(1.0);
93  }
94 
98  static inline memory_size_type block_memory_usage(double blockFactor) {
99  return block_size(blockFactor);
100  }
101 
105  memory_size_type block_items() const {
106  return m_blockItems;
107  }
108 
112  memory_size_type block_size() const {
113  return m_blockSize;
114  }
115 
124  template <typename TT>
125  void read_user_data(TT & data) throw(stream_exception) {
126  assert(m_open);
127  if (sizeof(TT) != user_data_size()) throw io_exception("Wrong user data size");
128  m_fileAccessor->read_user_data(reinterpret_cast<void*>(&data), sizeof(TT));
129  }
130 
138  memory_size_type read_user_data(void * data, memory_size_type count) {
139  assert(m_open);
140  return m_fileAccessor->read_user_data(data, count);
141  }
142 
151  template <typename TT>
152  void write_user_data(const TT & data) throw(stream_exception) {
153  assert(m_open);
154  if (sizeof(TT) > max_user_data_size()) throw io_exception("Wrong user data size");
155  m_fileAccessor->write_user_data(reinterpret_cast<const void*>(&data), sizeof(TT));
156  }
157 
167  void write_user_data(const void * data, memory_size_type count) {
168  assert(m_open);
169  m_fileAccessor->write_user_data(data, count);
170  }
171 
175  memory_size_type user_data_size() const {
176  assert(m_open);
177  return m_fileAccessor->user_data_size();
178  }
179 
183  memory_size_type max_user_data_size() const {
184  assert(m_open);
185  return m_fileAccessor->max_user_data_size();
186  }
187 
188 
194  inline const std::string & path() const throw() {
195  assert(m_open);
196  return m_fileAccessor->path();
197  }
198 
207  inline void open(const std::string & path,
208  access_type accessType=access_read_write,
209  memory_size_type userDataSize=0,
210  cache_hint cacheHint=access_sequential) throw (stream_exception) {
211  self().close();
212  self().open_inner(path, accessType, userDataSize, cacheHint);
213  }
214 
219  inline void open(memory_size_type userDataSize=0,
220  cache_hint cacheHint=access_sequential) throw (stream_exception) {
221  self().close();
222  m_ownedTempFile.reset(tpie_new<temp_file>());
223  m_tempFile=m_ownedTempFile.get();
224  self().open_inner(m_tempFile->path(), access_read_write, userDataSize, cacheHint);
225  }
226 
232  inline void open(temp_file & file,
233  access_type accessType=access_read_write,
234  memory_size_type userDataSize=0,
235  cache_hint cacheHint=access_sequential) throw (stream_exception) {
236  self().close();
237  m_tempFile=&file;
238  self().open_inner(m_tempFile->path(), accessType, userDataSize, cacheHint);
239  }
240 
246  inline void close() throw(stream_exception) {
247  if (m_open) m_fileAccessor->close();
248  m_open = false;
249  m_tempFile = NULL;
250  m_ownedTempFile.reset();
251  }
252 
256  inline bool is_open() const {
257  return m_open;
258  }
259 
268  inline stream_size_type file_size() const throw() {
269  return m_size;
270  }
271 
272 protected:
273  inline void open_inner(const std::string & path,
274  access_type accessType,
275  memory_size_type userDataSize,
276  cache_hint cacheHint) throw(stream_exception) {
277  m_canRead = accessType == access_read || accessType == access_read_write;
278  m_canWrite = accessType == access_write || accessType == access_read_write;
279  const bool preferCompression = false;
280  m_fileAccessor->open(path, m_canRead, m_canWrite, m_itemSize,
281  m_blockSize, userDataSize, cacheHint,
282  preferCompression);
283  if (m_fileAccessor->get_compressed()) {
284  m_fileAccessor->close();
285  throw stream_exception("Tried to open compressed stream as non-compressed");
286  }
287  m_size = m_fileAccessor->size();
288  m_open = true;
289  }
290 
291 
292  file_base_crtp(memory_size_type itemSize, double blockFactor,
293  file_accessor::file_accessor * fileAccessor);
294 
295 
296  template <typename BT>
297  void read_block(BT & b, stream_size_type block);
298  void get_block_check(stream_size_type block);
299 
300  memory_size_type m_blockItems;
301  memory_size_type m_blockSize;
302  bool m_canRead;
303  bool m_canWrite;
304  bool m_open;
305  memory_size_type m_itemSize;
306  file_accessor::file_accessor * m_fileAccessor;
307  tpie::unique_ptr<temp_file> m_ownedTempFile;
308  temp_file * m_tempFile;
309  stream_size_type m_size;
310 
311 private:
312  child_t & self() {return *static_cast<child_t *>(this);}
313  const child_t & self() const {return *static_cast<const child_t *>(this);}
314 };
315 
316 } // namespace tpie
317 
318 #endif // __TPIE_FILE_BASE_CRTP_H__
memory_size_type read_user_data(void *data, memory_size_type count)
Read user data into the given buffer.
Different hints for OS file caching.
Sequential access is intended.
Definition: cache_hint.h:36
void open(temp_file &file, access_type accessType=access_read_write, memory_size_type userDataSize=0, cache_hint cacheHint=access_sequential)
Open a temporary file.
void write_user_data(const void *data, memory_size_type count)
Write user data to the stream.
memory_size_type get_block_size()
Get the TPIE block size.
static memory_size_type block_memory_usage(double blockFactor)
Amount of memory used by a single block given the block factor.
void read_user_data(TT &data)
Read the user data associated with the file.
Typesafe bitflags.
Memory management subsystem.
stream_size_type size() const
Number of items in stream.
tpie_init and tpie_finish.
cache_hint
Definition: cache_hint.h:28
void open(const std::string &path, access_type accessType=access_read_write, memory_size_type userDataSize=0, cache_hint cacheHint=access_sequential)
Open a file.
const std::string & path() const
The path of the file opened or the empty string.
Central file abstraction.
Definition: file.h:44
static memory_size_type block_size(double blockFactor)
Calculate the block size in bytes used by a stream.
void write_user_data(const TT &data)
Write user data to the stream.
Open a file for reading.
Definition: access_type.h:31
void open(memory_size_type userDataSize=0, cache_hint cacheHint=access_sequential)
Open an anonymous temporary file.
Exception classes.
Declare default file accessor.
bool is_writable() const
Check if we can write to the file.
Base class of classes that access files.
stream_size_type file_size() const
Get the size of the file measured in items.
void close()
Close the file.
void open(const std::string &path, bool read, bool write, memory_size_type itemSize, memory_size_type blockSize, memory_size_type maxUserDataSize, cache_hint cacheHint, int compressionFlags)
Open file for reading and/or writing.
const std::string & path()
Get the path of the associated file.
Definition: tempname.h:244
Class representing a reference to a temporary file.
Definition: tempname.h:202
void write_user_data(const void *data, memory_size_type count)
Write variable length user data associated with the file.
memory_size_type user_data_size() const
Get current user data size.
bool is_readable() const
Check if we can read from the file.
Open a file for writing only, content is truncated.
Definition: access_type.h:33
memory_size_type block_size() const
Get the size of a block in bytes.
memory_size_type block_items() const
Get the number of items per block.
memory_size_type read_user_data(void *data, memory_size_type count)
Read variable length user data associated with the file.
bool is_open() const
Check if file is open.
std::unique_ptr< T, tpie_deleter > unique_ptr
like std::unique_ptr, but delete the object with tpie_delete.
Definition: memory.h:338
memory_size_type max_user_data_size() const
Get maximum user data size.
Temporary file names.
static double calculate_block_factor(memory_size_type blockSize)
Find the block factor that would result in the given block size measured in bytes.
Describes how to acces a file.
access_type
Type describing how we wish to access a file.
Definition: access_type.h:29
memory_size_type user_data_size() const
Size (in bytes) of the user data.
Header of streams.
memory_size_type max_user_data_size() const
Maximum size (in bytes) of the user data.
const std::string & path() const
Path of the file currently open.
Open a file for reading or writing.
Definition: access_type.h:35