Disk ARchive  2.6.8
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-2020 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 : http://dar.linux.free.fr/email.html
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 
74  generic_file *pop();
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 
109  generic_file *get_below(const generic_file *ref);
110 
112  generic_file *get_above(const generic_file *ref);
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 
139  void sync_write_above(generic_file *ptr);
140 
142  void flush_read_above(generic_file *ptr);
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 infinint get_position() const override;
152 
153  void copy_to(generic_file & ref) override;
154  void copy_to(generic_file & ref, const infinint & crc_size, crc * & value) override;
155 
156  protected:
157  virtual void inherited_read_ahead(const infinint & amount) override;
158  virtual U_I inherited_read(char *a, U_I size) override;
159  virtual void inherited_write(const char *a, U_I size) override;
160  virtual void inherited_sync_write() override;
161  virtual void inherited_flush_read() override;
162  virtual void inherited_terminate() override;
163 
164  private:
165  struct face
166  {
167  generic_file * ptr;
168  std::list<std::string> labels;
169  }; // 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
170  // 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
171  // because often a face is design there and the expression "tirer `a pile ou face" (meaning "to toss up") is very common.
172 
173  std::deque<face> stack;
174 
175  void detruit();
176  std::deque<face>::iterator look_for_label(const std::string & label);
177  };
178 
179 
180  template <class T> bool pile::pop_and_close_if_type_is(T *ptr)
181  {
182  generic_file *top = nullptr;
183 
184  if(!stack.empty())
185  {
186  top = stack.back().ptr;
187  ptr = dynamic_cast<T *>(top);
188  if(ptr != nullptr)
189  {
190  ptr->terminate();
191  stack.pop_back();
192  delete ptr;
193  return true;
194  }
195  else
196  return false;
197  }
198  else
199  return false;
200  }
201 
202  template <class T> void pile::find_first_from_top(T * & ref) const
203  {
204  ref = nullptr;
205  for(std::deque<face>::const_reverse_iterator it = stack.rbegin(); it != stack.rend() && ref == nullptr; ++it)
206  ref = dynamic_cast<T *>(it->ptr);
207  }
208 
209 
210  template <class T> void pile::find_first_from_bottom(T * & ref) const
211  {
212  ref = nullptr;
213  for(std::deque<face>::const_iterator it = stack.begin(); it != stack.end() && ref == nullptr; ++it)
214  ref = dynamic_cast<T *>(it->ptr);
215  }
216 
218 
219 } // end of namespace
220 
221 #endif
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::generic_file::generic_file
generic_file(gf_mode m)
main constructor
Definition: generic_file.hpp:80
libdar::label
manage label data structure used in archive slice headers
Definition: label.hpp:42
libdar::pile
stores a stack of generic_files writing/reading on each others
Definition: pile.hpp:43
libdar::pile::get_by_label
generic_file * get_by_label(const std::string &label)
find the object associated to a given label
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::infinint
the arbitrary large positive integer class
Definition: real_infinint.hpp:61
libdar::pile::pile
pile()
the constructor
Definition: pile.hpp:51
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::skip_relative
virtual bool skip_relative(S_I x) override
skip relatively to the current position
libdar::pile::inherited_write
virtual void inherited_write(const char *a, U_I size) override
implementation of the write() operation
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:180
libdar::crc
pure virtual class defining interface of a CRC object
Definition: crc.hpp:46
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::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::clear
void clear()
clears the stack
Definition: pile.hpp:96
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:202
libdar::pile::skip
virtual bool skip(const infinint &pos) override
skip at the absolute position
libdar::pile::inherited_sync_write
virtual void inherited_sync_write() override
write down any pending data
generic_file.hpp
class generic_file is defined here as well as class fichier
libdar::pile::is_empty
bool is_empty() const
returns true if the stack is empty, false otherwise.
Definition: pile.hpp:93
libdar::pile::inherited_terminate
virtual void inherited_terminate() override
destructor-like call, except that it is allowed to throw exceptions
libdar::pile::bottom
generic_file * bottom() const
returns the address of the bottom generic_file
Definition: pile.hpp:87
libdar::generic_file
this is the interface class from which all other data transfer classes inherit
Definition: generic_file.hpp:76
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::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:210
libdar::pile::size
U_I size() const
returns the number of objects in the stack
Definition: pile.hpp:90
libdar::pile::copy_to
void copy_to(generic_file &ref) override
copy all data from current position to the object in argument
libdar::gf_read_only
read only access
Definition: gf_mode.hpp:45
libdar::pile::skippable
virtual bool skippable(skippability direction, const infinint &amount) override
whether the implementation is able to skip
libdar::pile::get_position
virtual infinint get_position() const override
get the current read/write 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...
libdar::pile::pop
generic_file * pop()
remove the top generic_file from the top
libdar::pile::inherited_read
virtual U_I inherited_read(char *a, U_I size) override
implementation of read() operation
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::skip_to_eof
virtual bool skip_to_eof() override
skip to the end of file
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
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:46