TPIE

11a2c2d
tpie::file_base_crtp< child_t > Class Template Reference

Base class of classes that access files. More...

#include <tpie/file_base_crtp.h>

Inherited by tpie::file_stream_base.

Public Member Functions

bool is_readable () const throw ()
 Check if we can read from the file. More...
 
bool is_writable () const throw ()
 Check if we can write to the file. More...
 
memory_size_type block_items () const
 Get the number of items per block. More...
 
memory_size_type block_size () const
 Get the size of a block in bytes. More...
 
template<typename TT >
void read_user_data (TT &data)
 Read the user data associated with the file. More...
 
memory_size_type read_user_data (void *data, memory_size_type count)
 Read variable length user data associated with the file. More...
 
template<typename TT >
void write_user_data (const TT &data)
 Write user data to the stream. More...
 
void write_user_data (const void *data, memory_size_type count)
 Write variable length user data associated with the file. More...
 
memory_size_type user_data_size () const
 Get current user data size. More...
 
memory_size_type max_user_data_size () const
 Get maximum user data size. More...
 
const std::string & path () const throw ()
 The path of the file opened or the empty string. More...
 
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. More...
 
void open (memory_size_type userDataSize=0, cache_hint cacheHint=access_sequential)
 Open an anonymous temporary file. More...
 
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. More...
 
void close ()
 Close the file. More...
 
bool is_open () const
 Check if file is open. More...
 
stream_size_type file_size () const throw ()
 Get the size of the file measured in items. More...
 

Static Public Member Functions

static memory_size_type block_size (double blockFactor) throw ()
 Calculate the block size in bytes used by a stream. More...
 
static double calculate_block_factor (memory_size_type blockSize) throw ()
 Find the block factor that would result in the given block size measured in bytes. More...
 
static memory_size_type block_memory_usage (double blockFactor)
 Amount of memory used by a single block given the block factor. More...
 

Protected Member Functions

void open_inner (const std::string &path, access_type accessType, memory_size_type userDataSize, cache_hint cacheHint)
 
 file_base_crtp (memory_size_type itemSize, double blockFactor, file_accessor::file_accessor *fileAccessor)
 
template<typename BT >
void read_block (BT &b, stream_size_type block)
 
void get_block_check (stream_size_type block)
 

Protected Attributes

memory_size_type m_blockItems
 
memory_size_type m_blockSize
 
bool m_canRead
 
bool m_canWrite
 
bool m_open
 
memory_size_type m_itemSize
 
file_accessor::file_accessorm_fileAccessor
 
tpie::unique_ptr< temp_filem_ownedTempFile
 
temp_filem_tempFile
 
stream_size_type m_size
 

Detailed Description

template<typename child_t>
class tpie::file_base_crtp< child_t >

Base class of classes that access files.

Inheriting classes may wish to override open_inner() and close(), e.g. to initialize and deinitialize block buffers. open_inner() in the inheriting class will not be called twice in a row without an intervening call to close(). The default implementation of open_inner() passes the open on to the file accessor and sets some attributes.

Definition at line 50 of file file_base_crtp.h.

Member Function Documentation

◆ block_items()

template<typename child_t >
memory_size_type tpie::file_base_crtp< child_t >::block_items ( ) const
inline

Get the number of items per block.

Definition at line 106 of file file_base_crtp.h.

106  {
107  return m_blockItems;
108  }

◆ block_memory_usage()

template<typename child_t >
static memory_size_type tpie::file_base_crtp< child_t >::block_memory_usage ( double  blockFactor)
inlinestatic

Amount of memory used by a single block given the block factor.

Definition at line 99 of file file_base_crtp.h.

99  {
100  return block_size(blockFactor);
101  }

Referenced by tpie::uncompressed_stream< T >::memory_usage().

◆ block_size() [1/2]

template<typename child_t >
memory_size_type tpie::file_base_crtp< child_t >::block_size ( ) const
inline

Get the size of a block in bytes.

Definition at line 113 of file file_base_crtp.h.

113  {
114  return m_blockSize;
115  }

Referenced by tpie::file_base_crtp< file_stream_base >::block_memory_usage(), and tpie::file_base_crtp< file_stream_base >::calculate_block_factor().

◆ block_size() [2/2]

template<typename child_t >
static memory_size_type tpie::file_base_crtp< child_t >::block_size ( double  blockFactor)
throw (
)
inlinestatic

Calculate the block size in bytes used by a stream.

We have block_size(calculate_block_factor(b)) ~= b.

Parameters
blockFactorFactor of the global block size to use.
Returns
Size in bytes.

Definition at line 79 of file file_base_crtp.h.

79  {
80  return static_cast<memory_size_type>(get_block_size() * blockFactor);
81  }

◆ calculate_block_factor()

