24 #ifndef __TPIE_FILE_BASE_CRTP_H__
25 #define __TPIE_FILE_BASE_CRTP_H__
48 template <
typename child_t>
78 static inline memory_size_type
block_size(
double blockFactor)
throw () {
79 return static_cast<memory_size_type
>(
get_block_size() * blockFactor);
92 return (
double)blockSize / (double)
block_size(1.0);
124 template <
typename TT>
128 m_fileAccessor->
read_user_data(reinterpret_cast<void*>(&data),
sizeof(TT));
151 template <
typename TT>
155 m_fileAccessor->
write_user_data(reinterpret_cast<const void*>(&data),
sizeof(TT));
194 inline const std::string &
path()
const throw() {
196 return m_fileAccessor->
path();
209 memory_size_type userDataSize=0,
212 self().open_inner(path, accessType, userDataSize, cacheHint);
219 inline void open(memory_size_type userDataSize=0,
222 m_ownedTempFile.reset(tpie_new<temp_file>());
223 m_tempFile=m_ownedTempFile.get();
234 memory_size_type userDataSize=0,
238 self().open_inner(m_tempFile->
path(), accessType, userDataSize, cacheHint);
247 if (m_open) m_fileAccessor->close();
250 m_ownedTempFile.reset();
273 inline void open_inner(
const std::string &
path,
275 memory_size_type userDataSize,
279 const bool preferCompression =
false;
280 m_fileAccessor->
open(
path, m_canRead, m_canWrite, m_itemSize,
281 m_blockSize, userDataSize, cacheHint,
283 if (m_fileAccessor->get_compressed()) {
284 m_fileAccessor->close();
285 throw stream_exception(
"Tried to open compressed stream as non-compressed");
287 m_size = m_fileAccessor->
size();
292 file_base_crtp(memory_size_type itemSize,
double blockFactor,
293 file_accessor::file_accessor * fileAccessor);
296 template <
typename BT>
297 void read_block(BT & b, stream_size_type block);
298 void get_block_check(stream_size_type block);
300 memory_size_type m_blockItems;
301 memory_size_type m_blockSize;
305 memory_size_type m_itemSize;
306 file_accessor::file_accessor * m_fileAccessor;
308 temp_file * m_tempFile;
309 stream_size_type m_size;
312 child_t &
self() {
return *
static_cast<child_t *
>(
this);}
313 const child_t &
self()
const {
return *
static_cast<const child_t *
>(
this);}
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.
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.
Memory management subsystem.
stream_size_type size() const
Number of items in stream.
tpie_init and tpie_finish.
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.
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.
void open(memory_size_type userDataSize=0, cache_hint cacheHint=access_sequential)
Open an anonymous temporary file.
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.
Class representing a reference to a temporary file.
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.
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.
memory_size_type max_user_data_size() const
Get maximum user data size.
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.
memory_size_type user_data_size() const
Size (in bytes) of the user data.
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.