LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SmartFile Class Reference

The abstract SmartFile class represents a file and provides some useful methods to work with that file. More...

#include <smartfile.h>

+ Inheritance diagram for SmartFile:
+ Collaboration diagram for SmartFile:

Public Member Functions

 SmartFile ()=delete
 
 SmartFile (const SmartFile &other)=delete
 
 SmartFile (const FilePath &filepath, bool restore, bool readOnly, bool create)
 The constructor. More...
 
virtual ~SmartFile () noexcept
 The destructor. More...
 
const FilePathgetFilepath () const noexcept
 Get the filepath to the file which was passed to the constructor. More...
 
bool isRestored () const noexcept
 Check if this file was restored from a backup. More...
 
bool isReadOnly () const noexcept
 Check if this file was opened in read-only mode. More...
 
bool isCreated () const noexcept
 Check if this file is created and not yet saved to the harddisc. More...
 
void removeFile (bool original)
 Remove the file from the file system. More...
 
SmartFileoperator= (const SmartFile &rhs)=delete
 

Protected Member Functions

const FilePathprepareSaveAndReturnFilePath (bool toOriginal)
 Prepare to save the file and return the filepath to the file. More...
 
void updateMembersAfterSaving (bool toOriginal) noexcept
 Update the member variables mIsRestored and mIsCreated after saving. More...
 

Protected Attributes

FilePath mFilePath
 The filepath which was passed to the constructor. More...
 
FilePath mTmpFilePath
 The filepath to the temporary file (mFilePath + '~') More...
 
FilePath mOpenedFilePath
 The filepath from where the content was loaded. More...
 
bool mIsRestored
 This variable determines whether the file was restored or not. More...
 
bool mIsReadOnly
 If true, the file is opened as read-only. More...
 
bool mIsCreated
 If true, the file was created and not yet written to the filesystem (so the file mFilePath does not yet exist!) More...
 

Detailed Description

The abstract SmartFile class represents a file and provides some useful methods to work with that file.

Features:

  • Open files in read-only mode (this class then guarantees that no write operations are possible to that file)
  • Creation of backup files ('~' at the end of the filename)
  • Restoring backup files
  • Helper methods for subclasses to load/save files
Note
See Saving Procedere / Automatic Periodically Saving for more details about the backup/restore feature.
Author
ubruhin
Date
2015-01-25
Todo:
Test the class SmartFile and all of its subclasses!

Constructor & Destructor Documentation

SmartFile ( )
delete
SmartFile ( const SmartFile other)
delete
SmartFile ( const FilePath filepath,
bool  restore,
bool  readOnly,
bool  create 
)

The constructor.

Parameters
filepathThe filepath to the file (always to a original file, not to a backup file with "~" at the end of the filename!)
restoreIf true and a backup (*~) of the specified file exists, the backup will be opened instead of the original file. If no backup exists or this parameter is false, the original file will be opened.
readOnlyIf true, the file will be opened read-only (see mIsReadOnly)
createIf true, the file will be created/overwritten after saving it the first time.
Exceptions
ExceptionIf the specified file does not exist, an exception will be thrown.

+ Here is the call graph for this function:

~SmartFile ( )
virtualnoexcept

The destructor.

Removes the temporary file (if existing)

+ Here is the call graph for this function:

Member Function Documentation

const FilePath& getFilepath ( ) const
inlinenoexcept

Get the filepath to the file which was passed to the constructor.

Returns
The filepath to the file
bool isRestored ( ) const
inlinenoexcept

Check if this file was restored from a backup.

Returns
true if restored, false if not
See Also
mIsRestored
bool isReadOnly ( ) const
inlinenoexcept

Check if this file was opened in read-only mode.

Returns
true if opened read-only, false if not
See Also
mIsReadOnly
bool isCreated ( ) const
inlinenoexcept

Check if this file is created and not yet saved to the harddisc.

Returns
true if the file was not yet written the the harddisc, false otherwise
See Also
mIsCreated
void removeFile ( bool  original)

Remove the file from the file system.

Parameters
originalSpecifies whether the original or the backup file should be removed.
Exceptions
ExceptionIf an error occurs, an exception will be thrown

+ Here is the call graph for this function:

SmartFile& operator= ( const SmartFile rhs)
delete
const FilePath & prepareSaveAndReturnFilePath ( bool  toOriginal)
protected

Prepare to save the file and return the filepath to the file.

This method:

  • throws an exception if the file was opened in read-only mode
  • tries to create all parent directories of the file to save
Note
This method must be called from all subclasses BEFORE saving the changes to the file!
Parameters
toOriginalSpecifies whether the original or the backup file should be overwritten/created. The path to that file will be returned afterwards.
Returns
The filepath to the file to save (either original or backup file)
Exceptions
ExceptionIf an error occurs

+ Here is the caller graph for this function:

void updateMembersAfterSaving ( bool  toOriginal)
protectednoexcept

Update the member variables mIsRestored and mIsCreated after saving.

Note
This method must be called from all subclasses AFTER saving the changes to the file!
Parameters
toOriginalSpecifies whether the original or the backup file was saved.

+ Here is the caller graph for this function:

Member Data Documentation

FilePath mFilePath
protected

The filepath which was passed to the constructor.

FilePath mTmpFilePath
protected

The filepath to the temporary file (mFilePath + '~')

FilePath mOpenedFilePath
protected

The filepath from where the content was loaded.

If the backup file was loaded, this equals to mFilePath with appended tilde ('~'). If the original file was loaded, this equals to mFilePath.

bool mIsRestored
protected

This variable determines whether the file was restored or not.

This file is set to true when the constructor was called with the parameter "restore == true". After calling updateMembersAfterSaving() with parameter "toOriginal == true", this flag will be reset to false. The destructor needs this flag to decide whether the temporary file should be removed or not.

bool mIsReadOnly
protected

If true, the file is opened as read-only.

bool mIsCreated
protected

If true, the file was created and not yet written to the filesystem (so the file mFilePath does not yet exist!)


The documentation for this class was generated from the following files: