Loading...
Searching...
No Matches
pile.hpp
Go to the documentation of this file.
/*********************************************************************/
// dar - disk archive - a backup/restoration program
// Copyright (C) 2002-2026 Denis Corbin
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// to contact the author, see the AUTHOR file
/*********************************************************************/
#ifndef PILE_HPP
#define PILE_HPP
#include "../my_config.h"
#include <list>
#include "generic_file.hpp"
namespace libdar
{
class pile : public generic_file
{
public:
pile() : generic_file(gf_read_only) { stack.clear(); };
pile(const pile & ref) = delete;
pile(pile && ref) noexcept = delete;
pile & operator = (const pile & ref) = delete;
pile & operator = (pile && ref) noexcept = delete;
~pile() { detruit(); };
void push(generic_file *f, const std::string & label = "", bool extend_mode = false);
generic_file *pop();
template <class T> bool pop_and_close_if_type_is(T *ptr);
generic_file *top() const { if(stack.empty()) return nullptr; else return stack.back().ptr; };
generic_file *bottom() const { if(stack.empty()) return nullptr; else return stack[0].ptr; };
U_I size() const { return stack.size(); };
bool is_empty() const { return stack.empty(); };
void clear() { detruit(); };
template<class T> void find_first_from_top(T * & ref) const;
template<class T> void find_first_from_bottom(T * & ref) const;
generic_file *get_below(const generic_file *ref);
generic_file *get_above(const generic_file *ref);
generic_file *get_by_label(const std::string & label);
void clear_label(const std::string & label);
void add_label(const std::string & label);
void sync_write_above(generic_file *ptr);
void flush_read_above(generic_file *ptr);
// inherited methods from generic_file
// they all apply to the top generic_file object, they fail by Erange() exception if the stack is empty
virtual bool skippable(skippability direction, const infinint & amount) override;
virtual bool skip(const infinint & pos) override;
virtual bool skip_to_eof() override;
virtual bool skip_relative(S_I x) override;
virtual bool truncatable(const infinint & amount) const override;
virtual infinint get_position() const override;
void copy_to(generic_file & ref) override;
void copy_to(generic_file & ref, const infinint & crc_size, crc * & value) override;
protected:
virtual void inherited_read_ahead(const infinint & amount) override;
virtual U_I inherited_read(char *a, U_I size) override;
virtual void inherited_write(const char *a, U_I size) override;
virtual void inherited_truncate(const infinint & pos) override;
virtual void inherited_sync_write() override;
virtual void inherited_flush_read() override;
virtual void inherited_terminate() override;
private:
struct face
{
generic_file * ptr;
std::list<std::string> labels;
}; // ok, had not much idea to find a name for that struct, "face" was the first idea found to be associated with "pile", which means stack
// in French but also is the name of the face of a coin where its value is written. The opposite face of a coin is called "face" in French
// because often a face is design there and the expression "tirer `a pile ou face" (meaning "to toss up") is very common.
std::deque<face> stack;
void detruit();
std::deque<face>::iterator look_for_label(const std::string & label);
};
template <class T> bool pile::pop_and_close_if_type_is(T *ptr)
{
generic_file *top = nullptr;
if(!stack.empty())
{
top = stack.back().ptr;
ptr = dynamic_cast<T *>(top);
if(ptr != nullptr)
{
ptr->terminate();
stack.pop_back();
delete ptr;
return true;
}
else
return false;
}
else
return false;
}
template <class T> void pile::find_first_from_top(T * & ref) const
{
ref = nullptr;
for(std::deque<face>::const_reverse_iterator it = stack.rbegin(); it != stack.rend() && ref == nullptr; ++it)
ref = dynamic_cast<T *>(it->ptr);
}
template <class T> void pile::find_first_from_bottom(T * & ref) const
{
ref = nullptr;
for(std::deque<face>::const_iterator it = stack.begin(); it != stack.end() && ref == nullptr; ++it)
ref = dynamic_cast<T *>(it->ptr);
}
} // end of namespace
#endif
libdar::crc
pure virtual class defining interface of a CRC object
Definition crc.hpp:47
libdar::generic_file::generic_file
generic_file(gf_mode m)
main constructor
Definition generic_file.hpp:80
libdar::infinint
the arbitrary large positive integer class
Definition real_infinint.hpp:62
libdar::label
manage label data structure used in archive slice headers
Definition label.hpp:43
libdar::pile
stores a stack of generic_files writing/reading on each others
Definition pile.hpp:44
libdar::pile::pop
generic_file * pop()
remove the top generic_file from the top
libdar::pile::push
void push(generic_file *f, const std::string &label="", bool extend_mode=false)
add a generic_file on the top
libdar::pile::size
U_I size() const
returns the number of objects in the stack
Definition pile.hpp:90
libdar::pile::add_label
void add_label(const std::string &label)
associate a additional label to the object currently at the top of the stack
libdar::pile::inherited_flush_read
virtual void inherited_flush_read() override
reset internal engine, flush caches in order to read the data at current position
libdar::pile::skippable
virtual bool skippable(skippability direction, const infinint &amount) override
whether the implementation is able to skip
libdar::pile::skip_to_eof
virtual bool skip_to_eof() override
skip to the end of file
libdar::pile::inherited_write
virtual void inherited_write(const char *a, U_I size) override
implementation of the write() operation
libdar::pile::inherited_read
virtual U_I inherited_read(char *a, U_I size) override
implementation of read() operation
libdar::pile::truncatable
virtual bool truncatable(const infinint &amount) const override
whether the implementation is able to truncate to the given position
libdar::pile::skip_relative
virtual bool skip_relative(S_I x) override
skip relatively to the current position
libdar::pile::copy_to
void copy_to(generic_file &ref, const infinint &crc_size, crc *&value) override
copy all data from the current position to the object in argument and computes a CRC value of the tra...
libdar::pile::inherited_terminate
virtual void inherited_terminate() override
destructor-like call, except that it is allowed to throw exceptions
libdar::pile::get_by_label
generic_file * get_by_label(const std::string &label)
find the object associated to a given label
libdar::pile::bottom
generic_file * bottom() const
returns the address of the bottom generic_file
Definition pile.hpp:87
libdar::pile::inherited_sync_write
virtual void inherited_sync_write() override
write down any pending data
libdar::pile::sync_write_above
void sync_write_above(generic_file *ptr)
call the generic_file::sync_write() method of all object found above ptr in the stack
libdar::pile::get_position
virtual infinint get_position() const override
get the current read/write position
libdar::pile::get_below
generic_file * get_below(const generic_file *ref)
return the generic_file object just below the given object or nullptr if the object is at the bottom ...
libdar::pile::inherited_read_ahead
virtual void inherited_read_ahead(const infinint &amount) override
tells the object that several calls to read() will follow to probably obtain at least the given amoun...
libdar::pile::top
generic_file * top() const
returns the address of the top generic_file
Definition pile.hpp:84
libdar::pile::flush_read_above
void flush_read_above(generic_file *ptr)
call the generic_file::flush_read() method of all objects found above ptr in the stack
libdar::pile::is_empty
bool is_empty() const
returns true if the stack is empty, false otherwise.
Definition pile.hpp:93
libdar::pile::get_above
generic_file * get_above(const generic_file *ref)
return the generic_file object just above the given object or nullptr if the object is at the bottom ...
libdar::pile::copy_to
void copy_to(generic_file &ref) override
copy all data from current position to the object in argument
libdar::pile::clear
void clear()
clears the stack
Definition pile.hpp:96
libdar::pile::inherited_truncate
virtual void inherited_truncate(const infinint &pos) override
truncate file at the give offset
libdar::pile::pile
pile()
the constructor
Definition pile.hpp:51
libdar::pile::skip
virtual bool skip(const infinint &pos) override
skip at the absolute position
libdar::pile::clear_label
void clear_label(const std::string &label)
if label is associated to a member of the stack, makes this member of the stack an anoymous member (t...
generic_file.hpp
class generic_file is defined here as well as class fichier
libdar::pile::pop_and_close_if_type_is
bool pop_and_close_if_type_is(T *ptr)
remove the top generic_file and destroy it
Definition pile.hpp:182
libdar::pile::find_first_from_top
void find_first_from_top(T *&ref) const
this template let the class user find out the higher object on the stack of the given type
Definition pile.hpp:204
libdar::pile::find_first_from_bottom
void find_first_from_bottom(T *&ref) const
this template is similar to the template "find_first_from_top" except that the search is started from...
Definition pile.hpp:212
libdar::gf_read_only
@ gf_read_only
read only access
Definition gf_mode.hpp:45
my_config.h
include macro defined by the configure script and some specific additional ones
libdar
libdar namespace encapsulate all libdar symbols
Definition archive.hpp:47