template<typename child_t >
static double tpie::file_base_crtp< child_t >::calculate_block_factor ( memory_size_type  blockSize)
throw (
)
inlinestatic

Find the block factor that would result in the given block size measured in bytes.

We have calculate_block_factor(block_size(f)) ~= f.

Parameters
blockSizeThe sought block size.
Returns
The block factor needed to achieve this block size.

Definition at line 92 of file file_base_crtp.h.

92  {
93  return (double)blockSize / (double)block_size(1.0);
94  }

◆ close()

template<typename child_t >
void tpie::file_base_crtp< child_t >::close ( )
inline

Close the file.

Note all streams into the file must be freed before you call close.

Definition at line 247 of file file_base_crtp.h.

247  {
248  if (m_open) m_fileAccessor->close();
249  m_open = false;
250  m_tempFile = NULL;
251  m_ownedTempFile.reset();
252  }

Referenced by tpie::file_base_crtp< file_stream_base >::open().

◆ file_size()

template<typename child_t >
stream_size_type tpie::file_base_crtp< child_t >::file_size ( ) const
throw (
)
inline

Get the size of the file measured in items.

If there are streams of this file that have extended the stream length but have not yet flushed these writes, we might report an incorrect size.

Returns
The number of items in the file.

Definition at line 269 of file file_base_crtp.h.

269  {
270  return m_size;
271  }

◆ is_open()

template<typename child_t >
bool tpie::file_base_crtp< child_t >::is_open ( ) const
inline

Check if file is open.

Definition at line 257 of file file_base_crtp.h.

257  {
258  return m_open;
259  }

◆ is_readable()

template<typename child_t >
bool tpie::file_base_crtp< child_t >::is_readable ( ) const
throw (
)
inline

Check if we can read from the file.

Returns
True if we can read from the file.

Definition at line 57 of file file_base_crtp.h.

57  {
58  return m_canRead;
59  }

◆ is_writable()

template<typename child_t >
bool tpie::file_base_crtp< child_t >::is_writable ( ) const
throw (
)
inline

Check if we can write to the file.

Returns
True if we can write to the file.

Definition at line 66 of file file_base_crtp.h.

66  {
67  return m_canWrite;
68  }

Referenced by tpie::uncompressed_stream< T >::write().

◆ max_user_data_size()

template<typename child_t >
memory_size_type tpie::file_base_crtp< child_t >::max_user_data_size ( ) const
inline

Get maximum user data size.

Definition at line 184 of file file_base_crtp.h.

184  {
185  assert(m_open);
186  return m_fileAccessor->max_user_data_size();
187  }

Referenced by tpie::file_base_crtp< file_stream_base >::write_user_data().

◆ open() [1/3]

template<typename child_t >
void tpie::file_base_crtp< child_t >::open ( const std::string &  path,
access_type  accessType = access_read_write,
memory_size_type  userDataSize = 0,
cache_hint  cacheHint = access_sequential 
)
inline

Open a file.

Parameters
pathThe path of the file to open.
accessTypeThe mode of operation.
userDataSizeThe size of the user data we want to store in the file.

Definition at line 208 of file file_base_crtp.h.

211  {
212  self().close();
213  self().open_inner(path, accessType, userDataSize, cacheHint);
214  }

◆ open() [2/3]

template<typename child_t >
void tpie::file_base_crtp< child_t >::open ( memory_size_type  userDataSize = 0,
cache_hint  cacheHint = access_sequential 
)
inline

Open an anonymous temporary file.

The temporary file is deleted when this file is closed.

Definition at line 220 of file file_base_crtp.h.

221  {
222  self().close();
223  m_ownedTempFile.reset(tpie_new<temp_file>());
224  m_tempFile=m_ownedTempFile.get();
225  self().open_inner(m_tempFile->path(), access_read_write, userDataSize, cacheHint);
226  }

◆ open() [3/3]

template<typename child_t >
void tpie::file_base_crtp< child_t >::open ( temp_file file,
access_type  accessType = access_read_write,
memory_size_type  userDataSize = 0,
cache_hint  cacheHint = access_sequential 
)
inline

Open a temporary file.

The temporary file is not deleted when this file is closed, so several tpie::file objects may use the same temporary file consecutively.

Definition at line 233 of file file_base_crtp.h.

236  {
237  self().close();
238  m_tempFile=&file;
239  self().open_inner(m_tempFile->path(), accessType, userDataSize, cacheHint);
240  }

◆ path()

template<typename child_t >
const std::string& tpie::file_base_crtp< child_t >::path ( ) const
throw (
)
inline

The path of the file opened or the empty string.

Returns
The path of the currently opened file.

Definition at line 195 of file file_base_crtp.h.

195  {
196  assert(m_open);
197  return m_fileAccessor->path();
198  }

Referenced by tpie::pipelining::serialization_bits::reverser_input_t< T >::begin(), tpie::pipelining::serialization_bits::buffer_input_t< T >::begin(), and tpie::file_base_crtp< file_stream_base >::open().

◆ read_user_data() [1/2]

template<typename child_t >
template<typename TT >
void tpie::file_base_crtp< child_t >::read_user_data ( TT &  data)
inline

Read the user data associated with the file.

Parameters
dataWhere to store the user data.
Template Parameters
TTThe type of user data. sizeof(TT) must be less than or equal to the maximum user data size of the stream. TT must be trivially copyable.

Definition at line 126 of file file_base_crtp.h.

126  {
127  assert(m_open);
128  if (sizeof(TT) != user_data_size()) throw io_exception("Wrong user data size");
129  m_fileAccessor->read_user_data(reinterpret_cast<void*>(&data), sizeof(TT));
130  }

◆ read_user_data() [2/2]

template<typename child_t >
memory_size_type tpie::file_base_crtp< child_t >::read_user_data ( void *  data,
memory_size_type  count 
)
inline

Read variable length user data associated with the file.

Parameters
dataThe buffer in which to write data.
countThe size of the buffer.
Returns
Number of bytes of user data actually read.

Definition at line 139 of file file_base_crtp.h.

139  {
140  assert(m_open);
141  return m_fileAccessor->read_user_data(data, count);
142  }

◆ user_data_size()

template<typename child_t >
memory_size_type tpie::file_base_crtp< child_t >::user_data_size ( ) const
inline

Get current user data size.

Definition at line 176 of file file_base_crtp.h.

176  {
177  assert(m_open);
178  return m_fileAccessor->user_data_size();
179  }

Referenced by tpie::file_base_crtp< file_stream_base >::read_user_data().

◆ write_user_data() [1/2]

template<typename child_t >
template<typename TT >
void tpie::file_base_crtp< child_t >::write_user_data ( const TT &  data)
inline

Write user data to the stream.

Parameters
dataThe user data to store in the stream.
Template Parameters
TTThe type of user data. sizeof(TT) must be less than or equal to the maximum user data size of the stream. TT must be trivially copyable.

Definition at line 153 of file file_base_crtp.h.

153  {
154  assert(m_open);
155  if (sizeof(TT) > max_user_data_size()) throw io_exception("Wrong user data size");
156  m_fileAccessor->write_user_data(reinterpret_cast<const void*>(&data), sizeof(TT));
157  }

◆ write_user_data() [2/2]

template<typename child_t >
void tpie::file_base_crtp< child_t >::write_user_data ( const void *  data,
memory_size_type  count 
)
inline

Write variable length user data associated with the file.

Throws a stream_exception if the size of the user data exceeds the maximum user data size of the stream.

Parameters
dataThe buffer from which to read data.
countThe size of the user data.

Definition at line 168 of file file_base_crtp.h.

168  {
169  assert(m_open);
170  m_fileAccessor->write_user_data(data, count);
171  }

The documentation for this class was generated from the following file:
tpie::file_accessor::stream_accessor_base::max_user_data_size
memory_size_type max_user_data_size() const
Maximum size (in bytes) of the user data.
Definition: stream_accessor_base.h:197
tpie::file_base_crtp::max_user_data_size
memory_size_type max_user_data_size() const
Get maximum user data size.
Definition: file_base_crtp.h:184
tpie::temp_file::path
const std::string & path()
Get the path of the associated file.
Definition: tempname.h:243
tpie::file_accessor::stream_accessor_base::path
const std::string & path() const
Path of the file currently open.
Definition: stream_accessor_base.h:187
tpie::file_base_crtp::block_size
memory_size_type block_size() const
Get the size of a block in bytes.
Definition: file_base_crtp.h:113
tpie::file_accessor::stream_accessor_base::write_user_data
void write_user_data(const void *data, memory_size_type count)
Write user data to the stream.
tpie::file_base_crtp::close
void close()
Close the file.
Definition: file_base_crtp.h:247
tpie::file_base_crtp::path
const std::string & path() const
The path of the file opened or the empty string.
Definition: file_base_crtp.h:195
tpie::file_accessor::stream_accessor_base::user_data_size
memory_size_type user_data_size() const
Size (in bytes) of the user data.
Definition: stream_accessor_base.h:192
tpie::file_accessor::stream_accessor_base::read_user_data
memory_size_type read_user_data(void *data, memory_size_type count)
Read user data into the given buffer.
tpie::get_block_size
TPIE_EXPORT memory_size_type get_block_size()
Get the TPIE block size.
tpie::file_base_crtp::user_data_size
memory_size_type user_data_size() const
Get current user data size.
Definition: file_base_crtp.h:176
tpie::access_read_write
@ access_read_write
Open a file for reading or writing.
Definition: access_type.h:35