Disk ARchive  2.7.15
Full featured and portable backup and archiving tool
pile.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2024 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // to contact the author, see the AUTHOR file
20 /*********************************************************************/
21 
25 
26 
27 #ifndef PILE_HPP
28 #define PILE_HPP
29 
30 #include "../my_config.h"
31 
32 #include <list>
33 #include "generic_file.hpp"
34 
35 namespace libdar
36 {
37 
40 
42 
43  class pile : public generic_file
44  {
45  public:
47 
50 
51  pile() : generic_file(gf_read_only) { stack.clear(); };
52  pile(const pile & ref) = delete;
53  pile(pile && ref) noexcept = delete;
54  pile & operator = (const pile & ref) = delete;
55  pile & operator = (pile && ref) noexcept = delete;
56  ~pile() { detruit(); };
57 
59 
68  void push(generic_file *f, const std::string & label = "", bool extend_mode = false);
69 
71 
75 
77 
81  template <class T> bool pop_and_close_if_type_is(T *ptr);
82 
84  generic_file *top() const { if(stack.empty()) return nullptr; else return stack.back().ptr; };
85 
87  generic_file *bottom() const { if(stack.empty()) return nullptr; else return stack[0].ptr; };
88 
90  U_I size() const { return stack.size(); };
91 
93  bool is_empty() const { return stack.empty(); };
94 
96  void clear() { detruit(); };
97 
99 
102  template<class T> void find_first_from_top(T * & ref) const;
103 
105  template<class T> void find_first_from_bottom(T * & ref) const;
106 
107 
110 
113 
114 
116 
119  generic_file *get_by_label(const std::string & label);
120 
121 
122 
124 
127  void clear_label(const std::string & label);
128 
129 
131 
135  void add_label(const std::string & label);
136 
137 
140 
143 
144  // inherited methods from generic_file
145  // they all apply to the top generic_file object, they fail by Erange() exception if the stack is empty
146 
147  virtual bool skippable(skippability direction, const infinint & amount) override;
148  virtual bool skip(const infinint & pos) override;
149  virtual bool skip_to_eof() override;
150  virtual bool skip_relative(S_I x) override;
151  virtual bool truncatable(const infinint & amount) const override;
152  virtual infinint get_position() const override;
153 
154  void copy_to(generic_file & ref) override;
155  void copy_to(generic_file & ref, const infinint & crc_size, crc * & value) override;
156 
157  protected:
158  virtual void inherited_read_ahead(const infinint & amount) override;
159  virtual U_I inherited_read(char *a, U_I size) override;
160  virtual void inherited_write(const char *a, U_I size) override;
161  virtual void inherited_truncate(const infinint & pos) override;
162  virtual void inherited_sync_write() override;
163  virtual void inherited_flush_read() override;
164  virtual void inherited_terminate() override;
165 
166  private:
167  struct face
168  {
169  generic_file * ptr;
170  std::list<std::string> labels;
171  }; // 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
172  // 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
173  // because often a face is design there and the expression "tirer `a pile ou face" (meaning "to toss up") is very common.
174 
175  std::deque<face> stack;
176 
177  void detruit();
178  std::deque<face>::iterator look_for_label(const std::string & label);
179  };
180 
181 
182  template <class T> bool pile::pop_and_close_if_type_is(T *ptr)
183  {
184  generic_file *top = nullptr;
185 
186  if(!stack.empty())
187  {
188  top = stack.back().ptr;
189  ptr = dynamic_cast<T *>(top);
190  if(ptr != nullptr)
191  {
192  ptr->terminate();
193  stack.pop_back();
194  delete ptr;
195  return true;
196  }
197  else
198  return false;
199  }
200  else
201  return false;
202  }
203 
204  template <class T> void pile::find_first_from_top(T * & ref) const
205  {
206  ref = nullptr;
207  for(std::deque<face>::const_reverse_iterator it = stack.rbegin(); it != stack.rend() && ref == nullptr; ++it)
208  ref = dynamic_cast<T *>(it->ptr);
209  }
210 
211 
212  template <class T> void pile::find_first_from_bottom(T * & ref) const
213  {
214  ref = nullptr;
215  for(std::deque<face>::const_iterator it = stack.begin(); it != stack.end() && ref == nullptr; ++it)
216  ref = dynamic_cast<T *>(it->ptr);
217  }
218 
220 
221 } // end of namespace
222 
223 #endif
pure virtual class defining interface of a CRC object
Definition: crc.hpp:47
this is the interface class from which all other data transfer classes inherit
the arbitrary large positive integer class
manage label data structure used in archive slice headers
Definition: label.hpp:43
stores a stack of generic_files writing/reading on each others
Definition: pile.hpp:44
void push(generic_file *f, const std::string &label="", bool extend_mode=false)
add a generic_file on the top
U_I size() const
returns the number of objects in the stack
Definition: pile.hpp:90
void add_label(const std::string &label)
associate a additional label to the object currently at the top of the stack
virtual void inherited_flush_read() override
reset internal engine, flush caches in order to read the data at current position
virtual bool skippable(skippability direction, const infinint &amount) override
whether the implementation is able to skip
virtual bool skip_to_eof() override
skip to the end of file
virtual void inherited_write(const char *a, U_I size) override
implementation of the write() operation
virtual U_I inherited_read(char *a, U_I size) override
implementation of read() operation
virtual bool truncatable(const infinint &amount) const override
whether the implementation is able to truncate to the given position
virtual bool skip_relative(S_I x) override
skip relatively to the current position
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...
virtual void inherited_terminate() override
destructor-like call, except that it is allowed to throw exceptions
virtual void inherited_sync_write() override
write down any pending data
void sync_write_above(generic_file *ptr)
call the generic_file::sync_write() method of all object found above ptr in the stack
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 ...
virtual infinint get_position() const override
get the current read/write position
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 ...
generic_file * bottom() const
returns the address of the bottom generic_file
Definition: pile.hpp:87
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...
void flush_read_above(generic_file *ptr)
call the generic_file::flush_read() method of all objects found above ptr in the stack
bool is_empty() const
returns true if the stack is empty, false otherwise.
Definition: pile.hpp:93
generic_file * pop()
remove the top generic_file from the top
void copy_to(generic_file &ref) override
copy all data from current position to the object in argument
generic_file * top() const
returns the address of the top generic_file
Definition: pile.hpp:84
void clear()
clears the stack
Definition: pile.hpp:96
virtual void inherited_truncate(const infinint &pos) override
truncate file at the give offset
generic_file * get_by_label(const std::string &label)
find the object associated to a given label
pile()
the constructor
Definition: pile.hpp:51
virtual bool skip(const infinint &pos) override
skip at the absolute position
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...
class generic_file is defined here as well as class fichier
bool pop_and_close_if_type_is(T *ptr)
remove the top generic_file and destroy it
Definition: pile.hpp:182
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
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
@ gf_read_only
read only access
Definition: gf_mode.hpp:45
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47