pstore2
Classes | Namespaces | Functions
file.hpp File Reference

Cross platform file management functions and classes. More...

#include <cstdint>
#include <ctime>
#include <functional>
#include "pstore/adt/error_or.hpp"
#include "pstore/support/array_elements.hpp"
#include "pstore/support/error.hpp"
#include "file_posix.hpp"
Include dependency graph for file.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  pstore::file::system_error
 
class  pstore::file::file_base
 An abstract file class. Provides the interface for file access. More...
 
class  pstore::file::range_lock
 A synchronization object that can be used to protect data in a file from being simultaneously accessed by multiple processes. More...
 
class  pstore::file::in_memory
 Implements an in-memory file which provides a file-like API to a chunk of pre-allocated memory. More...
 
class  pstore::file::file_handle
 Implements a portable file access API. More...
 
struct  pstore::file::file_handle::unique
 unique is an empty class type used to disambiguate the overloads of creating a file. More...
 
struct  pstore::file::file_handle::temporary
 temporary is an empty class type used to disambiguate the overloads of creating a file. More...
 
class  pstore::file::deleter_base
 A class which, on destruction, will delete a file whose name is passed to the constructor. More...
 

Namespaces

 pstore::file
 The 'file' namespace contains all of the database file management functions and classes .
 

Functions

template<typename RandomGenerator >
std::string pstore::file::details::name_from_template (std::string const &tmpl, RandomGenerator rng)
 The name_from_template() function takes the given file name template and returns a string in which a portion of the template is overwritten to create a file name. More...
 
template<typename WidthType , typename PointeeType , typename Function >
std::size_t pstore::file::details::split (PointeeType *buffer, std::size_t size, Function const &function)
 Unfortunately, the Win32 ReadFile() and WriteFile() functions accept size parameters whose type is DWORD whereas our API uses std::size_t. More...
 
template<typename WidthType , typename Function >
std::size_t pstore::file::details::split (void *const buffer, std::size_t const size, Function const &function)
 
template<typename WidthType , typename Function >
std::size_t pstore::file::details::split (void const *const buffer, std::size_t const size, Function const &function)
 
std::ostream & pstore::file::operator<< (std::ostream &os, file_handle const &fh)
 
bool pstore::file::exists (std::string const &path)
 Returns true if the file system contains an object at the location given by path. More...
 
void pstore::file::unlink (std::string const &path, bool allow_noent=false)
 Deletes the file system object at the location given by path. More...
 

Detailed Description

Cross platform file management functions and classes.

Function Documentation

◆ name_from_template()

template<typename RandomGenerator >
std::string pstore::file::details::name_from_template ( std::string const &  tmpl,
RandomGenerator  rng 
)

The name_from_template() function takes the given file name template and returns a string in which a portion of the template is overwritten to create a file name.

The template may be any file name with some number of `Xs' appended to it, for example /tmp/temp.XXXXXX. The trailing `Xs' are replaced with a unique alphanumeric combination. The number of unique file names mktemp() can return depends on the number of `Xs' provided.

Template Parameters
RandomGeneratorA Callable object with the signature equivalent to std::function<unsigned(unsigned)>. The function's result must be in the range 0 to the value given by the single parameter.
Parameters
tmplA template string which forms the basis for the result string. Any trailing X characters are replaced by characters derived from the result of the rng function.
rngA random number generator function which should return a value in the range [0,max).
Returns
A string derived from the tmpl argument but with trailing 'X's replaced by random characters from an internal alphabet.
Note
This function is used on platforms that don't have a native implementation of the mkstemp() function.

◆ split()

template<typename WidthType , typename PointeeType , typename Function >
std::size_t pstore::file::details::split ( PointeeType *  buffer,
std::size_t  size,
Function const &  function 
)

Unfortunately, the Win32 ReadFile() and WriteFile() functions accept size parameters whose type is DWORD whereas our API uses std::size_t.

std::size_t is obviously 64-bits on a 64-bit host, but DWORD is always 32-bit. This function splits up the request into chunks which are no larger than the WidthType max value.

Template Parameters
WidthTypeThe integer type whose largest value determines the largest 'chunk' into which the size parameter can be split.
PointeeTypeThe type of the elements of the buffer array.
FunctionA callable whose signature whose be equivalent to std::function<std::size_t(PointeeType*,WidthType)>. It is called for each chunk into which size is divided. The return value should be the number of bytes processed; the first argument is the first value to process; the second element is the number of contiguous elements to be processed.
Parameters
bufferThe base address of the buffer to be processed by the provided callback.
sizeThe total number of bytes to be processed.
functionThis function is called repeatedly to operate on each portion of the buffer.
Returns
The sum of the values returned by function.